Skip to content

Commit 2bdfc74

Browse files
valkumcijothomas
andauthored
Fix stackdriver resources (#149)
Co-authored-by: Cijo Thomas <[email protected]>
1 parent 0b31690 commit 2bdfc74

File tree

1 file changed

+25
-15
lines changed
  • opentelemetry-stackdriver/src

1 file changed

+25
-15
lines changed

opentelemetry-stackdriver/src/lib.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl Builder {
220220

221221
let count_clone = pending_count.clone();
222222
let resource = Arc::new(RwLock::new(None));
223+
let ctx_resource = resource.clone();
223224
let future = async move {
224225
let trace_client = TraceServiceClient::new(trace_channel);
225226
let authorizer = &authenticator;
@@ -229,7 +230,7 @@ impl Builder {
229230
let log_client = log_client.clone();
230231
let pending_count = count_clone.clone();
231232
let scopes = scopes.clone();
232-
let resource = resource.clone();
233+
let resource = ctx_resource.clone();
233234
ExporterContext {
234235
trace_client,
235236
log_client,
@@ -248,7 +249,7 @@ impl Builder {
248249
pending_count,
249250
maximum_shutdown_duration: maximum_shutdown_duration
250251
.unwrap_or_else(|| Duration::from_secs(5)),
251-
resource: Arc::new(RwLock::new(None)),
252+
resource,
252253
};
253254

254255
Ok((exporter, future))
@@ -772,8 +773,7 @@ fn transform_links(links: &opentelemetry_sdk::trace::SpanLinks) -> Option<Links>
772773
// Map conventional OpenTelemetry keys to their GCP counterparts.
773774
//
774775
// https://cloud.google.com/trace/docs/trace-labels
775-
const KEY_MAP: [(&str, &str); 16] = [
776-
(semconv::resource::SERVICE_NAME, GCP_SERVICE_NAME),
776+
const KEY_MAP: [(&str, &str); 19] = [
777777
(HTTP_PATH, GCP_HTTP_PATH),
778778
(semconv::attribute::HTTP_HOST, "/http/host"),
779779
("http.request.header.host", "/http/host"),
@@ -786,10 +786,27 @@ const KEY_MAP: [(&str, &str); 16] = [
786786
(semconv::attribute::HTTP_USER_AGENT, "/http/user_agent"),
787787
(semconv::attribute::USER_AGENT_ORIGINAL, "/http/user_agent"),
788788
(semconv::attribute::HTTP_STATUS_CODE, "/http/status_code"),
789+
// https://cloud.google.com/trace/docs/trace-labels#canonical-gke
789790
(
790791
semconv::attribute::HTTP_RESPONSE_STATUS_CODE,
791792
"/http/status_code",
792793
),
794+
(
795+
semconv::attribute::K8S_CLUSTER_NAME,
796+
"g.co/r/k8s_container/cluster_name",
797+
),
798+
(
799+
semconv::attribute::K8S_NAMESPACE_NAME,
800+
"g.co/r/k8s_container/namespace",
801+
),
802+
(
803+
semconv::attribute::K8S_POD_NAME,
804+
"g.co/r/k8s_container/pod_name",
805+
),
806+
(
807+
semconv::attribute::K8S_CONTAINER_NAME,
808+
"g.co/r/k8s_container/container_name",
809+
),
793810
(semconv::trace::HTTP_ROUTE, "/http/route"),
794811
(HTTP_PATH, GCP_HTTP_PATH),
795812
];
@@ -826,7 +843,6 @@ fn status(value: opentelemetry::trace::Status) -> Option<Status> {
826843
}
827844
const TRACE_APPEND: &str = "https://www.googleapis.com/auth/trace.append";
828845
const LOGGING_WRITE: &str = "https://www.googleapis.com/auth/logging.write";
829-
const GCP_SERVICE_NAME: &str = "g.co/gae/app/module";
830846
const MAX_ATTRIBUTES_PER_SPAN: usize = 32;
831847

832848
#[cfg(test)]
@@ -920,19 +936,13 @@ mod tests {
920936
actual.attribute_map.get("/http/status_code"),
921937
Some(&AttributeValue::from(Value::I64(200))),
922938
);
923-
assert_eq!(
924-
actual.attribute_map.get("g.co/gae/app/module"),
925-
Some(&AttributeValue::from(Value::String(
926-
"Test Service Name".into()
927-
))),
928-
);
929939
}
930940

931941
#[test]
932942
fn test_too_many() {
933943
let resources = Resource::new([KeyValue::new(
934-
semcov::resource::SERVICE_NAME,
935-
"Test Service Name",
944+
semconv::attribute::USER_AGENT_ORIGINAL,
945+
"Test Service Name UA",
936946
)]);
937947
let mut attributes = Vec::with_capacity(32);
938948
for i in 0..32 {
@@ -946,9 +956,9 @@ mod tests {
946956
assert_eq!(actual.attribute_map.len(), 32);
947957
assert_eq!(actual.dropped_attributes_count, 1);
948958
assert_eq!(
949-
actual.attribute_map.get("g.co/gae/app/module"),
959+
actual.attribute_map.get("/http/user_agent"),
950960
Some(&AttributeValue::from(Value::String(
951-
"Test Service Name".into()
961+
"Test Service Name UA".into()
952962
))),
953963
);
954964
}

0 commit comments

Comments
 (0)