Skip to content

Commit

Permalink
chore: forward tracing to grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Nov 6, 2023
1 parent 5b55f0f commit 6386d70
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion core/api-keys/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ pub struct Config {
pub server: ServerConfig,
#[serde(default)]
pub app: AppConfig,
#[serde(default)]
#[serde(default = "default_tracing_config")]
pub tracing: TracingConfig,
}

fn default_tracing_config() -> TracingConfig {
TracingConfig {
service_name: "api-keys".to_string(),
}
}

pub struct EnvOverride {
pub db_con: String,
}
Expand Down
2 changes: 2 additions & 0 deletions core/api-keys/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct CheckResponse {
sub: String,
}

#[tracing::instrument(name = "server.auth_check", skip(headers, app), err)]
async fn check_handler(
State((header, app)): State<(String, ApiKeysApp)>,
headers: HeaderMap,
Expand All @@ -65,6 +66,7 @@ async fn check_handler(
Ok(Json(CheckResponse { sub }))
}

#[tracing::instrument(name = "server.graphql", skip_all)]
pub async fn graphql_handler(
schema: Extension<Schema<graphql::Query, graphql::Mutation, EmptySubscription>>,
Claims(jwt_claims): Claims<JwtClaims>,
Expand Down
5 changes: 4 additions & 1 deletion core/api/dev/otel-agent-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ receivers:
thrift_binary: # on port 6832
otlp:
protocols:
http: # on port 4318
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

processors:
filter/ottl:
Expand Down
2 changes: 1 addition & 1 deletion dev/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ local_resource(
serve_env = {
"PG_CON": "postgres://user:password@localhost:5431/pg",
"API_KEYS_CONFIG": "../core/api-keys/api-keys.yml",
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4318",
"OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317",
},
deps = _buck2_dep_inputs(api_keys_target),
resource_deps = [
Expand Down
5 changes: 4 additions & 1 deletion dev/config/otel-agent-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ receivers:
thrift_binary: # on port 6832
otlp:
protocols:
http: # on port 4318
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

processors:
filter/ottl:
Expand Down
4 changes: 4 additions & 0 deletions dev/docker-compose.deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
- kratos
- hydra
- apollo-router
- otel-agent
hydra:
image: oryd/hydra:v2.1.2
ports:
Expand Down Expand Up @@ -95,6 +96,8 @@ services:
- APOLLO_ROUTER_CONFIG_PATH=/repo/dev/router.yaml
volumes:
- ${HOST_PROJECT_PATH:-.}/config/apollo-federation:/repo/dev
depends_on:
- otel-agent
redis:
image: redis:7.0.8
ports:
Expand Down Expand Up @@ -201,6 +204,7 @@ services:
otel-agent:
ports:
- "4318:4318" #! http receiver
- "4317:4317" #! grpc receiver
image: otel/opentelemetry-collector-contrib:0.84.0
command: ["--config=/etc/otel-agent-config.yaml"]
environment:
Expand Down
1 change: 1 addition & 0 deletions lib/tracing-rs/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust_library(
"//third-party/rust:opentelemetry-otlp",
"//third-party/rust:opentelemetry-semantic-conventions",
"//third-party/rust:opentelemetry",
"//third-party/rust:tracing",
"//third-party/rust:tracing-opentelemetry",
"//third-party/rust:tracing-subscriber",
],
Expand Down
1 change: 1 addition & 0 deletions lib/tracing-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
anyhow = { workspace = true }
serde = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-opentelemetry = { workspace = true }
opentelemetry-otlp = { workspace = true }
Expand Down
22 changes: 12 additions & 10 deletions lib/tracing-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
use opentelemetry::sdk::{
resource::{EnvResourceDetector, OsResourceDetector, ProcessResourceDetector},
trace, Resource,
use opentelemetry::{
global,
sdk::{
propagation::TraceContextPropagator,
resource::{EnvResourceDetector, OsResourceDetector, ProcessResourceDetector},
trace, Resource,
},
};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_semantic_conventions::resource;
use serde::{Deserialize, Serialize};
use tracing_subscriber::{filter::EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

pub use tracing::*;

use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracingConfig {
#[serde(default = "default_service_name")]
service_name: String,
pub service_name: String,
}

impl Default for TracingConfig {
fn default() -> Self {
Self {
service_name: default_service_name(),
service_name: "dev-rs".to_string(),
}
}
}

fn default_service_name() -> String {
env!("CARGO_PKG_NAME").to_string()
}

pub fn init_tracer(config: TracingConfig) -> anyhow::Result<()> {
global::set_text_map_propagator(TraceContextPropagator::new());
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic().with_env())
Expand Down

0 comments on commit 6386d70

Please sign in to comment.