Instrument a .NET application

Instrument a .NET application

The Grafana OpenTelemetry distribution for .NET provides a pre-configured and pre-packaged bundle of OpenTelemetry .NET components, optimized for Grafana Cloud Application Observability.

Grafana Labs recommends that you set up OpenTelemetry components, including instrumentation and an OpenTelemetry Collector distribution, using one of the Grafana Cloud setup guides.

These opinionated guides make it easy to get started. They include all the binaries, configuration, and connection parameters you need to set up OpenTelemetry for Grafana Cloud.

Before you begin

Ensure you have a .NET development environment and an application using .NET 6+ or .NET Framework version 4.6.2 or higher.

Install the SDK

Use either the command line or Visual Studio to install the package.

Command line:

Run the following command in your project folder:

sh
dotnet add package Grafana.OpenTelemetry

Additionally, install the console exporter for local testing:

sh
dotnet add package OpenTelemetry.Exporter.Console

Visual Studio:

Open your project in Visual Studio, search for, and install the Grafana.OpenTelemetry package.

Also install the OpenTelemetry.Exporter.Console package for local testing.

Instrument your application

Choose the code tab below that matches your environment and modify it for your application.

Warning

The AddConsoleExporter calls in the sample are only for local testing. Remove these calls for production applications.

netconsole
// .NET 6+ console
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using Grafana.OpenTelemetry;

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .UseGrafana()
    .AddConsoleExporter()
    .Build();
using var meterProvider = Sdk.CreateMeterProviderBuilder()
    .UseGrafana()
    .AddConsoleExporter()
    .Build();
using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddOpenTelemetry(logging =>
    {
        logging.UseGrafana()
            .AddConsoleExporter();
    });
});
aspnetcore
netfx

Run your application

Run your application with the following command:

sh
OTEL_RESOURCE_ATTRIBUTES="service.name=<name>,service.namespace=<namespace>,deployment.environment=<environment>" \
OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint> \
OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf" \
dotnet run

Replace variables in <...> with values specific to your application and backend. For more details, see the OpenTelemetry service attributes documentation.

  • <name>: your service name, for example, shoppingcart
  • <namespace>: a string to group related services, such as ecommerce for a team that manages several services
  • <environment>: the deployment environment, for example, the production deployment tier

Some backends, such as Grafana Cloud, also require you to set authentication headers in the OTEL_EXPORTER_OTLP_HEADERS environment variable.

Test your instrumentation

To verify successful instrumentation, run your application, generate some traffic, and check that metrics and logs appear in the console.

Example metrics output:

log
Metric Name: process.cpu.count, The number of processors (CPU cores) available to the current process., Unit: {processors}, Meter: OpenTelemetry.Instrumentation.Process/0.5.0.3
(2024-06-05T02:14:47.6851243Z, 2024-06-05T02:14:57.7092810Z] LongSumNonMonotonic
Value: 12

Example logs output:

log
LogRecord.Timestamp:               2024-06-05T02:14:47.9338272Z
LogRecord.CategoryName:            Microsoft.Hosting.Lifetime
LogRecord.Severity:                Info
LogRecord.SeverityText:            Information
LogRecord.Body:                    Now listening on: {address}
LogRecord.Attributes (Key:Value):
    address: http://localhost:5117
    OriginalFormat (a.k.a Body): Now listening on: {address}
LogRecord.EventId:                 14
LogRecord.EventName:               ListeningOnAddress

Example application

See the Rolldice service for a complete example setup.

Resources

  1. Grafana OpenTelemetry distribution for .NET on GitHub
  2. Grafana.OpenTelemetry NuGet package