-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add log appender example #108
Merged
+282
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ receivers: | |
grpc: | ||
exporters: | ||
logging: | ||
logLevel: DEBUG | ||
verbosity: detailed | ||
service: | ||
pipelines: | ||
metrics: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# OpenTelemetry Log Appender Example | ||
|
||
This example demonstrates an application configured to use the OpenTelemetry Log | ||
Appenders 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/library/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/library/README.md). | ||
* [JUL to SLF4J](./build.gradle), 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 | ||
../gradlew run | ||
``` | ||
|
||
Watch the collector logs to see exported log records |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
plugins { | ||
id 'java' | ||
id 'application' | ||
} | ||
|
||
description = 'OpenTelemetry Log Appender Example' | ||
ext.moduleName = "io.opentelemetry.examples.log-appender" | ||
|
||
dependencies { | ||
// Slf4J / logback | ||
implementation("org.slf4j:slf4j-api:2.0.6") | ||
implementation("ch.qos.logback:logback-core:1.4.5") | ||
implementation("ch.qos.logback:logback-classic:1.4.5") | ||
|
||
// JUL to SLF4J bridge | ||
implementation("org.slf4j:jul-to-slf4j:2.0.6") | ||
|
||
// Log4j | ||
implementation(platform("org.apache.logging.log4j:log4j-bom:2.20.0")) | ||
implementation("org.apache.logging.log4j:log4j-api") | ||
implementation("org.apache.logging.log4j:log4j-core") | ||
|
||
// OpenTelemetry core | ||
implementation("io.opentelemetry:opentelemetry-sdk") | ||
implementation("io.opentelemetry:opentelemetry-sdk-logs") | ||
implementation("io.opentelemetry:opentelemetry-exporter-otlp-logs") | ||
implementation("io.opentelemetry:opentelemetry-semconv") | ||
|
||
// OpenTelemetry log4j / logback appenders | ||
implementation 'io.opentelemetry.instrumentation:opentelemetry-log4j-appender-2.17' | ||
implementation 'io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0' | ||
} | ||
|
||
application { | ||
mainClass = 'io.opentelemetry.example.logappender.Application' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
version: '3' | ||
services: | ||
collector: | ||
image: otel/opentelemetry-collector-contrib:0.72.0 | ||
volumes: | ||
- ./otel-config.yaml:/otel-config.yaml | ||
command: ["--config=/otel-config.yaml"] | ||
ports: | ||
- "4317:4317" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
exporters: | ||
logging: | ||
verbosity: detailed | ||
service: | ||
pipelines: | ||
logs: | ||
receivers: [otlp] | ||
exporters: [logging] |
129 changes: 129 additions & 0 deletions
129
log-appender/src/main/java/io/opentelemetry/example/logappender/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.opentelemetry.example.logappender; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.logs.GlobalLoggerProvider; | ||
import io.opentelemetry.api.logs.Severity; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import io.opentelemetry.sdk.logs.SdkLoggerProvider; | ||
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.sdk.trace.SdkTracerProvider; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
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) { | ||
// Initialize OpenTelemetry as early as possible | ||
initializeOpenTelemetry(); | ||
|
||
// 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(); | ||
|
||
// 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); | ||
|
||
// 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); | ||
|
||
// 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 = | ||
GlobalLoggerProvider.get().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 initializeOpenTelemetry() { | ||
OpenTelemetrySdk sdk = | ||
OpenTelemetrySdk.builder() | ||
.setTracerProvider(SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build()) | ||
.setLoggerProvider( | ||
SdkLoggerProvider.builder() | ||
.setResource( | ||
Resource.getDefault().toBuilder() | ||
.put(ResourceAttributes.SERVICE_NAME, "log4j-example") | ||
.build()) | ||
.addLogRecordProcessor( | ||
BatchLogRecordProcessor.builder( | ||
OtlpGrpcLogRecordExporter.builder() | ||
.setEndpoint("http://localhost:4317") | ||
.build()) | ||
.build()) | ||
.build()) | ||
.build(); | ||
GlobalOpenTelemetry.set(sdk); | ||
GlobalLoggerProvider.set(sdk.getSdkLoggerProvider()); | ||
|
||
// Add hook to close SDK, which flushes logs | ||
Runtime.getRuntime().addShutdownHook(new Thread(sdk::close)); | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN" packages="io.opentelemetry.instrumentation.log4j.appender.v2_17"> | ||
<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"/> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="OpenTelemetryAppender" /> | ||
<AppenderRef ref="ConsoleAppender" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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> | ||
<appender name="OpenTelemetry" | ||
class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender" | ||
captureMdcAttributes="true" > | ||
</appender> | ||
<root level="INFO"> | ||
<appender-ref ref="console"/> | ||
<appender-ref ref="OpenTelemetry"/> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
😱 (to seem them used all in one file 😂)
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.
Ha, originally I had separate examples for each situation, but that just created a bunch of bloat and increased the maintenance burden. Let's show it all in one place instead!