-
Notifications
You must be signed in to change notification settings - Fork 91
/
manual-instrumentation.md
86 lines (58 loc) · 2.9 KB
/
manual-instrumentation.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Manually instrument a .NET application
The automatic instrumentation provides a base you can build on by adding your own
manual instrumentation. By using both automatic and manual instrumentation, you can
better instrument the logic and functionality of your applications, clients,
and frameworks.
## Traces
To create your custom traces manually, follow these steps:
1. Add the `System.Diagnostics.DiagnosticSource` dependency to your project:
```xml
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
```
2. Create an `ActivitySource` instance:
```csharp
private static readonly ActivitySource RegisteredActivity = new ActivitySource("Examples.ManualInstrumentations.Registered");
```
3. Create an `Activity`. Optionally, set tags:
```csharp
using (var activity = RegisteredActivity.StartActivity("Main"))
{
activity?.SetTag("foo", "bar1");
// your logic for Main activity
}
```
4. Register your `ActivitySource` in OpenTelemetry.AutoInstrumentation
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.
You can see a sample console application with manual instrumentation [here](../examples/demo/Service/Program.cs).
> Note that an `Activity` created for `NonRegistered.ManualInstrumentations`
`ActivitySource` is not handled by the OpenTelemetry Automatic Instrumentation.
## Metrics
To create your custom metrics manually, follow these steps:
1. Add the `System.Diagnostics.DiagnosticSource` dependency to your project:
```xml
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.0" />
```
2. Create a `Meter` instance:
```csharp
using var meter = new Meter("Examples.Service", "1.0");
```
3. Create an `Instrument`:
```csharp
var successCounter = meter.CreateCounter<long>("srv.successes.count", description: "Number of successful responses");
```
4. Update the `Instrument` value. Optionally, set tags:
```csharp
successCounter.Add(1, new KeyValuePair<string, object?>("tagName", "tagValue"));
```
5. Register your `Meter` with OpenTelemetry.AutoInstrumentation by setting the
`OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES` environment variable:
```bash
OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES=Examples.Service
```
You can set the value to either `Examples.Service`
or to `Examples.*`, which registers the entire prefix.
You can see a sample console application with manual metric instrumentation [here](../examples/demo/Service/Program.cs).
## Further reading
- [OpenTelemetry.io documentation for .NET Manual Instrumentation](https://opentelemetry.io/docs/instrumentation/net/manual/#setting-up-an-activitysource)