diff --git a/Cargo.lock b/Cargo.lock index 205e5a0b8c..a11f01a71e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2726,6 +2726,7 @@ dependencies = [ "opentelemetry-otlp", "opentelemetry-semantic-conventions", "serde", + "tracing 0.1.40", "tracing-opentelemetry", "tracing-subscriber", ] diff --git a/core/api-keys/src/cli/config.rs b/core/api-keys/src/cli/config.rs index 248ef9aee3..f0c0e6156d 100644 --- a/core/api-keys/src/cli/config.rs +++ b/core/api-keys/src/cli/config.rs @@ -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, } diff --git a/core/api-keys/src/server/mod.rs b/core/api-keys/src/server/mod.rs index 68b9d09195..5a3f2a8108 100644 --- a/core/api-keys/src/server/mod.rs +++ b/core/api-keys/src/server/mod.rs @@ -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, @@ -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>, Claims(jwt_claims): Claims, diff --git a/core/api/dev/otel-agent-config.yaml b/core/api/dev/otel-agent-config.yaml index ed63c32977..0a7995ab2c 100644 --- a/core/api/dev/otel-agent-config.yaml +++ b/core/api/dev/otel-agent-config.yaml @@ -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: diff --git a/dev/Tiltfile b/dev/Tiltfile index a81744baf6..5244cc2e31 100644 --- a/dev/Tiltfile +++ b/dev/Tiltfile @@ -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 = [ diff --git a/dev/config/otel-agent-config.yaml b/dev/config/otel-agent-config.yaml index ed63c32977..0a7995ab2c 100644 --- a/dev/config/otel-agent-config.yaml +++ b/dev/config/otel-agent-config.yaml @@ -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: diff --git a/dev/docker-compose.deps.yml b/dev/docker-compose.deps.yml index e6a1eb9def..76d90e107b 100644 --- a/dev/docker-compose.deps.yml +++ b/dev/docker-compose.deps.yml @@ -28,6 +28,7 @@ services: - kratos - hydra - apollo-router + - otel-agent hydra: image: oryd/hydra:v2.1.2 ports: @@ -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: @@ -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: diff --git a/lib/tracing-rs/BUCK b/lib/tracing-rs/BUCK index d67ff9c998..ae98c40a1e 100644 --- a/lib/tracing-rs/BUCK +++ b/lib/tracing-rs/BUCK @@ -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", ], diff --git a/lib/tracing-rs/Cargo.toml b/lib/tracing-rs/Cargo.toml index c78e04b753..10dec3539f 100644 --- a/lib/tracing-rs/Cargo.toml +++ b/lib/tracing-rs/Cargo.toml @@ -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 } diff --git a/lib/tracing-rs/src/lib.rs b/lib/tracing-rs/src/lib.rs index 3ac91b9260..07749ad4f0 100644 --- a/lib/tracing-rs/src/lib.rs +++ b/lib/tracing-rs/src/lib.rs @@ -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())