Skip to content

Commit 9177a10

Browse files
Extract opentelemetry ID retrieval code into a macro (#27)
Extract opentelemetry fetching methods into a macro to reuse with different otel versions to deduplicate code. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 2478acb commit 9177a10

File tree

1 file changed

+55
-122
lines changed

1 file changed

+55
-122
lines changed

src/layer/mod.rs

Lines changed: 55 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,131 +1001,64 @@ where
10011001
SchemaKey::from("openTelemetry"),
10021002
JsonValue::DynamicFromSpan(Box::new(|span| {
10031003
let mut ids: Option<serde_json::Value> = None;
1004-
#[cfg(feature = "tracing-opentelemetry-0-31")]
1005-
{
1006-
use opentelemetry_0_30::trace::{TraceContextExt, TraceId};
10071004

1008-
ids = ids.or_else(|| {
1009-
span.extensions()
1010-
.get::<tracing_opentelemetry_0_31::OtelData>()
1011-
.and_then(|otel_data| {
1012-
// We should use the parent first if available because we can
1013-
// create a new trace and then change the parent. In that case
1014-
// the value in the builder is not updated.
1015-
let mut trace_id =
1016-
otel_data.parent_cx.span().span_context().trace_id();
1017-
if trace_id == TraceId::INVALID {
1018-
trace_id = otel_data.builder.trace_id?;
1019-
}
1020-
let span_id = otel_data.builder.span_id?;
1021-
1022-
Some(serde_json::json!({
1023-
"traceId": trace_id.to_string(),
1024-
"spanId": span_id.to_string(),
1025-
}))
1026-
})
1027-
});
1005+
macro_rules! otel_extraction {
1006+
($feature:literal, $tracing_otel_crate:ident, $otel_crate:ident) => {
1007+
#[cfg(feature = $feature)]
1008+
{
1009+
use $otel_crate::trace::{TraceContextExt, TraceId};
1010+
ids = ids.or_else(|| {
1011+
span.extensions()
1012+
.get::<$tracing_otel_crate::OtelData>()
1013+
.and_then(|otel_data| {
1014+
// We should use the parent first if available because
1015+
// we can create a new trace and then change the
1016+
// parent. In that case the value in the builder is not
1017+
// updated.
1018+
let mut trace_id = otel_data
1019+
.parent_cx
1020+
.span()
1021+
.span_context()
1022+
.trace_id();
1023+
if trace_id == TraceId::INVALID {
1024+
trace_id = otel_data.builder.trace_id?;
1025+
}
1026+
let span_id = otel_data.builder.span_id?;
1027+
Some(serde_json::json!({
1028+
"traceId": trace_id.to_string(),
1029+
"spanId": span_id.to_string(),
1030+
}))
1031+
})
1032+
});
1033+
}
1034+
};
10281035
}
1029-
#[cfg(feature = "tracing-opentelemetry-0-30")]
1030-
{
1031-
use opentelemetry_0_29::trace::{TraceContextExt, TraceId};
10321036

1033-
ids = ids.or_else(|| {
1034-
span.extensions()
1035-
.get::<tracing_opentelemetry_0_30::OtelData>()
1036-
.and_then(|otel_data| {
1037-
// We should use the parent first if available because we can
1038-
// create a new trace and then change the parent. In that case
1039-
// the value in the builder is not updated.
1040-
let mut trace_id =
1041-
otel_data.parent_cx.span().span_context().trace_id();
1042-
if trace_id == TraceId::INVALID {
1043-
trace_id = otel_data.builder.trace_id?;
1044-
}
1045-
let span_id = otel_data.builder.span_id?;
1046-
1047-
Some(serde_json::json!({
1048-
"traceId": trace_id.to_string(),
1049-
"spanId": span_id.to_string(),
1050-
}))
1051-
})
1052-
});
1053-
}
1054-
#[cfg(feature = "tracing-opentelemetry-0-29")]
1055-
{
1056-
use opentelemetry_0_28::trace::{TraceContextExt, TraceId};
1057-
1058-
ids = ids.or_else(|| {
1059-
span.extensions()
1060-
.get::<tracing_opentelemetry_0_29::OtelData>()
1061-
.and_then(|otel_data| {
1062-
// We should use the parent first if available because we can
1063-
// create a new trace and then change the parent. In that case
1064-
// the value in the builder is not updated.
1065-
let mut trace_id =
1066-
otel_data.parent_cx.span().span_context().trace_id();
1067-
if trace_id == TraceId::INVALID {
1068-
trace_id = otel_data.builder.trace_id?;
1069-
}
1070-
let span_id = otel_data.builder.span_id?;
1071-
1072-
Some(serde_json::json!({
1073-
"traceId": trace_id.to_string(),
1074-
"spanId": span_id.to_string(),
1075-
}))
1076-
})
1077-
});
1078-
}
1079-
#[cfg(feature = "tracing-opentelemetry-0-28")]
1080-
{
1081-
use opentelemetry_0_27::trace::{TraceContextExt, TraceId};
1082-
1083-
ids = ids.or_else(|| {
1084-
span.extensions()
1085-
.get::<tracing_opentelemetry_0_28::OtelData>()
1086-
.and_then(|otel_data| {
1087-
// We should use the parent first if available because we can
1088-
// create a new trace and then change the parent. In that case
1089-
// the value in the builder is not updated.
1090-
let mut trace_id =
1091-
otel_data.parent_cx.span().span_context().trace_id();
1092-
if trace_id == TraceId::INVALID {
1093-
trace_id = otel_data.builder.trace_id?;
1094-
}
1095-
let span_id = otel_data.builder.span_id?;
1096-
1097-
Some(serde_json::json!({
1098-
"traceId": trace_id.to_string(),
1099-
"spanId": span_id.to_string(),
1100-
}))
1101-
})
1102-
});
1103-
}
1104-
#[cfg(feature = "opentelemetry")]
1105-
{
1106-
use opentelemetry_0_24::trace::{TraceContextExt, TraceId};
1107-
1108-
ids = ids.or_else(|| {
1109-
span.extensions()
1110-
.get::<tracing_opentelemetry_0_25::OtelData>()
1111-
.and_then(|otel_data| {
1112-
// We should use the parent first if available because we can
1113-
// create a new trace and then change the parent. In that case
1114-
// the value in the builder is not updated.
1115-
let mut trace_id =
1116-
otel_data.parent_cx.span().span_context().trace_id();
1117-
if trace_id == TraceId::INVALID {
1118-
trace_id = otel_data.builder.trace_id?;
1119-
}
1120-
let span_id = otel_data.builder.span_id?;
1121-
1122-
Some(serde_json::json!({
1123-
"traceId": trace_id.to_string(),
1124-
"spanId": span_id.to_string(),
1125-
}))
1126-
})
1127-
});
1128-
}
1037+
otel_extraction!(
1038+
"tracing-opentelemetry-0-31",
1039+
tracing_opentelemetry_0_31,
1040+
opentelemetry_0_30
1041+
);
1042+
otel_extraction!(
1043+
"tracing-opentelemetry-0-30",
1044+
tracing_opentelemetry_0_30,
1045+
opentelemetry_0_29
1046+
);
1047+
otel_extraction!(
1048+
"tracing-opentelemetry-0-29",
1049+
tracing_opentelemetry_0_29,
1050+
opentelemetry_0_28
1051+
);
1052+
otel_extraction!(
1053+
"tracing-opentelemetry-0-28",
1054+
tracing_opentelemetry_0_28,
1055+
opentelemetry_0_27
1056+
);
1057+
otel_extraction!(
1058+
"opentelemetry",
1059+
tracing_opentelemetry_0_25,
1060+
opentelemetry_0_24
1061+
);
11291062

11301063
ids
11311064
})),

0 commit comments

Comments
 (0)