open-telemetry / opentelemetry-dotnet

The OpenTelemetry .NET Client
https://opentelemetry.io
Apache License 2.0
3.2k stars 758 forks source link

OtelEnvResourceDetector creates span attributes instead of resource attributes #1725

Closed markfink-splunk closed 3 years ago

markfink-splunk commented 3 years ago

Bug Report

OpenTelemetry 1.0.0-rc1.1

*

net5.0

*

Symptom

services.AddOpenTelemetryTracing(
    (builder) => builder
        .SetSampler(new AlwaysOnSampler())
        .SetResourceBuilder(ResourceBuilder.CreateDefault()
            .AddEnvironmentVariableDetector())

The above reads OTEL_RESOURCE_ATTRIBUTES and creates a resource attribute/label for service.name. But for anything else, it creates a span attribute. For instance...

OTEL_RESOURCE_ATTRIBUTES="service.name=cartservice,environment=hipster_shop"

This creates a resource attribute for service.name, but it creates a span attribute for environment. With all other languages (and I've tried them all now), OTEL_RESOURCE_ATTRIBUTES are always created as resource attributes.

What is the expected behavior?

OTEL_RESOURCE_ATTRIBUTES should be created as resource attributes, not span attributes.

What did you expect to see?

I expect to see this at the Otel Collector via the logging exporter:

Resource labels:
     -> service.name: STRING(cartservice)
     -> telemetry.sdk.version: STRING(1.0.0.1)
     -> telemetry.sdk.name: STRING(opentelemetry)
     -> telemetry.sdk.language: STRING(dotnet)
     -> environment: STRING(hipster_shop)

What is the actual behavior?

This is what I'm actually seeing at the Otel Collector. See how service.name shows up under resource labels at top. "environment" is showing up, but that's coming from the Otel Collector (via resource processor), not OTEL_RESOURCE_ATTRIBUTES. Scroll down and you see the OTEL_RESOURCE_ATTRIBUTES environment value showing up under Attributes. That's the issue. So now I have a conflict between the resource label and the span attribute.

Resource labels:
     -> service.name: STRING(cartservice)
     -> telemetry.sdk.version: STRING(1.0.0.1)
     -> telemetry.sdk.name: STRING(opentelemetry)
     -> telemetry.sdk.language: STRING(dotnet)
     -> environment: STRING(testbed)
InstrumentationLibrarySpans #0
InstrumentationLibrary  
Span #0
    Trace ID       : c6e55963e147cf48940e0aa16f7e70de
    Parent ID      : 25dcc6367ac72544
    ID             : e125b846d8829040
    Name           : PING
    Kind           : SPAN_KIND_CLIENT
    Start time     : 2021-01-25 23:58:10.641438 +0000 UTC
    End time       : 2021-01-25 23:58:10.643552 +0000 UTC
    Status code    : STATUS_CODE_UNSET
    Status message : 
Attributes:
     -> db.system: STRING(redis)
     -> net.peer.name: STRING(markf-0398)
     -> otel.library.version: STRING(1.0.0.1)
     -> db.redis.database_index: STRING(-1)
     -> otel.library.name: STRING(OpenTelemetry.StackExchange.Redis)
     -> db.statement: STRING(PING)
     -> environment: STRING(hipster_shop)
     -> net.peer.port: STRING(6379)
     -> otel.status_code: STRING(0)
     -> db.redis.flags: STRING(None)
     -> peer.service: STRING(markf-0398:6379)
     -> net.host.ip: STRING(172.17.0.5)
cijothomas commented 3 years ago

Could you try the daily build? I ran the example here: https://github.com/open-telemetry/opentelemetry-dotnet/blob/master/examples/Console/TestOtlpExporter.cs

with

.SetResourceBuilder(
                    ResourceBuilder.CreateDefault()
                        .AddEnvironmentVariableDetector())

and the following as env variable: env=cijo,service.name=cartservice

My output is pasted below, which shows Resources populated as expected.

Resource labels:
     -> telemetry.sdk.name: STRING(opentelemetry)
     -> telemetry.sdk.language: STRING(dotnet)
     -> telemetry.sdk.version: STRING(1.0.0.0)
     -> env: STRING(cijo)
     -> service.name: STRING(cartservice)
InstrumentationLibrarySpans #0
InstrumentationLibrary Samples.SampleServer
Span #0
    Trace ID       : 8256f4ddc87ef844b954edf975e3ca95
    Parent ID      : f04282da17fe6f44
    ID             : 61955aafdccf8841
    Name           : ReadStream
    Kind           : SPAN_KIND_CONSUMER
    Start time     : 2021-01-27 01:42:52.9067729 +0000 UTC
    End time       : 2021-01-27 01:42:52.9120235 +0000 UTC
Events:
SpanEvent #0
     -> Name: StreamReader.ReadToEnd
     -> Timestamp: 1611711772911302100
     -> DroppedAttributesCount: 0
Span #1
    Trace ID       : 8256f4ddc87ef844b954edf975e3ca95
    Parent ID      :
    ID             : f04282da17fe6f44
    Name           : POST:/api/request/
    Kind           : SPAN_KIND_SERVER
    Start time     : 2021-01-27 01:42:52.8896798 +0000 UTC
    End time       : 2021-01-27 01:42:52.9287588 +0000 UTC
Attributes:
     -> http.header.traceparent: STRING(00-1c8ecf589e866944932798713eafd2b9-6587a54dfd610e42-01)
     -> http.header.Content-Length: STRING(36)
     -> http.header.Content-Type: STRING(text/plain; charset=utf-8)
     -> http.header.Host: STRING(localhost:19999)
     -> request.content: STRING(client message: 1/26/2021 5:42:52 PM)
     -> request.length: STRING(36)
markfink-splunk commented 3 years ago

@cijothomas Thank you sir. I wasn't aware of the nightly builds. I tried out the latest build and it worked fine.