Help Docs

Instrument .NET application for OpenTelemetry

To monitor your .NET application with Site24x7 OpenTelemetry, follow the steps below.

Step 1:
Add the following major Nuget packages to your application to configure OpenTelemetry:

  • OpenTelemetry
  • OpenTelemetry.Instrumentation.AspNetCore
  • OpenTelemetry.Exporter.OpenTelemetryProtocol
  • OpenTelemetry.Extensions.Hosting

Step 2:
Create a resource builder in app/Program.cs.

ar builder = WebApplication.CreateBuilder(args);
var resourceBuilder = ResourceBuilder
.CreateDefault()
.AddService("OpenTelemetry-Dotnet-Example")
.AddTelemetrySdk();

Step 3:
Configure the OpenTelemetry SDK for traces.

// Configure the OpenTelemetry SDK for traces
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
// Step 1. Declare the resource to be used by this tracer provider.
tracerProviderBuilder.SetResourceBuilder(resourceBuilder);
// Step 2. Configure the SDK to listen to the following auto-instrumentation
tracerProviderBuilder.AddAspNetCoreInstrumentation(options =>
{
options.RecordException = true;
}).AddHttpClientInstrumentation();
// Step 3. Configure the SDK to listen to custom instrumentation.
tracerProviderBuilder.AddSource("WeatherForecast");
// Step 4. Configure the OTLP exporter to export to Site24x7
// The OTEL_EXPORTER_OTLP_ENDPOINT environment variable should be set to Site24x7's OTLP endpoint:
// OTEL_EXPORTER_OTLP_ENDPOINT=https://otel.site24x7rum.com:4318
//
// The OTEL_EXPORTER_OTLP_HEADERS environment variable should be set to include your Site24x7 API key:
// OTEL_EXPORTER_OTLP_HEADERS=api-key=<YOUR_API_KEY_HERE>
tracerProviderBuilder.AddOtlpExporter(options =>
{
options.Endpoint = new Uri($"{Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT")}");
options.Headers = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS");
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
});
});

Step 4:
Configure the OpenTelemetry for custom instrumentation.

// An ActivitySource is .NET's term for an OpenTelemetry Tracer.
// Spans generated from this ActivitySource are associated with the ActivitySource's name and version.
private static ActivitySource _tracer = new ActivitySource("WeatherForecast", "1.2.3");
private void DoSomeWork()
{
// Start a span using the OpenTelemetry API
using var span = _tracer.StartActivity("DoSomeWork", ActivityKind.Internal);
// Decorate the span with additional attributes
span?.AddTag("SomeKey", "SomeValue");
// Do some work
Thread.Sleep(50);// Make an external call
_httpClient.GetString("https://www.site24x7.com").GetAwaiter().GetResult();
// Do some more work
Thread.Sleep(50);
}
public IActionResult Index()
{
DoSomeWork();
return View();
}
Note

To know more about the OpenTelemetry instrumentation steps for .NET applications, click here.

You are all set. Now the agent will start sending the performance metrics to your Site24x7 web client portal.

Was this document helpful?

Would you like to help us improve our documents? Tell us what you think we could do better.


We're sorry to hear that you're not satisfied with the document. We'd love to learn what we could do to improve the experience.


Thanks for taking the time to share your feedback. We'll use your feedback to improve our online help resources.

Shortlink has been copied!