diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/CHANGELOG.md b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/CHANGELOG.md index 7273a4ffc43..1107c98e16f 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/CHANGELOG.md +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs/CHANGELOG.md @@ -16,6 +16,14 @@ and `Attributes` are equivalent). ([#4334](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4334)) +* Fixed issue where the + [observed time](https://github.com/open-telemetry/opentelemetry-proto/blob/395c8422fe90080314c7d9b4114d701a0c049e1f/opentelemetry/proto/logs/v1/logs.proto#L138) + field of the OTLP log record was not set. It is now correctly set to equal + the + [time](https://github.com/open-telemetry/opentelemetry-proto/blob/395c8422fe90080314c7d9b4114d701a0c049e1f/opentelemetry/proto/logs/v1/logs.proto#L121) + field. + ([#4444](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4444)) + ## 1.5.0-alpha.2 Released 2023-Mar-31 diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs index 8792c8d30ed..31e28adca19 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/LogRecordExtensions.cs @@ -66,9 +66,11 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO try { + var timestamp = (ulong)logRecord.Timestamp.ToUnixTimeNanoseconds(); otlpLogRecord = new OtlpLogs.LogRecord { - TimeUnixNano = (ulong)logRecord.Timestamp.ToUnixTimeNanoseconds(), + TimeUnixNano = timestamp, + ObservedTimeUnixNano = timestamp, SeverityNumber = GetSeverityNumber(logRecord.LogLevel), SeverityText = LogLevels[(int)logRecord.LogLevel], }; diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index 48128c19f75..0ce340fc9e4 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -277,6 +277,27 @@ public void CheckToOtlpLogRecordEventId() Assert.Contains("MyEvent10", otlpLogRecordAttributes); } + [Fact] + public void CheckToOtlpLogRecordTimestamps() + { + var logRecords = new List(); + using var loggerFactory = LoggerFactory.Create(builder => + { + builder.AddOpenTelemetry(options => + { + options.AddInMemoryExporter(logRecords); + }); + }); + + var logger = loggerFactory.CreateLogger("OtlpLogExporterTests"); + logger.LogInformation("Log message"); + var logRecord = logRecords[0]; + var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions); + + Assert.True(otlpLogRecord.TimeUnixNano > 0); + Assert.True(otlpLogRecord.ObservedTimeUnixNano > 0); + } + [Fact] public void CheckToOtlpLogRecordTraceIdSpanIdFlagWithNoActivity() {