From f63bbea30d4c6f862cf01f1a16c85c98631300ed Mon Sep 17 00:00:00 2001 From: Helio Frota <00hf11@gmail.com> Date: Wed, 22 Jan 2025 15:23:23 -0300 Subject: [PATCH] feat: Devmode tracing with OpenTelemetry * Adds a compose with Jaeger and OTELCOL * Fixes a problem with actix-web tracing --- common/infrastructure/Cargo.toml | 2 +- common/infrastructure/src/tracing.rs | 5 +++-- docs/design/log_tracing.md | 23 +++++++++++++++++++++++ etc/dev-traces/compose.yaml | 17 +++++++++++++++++ etc/dev-traces/config.yaml | 23 +++++++++++++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 etc/dev-traces/compose.yaml create mode 100644 etc/dev-traces/config.yaml diff --git a/common/infrastructure/Cargo.toml b/common/infrastructure/Cargo.toml index a1fb33c4b..92eaa4b68 100644 --- a/common/infrastructure/Cargo.toml +++ b/common/infrastructure/Cargo.toml @@ -23,7 +23,7 @@ mime = { workspace = true } openssl = { workspace = true } opentelemetry = { workspace = true } opentelemetry-otlp = { workspace = true } -opentelemetry_sdk = { workspace = true, features = ["rt-tokio"] } +opentelemetry_sdk = { workspace = true, features = ["rt-tokio-current-thread"] } parking_lot = { workspace = true } prometheus = { workspace = true } reqwest = { workspace = true } diff --git a/common/infrastructure/src/tracing.rs b/common/infrastructure/src/tracing.rs index 1704d503d..db973bb1f 100644 --- a/common/infrastructure/src/tracing.rs +++ b/common/infrastructure/src/tracing.rs @@ -123,13 +123,13 @@ fn init_otlp(name: &str) { "service.name", name.to_string(), )])) - .with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio) + .with_batch_exporter(exporter, opentelemetry_sdk::runtime::TokioCurrentThread) .with_sampler(opentelemetry_sdk::trace::Sampler::ParentBased(Box::new( sampler(), ))) .build(); - println!("Using Jaeger tracing."); + println!("Using OTEL Collector with Jaeger as the back-end."); println!("{:#?}", provider); let formatting_layer = tracing_subscriber::fmt::Layer::default(); @@ -142,6 +142,7 @@ fn init_otlp(name: &str) { { eprintln!("Error initializing tracing: {:?}", e); } + opentelemetry::global::set_tracer_provider(provider); } fn init_no_tracing() { diff --git a/docs/design/log_tracing.md b/docs/design/log_tracing.md index 5b2f80b83..586217e21 100644 --- a/docs/design/log_tracing.md +++ b/docs/design/log_tracing.md @@ -135,3 +135,26 @@ However, not all function variants might expose the same behavior. Just because mean it will panic. For example, the `Option::unwrap_or` function: ![Screenshot of rustdoc for Option::unwrap_or](drawings/log_tracing_2.png) + +## Sending traces to OpenTelemetry Collector (devmode) + +Jaeger and OTEL Collector: + +```shell +podman compose -f etc/dev-traces/compose.yaml up +``` + +Database: + +```shell +podman compose -f etc/deploy/compose/compose.yaml up +``` + +Trustify with traces: + +```shell +OTEL_TRACES_SAMPLER_ARG=1 OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4317" cargo run --bin trustd api --db-password trustify --devmode --auth-disabled --tracing enabled +``` + +Access Trustify at [localhost:8080](http://localhost:8080) and analyze the traces using the [Jaeger UI](http://localhost:16686/) + diff --git a/etc/dev-traces/compose.yaml b/etc/dev-traces/compose.yaml new file mode 100644 index 000000000..525de07a9 --- /dev/null +++ b/etc/dev-traces/compose.yaml @@ -0,0 +1,17 @@ +services: + jaeger-all-in-one: + hostname: jaeger-all-in-one + image: jaegertracing/all-in-one:1.53.0 # Using this version to align with trustify-helm-charts + ports: + - "16686:16686" + - "14250:14250" + environment: + - COLLECTOR_OTLP_ENABLED=true + collector: + image: ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector:0.115.1 # Using this version to align with trustify-helm-charts https://github.com/TylerHelmuth/opentelemetry-helm-charts/commit/86188fea6022a6424ef6a086e928d0056fb5dfe8#diff-55020f2b796ba5770731a3b4913592732431ff180c7f7473e5f469e92ed00e74R48 + command: ["--config=/otel-collector-config.yaml"] + volumes: + - './config.yaml:/otel-collector-config.yaml:z' + ports: + - "4317:4317" + depends_on: [jaeger-all-in-one] diff --git a/etc/dev-traces/config.yaml b/etc/dev-traces/config.yaml new file mode 100644 index 000000000..aa52cda78 --- /dev/null +++ b/etc/dev-traces/config.yaml @@ -0,0 +1,23 @@ +receivers: + otlp: + protocols: + grpc: + endpoint: "0.0.0.0:4317" + +exporters: + otlp: + endpoint: jaeger-all-in-one:4317 + tls: + insecure: true + debug: + verbosity: detailed + +processors: + batch: {} + +service: + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [debug, otlp]