Digma provides an optional capability to add instrumentation to your .NET methods and classes without changing your code. This is done by injecting OpenTelemetry attributes dynamically at runtime when the classes are first loaded. To control the scope of the automatic instrumentation, you can specify a list of relevant namespaces as well as other optional parameters to control how the code is instrumented.
Add the NuGet package
dotnetaddpackageOpenTel.AutoInstrumentation.Digma
Add the following environment variables to load the Digma OTEL plugin
Specify which namespaces to instrument automatically
Create a JSON file autoinstrumentation.rules.json and save it in the same directly as your .NET application DLLs.
In this file, you can specify rules for the inclusion/exclusion of specific namespaces, classes, and methods. If you wish to name the file differently or place it in a custom location, you can use the following environment variable
The JSON rules file uses a simple syntax to define a set of rules that are used to determine which code should be dynamically instrumented for tracing. You can reference your own namespace's classes and methods. It is recommended to be concise and avoid including low-level infrastructure code that may inflate the tracing data, making it less readable.
{"include":[{"namespaces":"Acme.Core.*"// namespaces field is the minimum},{"namespaces":"Acme.Repositories.*","classes":"Repo*","methods":"Get*","syncModifier":"Async",// Async or Sync, default is null"accessModifier":"Public",// Private or Public, default is null"nestedOnly":true// default is false},{"namespaces":"/^Acme\\.V\\d+/",// Regex"classes":"/^Repo\\w+/",// Regex"methods":"/^Get\\w+/",// Regex}],"exclude":[{"namespaces":"Acme.Tools.*"}]}
Check the Digma autoinstrumentation repo for a more comprehensive documentation of the JSON syntax.
Optional Context for Better Error Processing
For better parsing of .NET errors and to differentiate between external code and your application, it is recommended to add the code.namespace.rootattribute. The easiest way to add that attribute is via the OTEL_RESOURCE_ATTRIBUTES environment variable.