Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log correlation/redaction doc improvements #5262

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,23 @@ instances if they are created by you.
category name. Refer to the [.NET official
document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category)
to learn more.

## Log Correlation

In OpenTelemetry, logs are automatically correlated to traces. Check the [Log
Correlation](./correlation/README.md) tutorial to learn more.

## Log Enrichment

TBD

## Log Filtering

Check the [Customizing OpenTelemetry .NET SDK for
Logs](./customizing-the-sdk/README.md#log-filtering) document to learn more.

## Log Redaction

Logs might contain sensitive information such as passwords and credit card
numbers, proper redaction is required to prevent privacy and security incidents.
Check the [Log Redaction](./redaction/README.md) tutorial to learn more.
10 changes: 10 additions & 0 deletions docs/logs/correlation/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;

internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
}
38 changes: 19 additions & 19 deletions docs/logs/correlation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;

namespace Correlation;

public class Program
{
private static readonly ActivitySource MyActivitySource = new(
"MyCompany.MyProduct.MyLibrary");
private static readonly ActivitySource MyActivitySource = new("MyCompany.MyProduct.MyLibrary");

public static void Main()
{
// Setup Logging
using var loggerFactory = LoggerFactory.Create(builder =>
var tracerProvider = Sdk.CreateTracerProviderBuilder()
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
.AddSource("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.Build();

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
builder.AddOpenTelemetry(logging =>
{
options.AddConsoleExporter();
logging.AddConsoleExporter();
});
});

var logger = loggerFactory.CreateLogger<Program>();

// Setup Traces
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.Build();

// Emit activity
using (var activity = MyActivitySource.StartActivity("SayHello"))
{
activity?.SetTag("foo", 1);

// Emit logs within the context of activity
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
// Write a log within the context of an activity
logger.FoodPriceChanged("artichoke", 9.99);
}

// Dispose logger factory before the application ends.
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();

// Dispose tracer provider before the application ends.
// This will flush the remaining spans and shutdown the tracing pipeline.
tracerProvider.Dispose();
}
}
43 changes: 23 additions & 20 deletions docs/logs/correlation/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Logs correlation
# Log Correlation

The getting started docs for [logs](../getting-started-console/README.md) and
[traces](../../trace/getting-started-console/README.md) showed how to emit logs
Expand Down Expand Up @@ -27,29 +27,32 @@ of an active `Activity`. Running the application will show the following output
on the console:

```text
LogRecord.Timestamp: 2022-05-18T18:51:16.4348626Z
LogRecord.TraceId: d7aca5b2422ed8d15f56b6a93be4537d
LogRecord.SpanId: c90ac2ad41ab4d46
LogRecord.Timestamp: 2024-01-26T17:55:39.2273475Z
LogRecord.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5
LogRecord.SpanId: bd44308753200c58
LogRecord.TraceFlags: Recorded
LogRecord.CategoryName: Correlation.Program
LogRecord.LogLevel: Information
LogRecord.State: Hello from tomato 2.99.
LogRecord.CategoryName: Program
LogRecord.Severity: Info
LogRecord.SeverityText: Information
LogRecord.Body: Food `{name}` price changed to `{price}`.
LogRecord.Attributes (Key:Value):
name: artichoke
price: 9.99
OriginalFormat (a.k.a Body): Food `{name}` price changed to `{price}`.
LogRecord.EventId: 344095174
LogRecord.EventName: FoodPriceChanged

Resource associated with LogRecord:
service.name: unknown_service:correlation
...

Activity.TraceId: d7aca5b2422ed8d15f56b6a93be4537d
Activity.SpanId: c90ac2ad41ab4d46
Activity.TraceFlags: Recorded
Activity.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5
Activity.SpanId: bd44308753200c58
Activity.TraceFlags: Recorded
Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary
Activity.DisplayName: SayHello
Activity.Kind: Internal
Activity.StartTime: 2022-05-18T18:51:16.3427411Z
Activity.Duration: 00:00:00.2248932
Activity.Tags:
foo: 1
Resource associated with Activity:
service.name: unknown_service:correlation
Activity.DisplayName: SayHello
Activity.Kind: Internal
Activity.StartTime: 2024-01-26T17:55:39.2223849Z
Activity.Duration: 00:00:00.0361682
...
```

As you can see, the `LogRecord` automatically had the `TraceId`, `SpanId` fields
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/getting-started-aspnetcore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

app.Run();

public static partial class ApplicationLogs
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Starting the app...")]
public static partial void StartingApp(this ILogger logger);
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/getting-started-aspnetcore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ has been used across the example, which delivers high performance, structured
logging, and type-checked parameters:

```csharp
public static partial class ApplicationLogs
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Starting the app...")]
public static partial void StartingApp(this ILogger logger);
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();

public static partial class ApplicationLogs
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/getting-started-console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ has been used across the example, which delivers high performance, structured
logging, and type-checked parameters:

```csharp
public static partial class ApplicationLogs
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/redaction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();

public static partial class ApplicationLogs
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
Expand Down
Loading