Skip to content

Commit a2bb510

Browse files
Adding EventName to LogRecord (#6306)
Co-authored-by: Alan West <[email protected]>
1 parent 4f51654 commit a2bb510

File tree

10 files changed

+72
-13
lines changed

10 files changed

+72
-13
lines changed

src/OpenTelemetry.Api/.publicApi/Experimental/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ OpenTelemetry.Logs.LogRecordData.TraceFlags.get -> System.Diagnostics.ActivityTr
4141
OpenTelemetry.Logs.LogRecordData.TraceFlags.set -> void
4242
OpenTelemetry.Logs.LogRecordData.TraceId.get -> System.Diagnostics.ActivityTraceId
4343
OpenTelemetry.Logs.LogRecordData.TraceId.set -> void
44+
OpenTelemetry.Logs.LogRecordData.EventName.get -> string?
45+
OpenTelemetry.Logs.LogRecordData.EventName.set -> void
4446
OpenTelemetry.Logs.LogRecordSeverity
4547
OpenTelemetry.Logs.LogRecordSeverity.Debug = 5 -> OpenTelemetry.Logs.LogRecordSeverity
4648
OpenTelemetry.Logs.LogRecordSeverity.Debug2 = 6 -> OpenTelemetry.Logs.LogRecordSeverity

src/OpenTelemetry.Api/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Notes](../../RELEASENOTES.md).
1010
linking spans and associating optional attributes for advanced trace relationships.
1111
([#6305](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6305))
1212

13+
* Experimental (only in pre-release versions): Added the `EventName` property
14+
to `LogRecordData`
15+
([#6306](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6306))
16+
1317
## 1.12.0
1418

1519
Released 2025-Apr-29

src/OpenTelemetry.Api/Logs/LogRecordData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public DateTime Timestamp
125125
/// </summary>
126126
public string? Body { get; set; } = null;
127127

128+
/// <summary>
129+
/// Gets or sets the name of the event associated with the log.
130+
/// </summary>
131+
public string? EventName { get; set; } = null;
132+
128133
internal static void SetActivityContext(ref LogRecordData data, Activity? activity)
129134
{
130135
if (activity != null)

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ Notes](../../RELEASENOTES.md).
1212
write position, resulting in gRPC protocol errors.
1313
([#6280](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6280))
1414

15+
* If `EventName` is specified either through `ILogger` or the experimental
16+
log bridge API, it is exported as `EventName` by default instead of
17+
`logrecord.event.name` which was previously behind the
18+
`OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES` feature flag.
19+
Note that exporting `logrecord.event.id` is still behind that same feature
20+
flag. ([#6306](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6306))
21+
1522
## 1.12.0
1623

1724
Released 2025-Apr-29

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExperimentalOptions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ internal sealed class ExperimentalOptions
99
{
1010
public const string LogRecordEventIdAttribute = "logrecord.event.id";
1111

12-
public const string LogRecordEventNameAttribute = "logrecord.event.name";
13-
1412
public const string EmitLogEventEnvVar = "OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES";
1513

1614
public const string OtlpRetryEnvVar = "OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY";

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogFieldNumberConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal static class ProtobufOtlpLogFieldNumberConstants
3636
internal const int LogRecord_Flags = 8;
3737
internal const int LogRecord_Trace_Id = 9;
3838
internal const int LogRecord_Span_Id = 10;
39+
internal const int LogRecord_Event_Name = 12;
3940

4041
// SeverityNumber
4142
internal const int Severity_Number_Unspecified = 0;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ internal static int WriteLogRecord(byte[] buffer, int writePosition, SdkLimitOpt
192192
{
193193
AddLogAttribute(state, ExperimentalOptions.LogRecordEventIdAttribute, logRecord.EventId.Id);
194194
}
195-
196-
if (!string.IsNullOrEmpty(logRecord.EventId.Name))
197-
{
198-
AddLogAttribute(state, ExperimentalOptions.LogRecordEventNameAttribute, logRecord.EventId.Name!);
199-
}
200195
}
201196

202197
if (logRecord.Exception != null)
@@ -254,6 +249,11 @@ internal static int WriteLogRecord(byte[] buffer, int writePosition, SdkLimitOpt
254249
otlpTagWriterState.WritePosition = ProtobufSerializer.WriteFixed32WithTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpLogFieldNumberConstants.LogRecord_Flags, (uint)logRecord.TraceFlags);
255250
}
256251

252+
if (logRecord.EventId.Name != null)
253+
{
254+
otlpTagWriterState.WritePosition = ProtobufSerializer.WriteStringWithTag(otlpTagWriterState.Buffer, otlpTagWriterState.WritePosition, ProtobufOtlpLogFieldNumberConstants.LogRecord_Event_Name, logRecord.EventId.Name!);
255+
}
256+
257257
logRecord.ForEachScope(ProcessScope, state);
258258

259259
if (otlpTagWriterState.DroppedTagCount > 0)

src/OpenTelemetry/Logs/LoggerSdk.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using Microsoft.Extensions.Logging;
45
using OpenTelemetry.Internal;
56

67
namespace OpenTelemetry.Logs;
@@ -35,6 +36,7 @@ public override void EmitLog(in LogRecordData data, in LogRecordAttributeList at
3536

3637
logRecord.Data = data;
3738
logRecord.ILoggerData = default;
39+
logRecord.ILoggerData.EventId = new EventId(default, data.EventName);
3840

3941
logRecord.Logger = this;
4042

src/Shared/Proto/opentelemetry/proto/logs/v1/logs.proto

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,5 @@ message LogRecord {
221221
// as an event.
222222
//
223223
// [Optional].
224-
//
225-
// Status: [Development]
226224
string event_name = 12;
227-
}
225+
}

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,18 @@ public void CheckToOtlpLogRecordEventId(string? emitLogEventAttributes)
342342
Assert.NotNull(otlpLogRecord);
343343
Assert.Equal("Hello from tomato 2.99.", otlpLogRecord.Body.StringValue);
344344

345+
Assert.Equal("MyEvent10", otlpLogRecord.EventName);
346+
345347
// Event
346348
otlpLogRecordAttributes = otlpLogRecord.Attributes.ToString();
347349
if (emitLogEventAttributes == "true")
348350
{
349351
Assert.Contains(ExperimentalOptions.LogRecordEventIdAttribute, otlpLogRecordAttributes, StringComparison.Ordinal);
350352
Assert.Contains("10", otlpLogRecordAttributes, StringComparison.Ordinal);
351-
Assert.Contains(ExperimentalOptions.LogRecordEventNameAttribute, otlpLogRecordAttributes, StringComparison.Ordinal);
352-
Assert.Contains("MyEvent10", otlpLogRecordAttributes, StringComparison.Ordinal);
353353
}
354354
else
355355
{
356356
Assert.DoesNotContain(ExperimentalOptions.LogRecordEventIdAttribute, otlpLogRecordAttributes, StringComparison.Ordinal);
357-
Assert.DoesNotContain(ExperimentalOptions.LogRecordEventNameAttribute, otlpLogRecordAttributes, StringComparison.Ordinal);
358357
}
359358
}
360359

@@ -1478,6 +1477,49 @@ public void LogRecordLoggerNameIsExportedWhenUsingBridgeApi(string? loggerName,
14781477
Assert.Equal(expectedScopeName, request.ResourceLogs[0].ScopeLogs[0].Scope?.Name);
14791478
}
14801479

1480+
[Theory]
1481+
[InlineData(true)]
1482+
[InlineData(false)]
1483+
public void LogRecordEventNameIsExportedWhenUsingBridgeApi(bool emitEventName)
1484+
{
1485+
LogRecordAttributeList attributes = default;
1486+
attributes.Add("name", "tomato");
1487+
attributes.Add("price", 2.99);
1488+
attributes.Add("{OriginalFormat}", "Hello from {name} {price}.");
1489+
1490+
var logRecords = new List<LogRecord>();
1491+
1492+
using (var loggerProvider = Sdk.CreateLoggerProviderBuilder()
1493+
.AddInMemoryExporter(logRecords)
1494+
.Build())
1495+
{
1496+
var logger = loggerProvider.GetLogger();
1497+
1498+
logger.EmitLog(new LogRecordData
1499+
{
1500+
Body = "test body",
1501+
EventName = emitEventName ? "test event" : null,
1502+
});
1503+
}
1504+
1505+
Assert.Single(logRecords);
1506+
var logRecord = logRecords[0];
1507+
1508+
OtlpLogs.LogRecord? otlpLogRecord = ToOtlpLogs(DefaultSdkLimitOptions, new ExperimentalOptions(), logRecord);
1509+
1510+
Assert.NotNull(otlpLogRecord);
1511+
Assert.Equal("test body", otlpLogRecord.Body.StringValue);
1512+
1513+
if (!emitEventName)
1514+
{
1515+
Assert.Empty(otlpLogRecord.EventName);
1516+
}
1517+
else
1518+
{
1519+
Assert.Equal("test event", otlpLogRecord.EventName);
1520+
}
1521+
}
1522+
14811523
[Fact]
14821524
public void LogSerialization_ExpandsBufferForLogsAndSerializes()
14831525
{

0 commit comments

Comments
 (0)