Skip to content

Allow emitting events through common logging APIs #14632

@philsttr

Description

@philsttr

Is your feature request related to a problem? Please describe.

An OpenTelemetry Event is a LogRecord with an EventName field.

Currently, to emit an Event, one must use the OpenTelemetry SDK log API like this:

  1. Obtain an OpenTelemetry SDK LoggerProvider
  2. Obtain an OpenTelemetry SDK Logger via LoggerProvider.get(...)
  3. Obtain a OpenTelemetry SDK LogRecordBuilder via Logger.logRecordBuilder()
  4. Set the event name via logRecordBuilder.setEventName(...)
  5. Set other properties/attributes on the logRecordBuilder
  6. Call logRecordBuilder.emit().

While this is ok in general, it requires direct usage of the OpenTelemetry SDK log APIs, as opposed to using a more common log API (like slf4j). It also bypasses features and the event population performed when using a logging API that is bridged to OpenTelemetry (like logback/log4j). Therefore, you would need to duplicate the event population that those bridges perform (in Step 5).

Describe the solution you'd like

I would like

  1. to be able to emit OpenTelemetry Events using a common log API (like slf4j) without using the OpenTelemetry SDK log API directly
  2. to be able to specify a value for the EventName field when using a more common log API
  3. to take advantage of the features and the event population performed when using a logging API that is bridged to OpenTelemetry (like logback/log4j) to populate the other OpenTelemetry Event fields

The event.name semantic attribute was recently undeprecated in open-telemetry/semantic-conventions#2715 for this purpose.

Users could opt in to having the attribute named event.name be used for the log event name.

For example, when using opentelemetry-logback-appender-1.0...

First, tell the OpenTelemetry logback appender to opt-in to using the event.name attribute as the event name

otel.instrumentation.logback-appender.experimental.capture-event-name=true
otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes=true

or

<appender name="OpenTelemetry" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
  <captureEventName>true</captureEventName>
  <captureKeyValuePairAttributes>true</captureKeyValuePairAttributes>
</appender>

Then, to log an event ...

private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MyEventLogger.class);

LOGGER.atInfo()
    // specify the event name using the event.name key enabled above
    .addKeyValue("event.name", "MyEvent")
    // add other event-specific attributes
    //.addKeyValue(..., ...)
    .log("body...");

Internally, when capture-event-name is configured, the LoggingEventMapper would:

  1. call logRecordBuilder.setEventName(...) with the value of the event.name attribute (in this example, MyEvent).
  2. not include the event.name in attributes

This could be implemented for any logging API that supports capturing attributes

Describe alternatives you've considered

Use the OpenTelemetry SDK Log APIs directly.
Explicitly populate everything that a common log API bridge would populate, plus the EventName field.

Additional context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions