Skip to content

Commit 41fdac6

Browse files
authored
Propagate Resource from Span to proto (Tonic) (#386) (#390)
1 parent aa14bf7 commit 41fdac6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

opentelemetry-otlp/src/transform/traces.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::proto::grpcio::trace::{
1616
};
1717

1818
use crate::transform::common::{to_nanos, Attributes};
19-
use opentelemetry::sdk::export::trace::SpanData;
19+
use opentelemetry::sdk::{export::trace::SpanData, self};
2020
use opentelemetry::trace::{Link, SpanKind, StatusCode};
2121

2222
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
@@ -120,13 +120,21 @@ impl From<Link> for Span_Link {
120120
}
121121
}
122122

123+
fn resource_attributes(resource: &sdk::Resource) -> Attributes {
124+
resource
125+
.iter()
126+
.map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone()))
127+
.collect::<Vec<_>>()
128+
.into()
129+
}
130+
123131
impl From<SpanData> for ResourceSpans {
124132
#[cfg(feature = "tonic")]
125133
fn from(source_span: SpanData) -> Self {
126134
let span_kind: span::SpanKind = source_span.span_kind.into();
127135
ResourceSpans {
128136
resource: Some(Resource {
129-
attributes: Default::default(),
137+
attributes: resource_attributes(&source_span.resource).0,
130138
dropped_attributes_count: 0,
131139
}),
132140
instrumentation_library_spans: vec![InstrumentationLibrarySpans {
@@ -183,15 +191,9 @@ impl From<SpanData> for ResourceSpans {
183191

184192
#[cfg(all(feature = "grpc-sys", not(feature = "tonic")))]
185193
fn from(source_span: SpanData) -> Self {
186-
let resource_attributes: Attributes = source_span
187-
.resource
188-
.iter()
189-
.map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone()))
190-
.collect::<Vec<_>>()
191-
.into();
192194
ResourceSpans {
193195
resource: SingularPtrField::from(Some(Resource {
194-
attributes: resource_attributes.0,
196+
attributes: resource_attributes(&source_span.resource).0,
195197
dropped_attributes_count: 0,
196198
..Default::default()
197199
})),

0 commit comments

Comments
 (0)