DEV Community

Cover image for OpenTelemetry in ASP .NET with Aspire
Ssewannonda Keith Edwin
Ssewannonda Keith Edwin

Posted on

OpenTelemetry in ASP .NET with Aspire

OpenTelemetry

Its an Observability framework and toolkit designed to manage telemetry data that is traces, metrics, and logs.
It can be paired either #open source# tools like Aspire dashboard, Prometheus, and commercial ones like datadog.

Observability

Observability in software, is the potential to understand the internal state of a system by examining its telemetry data.
To make a system observable, it must be instrumented.

Why OpenTelemetry?

  1. You own the data that you generate which is essential today in AI.
  2. You need a single set of APIs and standards for all. These create the flexibility needed to create a modern application.

OpenTelemetry in ASP .NET

  1. Search for OpenTelemetry.* in your project. There are many nuget packages for open telemetry as seen below. OpenTelemetry packages
  2. Add them to your project:
  3. Update your Program.cs file.
using OpenTelemetry.Resources;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
using OpenTelemetry.Logs;
using OpenTelemetry.Exporter;

var builder = WebApplication.CreateBuilder(args);
// code ommitted for brevity

builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => 
    resource.AddService(ApiConstants.ApiName))
    .WithMetrics(metric =>
     {
       metric.AddAspNetCoreInstrumentation()
       .AddHttpClientInstrumentation();
       metric.AddOtlpExporter(options => 
       {
          options.Endpoint = new Uri("<url_to_export _to>");
       });
 })
 .WithTracing(trace =>
 {
     trace.AddAspNetCoreInstrumentation()
     .AddHttpClientInstrumentation();
     trace.AddOtlpExporter(options =>
     {
          options.Endpoint = new Uri("<url_to_export _to>");
      });
 });

 builder.Logging.AddOpenTelemetry(options => 
 {    
      options.AddConsoleExporter()
      .SetResourceBuilder(ResourceBuilder.CreateDefault()
      .AddService(ApiConstants.ApiName));

      options.AddOtlpExporter(options =>
      {
           options.Endpoint = new Uri("<url_to_export _to>");                   
      });
  });
Enter fullscreen mode Exit fullscreen mode

Your application should be able to emit logs, traces and metrics.

Aspire dashboard

The open source standalone dashboard provides a great UI for viewing telemetry and can be used by any application.
To spin it up locally without auth, use this command
docker run --rm -it -d -p 18888:18888 -p 4317:18889 --name aspire-dashboard mcr.microsoft.com/dotnet/aspire-dashboard:9.0 -e DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true
For more .NET Aspire dashboard configuration

Aspire dashboard

Feel free to leave question or a comment about this.
Star these wonderful projects to give them the platform they deserve:

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 👀

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay