简体   繁体   中英

.NET - Instrumentation Library For Azure SDK

I'm working on instrument our services.

managed to add instrumentation libraries for:

  • Incoming Http Request.

  • Outgoing Http Requests.

  • Grpc Requests/Responses.

  • MongoDB

  • AWS SDK.

As can be seen below:

public static IServiceCollection AddTracing(this IServiceCollection services) =>
            string.IsNullOrEmpty(ActivitySourceProvider.ActivitySourceName)
                ? services
                : services.
                    AddSingleton<ActivitySourceProvider>().
                    AddOpenTelemetryTracing(
                        tracerProviderBuilder =>
                            tracerProviderBuilder.
                                SetResourceBuilder(
                                    ResourceBuilder.CreateDefault().AddService(ActivitySourceProvider.ActivitySourceName)).
                                AddSource(ActivitySourceProvider.ActivitySourceName).
                                AddAspNetCoreInstrumentation().
                                AddMongoDBInstrumentation().
                                AddGrpcClientInstrumentation(grpcClientInstrumentationOptions => grpcClientInstrumentationOptions.SuppressDownstreamInstrumentation = true).
                                AddHttpClientInstrumentation().
                                AddAWSInstrumentation().
                                AddOtlpExporter(
                                    openTelemetryExporterOptions =>
                                    {
                                        var openTelemetryCollectorUrl =
                                            EnvironmentVariables.TryGet(InfrastructureContext.OpenTelemetryCollectorUrlEnvironmentVariableName);
                                        if (openTelemetryCollectorUrl is not null)
                                        {
                                            openTelemetryExporterOptions.Endpoint = new Uri(openTelemetryCollectorUrl);
                                        }
                                    }));

I wonder if there is library for instrumenting Azure SDK, some thing like the following line:

tracerProviderBuilder.AddAzureInstrumentation();

Does anyone know about library that achieves this?

We have OpenTelemetry support in Azure SDK for.Net as well, which is similar to Amazon Web Services.

Below are few ways where we can enable it:

  1. We can add below variable in application settings and add true AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE

  2. Or we can add the variable in the functions as below and set it to true, this can be customized in respective code languages

     AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
  3. Add below configurations to our project file:

 <ItemGroup> <RuntimeHostConfigurationOption Include="Azure.Experimental.EnableActivitySource" Value="true" /> </ItemGroup>

These are the few things we need to follow to enable the activity source.

From MS Docs we can understand how to add the OpenTelemetry Instrumentation code:

using System.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter;
using OpenTelemetry;
using OpenTelemetry.Trace;

    public class Program
    {
        private static readonly ActivitySource MyActivitySource = new ActivitySource(
            "OTel.AzureMonitor.Demo");

    public static void Main()
    {
        using var tracerProvider = Sdk.CreateTracerProviderBuilder()
            .AddSource("OTel.AzureMonitor.Demo")
            .AddAzureMonitorTraceExporter(o =>
            {
                o.ConnectionString = "<Your Connection String>";
            })
            .Build();

        using (var activity = MyActivitySource.StartActivity("TestActivity"))
        {
            activity?.SetTag("CustomTag1", "Value1");
            activity?.SetTag("CustomTag2", "Value2");
        }

        System.Console.WriteLine("Press Enter key to exit.");
        System.Console.ReadLine();
       }
    }

Have a look on Microsoft documentation and a blog stating devblogs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM