Skip to content

Commit

Permalink
OTLPLogExporter bug fix to handle null EventName (open-telemetry#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Feb 7, 2022
1 parent 563236b commit 7ce2e4c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Changelog

* Changed `OtlpLogExporter` to convert `ILogger` structured log inputs to
`Attributes` in OpenTelemetry (only active when `ParseStateValues` is `true`
on `OpenTelemetryLoggerOptions`)

## Unreleased

* LogExporter bug fix to handle null EventName.
([#2870](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2871))

## 1.2.0-rc2

Released 2022-Feb-02
Expand All @@ -14,6 +13,10 @@ Released 2022-Feb-02
.NET Core 3.x for gRPC-based exporting.
([#2691](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2691))

* Changed `OtlpLogExporter` to convert `ILogger` structured log inputs to
`Attributes` in OpenTelemetry (only active when `ParseStateValues` is `true`
on `OpenTelemetryLoggerOptions`)

## 1.2.0-rc1

Released 2021-Nov-29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
}
}

if (logRecord.EventId != default)
if (logRecord.EventId.Id != default)
{
otlpLogRecord.Attributes.AddIntAttribute(nameof(logRecord.EventId.Id), logRecord.EventId.Id);
}

if (!string.IsNullOrEmpty(logRecord.EventId.Name))
{
otlpLogRecord.Attributes.AddStringAttribute(nameof(logRecord.EventId.Name), logRecord.EventId.Name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// <copyright file="OtlpLogExporterTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
public class OtlpLogExporterTests : Http2UnencryptedSupportTests
{
[Fact]
public void ToOtlpLogRecordTest()
{
// Just a basic test to demonstrate an
// approach useful for testing.
// This needs to be expanded to
// actually test the conversion.
List<LogRecord> logRecords = new List<LogRecord>();

using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeFormattedMessage = true;
options.ParseStateValues = true;
options.AddInMemoryExporter(logRecords);
});
});

// TODO:
// Validate attributes, severity, traceid,spanid etc.

var logger = loggerFactory.CreateLogger("log-category");
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
var logRecord = logRecords[0];
var otlpLogRecord = logRecord.ToOtlpLog();
Assert.NotNull(otlpLogRecord);
Assert.Equal("Hello from tomato 2.99.", otlpLogRecord.Body.StringValue);
logRecords.Clear();

logger.LogInformation(new EventId(10, null), "Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
logRecord = logRecords[0];
otlpLogRecord = logRecord.ToOtlpLog();
Assert.NotNull(otlpLogRecord);
Assert.Equal("Hello from tomato 2.99.", otlpLogRecord.Body.StringValue);
logRecords.Clear();

logger.LogInformation(new EventId(10, "MyEvent10"), "Hello from {name} {price}.", "tomato", 2.99);
Assert.Single(logRecords);
logRecord = logRecords[0];
otlpLogRecord = logRecord.ToOtlpLog();
Assert.NotNull(otlpLogRecord);
Assert.Equal("Hello from tomato 2.99.", otlpLogRecord.Body.StringValue);
}
}
}

0 comments on commit 7ce2e4c

Please sign in to comment.