Document how to add custom metrics (#2350)

This commit is contained in:
Mateusz Łach 2023-03-21 09:32:53 +01:00 committed by GitHub
parent 1e22bb13ac
commit f621dec002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 1 deletions

View File

@ -5,6 +5,8 @@ manual instrumentation. By using both automatic and manual instrumentation, you
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:
@ -39,6 +41,46 @@ You can see a sample console application with manual instrumentation [here](../e
> Note that an `Activity` created for `NonRegistered.ManualInstrumentations`
`ActivitySource` is not handled by the OpenTelemetry Automatic Instrumentation.
Further reading:
## 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)