-
Notifications
You must be signed in to change notification settings - Fork 678
chore: decouple opentelemetry-proto from opentelemetry-sdk #3512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
scottgerring
wants to merge
7
commits into
open-telemetry:main
Choose a base branch
from
scottgerring:sgg/proto-decouple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8600202
refactor(otlp): copy transform module from opentelemetry-proto
scottgerring 0f15c5f
refactor(otlp): convert From impls to free functions
scottgerring f09832a
refactor(otlp): update exporters to use local transform functions
scottgerring 2826666
refactor(proto): remove transform module and SDK dependency
scottgerring bda7e29
remove proto->sdk dep
scottgerring 3836943
docs(proto): changelog and docs
scottgerring ec5443d
chore: fix build issue
scottgerring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| #[cfg(all( | ||
| any(feature = "trace", feature = "metrics", feature = "logs"), | ||
| any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic") | ||
| ))] | ||
| use std::time::{Duration, SystemTime, UNIX_EPOCH}; | ||
|
|
||
| #[cfg(all( | ||
| any(feature = "trace", feature = "metrics", feature = "logs"), | ||
| any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic") | ||
| ))] | ||
| pub(crate) fn to_nanos(time: SystemTime) -> u64 { | ||
| time.duration_since(UNIX_EPOCH) | ||
| .unwrap_or_else(|_| Duration::from_secs(0)) | ||
| .as_nanos() as u64 | ||
| } | ||
|
|
||
| #[cfg(all( | ||
| any(feature = "trace", feature = "metrics", feature = "logs"), | ||
| any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic") | ||
| ))] | ||
| pub(crate) mod tonic { | ||
| use opentelemetry::{Array, Value}; | ||
| use opentelemetry_proto::tonic::common::v1::{ | ||
| any_value, AnyValue, ArrayValue, InstrumentationScope, KeyValue, | ||
| }; | ||
| use std::borrow::Cow; | ||
|
|
||
| #[cfg(any(feature = "trace", feature = "logs"))] | ||
| #[derive(Debug, Default)] | ||
| pub(crate) struct ResourceAttributesWithSchema { | ||
| pub(crate) attributes: Attributes, | ||
| pub(crate) schema_url: Option<String>, | ||
| } | ||
|
|
||
| #[cfg(any(feature = "trace", feature = "logs"))] | ||
| use opentelemetry_sdk::Resource; | ||
|
|
||
| #[cfg(any(feature = "trace", feature = "logs"))] | ||
| pub(crate) fn resource_to_attributes_with_schema( | ||
| resource: &Resource, | ||
| ) -> ResourceAttributesWithSchema { | ||
| ResourceAttributesWithSchema { | ||
| attributes: resource_attributes(resource), | ||
| schema_url: resource.schema_url().map(ToString::to_string), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(any(feature = "trace", feature = "logs"))] | ||
| impl From<&Resource> for ResourceAttributesWithSchema { | ||
| fn from(resource: &Resource) -> Self { | ||
| resource_to_attributes_with_schema(resource) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn instrumentation_scope_to_proto( | ||
| library: opentelemetry::InstrumentationScope, | ||
| target: Option<Cow<'static, str>>, | ||
| ) -> InstrumentationScope { | ||
| InstrumentationScope { | ||
| name: target.map_or_else(|| library.name().to_owned(), Cow::into_owned), | ||
| version: library.version().unwrap_or_default().to_owned(), | ||
| attributes: keyvalues_to_proto(library.attributes().cloned()), | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn instrumentation_scope_ref_to_proto( | ||
| library: &opentelemetry::InstrumentationScope, | ||
| target: Option<Cow<'static, str>>, | ||
| ) -> InstrumentationScope { | ||
| InstrumentationScope { | ||
| name: target.map_or_else(|| library.name().to_owned(), Cow::into_owned), | ||
| version: library.version().unwrap_or_default().to_owned(), | ||
| attributes: keyvalues_to_proto(library.attributes().cloned()), | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| /// Wrapper type for Vec<`KeyValue`> | ||
| #[derive(Default, Debug)] | ||
| pub(crate) struct Attributes(pub(crate) ::std::vec::Vec<KeyValue>); | ||
|
|
||
| pub(crate) fn keyvalues_to_proto<I: IntoIterator<Item = opentelemetry::KeyValue>>( | ||
| kvs: I, | ||
| ) -> Vec<KeyValue> { | ||
| kvs.into_iter() | ||
| .map(|api_kv| KeyValue { | ||
| key: api_kv.key.as_str().to_string(), | ||
| value: Some(value_to_any_value(api_kv.value)), | ||
| key_strindex: 0, | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| // Kept as a `From` impl since `Attributes` is a local type, so orphan rule is satisfied. | ||
| impl<I: IntoIterator<Item = opentelemetry::KeyValue>> From<I> for Attributes { | ||
| fn from(kvs: I) -> Self { | ||
| Attributes(keyvalues_to_proto(kvs)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "logs")] | ||
| impl<K: Into<String>, V: Into<AnyValue>> FromIterator<(K, V)> for Attributes { | ||
| fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self { | ||
| Attributes( | ||
| iter.into_iter() | ||
| .map(|(k, v)| KeyValue { | ||
| key: k.into(), | ||
| value: Some(v.into()), | ||
| key_strindex: 0, | ||
| }) | ||
| .collect(), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn value_to_any_value(value: Value) -> AnyValue { | ||
| AnyValue { | ||
| value: match value { | ||
| Value::Bool(val) => Some(any_value::Value::BoolValue(val)), | ||
| Value::I64(val) => Some(any_value::Value::IntValue(val)), | ||
| Value::F64(val) => Some(any_value::Value::DoubleValue(val)), | ||
| Value::String(val) => Some(any_value::Value::StringValue(val.to_string())), | ||
| Value::Array(array) => Some(any_value::Value::ArrayValue(match array { | ||
| Array::Bool(vals) => array_into_proto(vals), | ||
| Array::I64(vals) => array_into_proto(vals), | ||
| Array::F64(vals) => array_into_proto(vals), | ||
| Array::String(vals) => array_into_proto(vals), | ||
| _ => unreachable!("Nonexistent array type"), | ||
| })), | ||
| _ => unreachable!("Nonexistent value type"), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn array_into_proto<T>(vals: Vec<T>) -> ArrayValue | ||
| where | ||
| Value: From<T>, | ||
| { | ||
| let values = vals | ||
| .into_iter() | ||
| .map(|val| value_to_any_value(Value::from(val))) | ||
| .collect(); | ||
|
|
||
| ArrayValue { values } | ||
| } | ||
|
|
||
| #[cfg(any(feature = "trace", feature = "logs"))] | ||
| pub(crate) fn resource_attributes(resource: &Resource) -> Attributes { | ||
| Attributes(keyvalues_to_proto( | ||
| resource | ||
| .iter() | ||
| .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone())), | ||
| )) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use opentelemetry::KeyValue; | ||
|
|
||
| fn assert_scope_fields( | ||
| proto_scope: &InstrumentationScope, | ||
| expected_name: &str, | ||
| expected_version: &str, | ||
| expected_attr_key: &str, | ||
| ) { | ||
| assert_eq!(proto_scope.name, expected_name); | ||
| assert_eq!(proto_scope.version, expected_version); | ||
| assert_eq!(proto_scope.attributes.len(), 1); | ||
| assert_eq!(proto_scope.attributes[0].key, expected_attr_key); | ||
| } | ||
|
|
||
| #[test] | ||
| fn instrumentation_scope_with_target_overrides_name_but_preserves_version_and_attributes() { | ||
| let scope = opentelemetry::InstrumentationScope::builder("my-lib") | ||
| .with_version("1.0.0") | ||
| .with_attributes([KeyValue::new("feature", "metrics")]) | ||
| .build(); | ||
| let target: Option<Cow<'static, str>> = Some(Cow::Borrowed("my_app::handlers")); | ||
|
|
||
| let from_owned = instrumentation_scope_to_proto(scope.clone(), target.clone()); | ||
| let from_ref = instrumentation_scope_ref_to_proto(&scope, target); | ||
|
|
||
| assert_scope_fields(&from_owned, "my_app::handlers", "1.0.0", "feature"); | ||
| assert_scope_fields(&from_ref, "my_app::handlers", "1.0.0", "feature"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn instrumentation_scope_without_target_preserves_all_fields() { | ||
| let scope = opentelemetry::InstrumentationScope::builder("my-lib") | ||
| .with_version("1.0.0") | ||
| .with_attributes([KeyValue::new("feature", "metrics")]) | ||
| .build(); | ||
| let target: Option<Cow<'static, str>> = None; | ||
|
|
||
| let from_owned = instrumentation_scope_to_proto(scope.clone(), target.clone()); | ||
| let from_ref = instrumentation_scope_ref_to_proto(&scope, target); | ||
|
|
||
| assert_scope_fields(&from_owned, "my-lib", "1.0.0", "feature"); | ||
| assert_scope_fields(&from_ref, "my-lib", "1.0.0", "feature"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More of a style suggestion. But I wonder if it would be better to package this as a extension trait against SDK resource, instead of
resource_to_attributes_with_schemafunctionUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TommyCpp thanks for taking a look, and good point!
I think this conversion is one of the few places where a method-style API could be reasonable, since it’s a high-level boundary used directly by the exporters.
That said, I’m a bit hesitant to make it look like an operation that inherently belongs on
Resource: this is really an OTLP/proto-specific projection of aResource, including how we carry the schema URL alongside attributes. Since we already have a local target type here,From<&Resource>gives us a short call site without adding an extension trait or implying this is a generalResourcecapability; maybe something like this? -->For the broader transform module I’d probably keep the explicit *_to_proto functions, since most are lower-level implementation details or external-to-external mappings where
Fromisn’t available due to orphan rules.