opentelemetry-rust icon indicating copy to clipboard operation
opentelemetry-rust copied to clipboard

Difficulty using Resource::from_detectors to set `service.name` correctly

Open BrynCooke opened this issue 2 years ago • 6 comments

SdkProvidedResource detector currently has to know about all other detectors to correctly set service.name.

Detectors currently fill out resources in a last one wins fashion. In the following situation EnvResourceDetector, upon finding service.name will overwrite the value even if the SdkProvidedResourceDetector obtained it from OTEL_SERVICE_NAME.

let resource = Resource::from_detectors(
            time::Duration::from_secs(5),
            vec![
                Box::new(SdkProvidedResourceDetector),
                Box::new(EnvResourceDetector::new()),
            ],
        );

It's possible to reverse to ordering of the resource detectors to get the right result, but this doesn't support adding other resource detectors.

let resource = Resource::from_detectors(
            time::Duration::from_secs(5),
            vec![
                Box::new(MyResourceDetector::new()),
                Box::new(EnvResourceDetector::new()),
                Box::new(SdkProvidedResourceDetector),
            ],
        );

In this case SdkProvidedResourceDetector will override any value obtained by MyResourceDetector with unknown_service if it can't find anything from env.

Relates to #1295

BrynCooke avatar Oct 13 '23 12:10 BrynCooke

The issue seems to be that if a detector does defaulting then it leads to issues. The following works:

 let resource = Resource::from_detectors(
            time::Duration::from_secs(5),
            vec![
                Box::new(MyResourceDetector::new()),
                Box::new(EnvResourceDetector::new()),
                Box::new(EnvServiceNameDetector::new()), //Only sets service.name if it exists in env.
            ],
        );
        

Proposal: If instead Resouce itself is responsible for defaulting service name then it can take a look at the attributes after all detectors have been resolved and see if service name is missing.

BrynCooke avatar Oct 13 '23 12:10 BrynCooke

I'm interested in this as well. I initially made an attempt extending SdkProvidedResourceDetector with a fallback_service_name: Option<String> (deriving Default) and adding a constructor for the fallback name, but it requires everyone to "properly" create an instance (you can't just use SdkProvidedResourceDetector.detect anymore).

I think your approach of letting Resource take care of defaulting a service name if unset is nicer, and I favor it.

flokli avatar Jan 03 '24 12:01 flokli

@pitoniak32 Is this something you can help triage, as you already helping in this area?

cijothomas avatar Dec 11 '24 03:12 cijothomas

I started looking into this one

Resource detector packages MAY detect resource information from multiple possible sources and merge the result using the Merge operation described above.

The spec seems to state that the merge of the result of multiple detectors should be resolved according to https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#merge.

I just made the change so thats accurate, I will add some tests around this to make sure we have the correct behavior. I will also see how that aligns with what is being requested in this post.

I tend to agree that it's not a great experience that defaults will override existing values.

pitoniak32 avatar Jan 27 '25 21:01 pitoniak32

Moving out of 0.28 milestone.

@pitoniak32 Please continue to work on it, I simply moved out of 0.28 which is about to be shipped.

cijothomas avatar Feb 06 '25 01:02 cijothomas

Removing from Logging SDK Stable milestone.

cijothomas avatar Mar 10 '25 16:03 cijothomas