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

Doc updates to include ForceFlush() log practice #6012

Closed
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ var sdk = OpenTelemetrySdk.Create(builder => builder
.WithMetrics(metrics => /* Metrics configuration goes here */)
.WithTracing(tracing => /* Tracing configuration goes here */));

// Optionally ForceFlush() telemetry objects in memory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. show shutdown here, no need to show flush.
  2. Few lines above in this doc, it says Dispose() will gracefully shutdown and flush, so we need to add some wording here to indicate why an explicit shutdown() is needed and what scenarios requires using shutdown() as opposed to just relying on dispose().

sdk.LoggerProvider.ForceFlush();
sdk.MeterProvider.ForceFlush();
sdk.TracerProvider.ForceFlush();

// During application shutdown
sdk.Dispose();
```
Expand Down
17 changes: 6 additions & 11 deletions docs/logs/complex-objects/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
{
logging.AddConsoleExporter();
});
});
var sdk = OpenTelemetrySdk.Create(builder => builder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think entire examples should be moved to newer pattern. It maybe a good change, but definitely outside this PR.

.WithLogging(logging => logging.AddConsoleExporter()));

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

var foodRecallNotice = new FoodRecallNotice
{
Expand All @@ -25,9 +21,8 @@

logger.FoodRecallNotice(foodRecallNotice);

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

internal static partial class LoggerExtensions
{
Expand Down
28 changes: 9 additions & 19 deletions docs/logs/correlation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,23 @@ public class Program

public static void Main()
{
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.Build();

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
var sdk = OpenTelemetrySdk.Create(builder => builder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep existing code as-is. If there is a need to move them to new format, they can be done in separate PR.

.WithLogging(logging => logging.AddConsoleExporter())
.WithTracing(tracing =>
{
logging.AddConsoleExporter();
});
});
tracing.AddSource("MyCompany.MyProduct.MyLibrary");
tracing.AddConsoleExporter();
}));

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

using (var activity = MyActivitySource.StartActivity("SayHello"))
{
// 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();
// Dispose SDK before the application ends.
sdk.Dispose();
}
}
31 changes: 15 additions & 16 deletions docs/logs/customizing-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
{
logging.IncludeScopes = true;
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
});
});

var logger = loggerFactory.CreateLogger<Program>();
var sdk = OpenTelemetrySdk.Create(builder => builder
.WithLogging(
logging =>
{
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
},
options => options.IncludeScopes = true));

var logger = sdk.GetLoggerFactory().CreateLogger<Program>();

logger.FoodPriceChanged("artichoke", 9.99);

Expand All @@ -36,9 +36,8 @@
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");

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

internal static partial class LoggerExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion docs/logs/extending-the-sdk/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

internal static class LoggerExtensions
{
public static OpenTelemetryLoggerOptions AddMyExporter(this OpenTelemetryLoggerOptions options)
public static LoggerProviderBuilder AddMyExporter(this LoggerProviderBuilder options)
{
if (options == null)
{
Expand Down
31 changes: 20 additions & 11 deletions docs/logs/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

namespace ExtendingTheSdk;

public class Program
{
public static void Main()
{
using var loggerFactory = LoggerFactory.Create(builder =>
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddProcessor(new MyProcessor("ProcessorA"))
.AddProcessor(new MyProcessor("ProcessorB"))
.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX")))
.AddMyExporter();
}));

var logger = loggerFactory.CreateLogger<Program>();
var sdk = OpenTelemetrySdk.Create(builder => builder
.WithLogging(
logging =>
{
logging.AddProcessor(new MyProcessor("ProcessorA"));
logging.AddProcessor(new MyProcessor("ProcessorB"));
logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX")));
logging.AddMyExporter();
},
options => options.IncludeScopes = true));

var logger = sdk.GetLoggerFactory().CreateLogger<Program>();

// unstructured log
logger.LogInformation("Hello, World!");
Expand Down Expand Up @@ -53,6 +55,13 @@ public static void Main()

// message will be redacted by MyRedactionProcessor
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>");

// Attempt to flush the remaining logs when using an exporter that may require it, such as the custom one defined here.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is not exactly what you wanted @reyang but hopefully it's closer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely moving closer, what do you think regarding #6012 (review)? I want to gather ideas from folks here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not super familiar with github PRs and I didn't have a reply option there - let me try to respond there as well.

// Ignoring success or failure for flush after 5 seconds, but a richer application should choose to handle this in some manner.
sdk.LoggerProvider.ForceFlush(5000);

// Dispose SDK before the application ends.
sdk.Dispose();
}

internal struct Food
Expand Down
17 changes: 6 additions & 11 deletions docs/logs/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

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

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

logger.FoodPriceChanged("artichoke", 9.99);

Expand All @@ -23,9 +19,8 @@
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");

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

internal static partial class LoggerExtensions
{
Expand Down
16 changes: 7 additions & 9 deletions docs/logs/redaction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
// SPDX-License-Identifier: Apache-2.0

using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(logging =>
var sdk = OpenTelemetrySdk.Create(builder => builder
.WithLogging(logging =>
{
logging.AddProcessor(new MyRedactionProcessor());
logging.AddConsoleExporter();
});
});
}));

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

// Message will be redacted by MyRedactionProcessor
logger.FoodPriceChanged("<secret>", 9.99);

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

internal static partial class LoggerExtensions
{
Expand Down
Loading