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

Viewed 36

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

1 Answers

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

But they did have an example, when I scrolled the code over to the right revealed some extra attributes they are adding. Without even mentioning it in the lines just under it.

The attribute that you need to pass along with OTEL Exporter is deployment.environment

Here is 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

Related