From bd36c4a5b04ec7ed639db9e60578cf046be6ebc9 Mon Sep 17 00:00:00 2001 From: bryn Date: Mon, 12 Aug 2024 18:28:02 +0100 Subject: [PATCH 01/10] Add `format` for trace ID propagation. --- ...nfiguration__tests__schema_generation.snap | 5 +++ apollo-router/src/plugins/telemetry/config.rs | 3 ++ apollo-router/src/plugins/telemetry/mod.rs | 44 ++++++++++++++++--- .../telemetry/exporters/tracing/overview.mdx | 17 +++++++ 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/apollo-router/src/configuration/snapshots/apollo_router__configuration__tests__schema_generation.snap b/apollo-router/src/configuration/snapshots/apollo_router__configuration__tests__schema_generation.snap index 74294b07ba..e615c2b834 100644 --- a/apollo-router/src/configuration/snapshots/apollo_router__configuration__tests__schema_generation.snap +++ b/apollo-router/src/configuration/snapshots/apollo_router__configuration__tests__schema_generation.snap @@ -4625,12 +4625,17 @@ expression: "&schema" "RequestPropagation": { "additionalProperties": false, "properties": { + "format": { + "$ref": "#/definitions/TraceIdFormat", + "description": "#/definitions/TraceIdFormat" + }, "header_name": { "description": "Choose the header name to expose trace_id (default: apollo-trace-id)", "type": "string" } }, "required": [ + "format", "header_name" ], "type": "object" diff --git a/apollo-router/src/plugins/telemetry/config.rs b/apollo-router/src/plugins/telemetry/config.rs index 42c6090b3b..5a506401a8 100644 --- a/apollo-router/src/plugins/telemetry/config.rs +++ b/apollo-router/src/plugins/telemetry/config.rs @@ -322,6 +322,9 @@ pub(crate) struct RequestPropagation { #[schemars(with = "String")] #[serde(deserialize_with = "deserialize_option_header_name")] pub(crate) header_name: Option, + + /// The trace ID format that will be used when propagating to subgraph services. + pub(crate) format: TraceIdFormat, } #[derive(Debug, Clone, Deserialize, JsonSchema)] diff --git a/apollo-router/src/plugins/telemetry/mod.rs b/apollo-router/src/plugins/telemetry/mod.rs index c50f98bd00..4500c05ef9 100644 --- a/apollo-router/src/plugins/telemetry/mod.rs +++ b/apollo-router/src/plugins/telemetry/mod.rs @@ -939,6 +939,7 @@ impl Telemetry { if let Some(from_request_header) = &propagation.request.header_name { propagators.push(Box::new(CustomTraceIdPropagator::new( from_request_header.to_string(), + propagation.request.format.clone(), ))); } @@ -2020,13 +2021,15 @@ fn store_ftv1(subgraph_name: &ByteString, resp: SubgraphResponse) -> SubgraphRes struct CustomTraceIdPropagator { header_name: String, fields: [String; 1], + format: TraceIdFormat, } impl CustomTraceIdPropagator { - fn new(header_name: String) -> Self { + fn new(header_name: String, format: TraceIdFormat) -> Self { Self { fields: [header_name.clone()], header_name, + format, } } @@ -2058,9 +2061,9 @@ impl TextMapPropagator for CustomTraceIdPropagator { fn inject_context(&self, cx: &opentelemetry::Context, injector: &mut dyn Injector) { let span = cx.span(); let span_context = span.span_context(); - if span_context.is_valid() { - let header_value = format!("{}", span_context.trace_id()); - injector.set(&self.header_name, header_value); + if span_context.trace_id() != TraceId::INVALID { + let formatted_trace_id = self.format.format(span_context.trace_id()); + injector.set(&self.header_name, formatted_trace_id); } } @@ -2130,6 +2133,10 @@ mod tests { use http::StatusCode; use insta::assert_snapshot; use itertools::Itertools; + use opentelemetry_api::propagation::{Injector, TextMapPropagator}; + use opentelemetry_api::trace::{ + SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState, + }; use serde_json::Value; use serde_json_bytes::json; use serde_json_bytes::ByteString; @@ -2159,6 +2166,7 @@ mod tests { use crate::plugin::test::MockSubgraphService; use crate::plugin::test::MockSupergraphService; use crate::plugin::DynPlugin; + use crate::plugins::telemetry::config::TraceIdFormat; use crate::plugins::telemetry::handle_error_internal; use crate::services::router::body::get_body_bytes; use crate::services::RouterRequest; @@ -3195,11 +3203,37 @@ mod tests { let trace_id = String::from("04f9e396-465c-4840-bc2b-f493b8b1a7fc"); let expected_trace_id = String::from("04f9e396465c4840bc2bf493b8b1a7fc"); - let propagator = CustomTraceIdPropagator::new(header.clone()); + let propagator = CustomTraceIdPropagator::new(header.clone(), TraceIdFormat::Uuid); let mut headers: HashMap = HashMap::new(); headers.insert(header, trace_id); let span = propagator.extract_span_context(&headers); assert!(span.is_some()); assert_eq!(span.unwrap().trace_id().to_string(), expected_trace_id); } + + #[test] + fn test_header_propagation_format() { + struct Injected(HashMap); + impl Injector for Injected { + fn set(&mut self, key: &str, value: String) { + self.0.insert(key.to_string(), value); + } + } + let mut injected = Injected(HashMap::new()); + let _ctx = opentelemetry::Context::new() + .with_remote_span_context(SpanContext::new( + TraceId::from_u128(0x04f9e396465c4840bc2bf493b8b1a7fc), + SpanId::INVALID, + TraceFlags::default(), + false, + TraceState::default(), + )) + .attach(); + let propagator = CustomTraceIdPropagator::new("my_header".to_string(), TraceIdFormat::Uuid); + propagator.inject_context(&opentelemetry::Context::current(), &mut injected); + assert_eq!( + injected.0.get("my_header").unwrap(), + "04f9e396-465c-4840-bc2b-f493b8b1a7fc" + ); + } } diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index 1bab45a2c2..797980ea4b 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -144,9 +144,26 @@ telemetry: # If you have your own way to generate a trace id and you want to pass it via a custom request header request: + # The name of the header to read the trace id from header_name: my-trace-id + # The format of the trace when propagating to subgraphs. + format: uuid ``` +#### `request` configuration reference + +| Option | Values | Default | Description | +|---------------|---------------------------------------------------------------|-----------------------------------|-------------------------------------| +| `header_name` | | Enable or disable stdout logging. | | +| `format` | `hexadecimal`\|`open_telemetry`\|`decimal`\|`datadog`\|`uuid` | `hexadecimal` | The output format of the `trace_id` | + +The formats are: +* `hexadecimal` - 32-character hexadecimal string (e.g. `0123456789abcdef0123456789abcdef`) +* `open_telemetry` - 32-character hexadecimal string (e.g. `0123456789abcdef0123456789abcdef`) +* `decimal` - 16-character decimal string (e.g. `1234567890123456`) +* `datadog` - 16-character decimal string (e.g. `1234567890123456`) +* `uuid` - 36-character UUID string (e.g. `01234567-89ab-cdef-0123-456789abcdef`) + ### Limits You may set limits on spans to prevent sending too much data to your APM. For example: From 6c9fec675bec3591cc4f86d9d4dcb061b95da9f9 Mon Sep 17 00:00:00 2001 From: bryn Date: Mon, 12 Aug 2024 18:42:32 +0100 Subject: [PATCH 02/10] Add `format` for trace ID propagation. --- apollo-router/src/plugins/telemetry/mod.rs | 12 ++++++++---- .../telemetry/exporters/tracing/overview.mdx | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apollo-router/src/plugins/telemetry/mod.rs b/apollo-router/src/plugins/telemetry/mod.rs index 4500c05ef9..6fea2af4b1 100644 --- a/apollo-router/src/plugins/telemetry/mod.rs +++ b/apollo-router/src/plugins/telemetry/mod.rs @@ -2133,10 +2133,14 @@ mod tests { use http::StatusCode; use insta::assert_snapshot; use itertools::Itertools; - use opentelemetry_api::propagation::{Injector, TextMapPropagator}; - use opentelemetry_api::trace::{ - SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState, - }; + use opentelemetry_api::propagation::Injector; + use opentelemetry_api::propagation::TextMapPropagator; + use opentelemetry_api::trace::SpanContext; + use opentelemetry_api::trace::SpanId; + use opentelemetry_api::trace::TraceContextExt; + use opentelemetry_api::trace::TraceFlags; + use opentelemetry_api::trace::TraceId; + use opentelemetry_api::trace::TraceState; use serde_json::Value; use serde_json_bytes::json; use serde_json_bytes::ByteString; diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index 797980ea4b..cc66bc119d 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -164,6 +164,8 @@ The formats are: * `datadog` - 16-character decimal string (e.g. `1234567890123456`) * `uuid` - 36-character UUID string (e.g. `01234567-89ab-cdef-0123-456789abcdef`) +Note that incoming trace IDs must be in `open_telemetry` or `uuid` format. + ### Limits You may set limits on spans to prevent sending too much data to your APM. For example: From 14ccf477222d2caca2ae1cc0309bb3de9d4cefbd Mon Sep 17 00:00:00 2001 From: bryn Date: Mon, 12 Aug 2024 18:53:13 +0100 Subject: [PATCH 03/10] Changeset --- .changesets/feat_propagation_format.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .changesets/feat_propagation_format.md diff --git a/.changesets/feat_propagation_format.md b/.changesets/feat_propagation_format.md new file mode 100644 index 0000000000..497e946faa --- /dev/null +++ b/.changesets/feat_propagation_format.md @@ -0,0 +1,17 @@ +### Add `format` for trace ID propagation. ([PR #5803](https://github.com/apollographql/router/pull/5803)) + +When propagating trace ID to subgraph via header is is now possible to specify the format. + +```yaml +telemetry: + exporters: + tracing: + propagation: + request: + header_name: "my_header" + format: uuid +``` + +Note that incoming requests must some form of UUID either with or without dashes. + +By [@BrynCooke](https://github.com/BrynCooke) in https://github.com/apollographql/router/pull/5803 From 2a837723662556e5557b6abad5deea8a9ba4c8b7 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:51:36 +0100 Subject: [PATCH 04/10] Update .changesets/feat_propagation_format.md Co-authored-by: Edward Huang --- .changesets/feat_propagation_format.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changesets/feat_propagation_format.md b/.changesets/feat_propagation_format.md index 497e946faa..635ba6847c 100644 --- a/.changesets/feat_propagation_format.md +++ b/.changesets/feat_propagation_format.md @@ -9,6 +9,7 @@ telemetry: propagation: request: header_name: "my_header" + # Must be in UUID form, with or without dashes format: uuid ``` From 40c33b477dde03d2f472c81d0b22bec83f9acd10 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:52:02 +0100 Subject: [PATCH 05/10] Update docs/source/configuration/telemetry/exporters/tracing/overview.mdx Co-authored-by: Edward Huang --- .../configuration/telemetry/exporters/tracing/overview.mdx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index cc66bc119d..3d3bf23079 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -164,7 +164,11 @@ The formats are: * `datadog` - 16-character decimal string (e.g. `1234567890123456`) * `uuid` - 36-character UUID string (e.g. `01234567-89ab-cdef-0123-456789abcdef`) -Note that incoming trace IDs must be in `open_telemetry` or `uuid` format. + + +Incoming trace IDs must be in `open_telemetry` or `uuid` format. + + ### Limits From e6d90093ae41972645664e577c7b6e4bdb345693 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:52:34 +0100 Subject: [PATCH 06/10] Update docs/source/configuration/telemetry/exporters/tracing/overview.mdx Co-authored-by: Edward Huang --- .../configuration/telemetry/exporters/tracing/overview.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index 3d3bf23079..9dfa865b79 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -157,7 +157,7 @@ telemetry: | `header_name` | | Enable or disable stdout logging. | | | `format` | `hexadecimal`\|`open_telemetry`\|`decimal`\|`datadog`\|`uuid` | `hexadecimal` | The output format of the `trace_id` | -The formats are: +Valid values for `format`: * `hexadecimal` - 32-character hexadecimal string (e.g. `0123456789abcdef0123456789abcdef`) * `open_telemetry` - 32-character hexadecimal string (e.g. `0123456789abcdef0123456789abcdef`) * `decimal` - 16-character decimal string (e.g. `1234567890123456`) From 6e2c3351f395be8030fe0922c526de9f4f350abc Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:53:29 +0100 Subject: [PATCH 07/10] Update docs/source/configuration/telemetry/exporters/tracing/overview.mdx --- .../configuration/telemetry/exporters/tracing/overview.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index 9dfa865b79..13296f5046 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -154,7 +154,7 @@ telemetry: | Option | Values | Default | Description | |---------------|---------------------------------------------------------------|-----------------------------------|-------------------------------------| -| `header_name` | | Enable or disable stdout logging. | | +| `header_name` | | The name of the http header to use for propagation. | | | `format` | `hexadecimal`\|`open_telemetry`\|`decimal`\|`datadog`\|`uuid` | `hexadecimal` | The output format of the `trace_id` | Valid values for `format`: From 1cdb647df12137bd66c053eb3f713802867f53b9 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:55:19 +0100 Subject: [PATCH 08/10] Update .changesets/feat_propagation_format.md Co-authored-by: Edward Huang --- .changesets/feat_propagation_format.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.changesets/feat_propagation_format.md b/.changesets/feat_propagation_format.md index 635ba6847c..4f53374e63 100644 --- a/.changesets/feat_propagation_format.md +++ b/.changesets/feat_propagation_format.md @@ -1,6 +1,8 @@ ### Add `format` for trace ID propagation. ([PR #5803](https://github.com/apollographql/router/pull/5803)) -When propagating trace ID to subgraph via header is is now possible to specify the format. +The router now supports specifying the format of trace IDs that are propagated to subgraphs via headers. + +You can configure the format with the `format` option: ```yaml telemetry: From 6d94f138d102bc49bd01d33148a4a23659c3f1ab Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:55:26 +0100 Subject: [PATCH 09/10] Update .changesets/feat_propagation_format.md Co-authored-by: Edward Huang --- .changesets/feat_propagation_format.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.changesets/feat_propagation_format.md b/.changesets/feat_propagation_format.md index 4f53374e63..9aaad0efb2 100644 --- a/.changesets/feat_propagation_format.md +++ b/.changesets/feat_propagation_format.md @@ -15,6 +15,8 @@ telemetry: format: uuid ``` -Note that incoming requests must some form of UUID either with or without dashes. +Note that incoming requests must be some form of UUID, either with or without dashes. + +To learn about supported formats, go to [`request` configuration reference](https://apollographql.com/docs/router/configuration/telemetry/exporters/tracing/overview#request-configuration-reference) docs. By [@BrynCooke](https://github.com/BrynCooke) in https://github.com/apollographql/router/pull/5803 From 7db1615c9ec650c61b6d2f1fb276b0142bf40fa5 Mon Sep 17 00:00:00 2001 From: Bryn Cooke Date: Tue, 13 Aug 2024 10:55:51 +0100 Subject: [PATCH 10/10] Update docs/source/configuration/telemetry/exporters/tracing/overview.mdx --- .../configuration/telemetry/exporters/tracing/overview.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx index 13296f5046..c7b81cca70 100644 --- a/docs/source/configuration/telemetry/exporters/tracing/overview.mdx +++ b/docs/source/configuration/telemetry/exporters/tracing/overview.mdx @@ -154,7 +154,7 @@ telemetry: | Option | Values | Default | Description | |---------------|---------------------------------------------------------------|-----------------------------------|-------------------------------------| -| `header_name` | | The name of the http header to use for propagation. | | +| `header_name` | | | The name of the http header to use for propagation. | | `format` | `hexadecimal`\|`open_telemetry`\|`decimal`\|`datadog`\|`uuid` | `hexadecimal` | The output format of the `trace_id` | Valid values for `format`: