Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
scr-oath opened this issue Feb 20, 2025 · 2 comments
Labels
bug Something isn't working needs author feedback Waiting for additional feedback from the author needs triage New issue that requires triage stale

Comments

@scr-oath
Copy link

scr-oath commented Feb 20, 2025

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

@scr-oath scr-oath added bug Something isn't working needs triage New issue that requires triage labels Feb 20, 2025
@laurit
Copy link
Contributor

laurit commented Feb 20, 2025

var otelSdk = OpenTelemetrySdk.builder().build();
OpenTelemetryAppender.install(otelSdk);

means that you create a new otel sdk without any configuration (no logging, spans or metrics is configured). If you wish to use the sdk from the agent you should be using GlobalOpenTelemetry.get().
Also note that since agent already automatically adds the otel appender manually adding it again may cause duplicate logs, you may need to disable the agent instrumentation. If you wish to configure agent instrumentation use the flags from https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/log4j/log4j-appender-2.17/javaagent/README.md log4j.xml has no effect for the agent instrumetation.

@laurit laurit added the needs author feedback Waiting for additional feedback from the author label Feb 20, 2025
Copy link
Contributor

This has been automatically marked as stale because it has been marked as needing author feedback and has not had any activity for 7 days. It will be closed automatically if there is no response from the author within 7 additional days from this comment.

@github-actions github-actions bot added the stale label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs author feedback Waiting for additional feedback from the author needs triage New issue that requires triage stale
Projects
None yet
Development

No branches or pull requests

2 participants