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

OTLP LogExporter to enable ParseStateValues by default #3186

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static OpenTelemetryLoggerOptions AddOtlpExporter(OpenTelemetryLoggerOpt
{
configure?.Invoke(exporterOptions);
var otlpExporter = new OtlpLogExporter(exporterOptions);

loggerOptions.ParseStateValues = true;
Copy link
Member

Choose a reason for hiding this comment

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

Might be useful to have a comment here explaining why it is being done?

if (exporterOptions.ExportProcessorType == ExportProcessorType.Simple)
{
return loggerOptions.AddProcessor(new SimpleLogRecordExportProcessor(otlpExporter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs\OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;
Expand All @@ -30,6 +32,87 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
public class OtlpLogExporterTests : Http2UnencryptedSupportTests
{
[Fact]
public void AddOtlpLogExporterOptionstest()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var loggerOptions = new OpenTelemetryLoggerOptions();
Assert.False(loggerOptions.ParseStateValues);
loggerOptions.AddOtlpExporter();
Assert.True(loggerOptions.ParseStateValues);
}

[Fact]
public void AddOtlpLogExporterSetsParseStateValueToTrue()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
options.AddOtlpExporter();
});
});

var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
var logRecord = logRecords[0];
Assert.Null(logRecord.State);
Assert.NotNull(logRecord.StateValues);
}

[Fact]
public void AddOtlpLogExporterParseStateValueCanBeTurnedOff()
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
options.AddOtlpExporter();
options.ParseStateValues = false;
});
});

var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
var logRecord = logRecords[0];
Assert.NotNull(logRecord.State);
Assert.Null(logRecord.StateValues);
}

[Fact]
public void AddOtlpLogExporterParseStateValueCanBeTurnedOffHosting()
{
var logRecords = new List<LogRecord>();

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureLogging(logging => logging.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
options.AddOtlpExporter();
}));

hostBuilder.ConfigureServices(services =>
services.Configure<OpenTelemetryLoggerOptions>(options => options.ParseStateValues = false));

var host = hostBuilder.Build();
var loggerFactory = host.Services.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("OtlpLogExporterTests");
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
var logRecord = logRecords[0];
Assert.NotNull(logRecord.State);
Assert.Null(logRecord.StateValues);
}

[Fact]
public void OtlpLogRecordTestWhenStateValuesArePopulated()
{
Expand Down