# Unable to specify Environment value when using APM in .NET with AddOtlpExporter

**URL:** <https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169>\
**Category:** APM\
**Tags:** dotnet, language-clients\
**Created:** [March 31, 2022, 8:43am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169 "2022-03-31T08:43:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vagif\_Abilov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vagif_abilov/32/788_2.png) [@Vagif\_Abilov](https://discuss.elastic.co/u/Vagif_Abilov)\
**Post date:** [March 31, 2022, 8:43am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/1 "2022-03-31T08:43:52Z")

</div>

My application sends traces to the Elastic APM (v.8.1) using .NET OpenTelemetry OTLP Exporter:

```auto
builder.AddOtlpExporter(o =>
{
  o.Endpoint = new Uri("...");
  o.Headers = "Authorization=Bearer ...";
}

```

It works fines and traces are sent to the specified Elastic APM endpoint URL.  
I also need to distinguish between deployment environments (test, stage, prod), this is done by setting hostContext.HostingEnvironmentName to the respective value. But the value is ignored by the Elastic APM. I tried other alternative like Environment.SetEnvironmentVariable("ELASTIC\_APM\_ENVIRONMENT", "test"), but nothing worked.

I know if I import ElasticAPM NuGet packages it will do this for me, but I would like to stay with bare OTLP Exporter. Is it possible to specify environment name in that case?

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [March 31, 2022, 9:19am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/2 "2022-03-31T09:19:21Z")

</div>

Hi @Vagif_Abilov,

for this, you'll need to configure the .NET OpenTelemetry OTLP Exporter to send the environment. It's worth to keep in mind that that component is not from Elastic, so that's why the `ELASTIC_APM_ENVIRONMENT` is ignored - that is known to the Elastic .NET APM Agent (and other Elastic APM Agents) - but not for the OpenTelemetry agent.

I think you can do this with the .NET OTel libraries as well. Could you try something like this?

```auto
var resourceBuilder = ResourceBuilder
                .CreateDefault()
                .AddAttributes(new Dictionary<string, object>
                {
                    {"environment", "production"}
                });

```

and then pass it to the exporter, like this:

```auto
X.AddOpenTelemetryTracing(i => i.SetResourceBuilder(resourceBuilder)); //don't forget to add other things you need...

```

---

<div class="post-metadata">

**Author:** ![Vagif\_Abilov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vagif_abilov/32/788_2.png) [@Vagif\_Abilov](https://discuss.elastic.co/u/Vagif_Abilov)\
**Post date:** [March 31, 2022, 10:23am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/3 "2022-03-31T10:23:22Z")

</div>

Hi @GregKalapos,

Thank you for a quick response. Yes, I already tried that but it didn't work. Here's my code (in F# but very similar to C# code):

```auto
        let resource =
            ResourceBuilder
                .CreateDefault()
                .AddService(Assembly.GetEntryAssembly().GetName().Name, serviceInstanceId = $"{Dns.GetHostName()}")
                .AddAttributes(dict ["environment", box "production"])
        let builder =
            builder
                .AddSource(getAssemblyName ())
                .SetResourceBuilder(resource)
                .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddSqlClientInstrumentation(fun o ->
                    o.SetDbStatementForText <- true
                    o.RecordException <- true)
                .AddOtlpExporter(fun o ->
                    o.Protocol <- OpenTelemetry.Exporter.OtlpExportProtocol.Grpc
                    o.Endpoint <- Uri(elasticSettings.["Endpoint"])
                    o.Headers <- "Authorization=Bearer " + elasticSettings.["SecretToken"])

```

---

<div class="post-metadata">

**Author:** ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)\
**Post date:** [March 31, 2022, 11:10am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/4 "2022-03-31T11:10:20Z")

</div>

Hey Vagif!

I think the correct semantic convention attribute to apply here is `deployment.environment` see:

> <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/deployment_environment.md>

And here where we read that value in `apm-server` [apm-server/metadata.go at f2278410fabe38b0a12c0f88d62e7c2871084001 · elastic/apm-server · GitHub](https://github.com/elastic/apm-server/blob/f2278410fabe38b0a12c0f88d62e7c2871084001/processor/otel/metadata.go#L54)

---

<div class="post-metadata">

**Author:** ![Vagif\_Abilov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vagif_abilov/32/788_2.png) [@Vagif\_Abilov](https://discuss.elastic.co/u/Vagif_Abilov)\
**Post date:** [April 1, 2022, 6:41am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/5 "2022-04-01T06:41:28Z")

</div>

> [@Martijn\_Laarman](#):
>
> `deployment.environment`

Bingo! That did the trick. deployment\_environment is the anwer.

Thanks you @Martijn_Laarman and @GregKalapos

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 22, 2022, 2:41am UTC](https://discuss.elastic.co/t/unable-to-specify-environment-value-when-using-apm-in-net-with-addotlpexporter/301169/6 "2022-04-22T02:41:47Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
