Docs » Supported integrations in Splunk Observability Cloud » Instrument back-end applications to send spans to Splunk APM » Instrument .NET applications for Splunk Observability Cloud (OpenTelemetry) » Manually instrument .NET applications for Splunk Observability Cloud

Manually instrument .NET applications for Splunk Observability Cloud ๐Ÿ”—

The Splunk Distribution of OpenTelemetry .NET zero-code instrumentation provides a base you can build on by adding your own manual instrumentation. By using both zero-code and manual instrumentation, you can better instrument the logic and functionality of your applications, clients, and frameworks.

Create custom traces ๐Ÿ”—

To create custom spans and traces, follow these steps:

  1. Install the Splunk Distribution of OpenTelemetry .NET. See Instrument your .NET application.

  2. Add the System.Diagnostics.DiagnosticSource dependency to your project:

    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
    
  3. Create an ActivitySource instance:

    private static readonly ActivitySource RegisteredActivity = new ActivitySource("Examples.ManualInstrumentations.Registered");
    
  4. Create an Activity (Span). Optionally, set tags:

    using (var activity = RegisteredActivity.StartActivity("Custom Span Name"))
    {
       // Check if the activity is sampled and if full data collection is enabled.
       // This ensures that tags and other custom attributes are only set when the activity is being recorded.
       // Note: Ensure that skipping logic based on sampling does not interfere with essential business operations.
       if(activity?.IsAllDataRequested)
       {
          // your logic for custom activity
          activity.SetTag("foo", "bar1");
       }
    }
    
  5. Register your ActivitySource by setting the OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES environmental variable. You can set the value to either Examples.ManualInstrumentations.Registered or to Examples.ManualInstrumentations.*, which registers the entire prefix.

  6. Invoke the action that generates an Activity, note the trace ID of the Activity, and locate the trace in Splunk APM. You should now see a span with the display name โ€œCustom Span Nameโ€ in the trace tree.

See the OpenTelemetry official documentation for additional information and examples.

Create custom metrics ๐Ÿ”—

To create custom metrics, follow these steps:

  1. Add the System.Diagnostics.DiagnosticSource dependency to your project:

    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
    
  2. Create a Meter instance:

    using var meter = new Meter("My.Application", "1.0");
    
  3. Create an Instrument instance:

    var counter = meter.CreateCounter<long>("custom.counter", description: "Custom counter's description");
    
  4. Update the Instrument value:

    counter.Add(1);
    
  5. Register your Meter with OpenTelemetry.AutoInstrumentation by setting the OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES environment variable:

    OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES=My.Application
    

See the OpenTelemetry official documentation for additional information and examples.

This page was last updated on Mar 19, 2025.