-
Notifications
You must be signed in to change notification settings - Fork 126
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
Example of Log Appenders when using OpenTelemetry JavaAgent #503
base: main
Are you sure you want to change the base?
Changes from 6 commits
cbe87ab
8d834aa
55a3557
0997ec5
8daa9fe
da88e35
cfe4157
ff05f0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# OpenTelemetry Log Appender built-in OpenTelemetry Java Agent Example | ||
|
||
This example demonstrates an application configured to use the OpenTelemetry Log | ||
Appenders built-in OpenTelemetry Java Agent to bridge logs into the OpenTelemetry Log SDK, and export | ||
via [OTLP](https://opentelemetry.io/docs/reference/specification/protocol/otlp/). | ||
|
||
Details about the example: | ||
|
||
* The OpenTelemetry Log SDK is configured to export data to | ||
the [collector](https://opentelemetry.io/docs/collector/), which prints the | ||
logs to the console. | ||
* The application is configured with a variety of log solutions: | ||
* Log4j API [configured](./src/main/resources/log4j2.xml) to print logs to the | ||
console and | ||
the [OpenTelemetry Log4J Appender](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/log4j/log4j-appender-2.17/javaagent/README.md). | ||
* SLF4J API [configured with Logback](./src/main/resources/logback.xml) to | ||
print logs to the console and | ||
the [OpenTelemetry Logback Appender](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/logback/logback-appender-1.0/javaagent/README.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow what it means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is needed, I have clarified it further. |
||
* [JUL to SLF4J](./build.gradle.kts), which bridges JUL logs to the SLF4J API, and | ||
ultimately to Logback. | ||
* Demonstrates how trace context is propagated to logs when recorded within a | ||
span. | ||
|
||
## Prerequisites | ||
|
||
* Java 1.8 | ||
* Docker compose | ||
|
||
# How to run | ||
|
||
Run the collector via docker | ||
|
||
```shell | ||
docker-compose up | ||
``` | ||
|
||
In a separate shell, run the application | ||
|
||
```shell | ||
export \ | ||
OTEL_SERVICE_NAME=log4j-example \ | ||
OTEL_METRICS_EXPORTER=none \ | ||
OTEL_TRACES_EXPORTER=none \ | ||
OTEL_LOGS_EXPORTER=otlp | ||
|
||
../gradlew run | ||
``` | ||
|
||
Watch the collector logs to see exported log records | ||
|
||
NOTE: The OpenTelemetry Java Agent uses `http/protobuf` by default, optionally switch to use `grpc` protocol | ||
```shell | ||
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc | ||
``` |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||
plugins { | ||||
id("java") | ||||
id("application") | ||||
} | ||||
|
||||
description = "OpenTelemetry Log Appender Example using JavaAgent" | ||||
val moduleName by extra { "io.opentelemetry.examples.javaagent-log-appender" } | ||||
|
||||
java { | ||||
toolchain { | ||||
// logback 1.4.x+ requires java 11 | ||||
languageVersion.set(JavaLanguageVersion.of(11)) | ||||
} | ||||
} | ||||
|
||||
val agent = configurations.create("agent") | ||||
|
||||
dependencies { | ||||
// Slf4J / logback | ||||
implementation("org.slf4j:slf4j-api:2.0.16") | ||||
implementation("ch.qos.logback:logback-core:1.5.9") | ||||
implementation("ch.qos.logback:logback-classic:1.5.9") | ||||
|
||||
// JUL to SLF4J bridge | ||||
implementation("org.slf4j:jul-to-slf4j:2.0.16") | ||||
|
||||
// Log4j | ||||
implementation(platform("org.apache.logging.log4j:log4j-bom:2.24.1")) | ||||
implementation("org.apache.logging.log4j:log4j-api") | ||||
implementation("org.apache.logging.log4j:log4j-core") | ||||
|
||||
// OpenTelemetry core | ||||
implementation("io.opentelemetry:opentelemetry-api") | ||||
implementation("io.opentelemetry.semconv:opentelemetry-semconv") | ||||
|
||||
// OpenTelemetry Java Agent, this brings its own standalone log4j / logback appenders | ||||
agent("io.opentelemetry.javaagent:opentelemetry-javaagent:2.8.0") | ||||
} | ||||
|
||||
application { | ||||
mainClass = "io.opentelemetry.example.logappender.Application" | ||||
} | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
tasks.named<JavaExec>("run") { | ||||
doFirst { | ||||
jvmArgs("-javaagent:${agent.singleFile}") | ||||
// log4j-appender properties | ||||
jvmArgs( | ||||
"-Dotel.instrumentation.log4j-appender.experimental.capture-map-message-attributes=true", | ||||
"-Dotel.instrumentation.log4j-appender.experimental-log-attributes=true" | ||||
) | ||||
// logback-appender properties | ||||
jvmArgs( | ||||
"-Dotel.instrumentation.logback-appender.experimental-log-attributes=true", | ||||
"-Dotel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes=true" | ||||
) | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: '3' | ||
services: | ||
collector: | ||
image: otel/opentelemetry-collector-contrib:0.111.0 | ||
volumes: | ||
- ./otel-config.yaml:/otel-config.yaml | ||
command: ["--config=/otel-config.yaml"] | ||
ports: | ||
- "4317:4317" | ||
- "4318:4318" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 4 and 5 don't seem necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are needed as I opted to provide optional support for |
||
endpoint: collector:4317 | ||
http: | ||
endpoint: "0.0.0.0:4318" | ||
exporters: | ||
debug: | ||
verbosity: detailed | ||
nop: | ||
service: | ||
pipelines: | ||
metrics: | ||
receivers: [otlp] | ||
exporters: [debug] | ||
traces: | ||
receivers: [otlp] | ||
exporters: [debug] | ||
logs: | ||
receivers: [otlp] | ||
exporters: [debug] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.opentelemetry.example.logappender; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.logs.Severity; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import org.apache.logging.log4j.message.MapMessage; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.bridge.SLF4JBridgeHandler; | ||
|
||
public class Application { | ||
|
||
private static final org.apache.logging.log4j.Logger log4jLogger = | ||
LogManager.getLogger("log4j-logger"); | ||
private static final org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger("slf4j-logger"); | ||
private static final java.util.logging.Logger julLogger = Logger.getLogger("jul-logger"); | ||
|
||
public static void main(String[] args) { | ||
// Route JUL logs to slf4j | ||
SLF4JBridgeHandler.removeHandlersForRootLogger(); | ||
SLF4JBridgeHandler.install(); | ||
|
||
// Log using log4j API | ||
maybeRunWithSpan(() -> log4jLogger.info("A log4j log message without a span"), false); | ||
maybeRunWithSpan(() -> log4jLogger.info("A log4j log message with a span"), true); | ||
Map<String, Object> mapMessage = new HashMap<>(); | ||
mapMessage.put("key", "value"); | ||
mapMessage.put("message", "A log4j structured message"); | ||
maybeRunWithSpan(() -> log4jLogger.info(new MapMessage<>(mapMessage)), false); | ||
ThreadContext.clearAll(); | ||
maybeRunWithSpan( | ||
() -> log4jLogger.info("A log4j log message with an exception", new Exception("error!")), | ||
false); | ||
|
||
// Log using slf4j API w/ logback backend | ||
maybeRunWithSpan(() -> slf4jLogger.info("A slf4j log message without a span"), false); | ||
maybeRunWithSpan(() -> slf4jLogger.info("A slf4j log message with a span"), true); | ||
maybeRunWithSpan( | ||
() -> | ||
slf4jLogger | ||
.atInfo() | ||
.setMessage("A slf4j structured message") | ||
.addKeyValue("key", "value") | ||
.log(), | ||
false); | ||
maybeRunWithSpan( | ||
() -> slf4jLogger.info("A slf4j log message with an exception", new Exception("error!")), | ||
false); | ||
|
||
// Log using JUL API, bridged to slf4j, w/ logback backend | ||
maybeRunWithSpan(() -> julLogger.info("A JUL log message without a span"), false); | ||
maybeRunWithSpan(() -> julLogger.info("A JUL log message with a span"), true); | ||
maybeRunWithSpan( | ||
() -> | ||
julLogger.log( | ||
Level.INFO, "A JUL log message with an exception", new Exception("error!")), | ||
false); | ||
|
||
// Log using OpenTelemetry Log Bridge API | ||
// WARNING: This illustrates how to write appenders which bridge logs from | ||
// existing frameworks into the OpenTelemetry Log Bridge API. These APIs | ||
// SHOULD NOT be used by end users in place of existing log APIs (i.e. Log4j, Slf4, JUL). | ||
io.opentelemetry.api.logs.Logger customAppenderLogger = | ||
GlobalOpenTelemetry.get().getLogsBridge().get("custom-log-appender"); | ||
maybeRunWithSpan( | ||
() -> | ||
customAppenderLogger | ||
.logRecordBuilder() | ||
.setSeverity(Severity.INFO) | ||
.setBody("A log message from a custom appender without a span") | ||
.setAttribute(AttributeKey.stringKey("key"), "value") | ||
.emit(), | ||
false); | ||
maybeRunWithSpan( | ||
() -> | ||
customAppenderLogger | ||
.logRecordBuilder() | ||
.setSeverity(Severity.INFO) | ||
.setBody("A log message from a custom appender with a span") | ||
.setAttribute(AttributeKey.stringKey("key"), "value") | ||
.emit(), | ||
true); | ||
} | ||
|
||
private static void maybeRunWithSpan(Runnable runnable, boolean withSpan) { | ||
if (!withSpan) { | ||
runnable.run(); | ||
return; | ||
} | ||
Span span = GlobalOpenTelemetry.getTracer("my-tracer").spanBuilder("my-span").startSpan(); | ||
try (Scope unused = span.makeCurrent()) { | ||
runnable.run(); | ||
} finally { | ||
span.end(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?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> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="ConsoleAppender" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern> | ||
logback: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg %kvp{DOUBLE}%n | ||
</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="INFO"> | ||
<appender-ref ref="console"/> | ||
</root> | ||
</configuration> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow what it means.
Is it needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is needed, I have clarified it further.