Manually instrument .NET applications for Splunk Observability Cloud
Manually instrument your .NET application to add custom attributes to spans or manually generate spans. Keep reading to learn how to manually instrument your .NET application 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:
-
Install the Splunk Distribution of OpenTelemetry .NET. See Instrument your .NET application.
-
Add the
System.Diagnostics.DiagnosticSourcedependency to your project:XML<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" /> -
Create an
ActivitySourceinstance:CSHARPprivate static readonly ActivitySource RegisteredActivity = new ActivitySource("Examples.ManualInstrumentations.Registered"); -
Create an
Activity. Optionally, set tags:CSHARPusing (var activity = RegisteredActivity.StartActivity("Main")) { activity?.SetTag("foo", "bar1"); // your logic for Main activity } -
Register your
ActivitySourceby setting theOTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCESenvironmental variable. You can set the value to eitherExamples.ManualInstrumentations.Registeredor toExamples.ManualInstrumentations.*, which registers the entire prefix.
See the OpenTelemetry official documentation for additional information and examples.
Create custom metrics
To create custom metrics, follow these steps:
-
Add the
System.Diagnostics.DiagnosticSourcedependency to your project:XML<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" /> -
Create a
Meterinstance:CSHARPusing var meter = new Meter("My.Application", "1.0"); -
Create an
Instrumentinstance:CSHARPvar counter = meter.CreateCounter<long>("custom.counter", description: "Custom counter's description"); -
Update the
Instrumentvalue:CSHARPcounter.Add(1); -
Register your
Meterwith OpenTelemetry.AutoInstrumentation by setting theOTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCESenvironment variable:BASHOTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES=My.Application
See https://opentelemetry.io/docs/languages/net/instrumentation/#metrics in the official OpenTelemetry documentation for additional information and examples.