Search code examples
.netelastic-stackopen-telemetry

How do I set the Environment for Elastic Cloud Observability using Open Telemetry in DotNet C#?


I have configured my tracing provider, instrumentation and OTLP Exporter which works great in Elastic Cloud Observability dashboards.

It is easy to set the service name but there does not seem seem to be a common API to set the Environment name.

So when I click on Traces all I see is All or Not Defined.

enter image description here


Solution

  • After carefully skimming the Elastic Search documentation around Open Telemetry I found absolutely no information on how to do this!?

    -Edit - Recently Elastic had allowed W3C to use the Elastics Common Schema for... everything. So reference that going forward

    The attribute that you need to pass along with OTEL Exporter would be like

    • service.environment *
    • but I also pass deployment.environment
    • and 'host.name'
    • and 'host.hostname'
    • any other Elastic Common Schema that make sense

    Here is the code snippet that will solve the problem for you.

     var otelAttributes = new List<KeyValuePair<string, object>>();
     otelAttributes.Add(new KeyValuePair<string, object>("deployment.environment", environmentName));
    

    Then you need to add the attributes within the ResourceBuilder

     sdkSetup
     .AddSource("*")
     .SetSampler(new TraceIdRatioBasedSampler(samplerRateValue))
     .SetResourceBuilder(
          ResourceBuilder
           CreateDefault()
           .AddService(ResourceNameHelper.ServiceName)
           .AddAttributes(otelAttributes) // <-- Over Here
           .AddTelemetrySdk()
      )
    

    And now environments will be tagged with all your Traces, Metrics and Logs

    enter image description here