@azure/mcp-linux-arm64 2.0.0-beta.27 → 2.0.0-beta.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -6
- package/dist/Resources/api-reference/dotnet/ActivityProcessors.md +119 -0
- package/dist/Resources/api-reference/dotnet/AddApplicationInsightsTelemetry.md +129 -0
- package/dist/Resources/api-reference/dotnet/AddOpenTelemetry.md +153 -0
- package/dist/Resources/api-reference/dotnet/AzureMonitorExporter.md +137 -0
- package/dist/Resources/api-reference/dotnet/ConfigureOpenTelemetryProvider.md +195 -0
- package/dist/Resources/api-reference/dotnet/ConfigureResource.md +119 -0
- package/dist/Resources/api-reference/dotnet/LogProcessors.md +99 -0
- package/dist/Resources/api-reference/dotnet/OpenTelemetrySdkCreate.md +146 -0
- package/dist/Resources/api-reference/dotnet/Sampling.md +86 -0
- package/dist/Resources/api-reference/dotnet/SdkCreateTracerProviderBuilder.md +127 -0
- package/dist/Resources/api-reference/dotnet/UseAzureMonitor.md +96 -0
- package/dist/Resources/api-reference/dotnet/UseAzureMonitorExporter.md +146 -0
- package/dist/Resources/api-reference/dotnet/WithLogging.md +109 -0
- package/dist/Resources/api-reference/dotnet/WithMetrics.md +105 -0
- package/dist/Resources/api-reference/dotnet/WithTracing.md +91 -0
- package/dist/Resources/concepts/dotnet/azure-monitor-distro.md +102 -0
- package/dist/Resources/concepts/dotnet/opentelemetry-pipeline.md +57 -0
- package/dist/Resources/examples/dotnet/aspnetcore-setup.md +156 -0
- package/dist/Resources/migration/dotnet/appinsights-2x-to-3x-code-migration.md +134 -0
- package/dist/Resources/migration/dotnet/appinsights-2x-to-3x-no-code-change.md +92 -0
- package/dist/azmcp +0 -0
- package/package.json +1 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: SdkCreateTracerProviderBuilder
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 3.x
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Sdk.CreateTracerProviderBuilder / Sdk.CreateMeterProviderBuilder
|
|
8
|
+
|
|
9
|
+
> [!WARNING]
|
|
10
|
+
> **Legacy pattern (<= 1.9.0).** These APIs still work but are no longer the
|
|
11
|
+
> recommended path. For new code, prefer
|
|
12
|
+
> Unified Multi-Signal (`OpenTelemetrySdk.Create`)(see in OpenTelemetrySdkCreate.md) (non-hosted) or
|
|
13
|
+
> Host & DI-Integrated (`AddOpenTelemetry`)(see in AddOpenTelemetry.md) (hosted).
|
|
14
|
+
|
|
15
|
+
## When to use
|
|
16
|
+
|
|
17
|
+
- Codebases targeting OpenTelemetry .NET SDK **< 1.10.0**.
|
|
18
|
+
- **Single-signal** scenarios (e.g., only tracing in a unit test).
|
|
19
|
+
- Quick benchmarks or throwaway scripts where minimal setup is preferred.
|
|
20
|
+
|
|
21
|
+
## Minimal example - tracing only
|
|
22
|
+
|
|
23
|
+
```csharp
|
|
24
|
+
using System.Diagnostics;
|
|
25
|
+
using OpenTelemetry;
|
|
26
|
+
using OpenTelemetry.Trace;
|
|
27
|
+
|
|
28
|
+
// 1. Define an ActivitySource
|
|
29
|
+
var activitySource = new ActivitySource("MyApp");
|
|
30
|
+
|
|
31
|
+
// 2. Build a TracerProvider
|
|
32
|
+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
|
33
|
+
.AddSource("MyApp")
|
|
34
|
+
.ConfigureResource(r => r.AddService("my-service"))
|
|
35
|
+
.AddConsoleExporter()
|
|
36
|
+
.Build();
|
|
37
|
+
|
|
38
|
+
// 3. Create spans
|
|
39
|
+
using (var activity = activitySource.StartActivity("DoWork"))
|
|
40
|
+
{
|
|
41
|
+
activity?.SetTag("result", "ok");
|
|
42
|
+
}
|
|
43
|
+
// TracerProvider.Dispose() is called by `using`, which flushes buffered spans.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Multi-signal example
|
|
47
|
+
|
|
48
|
+
Each signal requires its own builder and its own `Dispose()` call:
|
|
49
|
+
|
|
50
|
+
```csharp
|
|
51
|
+
using System.Diagnostics;
|
|
52
|
+
using System.Diagnostics.Metrics;
|
|
53
|
+
using Microsoft.Extensions.Logging;
|
|
54
|
+
using OpenTelemetry;
|
|
55
|
+
using OpenTelemetry.Logs;
|
|
56
|
+
using OpenTelemetry.Metrics;
|
|
57
|
+
using OpenTelemetry.Trace;
|
|
58
|
+
|
|
59
|
+
// Tracing
|
|
60
|
+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
|
61
|
+
.AddSource("MyApp")
|
|
62
|
+
.AddConsoleExporter()
|
|
63
|
+
.Build();
|
|
64
|
+
|
|
65
|
+
// Metrics
|
|
66
|
+
using var meterProvider = Sdk.CreateMeterProviderBuilder()
|
|
67
|
+
.AddMeter("MyApp")
|
|
68
|
+
.AddConsoleExporter()
|
|
69
|
+
.Build();
|
|
70
|
+
|
|
71
|
+
// Logging (uses ILoggerFactory, not a provider builder)
|
|
72
|
+
using var loggerFactory = LoggerFactory.Create(builder => builder
|
|
73
|
+
.AddOpenTelemetry(options => options.AddConsoleExporter()));
|
|
74
|
+
|
|
75
|
+
var logger = loggerFactory.CreateLogger<Program>();
|
|
76
|
+
logger.LogInformation("Application started");
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
> [!NOTE]
|
|
80
|
+
> Compare the above with
|
|
81
|
+
> Unified Multi-Signal (`OpenTelemetrySdk.Create`)(see in OpenTelemetrySdkCreate.md), which achieves
|
|
82
|
+
> the same result with a single builder and a single `Dispose()`.
|
|
83
|
+
|
|
84
|
+
## API surface
|
|
85
|
+
|
|
86
|
+
### TracerProviderBuilder
|
|
87
|
+
|
|
88
|
+
| Method | Purpose |
|
|
89
|
+
| --- | --- |
|
|
90
|
+
| `.AddSource(params string[] names)` | Subscribe to `ActivitySource` names |
|
|
91
|
+
| `.AddProcessor(BaseProcessor<Activity>)` | Add a span processor |
|
|
92
|
+
| `.SetSampler(Sampler)` | Set the sampling strategy |
|
|
93
|
+
| `.ConfigureResource(Action<ResourceBuilder>)` | Attach resource attributes |
|
|
94
|
+
| `.AddConsoleExporter()` | Export spans to stdout |
|
|
95
|
+
| `.AddOtlpExporter()` | Export spans via OTLP |
|
|
96
|
+
| `.Build()` | Returns a `TracerProvider` |
|
|
97
|
+
|
|
98
|
+
### MeterProviderBuilder
|
|
99
|
+
|
|
100
|
+
| Method | Purpose |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| `.AddMeter(params string[] names)` | Subscribe to `Meter` names |
|
|
103
|
+
| `.AddView(...)` | Configure metric views |
|
|
104
|
+
| `.SetExemplarFilter(ExemplarFilterType)` | Control exemplar collection |
|
|
105
|
+
| `.ConfigureResource(Action<ResourceBuilder>)` | Attach resource attributes |
|
|
106
|
+
| `.AddConsoleExporter()` | Export metrics to stdout |
|
|
107
|
+
| `.AddOtlpExporter()` | Export metrics via OTLP |
|
|
108
|
+
| `.Build()` | Returns a `MeterProvider` |
|
|
109
|
+
|
|
110
|
+
## Lifecycle
|
|
111
|
+
|
|
112
|
+
- **Caller owns disposal.** You must call `Dispose()` (or use `using`) on each
|
|
113
|
+
provider to flush pending telemetry and release resources.
|
|
114
|
+
- Each provider is **independent** - disposing one does not affect the others.
|
|
115
|
+
- In a typical app, create providers at startup and dispose at shutdown.
|
|
116
|
+
|
|
117
|
+
## Why this pattern is considered legacy
|
|
118
|
+
|
|
119
|
+
1. **Verbose for multi-signal apps.** Three builders, three disposals, three
|
|
120
|
+
independent resource configurations.
|
|
121
|
+
2. **No shared `IServiceProvider`.** Processors and exporters that need
|
|
122
|
+
dependency injection must be constructed manually.
|
|
123
|
+
3. **No unified lifecycle.** Each signal must be coordinated separately.
|
|
124
|
+
|
|
125
|
+
`OpenTelemetrySdk.Create()` (>= 1.10.0) resolves all three issues while staying
|
|
126
|
+
host-free.
|
|
127
|
+
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: UseAzureMonitor
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 3.x
|
|
5
|
+
source: Azure.Monitor.OpenTelemetry.AspNetCore/OpenTelemetryBuilderExtensions.cs
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# UseAzureMonitor
|
|
9
|
+
|
|
10
|
+
**Namespace:** `Azure.Monitor.OpenTelemetry.AspNetCore`
|
|
11
|
+
|
|
12
|
+
## Overview
|
|
13
|
+
|
|
14
|
+
Extension method on `IOpenTelemetryBuilder` that configures OpenTelemetry with Azure Monitor exporters and automatic instrumentation.
|
|
15
|
+
|
|
16
|
+
## Signature
|
|
17
|
+
|
|
18
|
+
```csharp
|
|
19
|
+
public static IOpenTelemetryBuilder UseAzureMonitor(
|
|
20
|
+
this IOpenTelemetryBuilder builder);
|
|
21
|
+
|
|
22
|
+
public static IOpenTelemetryBuilder UseAzureMonitor(
|
|
23
|
+
this IOpenTelemetryBuilder builder,
|
|
24
|
+
Action<AzureMonitorOptions> configureAzureMonitor);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Basic (environment variable connection string)
|
|
30
|
+
|
|
31
|
+
```csharp
|
|
32
|
+
using Azure.Monitor.OpenTelemetry.AspNetCore;
|
|
33
|
+
|
|
34
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
35
|
+
builder.Services.AddOpenTelemetry().UseAzureMonitor();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### With options
|
|
39
|
+
|
|
40
|
+
```csharp
|
|
41
|
+
using Azure.Monitor.OpenTelemetry.AspNetCore;
|
|
42
|
+
|
|
43
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
44
|
+
builder.Services.AddOpenTelemetry().UseAzureMonitor(options =>
|
|
45
|
+
{
|
|
46
|
+
options.ConnectionString = "InstrumentationKey=...";
|
|
47
|
+
options.SamplingRatio = 0.5f;
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## AzureMonitorOptions Properties
|
|
52
|
+
|
|
53
|
+
| Property | Type | Description |
|
|
54
|
+
|----------|------|-------------|
|
|
55
|
+
| `ConnectionString` | `string?` | Azure Monitor connection string. If not set, reads from `APPLICATIONINSIGHTS_CONNECTION_STRING` env var |
|
|
56
|
+
| `SamplingRatio` | `float` | Sampling ratio (0.0 to 1.0). Default: 1.0 (100%) |
|
|
57
|
+
| `EnableLiveMetrics` | `bool` | Enable Live Metrics stream. Default: true |
|
|
58
|
+
| `Credential` | `TokenCredential?` | Azure credential for AAD authentication |
|
|
59
|
+
| `Resource` | `Resource?` | Custom OpenTelemetry resource |
|
|
60
|
+
|
|
61
|
+
## What It Configures
|
|
62
|
+
|
|
63
|
+
Calling `UseAzureMonitor()` automatically adds:
|
|
64
|
+
|
|
65
|
+
**Tracing:**
|
|
66
|
+
- ASP.NET Core instrumentation
|
|
67
|
+
- HttpClient instrumentation
|
|
68
|
+
- SQL Client instrumentation
|
|
69
|
+
- Azure SDK instrumentation
|
|
70
|
+
- Azure Monitor trace exporter
|
|
71
|
+
|
|
72
|
+
**Metrics:**
|
|
73
|
+
- ASP.NET Core instrumentation
|
|
74
|
+
- HttpClient instrumentation
|
|
75
|
+
- Runtime instrumentation
|
|
76
|
+
- Azure Monitor metric exporter
|
|
77
|
+
|
|
78
|
+
**Logging:**
|
|
79
|
+
- Azure Monitor log exporter
|
|
80
|
+
|
|
81
|
+
## Chaining with Additional Configuration
|
|
82
|
+
|
|
83
|
+
```csharp
|
|
84
|
+
builder.Services.AddOpenTelemetry()
|
|
85
|
+
.UseAzureMonitor()
|
|
86
|
+
.WithTracing(tracing => tracing
|
|
87
|
+
.AddSource("MyCustomSource")
|
|
88
|
+
.AddProcessor<MyCustomProcessor>())
|
|
89
|
+
.WithMetrics(metrics => metrics
|
|
90
|
+
.AddMeter("MyCustomMeter"));
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## See Also
|
|
94
|
+
|
|
95
|
+
- AddOpenTelemetry(see in AddOpenTelemetry.md)
|
|
96
|
+
- Azure Monitor Distro concept(see in azure-monitor-distro.md)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: UseAzureMonitorExporter
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 3.x
|
|
5
|
+
source: src/OpenTelemetryBuilderExtensions.cs
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# UseAzureMonitorExporter
|
|
9
|
+
|
|
10
|
+
**Namespace:** `Azure.Monitor.OpenTelemetry.Exporter`
|
|
11
|
+
|
|
12
|
+
## When to use
|
|
13
|
+
|
|
14
|
+
- You want Azure Monitor export **without** the automatic instrumentation that `UseAzureMonitor` (distro) adds.
|
|
15
|
+
- You want a single call that registers exporters for **all three signals** (traces, metrics, logs).
|
|
16
|
+
- You want **Live Metrics** support (per-signal methods do not support it).
|
|
17
|
+
- You are adding Azure Monitor to a pipeline that already calls `AddOpenTelemetry()` with custom providers.
|
|
18
|
+
|
|
19
|
+
## Signatures
|
|
20
|
+
|
|
21
|
+
```csharp
|
|
22
|
+
public static IOpenTelemetryBuilder UseAzureMonitorExporter(
|
|
23
|
+
this IOpenTelemetryBuilder builder);
|
|
24
|
+
|
|
25
|
+
public static IOpenTelemetryBuilder UseAzureMonitorExporter(
|
|
26
|
+
this IOpenTelemetryBuilder builder,
|
|
27
|
+
Action<AzureMonitorExporterOptions> configureAzureMonitor);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Automatically calls `WithTracing()`, `WithMetrics()`, and `WithLogging()`.
|
|
31
|
+
Sets the sampler, configures `IncludeFormattedMessage = true` on the logger,
|
|
32
|
+
and enables Live Metrics.
|
|
33
|
+
|
|
34
|
+
## Behavior notes
|
|
35
|
+
|
|
36
|
+
- **Cannot** be combined with per-signal methods (`AddAzureMonitorTraceExporter`, etc.) in the same app — a runtime exception is thrown.
|
|
37
|
+
- Chaining additional `WithTracing()` / `WithMetrics()` / `WithLogging()` calls adds configuration to the existing provider — it does not create a second one.
|
|
38
|
+
- See AzureMonitorExporter(see in AzureMonitorExporter.md) for the per-signal approach.
|
|
39
|
+
|
|
40
|
+
## AzureMonitorExporterOptions
|
|
41
|
+
|
|
42
|
+
Inherits `Azure.Core.ClientOptions`.
|
|
43
|
+
|
|
44
|
+
| Property | Type | Default | Description |
|
|
45
|
+
|---|---|---|---|
|
|
46
|
+
| `ConnectionString` | `string` | `null` | Application Insights connection string. Falls back to `APPLICATIONINSIGHTS_CONNECTION_STRING` env var. |
|
|
47
|
+
| `Credential` | `TokenCredential` | `null` | Azure AD token credential for Entra-based auth. |
|
|
48
|
+
| `SamplingRatio` | `float` | `1.0F` | Fixed-percentage sampling ratio (0.0–1.0). Only used when `TracesPerSecond` is `null`. |
|
|
49
|
+
| `TracesPerSecond` | `double?` | `5.0` | Rate-limited sampling target. Takes precedence over `SamplingRatio`. Set to `null` to use fixed-percentage sampling. |
|
|
50
|
+
| `StorageDirectory` | `string` | `null` | Custom directory for offline/retry storage. Uses OS temp by default. |
|
|
51
|
+
| `DisableOfflineStorage` | `bool` | `false` | Disables persistent storage for failed telemetry. |
|
|
52
|
+
| `EnableLiveMetrics` | `bool` | `true` | Enables Live Metrics stream. |
|
|
53
|
+
| `EnableTraceBasedLogsSampler` | `bool` | `true` | When `true`, only logs correlated to sampled traces are exported. Logs without trace context are always exported. |
|
|
54
|
+
|
|
55
|
+
## Sampling
|
|
56
|
+
|
|
57
|
+
The exporter automatically configures a trace sampler — you do not need to set one yourself.
|
|
58
|
+
|
|
59
|
+
**Two modes**, controlled by `TracesPerSecond` and `SamplingRatio`:
|
|
60
|
+
|
|
61
|
+
| Mode | Activated when | Sampler | Example |
|
|
62
|
+
|---|---|---|---|
|
|
63
|
+
| Rate-limited (default) | `TracesPerSecond` is non-null | `RateLimitedSampler` — caps throughput to N traces/sec | `TracesPerSecond = 5.0` → ~5 traces/sec |
|
|
64
|
+
| Fixed-percentage | `TracesPerSecond` is `null` | `ApplicationInsightsSampler` — samples a fixed ratio | `SamplingRatio = 0.4F` → 40% of traces |
|
|
65
|
+
|
|
66
|
+
Because `TracesPerSecond` defaults to `5.0`, rate-limited sampling is active out of the box. To switch to fixed-percentage sampling:
|
|
67
|
+
|
|
68
|
+
```csharp
|
|
69
|
+
builder.Services.AddOpenTelemetry()
|
|
70
|
+
.UseAzureMonitorExporter(o =>
|
|
71
|
+
{
|
|
72
|
+
o.TracesPerSecond = null; // disable rate-limited sampling
|
|
73
|
+
o.SamplingRatio = 0.25F; // keep 25% of traces
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Environment variable overrides** — `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG`:
|
|
78
|
+
|
|
79
|
+
| `OTEL_TRACES_SAMPLER` | `OTEL_TRACES_SAMPLER_ARG` | Effect |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `microsoft.rate_limited` | `5` | `TracesPerSecond = 5.0` |
|
|
82
|
+
| `microsoft.fixed_percentage` | `0.4` | `SamplingRatio = 0.4F`, `TracesPerSecond = null` |
|
|
83
|
+
|
|
84
|
+
Environment variables override `IConfiguration` values, which override code defaults.
|
|
85
|
+
|
|
86
|
+
**Log sampling** — when `EnableTraceBasedLogsSampler` is `true` (default), logs correlated to a sampled-out trace are also dropped. Logs without trace context are always exported.
|
|
87
|
+
|
|
88
|
+
## Minimal example
|
|
89
|
+
|
|
90
|
+
```csharp
|
|
91
|
+
using Azure.Monitor.OpenTelemetry.Exporter;
|
|
92
|
+
|
|
93
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
94
|
+
|
|
95
|
+
// Connection string from APPLICATIONINSIGHTS_CONNECTION_STRING env var
|
|
96
|
+
builder.Services.AddOpenTelemetry().UseAzureMonitorExporter();
|
|
97
|
+
|
|
98
|
+
var app = builder.Build();
|
|
99
|
+
app.Run();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Full example
|
|
103
|
+
|
|
104
|
+
```csharp
|
|
105
|
+
using Azure.Identity;
|
|
106
|
+
using Azure.Monitor.OpenTelemetry.Exporter;
|
|
107
|
+
using OpenTelemetry.Trace;
|
|
108
|
+
|
|
109
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
110
|
+
|
|
111
|
+
builder.Services.AddOpenTelemetry()
|
|
112
|
+
.UseAzureMonitorExporter(o =>
|
|
113
|
+
{
|
|
114
|
+
o.ConnectionString = builder.Configuration["AzureMonitor:ConnectionString"];
|
|
115
|
+
o.Credential = new DefaultAzureCredential();
|
|
116
|
+
o.TracesPerSecond = null; // switch to fixed-percentage sampling
|
|
117
|
+
o.SamplingRatio = 0.5F; // keep 50% of traces
|
|
118
|
+
o.EnableTraceBasedLogsSampler = false; // export all logs
|
|
119
|
+
})
|
|
120
|
+
.WithTracing(tracing => tracing
|
|
121
|
+
.AddAspNetCoreInstrumentation()
|
|
122
|
+
.AddHttpClientInstrumentation()
|
|
123
|
+
.AddSource("MyApp"));
|
|
124
|
+
|
|
125
|
+
var app = builder.Build();
|
|
126
|
+
app.Run();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Non-hosted (console / CLI)
|
|
130
|
+
|
|
131
|
+
```csharp
|
|
132
|
+
using Azure.Monitor.OpenTelemetry.Exporter;
|
|
133
|
+
using OpenTelemetry;
|
|
134
|
+
|
|
135
|
+
using var sdk = OpenTelemetrySdk.Create(builder => builder
|
|
136
|
+
.UseAzureMonitorExporter()
|
|
137
|
+
.WithTracing(tracing => tracing.AddSource("MyApp")));
|
|
138
|
+
|
|
139
|
+
// sdk.Dispose() flushes all signals on exit.
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## See also
|
|
143
|
+
|
|
144
|
+
- UseAzureMonitor (distro)(see in UseAzureMonitor.md)
|
|
145
|
+
- Per-signal exporters (AzureMonitorExporter)(see in AzureMonitorExporter.md)
|
|
146
|
+
- AddOpenTelemetry(see in AddOpenTelemetry.md)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: WithLogging
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 1.x
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# .WithLogging()
|
|
8
|
+
|
|
9
|
+
## API surface
|
|
10
|
+
|
|
11
|
+
| Method | Purpose |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `.WithLogging()` | Enable logging with defaults |
|
|
14
|
+
| `.WithLogging(Action<LoggerProviderBuilder>)` | Enable logging and configure the provider builder |
|
|
15
|
+
| `.WithLogging(Action<LoggerProviderBuilder>?, Action<OpenTelemetryLoggerOptions>?)` | Configure both provider and logger options |
|
|
16
|
+
| `ILoggingBuilder.AddOpenTelemetry()` | Register OpenTelemetry logger without the host builder |
|
|
17
|
+
| `ILoggingBuilder.AddOpenTelemetry(Action<OpenTelemetryLoggerOptions>?)` | Register with options configuration |
|
|
18
|
+
| `builder.AddProcessor(BaseProcessor<LogRecord>)` | Add a log processor instance |
|
|
19
|
+
| `builder.AddProcessor<T>()` | Add a processor resolved from DI (singleton) |
|
|
20
|
+
| `builder.AddProcessor(Func<IServiceProvider, BaseProcessor<LogRecord>>)` | Add a processor via factory |
|
|
21
|
+
|
|
22
|
+
See also: ConfigureResource.md for resource configuration
|
|
23
|
+
|
|
24
|
+
## When to use
|
|
25
|
+
|
|
26
|
+
- Route `ILogger` log records through the OpenTelemetry pipeline in an ASP.NET Core or Worker Service app.
|
|
27
|
+
- Configure `OpenTelemetryLoggerOptions` (formatted messages, scopes, state parsing) alongside the provider.
|
|
28
|
+
- Use `AddOpenTelemetry()` on `ILoggingBuilder` when you need logging without the full `AddOpenTelemetry()` builder (e.g., non-host `ServiceCollection`).
|
|
29
|
+
- Call multiple times safely — only one `LoggerProvider` is created per `IServiceCollection`.
|
|
30
|
+
|
|
31
|
+
## Minimal example
|
|
32
|
+
|
|
33
|
+
```csharp
|
|
34
|
+
using OpenTelemetry;
|
|
35
|
+
using OpenTelemetry.Logs;
|
|
36
|
+
|
|
37
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
38
|
+
|
|
39
|
+
builder.Services.AddOpenTelemetry()
|
|
40
|
+
.WithLogging(logging => logging
|
|
41
|
+
.AddOtlpExporter());
|
|
42
|
+
|
|
43
|
+
var app = builder.Build();
|
|
44
|
+
app.Run();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Full example
|
|
48
|
+
|
|
49
|
+
```csharp
|
|
50
|
+
using OpenTelemetry;
|
|
51
|
+
using OpenTelemetry.Logs;
|
|
52
|
+
using OpenTelemetry.Resources;
|
|
53
|
+
|
|
54
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
55
|
+
|
|
56
|
+
builder.Services.AddOpenTelemetry()
|
|
57
|
+
.ConfigureResource(r => r.AddService("order-api"))
|
|
58
|
+
.WithLogging(
|
|
59
|
+
configureBuilder: logging => logging
|
|
60
|
+
.AddProcessor<MyLogEnrichmentProcessor>()
|
|
61
|
+
.AddOtlpExporter(),
|
|
62
|
+
configureOptions: options =>
|
|
63
|
+
{
|
|
64
|
+
options.IncludeFormattedMessage = true;
|
|
65
|
+
options.IncludeScopes = true;
|
|
66
|
+
options.ParseStateValues = true;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
var app = builder.Build();
|
|
70
|
+
app.Run();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Alternative: ILoggingBuilder.AddOpenTelemetry()
|
|
74
|
+
|
|
75
|
+
Use this when you don't have an `IOpenTelemetryBuilder` — for example, a bare `ServiceCollection`:
|
|
76
|
+
|
|
77
|
+
```csharp
|
|
78
|
+
using Microsoft.Extensions.DependencyInjection;
|
|
79
|
+
using Microsoft.Extensions.Logging;
|
|
80
|
+
using OpenTelemetry.Logs;
|
|
81
|
+
|
|
82
|
+
var services = new ServiceCollection();
|
|
83
|
+
|
|
84
|
+
services.AddLogging(logging => logging
|
|
85
|
+
.AddOpenTelemetry(options =>
|
|
86
|
+
{
|
|
87
|
+
options.IncludeFormattedMessage = true;
|
|
88
|
+
}));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Key parameters
|
|
92
|
+
|
|
93
|
+
`OpenTelemetryLoggerOptions` properties:
|
|
94
|
+
|
|
95
|
+
| Parameter | Type | Default | Description |
|
|
96
|
+
| --- | --- | --- | --- |
|
|
97
|
+
| `IncludeFormattedMessage` | `bool` | `false` | Include the formatted log message on `LogRecord` |
|
|
98
|
+
| `IncludeScopes` | `bool` | `false` | Include log scopes on `LogRecord` |
|
|
99
|
+
| `ParseStateValues` | `bool` | `false` | Parse state into `LogRecord.Attributes` (sets `State` to `null`) |
|
|
100
|
+
|
|
101
|
+
## Behavior notes
|
|
102
|
+
|
|
103
|
+
- `WithLogging()` automatically registers an `ILoggerProvider` named `"OpenTelemetry"`.
|
|
104
|
+
- `AddOpenTelemetry()` on `ILoggingBuilder` does **not** support all `IServiceCollection` features available to tracing/metrics (e.g., `ConfigureServices` extensions).
|
|
105
|
+
- When `IncludeFormattedMessage` is `false`, the formatted message is still included if no message template is found.
|
|
106
|
+
- When `ParseStateValues` is `true`, `LogRecord.State` is always `null` — use `LogRecord.Attributes` instead.
|
|
107
|
+
- State parsing happens automatically (regardless of `ParseStateValues`) if the logged state implements `IReadOnlyList<KeyValuePair<string, object>>`.
|
|
108
|
+
- Processors run in registration order. Export processors should be added last.
|
|
109
|
+
- `OpenTelemetryLoggerOptions` reloading is disabled internally to prevent unwanted re-creation of processors/exporters during `IConfiguration` reloads.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: WithMetrics
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 1.x
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# .WithMetrics()
|
|
8
|
+
|
|
9
|
+
## API surface
|
|
10
|
+
|
|
11
|
+
| Method | Purpose |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `.WithMetrics()` | Enable metrics with no additional configuration |
|
|
14
|
+
| `.WithMetrics(Action<MeterProviderBuilder>)` | Enable metrics and configure the `MeterProviderBuilder` |
|
|
15
|
+
| `builder.AddMeter(params string[] names)` | Subscribe to one or more `Meter` names |
|
|
16
|
+
| `builder.AddReader(MetricReader)` | Add a metric reader instance |
|
|
17
|
+
| `builder.AddReader<T>()` | Add a reader resolved from DI (registered as singleton) |
|
|
18
|
+
| `builder.AddReader(Func<IServiceProvider, MetricReader>)` | Add a reader via factory |
|
|
19
|
+
| `builder.AddView(string instrumentName, string name)` | Rename a metric stream (no wildcards) |
|
|
20
|
+
| `builder.AddView(string instrumentName, MetricStreamConfiguration)` | Customize aggregation for an instrument (wildcards allowed) |
|
|
21
|
+
| `builder.AddView(Func<Instrument, MetricStreamConfiguration?>)` | Programmatic view selection |
|
|
22
|
+
| `builder.SetMaxMetricStreams(int)` | Set max metric streams (default: 1000) |
|
|
23
|
+
| `builder.SetExemplarFilter(ExemplarFilterType)` | Set exemplar filter (`AlwaysOff`, `AlwaysOn`, `TraceBased`) |
|
|
24
|
+
|
|
25
|
+
See also: ConfigureResource.md for resource configuration
|
|
26
|
+
|
|
27
|
+
## When to use
|
|
28
|
+
|
|
29
|
+
- Enable metrics collection in an ASP.NET Core or Worker Service app via `AddOpenTelemetry()`.
|
|
30
|
+
- Enable metrics in a standalone/console app via `OpenTelemetrySdk.Create()`.
|
|
31
|
+
- Subscribe to `Meter` names from your application code or instrumentation libraries.
|
|
32
|
+
- Configure views to customize aggregation, rename streams, or set cardinality limits.
|
|
33
|
+
- Call multiple times safely — only one `MeterProvider` is created per `IServiceCollection`.
|
|
34
|
+
|
|
35
|
+
## Minimal example
|
|
36
|
+
|
|
37
|
+
```csharp
|
|
38
|
+
using OpenTelemetry;
|
|
39
|
+
using OpenTelemetry.Metrics;
|
|
40
|
+
|
|
41
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
42
|
+
|
|
43
|
+
builder.Services.AddOpenTelemetry()
|
|
44
|
+
.WithMetrics(metrics => metrics
|
|
45
|
+
.AddMeter("MyApp")
|
|
46
|
+
.AddOtlpExporter());
|
|
47
|
+
|
|
48
|
+
var app = builder.Build();
|
|
49
|
+
app.Run();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Full example
|
|
53
|
+
|
|
54
|
+
```csharp
|
|
55
|
+
using OpenTelemetry;
|
|
56
|
+
using OpenTelemetry.Metrics;
|
|
57
|
+
using OpenTelemetry.Resources;
|
|
58
|
+
|
|
59
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
60
|
+
|
|
61
|
+
builder.Services.AddOpenTelemetry()
|
|
62
|
+
.ConfigureResource(r => r.AddService("order-api"))
|
|
63
|
+
.WithMetrics(metrics => metrics
|
|
64
|
+
.AddMeter("OrderService", "PaymentService")
|
|
65
|
+
.AddAspNetCoreInstrumentation()
|
|
66
|
+
.AddHttpClientInstrumentation()
|
|
67
|
+
.AddView("request-duration", new ExplicitBucketHistogramConfiguration
|
|
68
|
+
{
|
|
69
|
+
Boundaries = new double[] { 0.01, 0.05, 0.1, 0.5, 1, 5, 10 }
|
|
70
|
+
})
|
|
71
|
+
.SetMaxMetricStreams(500)
|
|
72
|
+
.SetExemplarFilter(ExemplarFilterType.TraceBased)
|
|
73
|
+
.AddOtlpExporter());
|
|
74
|
+
|
|
75
|
+
var app = builder.Build();
|
|
76
|
+
app.Run();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Standalone (non-host) usage
|
|
80
|
+
|
|
81
|
+
```csharp
|
|
82
|
+
using OpenTelemetry;
|
|
83
|
+
using OpenTelemetry.Metrics;
|
|
84
|
+
|
|
85
|
+
using var sdk = OpenTelemetrySdk.Create(builder => builder
|
|
86
|
+
.WithMetrics(metrics => metrics
|
|
87
|
+
.AddMeter("MyConsoleApp")
|
|
88
|
+
.AddOtlpExporter()));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Key parameters
|
|
92
|
+
|
|
93
|
+
| Parameter | Type | Default | Description |
|
|
94
|
+
| --- | --- | --- | --- |
|
|
95
|
+
| `maxMetricStreams` | `int` | 1000 | Maximum number of metric streams allowed (min: 1) |
|
|
96
|
+
| `exemplarFilter` | `ExemplarFilterType` | `AlwaysOff` | Controls how measurements are offered to exemplar reservoirs |
|
|
97
|
+
|
|
98
|
+
## Behavior notes
|
|
99
|
+
|
|
100
|
+
- `AddMeter()` accepts exact `Meter` names — unregistered meters are ignored.
|
|
101
|
+
- `AddView()` with a wildcard (`*`) in `instrumentName` cannot rename a stream (would cause conflicts).
|
|
102
|
+
- Views are applied in registration order. The first matching view wins per instrument.
|
|
103
|
+
- `SetMaxMetricPointsPerMetricStream()` is obsolete since 1.10.0 — use `MetricStreamConfiguration.CardinalityLimit` via `AddView` instead.
|
|
104
|
+
- `WithMetrics()` automatically registers an `IMetricsListener` named `"OpenTelemetry"` to bridge `System.Diagnostics.Metrics`.
|
|
105
|
+
- When used with `AddOpenTelemetry()`, the host manages `MeterProvider` lifecycle — do **not** call `Dispose()` manually.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: WithTracing
|
|
3
|
+
category: api-reference
|
|
4
|
+
applies-to: 1.x
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# .WithTracing()
|
|
8
|
+
|
|
9
|
+
## API surface
|
|
10
|
+
|
|
11
|
+
| Method | Purpose |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `.WithTracing()` | Enable tracing with no additional configuration |
|
|
14
|
+
| `.WithTracing(Action<TracerProviderBuilder>)` | Enable tracing and configure the `TracerProviderBuilder` |
|
|
15
|
+
| `builder.AddSource(params string[] names)` | Subscribe to one or more `ActivitySource` names |
|
|
16
|
+
| `builder.AddInstrumentation<T>()` | Add an instrumentation library via DI |
|
|
17
|
+
| `builder.AddProcessor(BaseProcessor<Activity>)` | Add an activity processor instance |
|
|
18
|
+
| `builder.AddProcessor<T>()` | Add a processor resolved from DI (registered as singleton) |
|
|
19
|
+
| `builder.AddProcessor(Func<IServiceProvider, BaseProcessor<Activity>>)` | Add a processor via factory |
|
|
20
|
+
| `builder.SetErrorStatusOnException(bool)` | Auto-set `Status.Error` on unhandled exceptions (default `true`) |
|
|
21
|
+
|
|
22
|
+
See also: Sampling.md for `SetSampler()` overloads · ConfigureResource.md for resource configuration
|
|
23
|
+
|
|
24
|
+
## When to use
|
|
25
|
+
|
|
26
|
+
- Enable distributed tracing in an ASP.NET Core or Worker Service app via `AddOpenTelemetry()`.
|
|
27
|
+
- Enable tracing in a standalone/console app via `OpenTelemetrySdk.Create()`.
|
|
28
|
+
- Subscribe to `ActivitySource` names so the SDK captures spans from your code or instrumentation libraries.
|
|
29
|
+
- Attach processors (e.g., `BatchActivityExportProcessor`) or set a custom sampler.
|
|
30
|
+
- Call multiple times safely — only one `TracerProvider` is created per `IServiceCollection`.
|
|
31
|
+
|
|
32
|
+
## Minimal example
|
|
33
|
+
|
|
34
|
+
```csharp
|
|
35
|
+
using OpenTelemetry;
|
|
36
|
+
using OpenTelemetry.Trace;
|
|
37
|
+
|
|
38
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
39
|
+
|
|
40
|
+
builder.Services.AddOpenTelemetry()
|
|
41
|
+
.WithTracing(tracing => tracing
|
|
42
|
+
.AddSource("MyApp")
|
|
43
|
+
.AddOtlpExporter());
|
|
44
|
+
|
|
45
|
+
var app = builder.Build();
|
|
46
|
+
app.Run();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Full example
|
|
50
|
+
|
|
51
|
+
```csharp
|
|
52
|
+
using OpenTelemetry;
|
|
53
|
+
using OpenTelemetry.Resources;
|
|
54
|
+
using OpenTelemetry.Trace;
|
|
55
|
+
|
|
56
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
57
|
+
|
|
58
|
+
builder.Services.AddOpenTelemetry()
|
|
59
|
+
.ConfigureResource(r => r.AddService("order-api"))
|
|
60
|
+
.WithTracing(tracing => tracing
|
|
61
|
+
.AddSource("OrderService", "PaymentService")
|
|
62
|
+
.AddAspNetCoreInstrumentation()
|
|
63
|
+
.AddHttpClientInstrumentation()
|
|
64
|
+
.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(0.5)))
|
|
65
|
+
.AddProcessor<MyFilteringProcessor>()
|
|
66
|
+
.AddOtlpExporter());
|
|
67
|
+
|
|
68
|
+
var app = builder.Build();
|
|
69
|
+
app.Run();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Standalone (non-host) usage
|
|
73
|
+
|
|
74
|
+
```csharp
|
|
75
|
+
using OpenTelemetry;
|
|
76
|
+
using OpenTelemetry.Trace;
|
|
77
|
+
|
|
78
|
+
using var sdk = OpenTelemetrySdk.Create(builder => builder
|
|
79
|
+
.WithTracing(tracing => tracing
|
|
80
|
+
.AddSource("MyConsoleApp")
|
|
81
|
+
.AddOtlpExporter()));
|
|
82
|
+
|
|
83
|
+
// sdk.TracerProvider is available if needed
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Behavior notes
|
|
87
|
+
|
|
88
|
+
- `AddSource()` accepts wildcard-free `ActivitySource` names. Activities from unregistered sources are dropped.
|
|
89
|
+
- Processors run in registration order. Export processors should be added last.
|
|
90
|
+
- When used with `AddOpenTelemetry()`, the host manages `TracerProvider` lifecycle — do **not** call `Dispose()` manually.
|
|
91
|
+
- When used with `OpenTelemetrySdk.Create()`, you own the `OpenTelemetrySdk` instance and must dispose it.
|