OpenTelemetry for .NET | Honeycomb

We use cookies or similar technologies to personalize your online experience & tailor marketing to you. Many of our product features require cookies to function properly.

Read our privacy policy I accept cookies from this site

OpenTelemetry for .NET

Honeycomb has a OpenTelemetry Distribution for .NET that wraps the official OpenTelemetry .NET SDK to simplify the setup process and add instrumentation your applications to send telemetry data to Honeycomb.

Requirements 

These instructions will explain how to set instrumentation for a service written in .NET. In order to follow along, you will need:

  • A .NET application that conforms to NET Standard 2.0, or a .NET Framework 4.6.1+ application.
  • A Honeycomb API Key. You can find your API key on your Team Settings page. If you do not have an API key yet, sign up for a free Honeycomb account.

Examples 

There are several examples that configure applications to send OpenTelemetry data to Honeycomb.

Acquire Dependencies 

Install the Honeycomb.OpenTelemetry NuGet package.

For example, with the .NET CLI, use:

dotnet add package Honeycomb.OpenTelemetry --prerelease

Initialize 

Configure your app for Honeycomb OpenTelemetry Distribution for .NET with one of the following configuration methods:

ASP.NET Core 

If using the dataset-only data model, refer to the Honeycomb Classic tab for instructions. Not sure? Learn more about Honeycomb versus Honeycomb Classic.

Initialize your ASP.NET Core app to use the Honeycomb OpenTelemetry Distribution for .NET:

In your appsettings.json file, add a Honeycomb section like so:

{
  ...
  "Honeycomb": {
    "ServiceName": "my-app",
    "ApiKey": "{apikey}",
    "Dataset": "{dataset}",
  }
}

Then pass the IConfiguration object to configure the OpenTelemetry SDK in your application startup code:

using Honeycomb.OpenTelemetry;

// ...

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // configure OpenTelemetry SDK to send data to Honeycomb
    services.AddHoneycomb(Configuration);
}

Initialize your ASP.NET Core app to use the Honeycomb OpenTelemetry Distribution for .NET:

In your appsettings.json file, add a Honeycomb section like so:

{
  ...
  "Honeycomb": {
    "ServiceName": "my-app",
    "ApiKey": "{apikey}"
  }
}

Then pass the IConfiguration object to configure the OpenTelemetry SDK in your application startup code:

using Honeycomb.OpenTelemetry;

// ...

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();

    // configure OpenTelemetry SDK to send data to Honeycomb
    services.AddHoneycomb(Configuration);
}

Programmatically 

If using the dataset-only data model, refer to the Honeycomb Classic tab for instructions. Not sure? Learn more about Honeycomb versus Honeycomb Classic.

You can provide a HoneycombOptions object to programmatically configure the OpenTelemetry SDK.

using OpenTelemetry;

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(new HoneycombOptions
    {
        ServiceName = "my-app",
        ApiKey = "{apikey}"
    })
    .Build();

You can provide an HoneycombOptions object to programmatically configure the OpenTelemetry SDK.

using OpenTelemetry;

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(new HoneycombOptions
    {
        ServiceName = "my-app",
        ApiKey = "{apikey}"
        Dataset = "{dataset}"
    })
    .Build();

ASP.NET (.NET Framework) 

If using the dataset-only data model, refer to the Honeycomb Classic tab for instructions. Not sure? Learn more about Honeycomb versus Honeycomb Classic.

Add the following to your Web.config:

<system.webServer>
    <modules>
        <add
            name="TelemetryHttpModule"
            type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
                OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule"
            preCondition="integratedMode,managedHandler" />
    </modules>
</system.webServer>

Configure the OpenTelemetry SDK to use Honeycomb during application start:

using OpenTelemetry;
using OpenTelemetry.Trace;
using Honeycomb.OpenTelemetry;

public class WebApiApplication : HttpApplication
{
    private TracerProvider _tracerProvider;

    protected void Application_Start()
    {
        _tracerProvider = Sdk.CreateTracerProviderBuilder()
            .AddHoneycomb(new HoneycombOptions
            {
                ServiceName = "my-app",
                ApiKey = "{apikey}"
            })
            .Build();
    }

    protected void Application_End()
    {
        _tracerProvider?.Dispose();
    }
}

See the OpenTelemetry ASP.NET instrumentation for more advanced options.

Add the following to your Web.config:

<system.webServer>
    <modules>
        <add
            name="TelemetryHttpModule"
            type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
                OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule"
            preCondition="integratedMode,managedHandler" />
    </modules>
</system.webServer>

Configure the OpenTelemetry SDK to use Honeycomb during application start:

using OpenTelemetry;
using OpenTelemetry.Trace;
using Honeycomb.OpenTelemetry;

public class WebApiApplication : HttpApplication
{
    private TracerProvider _tracerProvider;

    protected void Application_Start()
    {
        _tracerProvider = Sdk.CreateTracerProviderBuilder()
            .AddHoneycomb(new HoneycombOptions
            {
                ServiceName = "my-app",
                ApiKey = "{apikey}",
                Dataset = "{dataset}"
            })
            .Build();
    }

    protected void Application_End()
    {
        _tracerProvider?.Dispose();
    }
}

See the OpenTelemetry ASP.NET instrumentation for more advanced options.

Command Line Arguments 

If using the dataset-only data model, refer to the Honeycomb Classic tab for instructions. Not sure? Learn more about Honeycomb versus Honeycomb Classic.

You can provide command line arguments when starting your application:

dotnet run \
  --service-name=my-app \
  --honeycomb-apikey={apikey}

Then, pass the command line arguments to configure the OpenTelemetry SDK:

using OpenTelemetry;

//...

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(args)
    .Build();

NOTE: AddHoneycomb sets the OpenTelemetry SDK ResourceBuilder, which is used to create the Resource that is associated to all spans created by the tracer. Please do not override the ResourceBuilder by calling SetResourceBuilder with another instance. Otherwise, some resource attributes may not be queryable in your Honeycomb data.

You can provide command line arguments when starting your application:

dotnet run \
  --service-name=my-app \
  --honeycomb-apikey={apikey} \
  --honeycomb-dataset={dataset}

Then, pass the command line arguments to configure the OpenTelemetry SDK:

using OpenTelemetry;

//...

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(args)
    .Build();

NOTE: AddHoneycomb sets the OpenTelemetry SDK ResourceBuilder, which is used to create the Resource that is associated to all spans created by the tracer. Please do not override the ResourceBuilder by calling SetResourceBuilder with another instance. Otherwise, some resource attributes may not be queryable in your Honeycomb data.

Enabled Instrumentation Packages 

The Honeycomb Distribution enables the following official instrumentation packages automatically as part of the initialization process:

  • ASP.NET Core (For .NET Core apps)
  • ASP.NET (For .NET Framework apps)
  • Redis
  • HTTP Client
  • SQL Client
  • gRPC Client

Additional instrumentation packages can be added during initialization. For example, to add the MySQL instrumentation from OpenTelemetry-Contrib (after installing the package), use:

using OpenTelemetry;

//...

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(new HoneycombOptions
    {
        ServiceName = "my-app",
        ApiKey = "{apikey}"
    })
    .AddMySqlDataInstrumentation()
    .Build();

Redis instrumentation requires access to the IConnection object to setup tracing. This can either be provided as part of the AddHoneycomb call by setting the RedisConnection property on HoneycombOptions or it can be automatically retrieved if it is registered in the ASP.NET ServiceCollection object.

Disabling Instrumentation Packages 

You can disable individual instrumented packages by setting them as false, as shown in the following configuration:

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(new HoneycombOptions
    {
        ServiceName = "my-app",
        ApiKey = "{apikey}"
        InstrumentHttpClient = false,
        InstrumentSqlClient = false,
        InstrumentGprcClient = false,
        InstrumentStackExchangeRedisClient = false
    })
    .Build();

Adding Attributes to Spans 

It is often beneficial to add attributes to a currently executing span in a trace. For example, you may have an application or service that handles users and you want to associate the user with the span when querying your dataset in Honeycomb. In order to do this, get the current span from the context and set an attribute with the user ID:

using OpenTelemetry.Trace;

//...

var currentSpan = Tracer.CurrentSpan;
currentSpan.SetAttribute("user.id", User.GetUserId())

This configuration will add a user.id attribute to the current span, so that you can use the field in WHERE, GROUP BY, or ORDER clauses in the Honeycomb query builder.

Creating New Spans 

In order to get the full picture of what is happening, you will have to add manual instrumentation and create custom spans to describe what is happening in your application. To do this, grab your tracer instance and use it to create a span:

using OpenTelemetry.Trace;

//...

using var span = TracerProvider.Default.GetTracer("my-service").StartActiveSpan("expensive-query")
// ... do cool stuff

Multi-Span Attributes 

Sometimes you want to add the same attribute to many spans within the same trace. These attributes may include variables calculated during your program, or other useful values for correlation or debugging purposes.

We will leverage the OpenTelemetry concept of baggage to add this attribute. Baggage allows you to add a key with a value as an attribute to every subsequent child span of the current application context.

using OpenTelemetry;

...

Baggage.Current.SetBaggage("app.username", name);

Note: Any Baggage attributes that you set in your application will be attached to outgoing network requests as a header. If your service communicates to a third party API, do NOT put sensitive information in the Baggage attributes.

Sending Data to a Collector or Other Endpoint 

You can send data to an OpenTelemetry Collector instance or another endpoint by specifying a different endpoint.

For example, here is how to send to an OpenTelemetry Collector instance over HTTP:

var options = new HoneycombOptions
{
    ServiceName = "my-app",
    ApiKey = "{apikey}"
    TracesEndpoint = "http://<your-collector-endpoint>/v1/traces"

    // MetricsEndpoint is not required if you are not sending metrics
    MetricsEndpoint = "http://<your-collector-endpoint>/v1/metrics"
}
{
  "Honeycomb": {
    "TracesEndpoint": "http://<your-collector-endpoint>/v1/traces",
    "MetricsEndpoint": "http://<your-collector-endpoint>/v1/metrics"
  }
}
dotnet run --honeycomb-traces-endpoint=${COLLECTOR_TRACES_ENDPOINT} --honeycomb-metrics-endpoint=${COLLECTOR_METRICS_ENDPOINT}

Sampling 

Deterministic sampling can be enabled by providing a SampleRate via the following methods. A sample rate of 5 means that one out of every five traces will be sent to Honeycomb.

var options = new HoneycombOptions
{
    ServiceName = "my-app",
    ApiKey = "{apikey}"
    SampleRate = 5 // sends 1/5 traces
}

The value of SampleRate must be a positive integer.

{
  "Honeycomb": {
    "SampleRate": 5,
  }
}

The value of SampleRate must be a positive integer.

dotnet run --honeycomb-samplerate=5

The value of --honeycomb-samplerate must be a positive integer.

If you have multiple services that communicate with each other, it is important that they have the same sampling configuration. Otherwise, each service might make a different sampling decision, resulting in incomplete or broken traces. You can sample using a standalone proxy as an alternative, like Honeycomb Refinery, or when you have more robust sampling needs.

Distributed Trace Propagation 

When a service calls another service, you want to ensure that the relevant trace information is propagated from one service to the other. This allows Honeycomb to connect the two services in a trace.

Distributed tracing enables you to trace and visualize interactions between multiple instrumented services. For example, your users may interact with a front-end API service, which talks to two internal APIs to fulfill their request. In order to have traces connect spans for all these services, it is necessary to propagate trace context between these services, usually by using an HTTP header.

Both the sending and receiving service must use the same propagation format, and both services must be configured to send data to the same Honeycomb dataset.

Using OpenTelemetry Without the Honeycomb Distribution 

The primary purpose of Honeycomb’s SDK Distribution for .NET is to streamline configuration and to instrument as quickly and easily as possible. Under the hood, the Honeycomb SDK Distribution is using OpenTelemetry for .NET, which means OpenTelemetry can be used with or without this Distribution. It may be unnecessary for advanced users or those already instrumented with OpenTelemetry to use the the Honeycomb SDK Distribution for .NET.

The Honeycomb SDK Distribution reads specific variables and translates them to variables understood by upstream OpenTelemetry. For example, when using the distribution, the environment variable HONEYCOMB_API_ENDPOINT is converted to the OpenTelemetry variable OTEL_EXPORTER_OTLP_ENDPOINT. Therefore, to send data to Honeycomb using OpenTelemetry without the Distribution, a different configuration is necessary to match expected variables.

When using OpenTelemetry for .NET, the following configuration properties are required:

OtlpExporterOptions property /
Environment Variable
Value
Endpoint
OTEL_EXPORTER_OTLP_ENDPOINT
https://api.honeycomb.io
Headers
OTEL_EXPORTER_OTLP_HEADERS
x-honeycomb-team=HONEYCOMB_API_KEY
ServiceName
OTEL_SERVICE_NAME
your-service-name

The service name must be set in either the TracerProvider configuration code or as an environment variable.

The code below configures a tracer provider that can export traces to Honeycomb.

services.AddOpenTelemetryTracing(builder => builder
    .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
    .AddAspNetCoreInstrumentation()
    .AddHttpClientInstrumentation()
    .AddOtlpExporter(option =>
    {
        option.Endpoint = new Uri("https://api.honeycomb.io");
        option.Headers = $"x-honeycomb-team={honeycombApiKey}";
    }));

More details on configuration options can be found on GitHub at OpenTelemetry for .NET.

Using HTTP Instead of gRPC 

By default, the OpenTelemetry .NET SDK uses the gRPC protocol to send telemetry data. To use HTTP instead of gRPC for traces, update the protocol in the OtlpExporter:

services.AddOpenTelemetryTracing(builder => builder
    .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
    .AddAspNetCoreInstrumentation()
    .AddHttpClientInstrumentation()
    .AddOtlpExporter(option =>
    {
        option.Endpoint = new Uri("https://api.honeycomb.io");
        option.Headers = $"x-honeycomb-team={honeycombApiKey}";
        option.Protocol = OtlpExportProtocol.HttpProtobuf;
    }));

If the application is running on .NET Core 3.x, also add the following at application startup:

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

Endpoint configuration options, as well as OpenTelemetry Headers, are listed in the OpenTelemetry for .NET chart if sending data to Honeycomb directly.

If using an OpenTelemetry Collector, specify the endpoint of the collector, and add the headers to the collector configuration file.

Sampling Without the Honeycomb Distribution 

services.AddOpenTelemetryTracing((builder) => builder
    .SetResourceBuilder(ResourceBuilder.CreateDefault()
        .AddService(serviceName)
        .AddAttributes(new[] { new KeyValuePair<string, object>("SampleRate", 2) }) // additional resource attribute
    )
    .SetSampler(new TraceIdRatioBasedSampler(0.5)) // sampler
    .AddAspNetCoreInstrumentation()
    .AddHttpClientInstrumentation()
    .AddOtlpExporter(option =>
    {
        option.Endpoint = new Uri("https://api.honeycomb.io");
        option.Headers = $"x-honeycomb-team={honeycombApiKey}";
    }));

You can configure the OpenTelemetry SDK to sample the data it generates. Honeycomb re-weights sampled data, so it is recommended that you set a resource attribute containing the sample rate.

In the example above, our goal is to keep approximately half (1/2) of the data volume. The resource attribute contains the denominator (2), while the OpenTelemetry sampler argument contains the decimal value (0.5).

Endpoint URLs for OTLP/HTTP 

When using the OTEL_EXPORTER_OTLP_ENDPOINT environment variable with an SDK and an HTTP exporter, the final path of the endpoint is actually modified by the SDK to represent the specific signal being sent.

For example, when exporting trace data, the endpoint is updated to append v1/traces. When exporting metrics data, the endpoint is updated to append v1/metrics. The same modification is not necessary for gRPC.

By setting OTEL_EXPORTER_OTLP_ENDPOINT to https://api.honeycomb.io, traces are sent to https://api.honeycomb.io/v1/traces and metrics to https://api.honeycomb.io/v1/metrics.

export OTEL_EXPORTER_OTLP_ENDPOINT=https://api.honeycomb.io

If the desired outcome is to send data to a different endpoint depending on the signal, use OTEL_EXPORTER_OTLP_<SIGNAL>_ENDPOINT instead of the more generic OTEL_EXPORTER_OTLP_ENDPOINT.

When using a signal-specific environment variable, these paths must be appended manually. Set OTEL_EXPORTER_OTLP_TRACES_ENDPOINT for traces, appending the endpoint with v1/traces, and OTEL_EXPORTER_OTLP_METRICS_ENDPOINT for metrics, appending the endpoint with v1/metrics.

Send both traces and metrics to Honeycomb using this method by setting the following variables:

export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.honeycomb.io/v1/traces
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://api.honeycomb.io/v1/metrics

More details about endpoints and signals can be found in the OpenTelemetry Specification.

Troubleshooting 

No Traces for a Service 

The service name is a required configuration value. If it is unspecified, all trace data will be sent to a default dataset called unknown_service.

Error Log 

By default, the .NET SDK does not emit error information to the console. The SDK does include a self-diagnostics feature to help with troubleshooting by writing errors to a log file. Enable self-diagnostics by creating a file in the current working directory called OTEL_DIAGNOSTICS.json:

{
    "LogDirectory": ".",
    "FileSize": 1024,
    "LogLevel": "Error"
}

Running the app with the above configuration in the OTEL_DIAGNOSTICS.json file will generate a file named for ExecutableName.ProcessId.log, such as console.30763.log. The newly generated log file will contain any applicable errors for the app. The LogDirectory represents the directory in which the log file will be stored, and can be changed to output to a different location. The FileSize is the maximum size the log file can grow in KiB, and can be adjusted if a larger size is needed to prevent overwriting of logging output. Adjust the log level as needed for more or less verbose logging, using the fields available with System Diagnostics.

To disable this error log, delete the OTEL_DIAGNOSTICS.json file.

Exporting to the Console 

If you are using the Honeycomb OpenTelemetry Distribution, warnings will appear in the console if you are missing an API Key or Dataset. If no errors appear but your data is not in Honeycomb as expected, use a ConsoleExporter to print your spans to the console. This will help confirm whether your app is being instrumented with the data you expect.

First, import the ConsoleExporter:

dotnet add package OpenTelemetry.Exporter.Console --prerelease

Then add the ConsoleExporter to your configuration:

using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
    .AddHoneycomb(options)
    .AddConsoleExporter() // for debugging
    .Build();

Keep in mind that printing to the console is not recommended for production and should only be used for debugging purposes.

Receiving 464 Errors 

You may receive a 464 error response from the Honeycomb API when sending telemetry using gRPC and HTTP1. The gRPC format depends on using HTTP2 and any request over HTTP1 will be rejected by the Honeycomb servers.

Did you find what you were looking for?