Getting started with traces - Console

Learn how to use OpenTelemetry Traces in a .NET Console application

Você está visualizando a versão em versão em inglês desta página porque ela ainda não foi traduzida. Possui interesse em ajudar? Veja como contribuir.

This guide will show you how to get started with OpenTelemetry .NET Traces in a console application in just a few minutes.

Prerequisites

Creating a console application

Create a new console application and run it:

dotnet new console --output getting-started
cd getting-started
dotnet run

You should see the following output:

Hello World!

Adding OpenTelemetry traces

Install the OpenTelemetry Console Exporter package:

dotnet add package OpenTelemetry.Exporter.Console

Update the Program.cs file with the following code:

using System;
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace GettingStarted
{
    class Program
    {
        // Define an ActivitySource to create activities from
        private static readonly ActivitySource MyActivitySource = new ActivitySource(
            "MyCompany.MyProduct.MyLibrary");

        static void Main(string[] args)
        {
            // Configure the OpenTelemetry TracerProvider
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("getting-started"))
                .AddSource("MyCompany.MyProduct.MyLibrary")
                .AddConsoleExporter()
                .Build();

            // Start an activity (span) with some tags (attributes)
            using (var activity = MyActivitySource.StartActivity("SayHello"))
            {
                // Set some attributes on the activity
                activity?.SetTag("foo", 1);
                activity?.SetTag("bar", "Hello, World!");
                activity?.SetTag("baz", new int[] { 1, 2, 3 });

                // Set the status of the activity
                activity?.SetStatus(ActivityStatusCode.Ok);

                // Do some work...
                Console.WriteLine("Hello World!");
            }

            Console.WriteLine("Trace has been exported. Press any key to exit.");
            Console.ReadKey();
        }
    }
}

Run the application again (using dotnet run) and you should see the trace output from the console:

Activity.TraceId:          d4a7d499698d62f0e2317a67abc559b6
Activity.SpanId:           a091d18fbe45bdf6
Activity.TraceFlags:       Recorded
Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary
Activity.DisplayName: SayHello
Activity.Kind:        Internal
Activity.StartTime:   2022-03-30T19:42:33.5178011Z
Activity.Duration:    00:00:00.0097620
StatusCode : Ok
Activity.Tags:
    foo: 1
    bar: Hello, World!
    baz: [1, 2, 3]
Resource associated with Activity:
    service.name: getting-started

You are now collecting traces using OpenTelemetry.

How it works

ActivitySource (Tracer)

The program creates an ActivitySource which represents an OpenTelemetry Tracer:

private static readonly ActivitySource MyActivitySource = new ActivitySource(
    "MyCompany.MyProduct.MyLibrary");

The ActivitySource is used to create and start new activities.

Activity (Span)

The ActivitySource instance is used to start an Activity which represents an OpenTelemetry Span. You can set several tags (attributes) on it and set its status:

using (var activity = MyActivitySource.StartActivity("SayHello"))
{
    activity?.SetTag("foo", 1);
    activity?.SetTag("bar", "Hello, World!");
    activity?.SetTag("baz", new int[] { 1, 2, 3 });
    activity?.SetStatus(ActivityStatusCode.Ok);
}

TracerProvider

A TracerProvider is configured to subscribe to activities from the specified source and export them:

var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddSource("MyCompany.MyProduct.MyLibrary")
    .AddConsoleExporter()
    .Build();

The TracerProvider is the central component in the OpenTelemetry SDK. It holds all the configuration for tracing like samplers, processors, exporters, etc. and is highly customizable.

Tracing pipeline

The tracing pipeline in OpenTelemetry .NET follows this flow:

  1. ActivitySource creates Activities
  2. TracerProvider receives Activities
  3. Processor processes Activities
  4. Exporter exports Activities to a backend

OpenTelemetry .NET and .NET Activity API

In OpenTelemetry .NET, the terms ActivitySource and Activity are used instead of Tracer and Span from the OpenTelemetry specification. This is because tracing in OpenTelemetry .NET is implemented on top of the .NET runtime’s built-in diagnostics system.

You can instrument your application by depending on the System.Diagnostics.DiagnosticSource package, which provides Activity and ActivitySource classes representing the OpenTelemetry concepts of Span and Tracer respectively.

Learn more