Skip to content

Using the javaagent does not configure BatchLogRecordProcessor and therefore MapMessage attributes aren't written #13356

@scr-oath

Description

@scr-oath

Describe the bug

The programmatic auto-configure mechanism initializes the BatchLogRecordProcessor, which honors the MapMessage attributes, whereas, when initializing with the javaagent, it seems the NoopLoggingCustomizer is used and, therefore, attributes are never written.

package my.sample;

import io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.message.StringMapMessage;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws IOException {
        var props = new Properties();
        try (var in = Files.newBufferedReader(Paths.get("src/main/resources/otel.conf"))) {
            props.load(in);
            System.getProperties().putAll(props);
        }
        var otelSdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
        OpenTelemetryAppender.install(otelSdk);
        var logger = LogManager.getLogger(Main.class);
        logger.info("Hello World");
        logger.info(new StringMapMessage()
                .with("message", "StringMapMessage")
                .with("foo", "bar"));
    }
}
Image

vs

package my.sample;

import io.opentelemetry.instrumentation.log4j.appender.v2_17.OpenTelemetryAppender;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.message.StringMapMessage;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

public class Main2 {
    public static void main(String[] args) throws IOException {
        var otelSdk = OpenTelemetrySdk.builder().build();
        OpenTelemetryAppender.install(otelSdk);
        var logger = LogManager.getLogger(Main2.class);
        logger.info("Hello World 2");
        logger.info(new StringMapMessage()
                .with("message", "StringMapMessage 2")
                .with("foo", "bar"));
    }
}

Run with -javaagent:/Users/scr/IdeaProjects/uup/online-activation-api/app/build/install/online-activation/agent-libs/opentelemetry-javaagent-2.12.0.jar -Dotel.javaagent.configuration-file=src/main/resources/otel.conf

Image
dependencies {
    implementation platform('io.opentelemetry:opentelemetry-bom:1.47.0')
    implementation 'io.opentelemetry:opentelemetry-sdk-extension-autoconfigure'
    implementation 'io.opentelemetry:opentelemetry-sdk'
    implementation 'io.opentelemetry:opentelemetry-exporter-otlp'

    implementation platform('org.apache.logging.log4j:log4j-bom:2.24.3')
    implementation 'org.apache.logging.log4j:log4j-api'
    implementation 'org.apache.logging.log4j:log4j-core'
    implementation 'io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17:2.13.1-alpha'

    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="log4j2: %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <OpenTelemetry name="OpenTelemetryAppender" captureMapMessageAttributes="true" captureExperimentalAttributes="false"/>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="OpenTelemetryAppender" />
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

Steps to reproduce

Run with javaagent vs AutoConfiguredOpenTelemetrySdk and a local otel collector and see that the logs only include attributes for the programmatic approach.

Expected behavior

javaagent should also configure a LogRecordProcessor (or be told which processor to use) so that attributes are sent to the collector.

Actual behavior

attributes are omitted when using the javaagent approach

Javaagent or library instrumentation version

2.12.0

Environment

JDK:
OpenJDK Runtime Environment Temurin-21.0.6+7 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.6+7 (build 21.0.6+7-LTS, mixed mode, sharing)
OS:
Darwin arm64
15.3 (24D60)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds author feedbackWaiting for additional feedback from the authorneeds triageNew issue that requires triagestale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions