• Home
  • Extend Dynatrace
  • Send data to Dynatrace with OpenTelemetry
  • OpenTelemetry metrics
  • Instrument for metrics
  • Instrument .NET applications with OpenTelemetry Metrics

Instrument .NET applications with OpenTelemetry Metrics

This guide shows how to instrument your .NET application with OpenTelemetry and export the metrics to Dynatrace.

  • To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.
  • To learn how to export traces to Dynatrace with OpenTelemetry, see Instrument Dotnet applications with OpenTelemetry.

Overview

To monitor your .NET application with OpenTelemetry

Instrument your application

Send data to Dynatrace

Verify that the traces are ingested into Dynatrace

Instrument your application

To instrument your application, add the package reference below to your .csproj file.

plaintext
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.8" />

At any point you want to collect metrics, you need to add the meter and an instrument. The meter should be created only once and then shared globally through the entire application lifecycle. Re-use the same meter when creating your instruments.
In this case, we used the counter instrument, which only supports non-negative increments. To find the instrument that fits your needs, see the official OpenTelemetry documentation.

  • Set names for the meter and the instrument.
  • Add a description and attributes to the instrument as you see fit.
cs
List<KeyValuePair<string, object>> dt_metadata = new List<KeyValuePair<string, object>>(); foreach (string name in new string[] {"dt_metadata_e617c525669e072eebe3d0f08212e8f2.properties", "/var/lib/dynatrace/enrichment/dt_metadata.properties"}) { try { foreach (string line in System.IO.File.ReadAllLines(System.IO.File.ReadAllText(name))) { var keyvalue = line.Split("="); dt_metadata.Add( new KeyValuePair<string, object>(keyvalue[0], keyvalue[1])); } } catch { } } Meter meter = new Meter("my_meter", "1.0.0"); Counter<long> counter = meter.CreateCounter<long>("my_counter"); //In this case, we need to add to our counter in each request. counter.Add(1, new("my-key-1", "my-value-1"), new("my-key-2", "my-value-2"));

Send data to Dynatrace

Dynatrace version 1.254+

To send data to Dynatrace, add the following package references to your .csproj file and add the code snippet below.

plaintext
<PackageReference Include="OpenTelemetry" Version="1.4.0-beta.2" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.1" />
cs
Sdk.CreateMeterProviderBuilder() .AddMeter("my_meter") .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddService("dotnet-quickstart").AddAttributes(dt_metadata)) .AddOtlpExporter((OtlpExporterOptions exporterOptions, MetricReaderOptions readerOptions) => { exporterOptions.Endpoint = new Uri("<URL>"); //TODO Replace <URL> to your SaaS/Managed URL as mentioned in the next step exporterOptions.Headers = "Authorization=Api-Token <TOKEN>"; //TODO Replace <TOKEN> with your API token as mentioned in the next step exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf; readerOptions.TemporalityPreference = MetricReaderTemporalityPreference.Delta; }) .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddService("dotnet-quickstart")) .Build();

To make sure your data arrives where it should be, you need to define the correct endpoint and token.

  • To set the endpoint
    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Dynatrace SaaS https://{your-environment-id}.live.dynatrace.com/api/v2/otlp/v1/metrics
      • Dynatrace Managed https://{your-domain}/e/{your-environment-id}/api/v2/otlp/v1/metrics
      • Dynatrace ActiveGate https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/otlp/v1/metrics
        • You may need to include the port to your ActiveGate endpoint. For example:
          • https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/otlp/v1/metrics
        • If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
          • https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/otlp/v1/metrics
    2. Replace <URL> in the code snippet above with your endpoint.
  • To create an authentication token
    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest metrics (under API v2 scopes), and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

Dynatrace version 1.222+

To send data to Dynatrace, add the following package references to your .csproj file and add the code snippet below.

plaintext
<PackageReference Include="OpenTelemetry" Version="1.4.0-beta.2" /> <PackageReference Include="Dynatrace.OpenTelemetry.Exporter.Metrics" Version="1.0.0" />
cs
Sdk.CreateMeterProviderBuilder() .AddMeter("my_meter") .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddService("dotnet-quickstart").AddAttributes(dt_metadata)) .AddDynatraceExporter(dtOptions => { dtOptions.ApiToken = "<TOKEN>"; //TODO Replace <TOKEN> with your API token as mentioned in the next step dtOptions.Url = "<URL>"; //TODO Replace <URL> to your SaaS/Managed URL as mentioned in the next step dtOptions.DefaultDimensions = new Dictionary<string, string> { { "default-key-1", "default-value-1" } }; dtOptions.MetricExportIntervalMilliseconds = 1000; }) .Build();

If no OneAgent is available locally, you need to define the correct endpoint and token to make sure your data arrives where it should be.

  • To set the endpoint
    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Dynatrace SaaS https://{your-environment-id}.live.dynatrace.com/api/v2/metrics/ingest
      • Dynatrace Managed https://{your-domain}/e/{your-environment-id}/api/v2/metrics/ingest
      • Dynatrace ActiveGate https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/metrics/ingest
        • You may need to include the port to your ActiveGate endpoint. For example:
          • https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/metrics/ingest
        • If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
          • https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/metrics/ingest
    2. Replace <URL> in the code snippet above with your endpoint.
  • To create an authentication token
    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest metrics (under API v2 scopes), and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

Dynatrace version 1.254+

To send data to Dynatrace, add the following package references to your .csproj file and add the code snippet below.

plaintext
<PackageReference Include="OpenTelemetry" Version="1.4.0-beta.2" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.1" /> <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.8" />
cs
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); // configures OTel Metrics SDK using the OpenTelemetry.Extensions.Hosting package and the OpenTelemetry.Exporter package services.AddOpenTelemetryMetrics(builder => { builder .AddMeter("my_meter") .SetResourceBuilder(ResourceBuilder.CreateEmpty().AddService("dotnet-quickstart").AddAttributes(dt_metadata)) .AddOtlpExporter((OtlpExporterOptions exporterOptions, MetricReaderOptions readerOptions) => { exporterOptions.Endpoint = new Uri("<URL>"); //TODO Replace <URL> to your SaaS/Managed URL as mentioned in the next step exporterOptions.Headers = "Authorization=Api-Token <TOKEN>"; //TODO Replace <TOKEN> with your API Token as mentioned in the next step exporterOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf; readerOptions.TemporalityPreference = MetricReaderTemporalityPreference.Delta; }); });

To make sure your data arrives where it should be, you need to define the correct endpoint and token.

  • To set the endpoint
    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Dynatrace SaaS https://{your-environment-id}.live.dynatrace.com/api/v2/otlp/v1/metrics
      • Dynatrace Managed https://{your-domain}/e/{your-environment-id}/api/v2/otlp/v1/metrics
      • Dynatrace ActiveGate https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/otlp/v1/metrics
        • You may need to include the port to your ActiveGate endpoint. For example:
          • https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/otlp/v1/metrics
        • If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
          • https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/otlp/v1/metrics
    2. Replace <URL> in the code snippet above with your endpoint.
  • To create an authentication token
    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest metrics (under API v2 scopes), and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

Dynatrace version 1.222+

To send data to Dynatrace, add the following package references to your .csproj file and add the code snippet below.

plaintext
<PackageReference Include="OpenTelemetry" Version="1.4.0-beta.2" /> <PackageReference Include="OpenTelemetry.Api" Version="1.4.0-beta.2" /> <PackageReference Include="Dynatrace.OpenTelemetry.Exporter.Metrics" Version="1.0.0" />
cs
public void ConfigureServices(IServiceCollection services) { var token = "<TOKEN>"; //TODO Replace <TOKEN> with your API token as mentioned in the next step services.AddControllers(); // configures OTel Metrics SDK using the OpenTelemetry.Extensions.Hosting package services.AddOpenTelemetryMetrics(builder => { builder.AddMeter("my_meter"); builder.SetResourceBuilder(ResourceBuilder.CreateEmpty().AddService("dotnet-quickstart").AddAttributes(dt_metadata)); builder.AddDynatraceExporter(dtOptions => { dtOptions.ApiToken = token; dtOptions.Url = "<URL>"; //TODO Replace <URL> to your SaaS/Managed URL as mentioned in the next step dtOptions.DefaultDimensions = new Dictionary<string, string> { { "default-key-1", "default-value-1" } }; dtOptions.MetricExportIntervalMilliseconds = 1000; }); });

If no OneAgent is available locally, you need to define the correct endpoint and token to make sure your data arrives where it should be.

  • To set the endpoint
    1. Use your Environment ID to set the endpoint to which your app will send traces as follows:
      • Dynatrace SaaS https://{your-environment-id}.live.dynatrace.com/api/v2/metrics/ingest
      • Dynatrace Managed https://{your-domain}/e/{your-environment-id}/api/v2/metrics/ingest
      • Dynatrace ActiveGate https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/metrics/ingest
        • You may need to include the port to your ActiveGate endpoint. For example:
          • https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/metrics/ingest
        • If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
          • https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/metrics/ingest
    2. Replace <URL> in the code snippet above with your endpoint.
  • To create an authentication token
    1. In the Dynatrace menu, go to Access tokens and select Generate new token.
    2. Provide a Token name.
    3. In the Search scopes box, search for Ingest metrics (under API v2 scopes), and select the checkbox.
    4. Select Generate token.
    5. Select Copy to copy the token to your clipboard.
    6. Save the token in a safe place; you can't display it again.
    7. Replace <TOKEN> in the code snippet above with your token.

Verify that the metrics are ingested into Dynatrace

A few minutes after restarting your app, look for your metrics:

  • In the Dynatrace menu, go to Metrics and filter for your metrics (by the names you have given them).
  • To get more detailed information, or to filter for different criteria, expand Details for a metric and select Create chart.

If your application does not receive any traffic, there will be no metrics.