Skip to content

Commit fa0a3bb

Browse files
kriswuolletthdost
authored andcommitted
Add generated modules that output const &str for tracing compatibility (open-telemetry#1334)
* feat: add generated modules that output const &str for tracing compatibility * fix: add tracing as a dev-dependency for doc examples * fix: remove unused code * fix: remove tracing examples from semconv docs * docs: remove extra whitespace that was added previously
1 parent c82fddc commit fa0a3bb

File tree

2 files changed

+48
-36
lines changed
  • opentelemetry-datadog/src/exporter
  • opentelemetry-stackdriver/src

2 files changed

+48
-36
lines changed

opentelemetry-datadog/src/exporter/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl DatadogPipelineBuilder {
212212
cfg.resource = Cow::Owned(Resource::new(
213213
cfg.resource
214214
.iter()
215-
.filter(|(k, _v)| **k != semcov::resource::SERVICE_NAME)
215+
.filter(|(k, _v)| k.as_str() != semcov::resource::SERVICE_NAME)
216216
.map(|(k, v)| KeyValue::new(k.clone(), v.clone())),
217217
));
218218
cfg
@@ -226,7 +226,7 @@ impl DatadogPipelineBuilder {
226226
} else {
227227
let service_name = SdkProvidedResourceDetector
228228
.detect(Duration::from_secs(0))
229-
.get(semcov::resource::SERVICE_NAME)
229+
.get(semcov::resource::SERVICE_NAME.into())
230230
.unwrap()
231231
.to_string();
232232
(

opentelemetry-stackdriver/src/lib.rs

+46-34
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ use opentelemetry_sdk::{
3737
},
3838
Resource,
3939
};
40-
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
41-
use opentelemetry_semantic_conventions::trace::{
42-
HTTP_METHOD, HTTP_ROUTE, HTTP_STATUS_CODE, HTTP_TARGET, HTTP_URL,
43-
};
40+
use opentelemetry_semantic_conventions as semconv;
4441
use thiserror::Error;
4542
#[cfg(any(feature = "yup-authorizer", feature = "gcp_auth"))]
4643
use tonic::metadata::MetadataValue;
@@ -54,8 +51,11 @@ use yup_oauth2::authenticator::Authenticator;
5451
#[allow(clippy::derive_partial_eq_without_eq)] // tonic doesn't derive Eq for generated types
5552
pub mod proto;
5653

57-
const HTTP_HOST: Key = Key::from_static_str("http.host");
58-
const HTTP_USER_AGENT: Key = Key::from_static_str("http.user_agent");
54+
const HTTP_HOST: &str = "http.host";
55+
const HTTP_PATH: &str = "http.path";
56+
const HTTP_USER_AGENT: &str = "http.user_agent";
57+
58+
const GCP_HTTP_PATH: &str = "/http/path";
5959

6060
use proto::devtools::cloudtrace::v2::span::time_event::Annotation;
6161
use proto::devtools::cloudtrace::v2::span::{
@@ -738,14 +738,14 @@ impl From<(Vec<KeyValue>, &Resource)> for Attributes {
738738
return None;
739739
}
740740

741-
if k == SERVICE_NAME {
741+
if k.as_str() == semconv::resource::SERVICE_NAME {
742742
return Some((GCP_SERVICE_NAME.to_owned(), v.into()));
743-
} else if key == HTTP_PATH_ATTRIBUTE {
743+
} else if key == HTTP_PATH {
744744
return Some((GCP_HTTP_PATH.to_owned(), v.into()));
745745
}
746746

747747
for (otel_key, gcp_key) in KEY_MAP {
748-
if otel_key == &k {
748+
if otel_key == k.as_str() {
749749
return Some((gcp_key.to_owned(), v.into()));
750750
}
751751
}
@@ -783,14 +783,15 @@ fn transform_links(links: &opentelemetry_sdk::trace::SpanLinks) -> Option<Links>
783783
}
784784

785785
// Map conventional OpenTelemetry keys to their GCP counterparts.
786-
const KEY_MAP: [(&Key, &str); 7] = [
787-
(&HTTP_HOST, "/http/host"),
788-
(&HTTP_METHOD, "/http/method"),
789-
(&HTTP_TARGET, "/http/path"),
790-
(&HTTP_URL, "/http/url"),
791-
(&HTTP_USER_AGENT, "/http/user_agent"),
792-
(&HTTP_STATUS_CODE, "/http/status_code"),
793-
(&HTTP_ROUTE, "/http/route"),
786+
const KEY_MAP: [(&str, &str); 8] = [
787+
(HTTP_HOST, "/http/host"),
788+
(semconv::trace::HTTP_METHOD, "/http/method"),
789+
(semconv::trace::HTTP_TARGET, "/http/path"),
790+
(semconv::trace::HTTP_URL, "/http/url"),
791+
(HTTP_USER_AGENT, "/http/user_agent"),
792+
(semconv::trace::HTTP_STATUS_CODE, "/http/status_code"),
793+
(semconv::trace::HTTP_ROUTE, "/http/route"),
794+
(HTTP_PATH, GCP_HTTP_PATH),
794795
];
795796

796797
impl From<opentelemetry::trace::SpanKind> for SpanKind {
@@ -822,8 +823,6 @@ fn status(value: opentelemetry::trace::Status) -> Option<Status> {
822823
}
823824
const TRACE_APPEND: &str = "https://www.googleapis.com/auth/trace.append";
824825
const LOGGING_WRITE: &str = "https://www.googleapis.com/auth/logging.write";
825-
const HTTP_PATH_ATTRIBUTE: &str = "http.path";
826-
const GCP_HTTP_PATH: &str = "/http/path";
827826
const GCP_SERVICE_NAME: &str = "g.co/gae/app/module";
828827
const MAX_ATTRIBUTES_PER_SPAN: usize = 32;
829828

@@ -839,33 +838,40 @@ mod tests {
839838
let mut attributes = Vec::with_capacity(capacity);
840839

841840
// hostAttribute = "http.host"
842-
attributes.push(HTTP_HOST.string("example.com:8080"));
841+
attributes.push(KeyValue::new(HTTP_HOST, "example.com:8080"));
843842

844843
// methodAttribute = "http.method"
845-
attributes.push(semcov::trace::HTTP_METHOD.string("POST"));
844+
attributes.push(KeyValue::new(semcov::trace::HTTP_METHOD, "POST"));
846845

847846
// pathAttribute = "http.path"
848-
attributes.push(KeyValue::new(
849-
"http.path",
850-
Value::String("/path/12314/?q=ddds#123".into()),
851-
));
847+
attributes.push(KeyValue::new(HTTP_PATH, "/path/12314/?q=ddds#123"));
852848

853849
// urlAttribute = "http.url"
854-
attributes.push(
855-
semcov::trace::HTTP_URL.string("https://example.com:8080/webshop/articles/4?s=1"),
856-
);
850+
attributes.push(KeyValue::new(
851+
semcov::trace::HTTP_URL,
852+
"https://example.com:8080/webshop/articles/4?s=1",
853+
));
857854

858855
// userAgentAttribute = "http.user_agent"
859-
attributes.push(HTTP_USER_AGENT.string("CERN-LineMode/2.15 libwww/2.17b3"));
856+
attributes.push(KeyValue::new(
857+
HTTP_USER_AGENT,
858+
"CERN-LineMode/2.15 libwww/2.17b3",
859+
));
860860

861861
// statusCodeAttribute = "http.status_code"
862-
attributes.push(semcov::trace::HTTP_STATUS_CODE.i64(200));
862+
attributes.push(KeyValue::new(semcov::trace::HTTP_STATUS_CODE, 200i64));
863863

864864
// statusCodeAttribute = "http.route"
865-
attributes.push(semcov::trace::HTTP_ROUTE.string("/webshop/articles/:article_id"));
865+
attributes.push(KeyValue::new(
866+
semcov::trace::HTTP_ROUTE,
867+
"/webshop/articles/:article_id",
868+
));
866869

867870
// serviceAttribute = "service.name"
868-
let resources = Resource::new([semcov::resource::SERVICE_NAME.string("Test Service Name")]);
871+
let resources = Resource::new([KeyValue::new(
872+
semcov::resource::SERVICE_NAME,
873+
"Test Service Name",
874+
)]);
869875

870876
let actual: Attributes = (attributes, &resources).into();
871877

@@ -919,7 +925,10 @@ mod tests {
919925

920926
#[test]
921927
fn test_too_many() {
922-
let resources = Resource::new([semcov::resource::SERVICE_NAME.string("Test Service Name")]);
928+
let resources = Resource::new([KeyValue::new(
929+
semcov::resource::SERVICE_NAME,
930+
"Test Service Name",
931+
)]);
923932
let mut attributes = Vec::with_capacity(32);
924933
for i in 0..32 {
925934
attributes.push(KeyValue::new(
@@ -942,7 +951,10 @@ mod tests {
942951

943952
#[test]
944953
fn test_attributes_mapping_http_target() {
945-
let attributes = vec![semcov::trace::HTTP_TARGET.string("/path/12314/?q=ddds#123")];
954+
let attributes = vec![KeyValue::new(
955+
semcov::trace::HTTP_TARGET,
956+
"/path/12314/?q=ddds#123",
957+
)];
946958

947959
// hostAttribute = "http.target"
948960

0 commit comments

Comments
 (0)