Skip to content

Commit e8115d3

Browse files
jszwedkoclaude
andcommitted
refactor(otlp-traces): resolve otel.scope.* gate at compile time
Now that agent_version::meets is a const fn, hoist the gate into a module-level const (EMIT_OTEL_SCOPE_META) computed once at compile time, instead of calling the gate twice per span on the transform hot path. Addresses Copilot review feedback on duplication and hot-path cost. Also make the unit test key its expectation off EMIT_OTEL_SCOPE_META and assert the emitted values, so it stays correct regardless of the DD_AGENT_VERSION the crate was built with. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent bb4eaab commit e8115d3

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

lib/saluki-components/src/common/otlp/traces/transform.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ const OTEL_LIBRARY_NAME_META_KEY: &str = "otel.library.name";
8585
const OTEL_LIBRARY_VERSION_META_KEY: &str = "otel.library.version";
8686
const OTEL_SCOPE_NAME_META_KEY: &str = "otel.scope.name";
8787
const OTEL_SCOPE_VERSION_META_KEY: &str = "otel.scope.version";
88+
89+
// Whether to emit the `otel.scope.*` span meta keys. These were added to the Datadog Agent's OTLP trace conversion in
90+
// 7.82+, so we only emit them when the Agent version ADP was built against meets that threshold. The Agent version is
91+
// baked in at build time, so this resolves to a compile-time constant and the unused branch is eliminated.
92+
const EMIT_OTEL_SCOPE_META: bool = datadog_agent_commons::agent_version::meets(7, 82, 0);
8893
const OTEL_STATUS_CODE_META_KEY: &str = "otel.status_code";
8994
const OTEL_STATUS_DESCRIPTION_META_KEY: &str = "otel.status_description";
9095
const INTERNAL_DD_HOSTNAME_KEY: &str = "_dd.hostname";
@@ -197,8 +202,7 @@ pub fn otel_span_to_dd_span(
197202
MetaString::from_static(OTEL_LIBRARY_NAME_META_KEY),
198203
AttributeValue::String(scope.name.as_str().into()),
199204
);
200-
// otel.scope.name was added in Agent 7.82+. Emit it when the Agent version meets that threshold.
201-
if datadog_agent_commons::agent_version::meets(7, 82, 0) {
205+
if EMIT_OTEL_SCOPE_META {
202206
attrs.insert(
203207
MetaString::from_static(OTEL_SCOPE_NAME_META_KEY),
204208
AttributeValue::String(scope.name.as_str().into()),
@@ -210,8 +214,7 @@ pub fn otel_span_to_dd_span(
210214
MetaString::from_static(OTEL_LIBRARY_VERSION_META_KEY),
211215
AttributeValue::String(scope.version.as_str().into()),
212216
);
213-
// otel.scope.version was added in Agent 7.82+. Emit it when the Agent version meets that threshold.
214-
if datadog_agent_commons::agent_version::meets(7, 82, 0) {
217+
if EMIT_OTEL_SCOPE_META {
215218
attrs.insert(
216219
MetaString::from_static(OTEL_SCOPE_VERSION_META_KEY),
217220
AttributeValue::String(scope.version.as_str().into()),
@@ -2418,14 +2421,14 @@ mod tests {
24182421
assert_eq!(meta("otel.library.name").as_deref(), Some("com.example.products"));
24192422
assert_eq!(meta("otel.library.version").as_deref(), Some("1.0.0"));
24202423

2421-
// The `otel.scope.*` keys are emitted only when the Agent version meets 7.82.0+.
2422-
// When DD_AGENT_VERSION is unset (test environment), the default is to emit them (version unknown → latest behavior).
2423-
let has_scope_keys = meta("otel.scope.name").is_some();
2424-
let has_scope_version = meta("otel.scope.version").is_some();
2425-
// In unit tests with no explicit DD_AGENT_VERSION, both keys should be present (default to latest behavior).
2426-
assert!(
2427-
has_scope_keys && has_scope_version,
2428-
"otel.scope.* keys should be present when Agent version is unknown (defaults to latest)"
2429-
);
2424+
// The `otel.scope.*` keys are gated on the Agent version this crate was built against (7.82.0+). Since the gate
2425+
// is resolved at build time, key the expectation off the same constant rather than assuming a build config.
2426+
if EMIT_OTEL_SCOPE_META {
2427+
assert_eq!(meta("otel.scope.name").as_deref(), Some("com.example.products"));
2428+
assert_eq!(meta("otel.scope.version").as_deref(), Some("1.0.0"));
2429+
} else {
2430+
assert_eq!(meta("otel.scope.name"), None);
2431+
assert_eq!(meta("otel.scope.version"), None);
2432+
}
24302433
}
24312434
}

0 commit comments

Comments
 (0)