Skip to content

RemyDuijkeren/OpenTelemetry.Instrumentation.DataverseServiceClient

Repository files navigation

Dataverse ServiceClient Instrumentation for OpenTelemetry .NET

CI NuGet NuGet

This is an Instrumentation Library, which instruments Dataverse ServiceClient and collect traces about incoming Dataverse requests.

This component is based on the v1.24 of database semantic conventions. For details on the default set of attributes that are added, checkout Traces sections below.

Getting Started

Step 1: Install Package

Add a reference to the RemyDuijkeren.OpenTelemetry.Instrumentation.DataverseServiceClient package. Also, add any other instrumentations & exporters you will need.

dotnet add package RemyDuijkeren.OpenTelemetry.Instrumentation.DataverseServiceClient

Step 2: Enable Dataverse ServiceClient Instrumentation at application startup

Dataverse ServiceClient instrumentation must be enabled at application startup, when setting up the host for OpenTelemetry. See the package OpenTelemetry.Extensions.Hosting for more info how to add OpenTelemetry to the application.

The following example demonstrates adding Dataverse ServiceClient instrumentation within the extension method WithTracing() on the OpenTelemetryBuilder. The extension method AddDataverseServiceClientInstrumentation() registers the instrumentation to the TracerProvider and tries to replace any registered IOrganizationService, IOrganizationServiceAsync or IOrganizationServiceAsync2 by wrapping the service into OpenTelemetryServiceClientDecorator that will do the actual tracing.

This example also sets up the Console Exporter, which requires adding the package OpenTelemetry.Exporter.Console to the application.

using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Trace;

public void ConfigureServices(IServiceCollection services)
{
    services.AddOpenTelemetry()
        .WithTracing(builder => builder
            .AddDataverseServiceClientInstrumentation()
            .AddConsoleExporter());
}

Optional create the OpenTelemetryServiceClientDecorator manually by passing it a ServiceClient like:

using Microsoft.PowerPlatform.Dataverse.Client;
using OpenTelemetry.Instrumentation.DataverseServiceClient;

// Add 'OpenTelemetry.Instrumentation.DataverseServiceClient' as source

// Create ServiceClient and wrap it into OpenTelemetryServiceClientDecorator
var serviceClient = new ServiceClient(new ConnectionOptions { /* your config */ } );
IOrganizationServiceAsync2 decoratedServiceClient = new OpenTelemetryServiceClientDecorator(serviceClient);

// Use the decoratedServiceClient as you would use the serviceClient
var response = decoratedServiceClient.Execute(new WhoAmIRequest());

Traces

Following list of attributes are added by default on activity. See db-spans for more details about each individual attribute:

  • db.operation
  • db.sql.table
  • db.statement
  • db.system
  • db.name
  • db.connection_string
  • db.user
  • db.dataverse.organization_id
  • db.dataverse.organization_version
  • db.dataverse.geo
  • error.type
  • server.address
  • server.port

Add Custom Activities

When you have helper methods for Dataverse calls that you want to trace, you can create custom activities, using the extension method StartDataverseActivity:

public class DataverseHelper
{
    private readonly IOrganizationService _service;

    public DataverseHelper(IOrganizationService service)
    {
        _service = service;
    }

    public void MyFirstHelperMethod()
    {
        using Activity? activity = _service.StartDataverseActivity();

        // Add custom attributes (optional)
        activity?.SetTag("custom.tag", "custom value");

        // Use the service with your own logic
        var response = _service.Execute(new WhoAmIRequest());
    }
}

There are overloads for StartDataverseActivity to pass in the Entity or EntityReference, statement and operation. By default the operation is the name of the method that called StartDataverseActivity, in this example MyFirstHelperMethod.

References

About

Dataverse ServiceClient Instrumentation for OpenTelemetry .NET

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages