Send traces to Cloud Observability with .NET

This Quick Start guide shows you how to configure your .net applications to send OpenTelemetry traces to Cloud Observability.

This guide does not provide documentation on application instrumentation. For .net-specific information on instrumentation, please see the .net OpenTelemetry getting started guide.

Sending OpenTelemetry data directly to Cloud Observability without a Collector for most developer setups will suffice. For non-development setups, however, it is highly recommended that you send OpenTelemetry data to Cloud Observability by way of the OpenTelemetry Collector. This can be done with or without a Launcher, as we’ll see below.

The sections below contain code snippets only. For full code listings, please see open-telemetry/opentelemetry-dotnet and open-telemetry/opentelemetry-dotnet-contrib in the OpenTelemetry GitHub .NET repos.

Before you get started with sending OpenTelemetry data to Cloud Observability, you will need the following:

In your application code, you will need to install dependencies and import OpenTelemetry packages before you can send data to Cloud Observability.

Start by installing the OpenTelemetry Nuget packages.

Console App

.NET Core App

1
2
3
dotnet add package OpenTelemetry --version 1.4.0-alpha.2
dotnet add package OpenTelemetry.Exporter.Console --version 1.4.0-alpha.2
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.4.0-alpha.2
Copied
1
2
3
4
5
dotnet add package OpenTelemetry --version 1.4.0-alpha.2
dotnet add package OpenTelemetry.Exporter.Console --version 1.4.0-alpha.2
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.4.0-alpha.2
dotnet add package OpenTelemetry.Extensions.Hosting --version 1.0.0-rc9.6
dotnet add package OpenTelemetry.Instrumentation.Http --version 1.0.0-rc9.6
Copied

End tabs

Where:

  • OpenTelemetry is the OpenTelemetry SDK, a reference implementation of the OpenTelemetry API.
  • OpenTelemetry.Exporter.Console outputs traces to the console during development.
  • OpenTelemetry.Exporter.OpenTelemetryProtocol exports traces to Cloud Observability or the OpenTelemetry Collector using the OpenTelemetry Protocol (OTLP).
  • OpenTelemetry.Extensions.Hosting is used to register the .NET OpenTelemetry provider (.NET Core only)

If you are using a .csproj file, your dependencies would look like this:

Console App

.NET Core App

1
2
3
4
5
6
7
8
9
...
  <ItemGroup>
	...
    <PackageReference Include="OpenTelemetry" Version="1.4.0-alpha.2" />
    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-alpha.2" />
    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-alpha.2" />
	...
  </ItemGroup>
...
Copied
1
2
3
4
5
6
7
8
9
10
11
...
  <ItemGroup>
	...
    <PackageReference Include="OpenTelemetry" Version="1.4.0-alpha.2" />
    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0-alpha.2" />
    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.4.0-alpha.2" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.6" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.6" />
	...
  </ItemGroup>
...
Copied

End tabs

Before you can start sending OpenTelemetry data to Cloud Observability, you will need to:

  • Configure an Exporter (tells OpenTelemetry how to send data to Cloud Observability)
  • Configure a TracerProvider (provides an entrypoint to the OpenTelemetry API, allowing you to create Spans)

Now that you’ve installed the OpenTelemetry packages, you will need to import them in your application code.

Open up your application code, and add the following imports to your .cs file:

Console App

.NET Core App

1
2
3
using OpenTelemetry;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;
Copied
1
2
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Copied

End tabs

The code snippet below shows how to configure OpenTelemetry for both a Console application and an .NET Core application.

Console App

.NET Core App

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...

// Define some important constants to initialize tracing with
var serviceName = "MyCompany.MyProduct.MyService";
var serviceVersion = "1.0.0";

// Configure important OpenTelemetry settings and the console exporter
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
	.AddOtlpExporter(opt =>
	{
		opt.Endpoint = new Uri("ingest.lightstep.com:443"); //US data center
		// opt.Endpoint = new Uri("ingest.eu.lightstep.com:443"); //EU data center
		opt.Headers = new Metadata
		{
			{ "lightstep-access-token", Environment.GetEnvironmentVariable("LS_ACCESS_TOKEN")}
		};
		opt.Credentials = new SslCredentials();
	})
	.AddSource(serviceName)
	.SetResourceBuilder(
		ResourceBuilder.CreateDefault()
			.AddService(serviceName: serviceName,serviceVersion: serviceVersion))
	.Build();

var MyActivitySource = new ActivitySource(serviceName);

...
Expand Code
Copied
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...

var serviceName = "MyCompany.MyProduct.MyService";
var serviceVersion = "1.0.0";

var builder = WebApplication.CreateBuilder(args);

// Configure to send data via the OTLP exporter.
// By default, it will send to port 4318, which the collector is listening on.
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
	tracerProviderBuilder
	.AddOtlpExporter(opt =>
	{
		opt.Endpoint = new Uri("ingest.lightstep.com:443"); //US data center
		// opt.Endpoint = new Uri("ingest.eu.lightstep.com:443"); //EU data center
		opt.Headers = new Metadata
		{
			{ "lightstep-access-token", Environment.GetEnvironmentVariable("LS_ACCESS_TOKEN")}
		};
		opt.Credentials = new SslCredentials();
	})
	.AddSource(serviceName)
	.SetResourceBuilder(
		ResourceBuilder.CreateDefault()
			.AddService(serviceName: serviceName, serviceVersion: serviceVersion))
	.AddHttpClientInstrumentation()
	.AddAspNetCoreInstrumentation();
});

var app = builder.Build();

...
Expand Code
Copied

End code tabs

More on OtlpExporter configuration can be found here

Expandable end

Expandable end

Expandable end

Expandable end

Expandable end

Cloud Observability access token

See also

Send traces to Cloud Observability with Python

Send traces to Cloud Observability with Java

Send traces to Cloud Observability with Node.JS

Updated Sep 20, 2022