Instrument .NET applications with OpenTelemetry

更新时间:
复制 MD 格式

Use OpenTelemetry to automatically or manually instrument your .NET application and report data to Cloud Monitor 2.0. After instrumentation, you can view application topology, call traces, abnormal transactions, slow transactions, and SQL analysis.

Background information

OpenTelemetry .NET supports automatic and manual instrumentation.

Step 1: Obtain endpoint information

  1. Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, click Integration Center.

  2. In the Server Applications section, click the .NET card, and then set Protocol Type to OpenTelemetry.

  3. In the Parameter Settings section, click Get to the right of LicenseKey. Then, set Instrumentation Method, Connection Method, and Reporting Method as needed. Enter Application Name, Version, and Environment.

    The integration code generated at the bottom of the page contains endpoint information such as the Endpoint and LicenseKey.

    # Download the bash script
    curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
    # Install core files
    sh ./otel-dotnet-auto-install.sh
    # Enable execution for the instrumentation script
    chmod +x $HOME/.otel-dotnet-auto/instrument.sh
    # Setup the instrumentation for the current shell session
    . $HOME/.otel-dotnet-auto/instrument.sh
    # Run your application with instrumentation
    OTEL_SERVICE_NAME=<your-service-name> \
    OTEL_RESOURCE_ATTRIBUTES=service.name=<your-service-name>,acs.cms.workspace=<workspace-id>,service.version=<version>,deployment.environment=<environment> \
    OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://<traces-endpoint-url> \
    OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://<metrics-endpoint-url> \
    OTEL_EXPORTER_OTLP_HEADERS="x-arms-license-key=<license-key>,x-arms-project=<project-name>" \
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
    OTEL_LOGS_EXPORTER=none \
    ./MyNetApp

Step 2: Set up dependency libraries

Automatic instrumentation (Recommended)

  1. Download and run the OpenTelemetry .NET automatic instrumentation installation script.

    # Download the bash script
    curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O
    # Install core files
    sh ./otel-dotnet-auto-install.sh
    # Enable execution for the instrumentation script
    chmod +x $HOME/.otel-dotnet-auto/instrument.sh
    # Setup the instrumentation for the current shell session
    . $HOME/.otel-dotnet-auto/instrument.sh
  2. Configure the following environment variables and then start your application.

    HTTP reporting

    Replace the placeholders in the following code with the endpoint information that you obtained in Step 1.

    # Run your application with instrumentation
    OTEL_SERVICE_NAME=<service name> \
    OTEL_RESOURCE_ATTRIBUTES=service.name=<service name>,acs.cms.workspace=<workspace>,service.version=<service version>,deployment.environment=<environment> \
    OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=<traces.endpoint> \
    OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=<metrics.endpoint> \
    OTEL_EXPORTER_OTLP_HEADERS="x-arms-license-key=<license-key>,x-arms-project=<arms-project>,x-cms-workspace=<workspace>" \
    OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
    OTEL_LOGS_EXPORTER=none \
    ./MyNetApp

    gRPC reporting

    Replace the placeholders in the following code with the endpoint information that you obtained in Step 1.

    # Run your application with instrumentation
    OTEL_SERVICE_NAME=<service name> \
    OTEL_RESOURCE_ATTRIBUTES=service.name=<service name>,acs.cms.workspace=<workspace>,service.version=<service version>,deployment.environment=<environment> \
    OTEL_EXPORTER_OTLP_ENDPOINT=<endpoint> \
    OTEL_EXPORTER_OTLP_HEADERS="x-arms-license-key=<license-key>,x-arms-project=<arms-project>,x-cms-workspace=<workspace>" \
    OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
    OTEL_LOGS_EXPORTER=none \
    ./MyNetApp

    To add more OpenTelemetry environment variables, see OpenTelemetry .NET automatic instrumentation configuration.

Manual instrumentation

  1. Add dependency packages.

    dotnet add package OpenTelemetry
    dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
    dotnet add package OpenTelemetry.Exporter.Console # Optional. Export data in the console.
    dotnet add package OpenTelemetry.Extensions.Hosting
  2. Create an OpentelemetryExporterDemo.cs file and add the following code.

    HTTP reporting

    Replace the placeholders in the following code with the endpoint information that you obtained in Step 1.

    using System.Diagnostics;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using OpenTelemetry.Resources;
    using OpenTelemetry.Exporter;
    namespace Demo
    {
        internal static class OpentelemetryExporterDemo
        {
            internal static void Run()
            {
                Console.WriteLine("otlp running");
                // Application name for OpenTelemetry reporting
                var serviceName = "<service name>"; // application name
                using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                    .AddSource(serviceName)
                    .SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder.CreateDefault()
                    .AddAttributes(new Dictionary<string, object> {
                        {"service.name", "<service name>"},  // application name
                        {"service.version", "<service version>"},  // version
                        {"deployment.environment", "<environment>"},  // deployment environment
                        {"host.name", "${hostName}"} // Replace ${hostName} with the actual hostname.
                    }))
                     .AddOtlpExporter(opt =>
                         {
                             opt.Headers = "x-arms-license-key=<license-key>,x-arms-project=<arms-project>,x-cms-workspace=<workspace>"; // Replace <license-key>, <arms-project>, and <workspace> with the endpoint information from Step 1.
                             opt.Endpoint = new Uri("<endpoint>");  // Replace <endpoint> with the endpoint information from Step 1.
                             opt.Protocol = OtlpExportProtocol.HttpProtobuf; // Send data over HTTP.
                         })
                    .AddConsoleExporter() // Optional. Print trace data to the console.
                    .Build();
                for(int i = 0; i<10; i++)
                {
                    var MyActivitySource = new ActivitySource(serviceName);
                    using var activity = MyActivitySource.StartActivity("SayHello");
                    activity?.SetTag("bar", "Hello World");
                }
            }
        }
    }

    gRPC reporting

    Replace the placeholders in the following code with the endpoint information that you obtained in Step 1.

    using System.Diagnostics;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using OpenTelemetry.Resources;
    using OpenTelemetry.Exporter;
    namespace Demo
    {
        internal static class OpentelemetryExporterDemo
        {
            internal static void Run()
            {
                Console.WriteLine("otlp running");
                // Application name for OpenTelemetry reporting
                var serviceName = "<service name>";  // application name
                using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                    .AddSource(serviceName)
                    .SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder.CreateDefault()
                    .AddAttributes(new Dictionary<string, object> {
                        {"service.name", "<service name>"},  // application name
                        {"service.version", "<service version>"},  // version
                        {"deployment.environment", "<environment>"},  // deployment environment
                        {"host.name", "${hostName}"} // Replace ${hostName} with the actual hostname.
                    }))
                     .AddOtlpExporter(opt =>
                         {
                             opt.Headers = "x-arms-license-key=<license-key>,x-arms-project=<arms-project>,x-cms-workspace=<workspace>"; // Replace <license-key>, <arms-project>, and <workspace> with the endpoint information from Step 1.
                             opt.Endpoint = new Uri("<endpoint>");  // Replace <endpoint> with the endpoint information from Step 1.
                             opt.Protocol = OtlpExportProtocol.Grpc; // Send data over gRPC.
                         })
                    .AddConsoleExporter() // Optional. Print trace data to the console.
                    .Build();
                for(int i = 0; i<10; i++)
                {
                    var MyActivitySource = new ActivitySource(serviceName);
                    using var activity = MyActivitySource.StartActivity("SayHello");
                    activity?.SetTag("bar", "Hello World");
                }
            }
        }
    }
  3. Run the program to generate and report trace data.

    dotnet run

View monitoring data

  1. Log on to the Cloud Monitor 2.0 console, and select a workspace. In the left navigation pane, choose Application Center > Application Observability > Application Insights.

  2. On the Application List page, click the name of the target application to view its monitoring details. For more information, see Application Monitoring.