open-telemetry / opentelemetry-java-instrumentation

OpenTelemetry auto-instrumentation and instrumentation libraries for Java
https://opentelemetry.io
Apache License 2.0
1.96k stars 860 forks source link

LogRecordProcessor extensions #8921

Open daoweili opened 1 year ago

daoweili commented 1 year ago

What version are you using? 1.27

Environment Compiler: openjdk 17 OS: ubuntu 16.0.4

Additional context

LogAutoConfigurationCustomizerProvider:

@AutoService(AutoConfigurationCustomizerProvider.class) public class LogAutoConfigurationCustomizerProvider implements AutoConfigurationCustomizerProvider {

@Override
public void customize(AutoConfigurationCustomizer autoConfiguration) {
    autoConfiguration.addLoggerProviderCustomizer(this::configureSdkLoggerProvider)
            .addPropertiesSupplier(this::getDefaultProperties);
}

private SdkLoggerProviderBuilder configureSdkLoggerProvider(
        SdkLoggerProviderBuilder loggerProviderBuilder, ConfigProperties config) {
    return loggerProviderBuilder
            .addLogRecordProcessor(new ErrorLogBatchProcessor());
}

private Map<String, String> getDefaultProperties() {
    Map<String, String> properties = new HashMap<>();
    properties.put("otel.logs.test", "test");
    return properties;
}

}

ErrorLogBatchProcessor: public class ErrorLogBatchProcessor implements LogRecordProcessor {

private final LogRecordProcessor logRecordProcessor;

public ErrorLogBatchProcessor() {
    logRecordProcessor = BatchLogRecordProcessor.builder(OtlpHttpLogRecordExporter.getDefault()).build();
}

@Override
public void onEmit(Context context, ReadWriteLogRecord readWriteLogRecord) {
    if (readWriteLogRecord.toLogRecordData().getSeverity().getSeverityNumber() >= Severity.ERROR.getSeverityNumber()) {
        logRecordProcessor.onEmit(context, readWriteLogRecord);
    }
}

@Override
public CompletableResultCode shutdown() {
    return logRecordProcessor.shutdown();
}

@Override
public CompletableResultCode forceFlush() {
    return logRecordProcessor.forceFlush();
}

@Override
public void close() {
    logRecordProcessor.close();
}

}

image

I only want to collect error logs, But the above code doesn't work properly. Do you have any better suggestions?

zeitlinger commented 1 year ago

I think you can register a logback or log4j appender and set the log level on the appender - no extension needed.

See https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/logback/logback-appender-1.0/library/README.md

trask commented 1 year ago

@daoweili also check out this slack discussion which may be helpful in your case https://cloud-native.slack.com/archives/CJFCJHG4Q/p1689011600283179, in particular:

did you enable the logs exporter, e.g. otel.logs.exporter=otlp?

logs exporter will be enabled by default in the upcoming 2.x release in a few months, but for now you need to opt-in to it

https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/6f09cf79b1a18850e88897a54c1e39ef167b803a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java#L31-L33

daoweili commented 1 year ago

@daoweili also check out this slack discussion which may be helpful in your case https://cloud-native.slack.com/archives/CJFCJHG4Q/p1689011600283179, in particular:

did you enable the logs exporter, e.g. otel.logs.exporter=otlp? logs exporter will be enabled by default in the upcoming 2.x release in a few months, but for now you need to opt-in to it https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/6f09cf79b1a18850e88897a54c1e39ef167b803a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OpenTelemetryInstaller.java#L31-L33

yes, I enabled otel.logs.exporter=otlp

daoweili commented 1 year ago

I think you can register a logback or log4j appender and set the log level on the appender - no extension needed.

See https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/logback/logback-appender-1.0/library/README.md

Hi, I want to use the extensions for docker images, not only in a few projects.

daoweili commented 1 year ago

https://cloud-native.slack.com/archives/CJFCJHG4Q/p1689011600283179 hi, I don't have this slack account

mateuszrzeszutek commented 1 year ago

I only want to collect error logs, But the above code doesn't work properly. Do you have any better suggestions?

How does it not work properly? Can you post expected and actual output? Or, can you provide a repro scenario?

daoweili commented 1 year ago

I only want to collect error logs, But the above code doesn't work properly. Do you have any better suggestions?

How does it not work properly? Can you post expected and actual output? Or, can you provide a repro scenario?

I want to collect only error logs from the SDK by extending it. But the code didn't take effect and execute properly. It still collects all levels of logs. I think the extensions code is not excute.

trask commented 1 year ago

hi @daoweili, I think we would need a repro in order to understand your issue and and help you further