Skip to content

Commit 5d9c7ff

Browse files
authored
Merge pull request #1002 from DuendeSoftware/ar/logs-to-otel
Add Logs info to OTel docs
2 parents 9b60db5 + 1b46eae commit 5d9c7ff

4 files changed

Lines changed: 64 additions & 9 deletions

File tree

92.2 KB
Loading
91.8 KB
Loading

src/content/docs/identityserver/diagnostics/logging.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ var isBuilder = builder.Services.AddIdentityServer(options =>
114114
```
115115

116116
Returning `true` means the exception will be logged, while returning `false` indicates the exception should not be logged.
117+
118+
## OpenTelemetry
119+
120+
Logs written to the standard `ILogger` system in .NET 8+ can be exported to OpenTelemetry traces at runtime. This helps visualize when the log statement occurred in relation to the entire request. The logs are augmented with trace ids and correlated with traces. Have a look at [logs in OpenTelemetry](/identityserver/diagnostics/otel.md#logs) for setup details.

src/content/docs/identityserver/diagnostics/otel.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ redirect_from:
1919
Added in Duende IdentityServer v6.1 and expanded in v7.0
2020
:::
2121

22-
[OpenTelemetry](https://opentelemetry.io) is a collection of tools, APIs, and SDKs for generating and collecting
22+
[OpenTelemetry](https://opentelemetry.io) (OTel) is a collection of tools, APIs, and SDKs for generating and collecting
2323
telemetry data (metrics, logs, and traces). This is very useful for analyzing software performance and behavior,
2424
especially in highly distributed systems.
2525

26-
.NET 8 comes with first class support for OpenTelemetry. IdentityServer emits traces, metrics, and logs.
2726

28-
### Metrics
27+
## OpenTelemetry Signals
2928

30-
Metrics are high level statistic counters. They provide an aggregated overview and can be used to set monitoring rules.
29+
OpenTelemetry signals are the information collected and processed to describe the internal activity of the system. The most common signals are traces, metrics, and logs.
3130

32-
### Logs
31+
.NET 8+ comes with first class support for OpenTelemetry. IdentityServer emits traces, metrics, and logs you can collect.
3332

34-
OpenTelemetry in .NET 8 exports the logs written to the standard ILogger system. The logs are augmented with
35-
trace ids to be able to correlate log entries with traces.
33+
### Metrics
34+
35+
Metrics are high level statistic counters. They provide an aggregated overview and can be used to set monitoring rules.
3636

3737
### Traces
3838

@@ -45,20 +45,47 @@ IdentityServer to get a new access token and then calls the API. The API reads t
4545
url and then gets the keys from jwks endpoint.
4646
![.NET Aspire dashboard showing Duende IdentityServer traces](images/aspire_traces.png)
4747

48+
### Logs
49+
50+
OpenTelemetry in .NET 8+ can export logs written to the standard `ILogger` system. The logs are augmented with
51+
trace ids and correlated with traces.
52+
53+
This is an example of a structured log message from a web application calling an API (also displayed using our
54+
[Aspire sample](/identityserver/samples/diagnostics.mdx)).
55+
56+
![.NET Aspire dashboard showing Duende IdentityServer Structured Logs](images/aspire_structured_logs.png)
57+
58+
Here is an example of that same log message appearing in the trace. Aspire displays the log entry details as dots on the trace timeline.
59+
60+
![.NET Aspire dashboard showing Duende IdentityServer a trace with a log entry](images/aspire_structured_logs_in_trace.png)
61+
4862
## Setup
4963

5064
To start emitting OpenTelemetry tracing and metrics information you need to:
5165

5266
* add the OpenTelemetry libraries to your IdentityServer and client applications
53-
* start collecting traces and Metrics from the various IdentityServer sources (and other sources e.g. ASP.NET Core)
67+
* start collecting traces and metrics from the various IdentityServer sources (and other sources e.g. ASP.NET Core)
68+
* add the OpenTelemetry configuration to your service setup
5469

5570
For development a simple option is to export the tracing information to the console and use the Prometheus
5671
exporter to create a human-readable `/metrics` endpoint for the metrics.
5772

58-
Add the OpenTelemetry configuration to your service setup.
73+
```bash
74+
dotnet add package OpenTelemetry
75+
dotnet add package OpenTelemetry.Extensions.Hosting
76+
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
77+
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
78+
```
5979

6080
```csharp
6181
// Program.cs
82+
using OpenTelemetry.Resources;
83+
84+
// Add OpenTelemetry logging infrastructure
85+
// to correlate logs with traces
86+
builder.Logging.AddOpenTelemetry();
87+
88+
// Enable OpenTelemetry
6289
var openTelemetry = builder.Services.AddOpenTelemetry();
6390

6491
openTelemetry.ConfigureResource(r => r
@@ -432,3 +459,27 @@ You can select which information you are interested in by selectively listening
432459
* *`IdentityServerConstants.Tracing.Validation`*
433460

434461
More detailed tracing related to validation
462+
463+
## OpenTelemetry From 3rd Party Logging Frameworks
464+
465+
If you're unable to use the `ILogger` system in .NET, your choice of logging framework may be able to push log messages to traces. You can view their documentation to set that up.
466+
467+
### OpenTelemetry with Serilog
468+
469+
If you are logging with Serilog and want to use that framework's native API to push log messages to traces, you need to:
470+
471+
* Add the Serilog OpenTelemetry sink library
472+
* Instruct the Serilog logger object to write to the OpenTelemetry sink
473+
474+
Note: See the Serilog [OpenTelemetry sink](https://github.com/serilog/serilog-sinks-opentelemetry) documentation for the most up to date information.
475+
476+
```bash
477+
dotnet add package Serilog.Sinks.OpenTelemetry
478+
```
479+
480+
```csharp {2}
481+
Log.Logger = new LoggerConfiguration()
482+
.WriteTo.OpenTelemetry()
483+
.CreateLogger();
484+
```
485+

0 commit comments

Comments
 (0)