Last active
June 11, 2021 12:41
-
-
Save liias/b2b68dbade7a33dcd36063744ac64448 to your computer and use it in GitHub Desktop.
OpenTelemetry Tracing Span Event Logback configuration. Based on a Spring Boot 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
# Adds trace_id, span_id and trace_flags to log message, so when you read the logs, you can also find the trace | |
# Those are MDC properties and automatically added by opentelemetry-javaagent-all.jar javaagent | |
logging.pattern.console = %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg trace_id=%X{trace_id} span_id=%X{span_id} trace_flags=%X{trace_flags} %n |
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
# Example Dockerfile for java application with OpenTelemetry java agent added | |
FROM openjdk:11-jre-slim-stretch | |
RUN apt update && apt install -y libtcnative-1 && rm -rf /var/lib/apt/lists/* | |
# mount container /tmp to host /var/lib/docker (or whatever is set on runtime) | |
# /tmp is the default working directory for Tomcat in Sprint Boot application | |
VOLUME /tmp | |
EXPOSE 8080 | |
COPY ./target/myapplication.jar /app/myapplication.jar | |
COPY ./opentelemetry-javaagent-all.jar /app/opentelemetry-javaagent-all.jar | |
WORKDIR /app | |
#https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md | |
ENTRYPOINT ["java", \ | |
"-javaagent:/app/opentelemetry-javaagent-all.jar", \ | |
"-Dotel.traces.exporter=jaeger", \ | |
"-Dotel.exporter.jaeger.endpoint=http://jaeger:14250", \ | |
"-Dotel.metrics.exporter=logging", \ | |
"-Dotel.resource.attributes=service.name=myapplication", \ | |
"-Dapplication.home=/app/", \ | |
"-Dapplication.name=myapplication", \ | |
"-jar","/app/myapplication.jar"] |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- | |
Base logback configuration provided for compatibility with Spring Boot 1.1 | |
--> | |
<configuration> | |
<include resource="org/springframework/boot/logging/logback/defaults.xml" /> | |
<include resource="org/springframework/boot/logging/logback/console-appender.xml" /> | |
<!-- Adds log to current trace span event --> | |
<appender name="SPAN_EVENT_APPENDER" class="my.pkg.LogbackSpanEventAppender"></appender> | |
<root level="INFO"> | |
<appender-ref ref="SPAN_EVENT_APPENDER" /> | |
<appender-ref ref="CONSOLE" /> | |
</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
package my.pkg; | |
import ch.qos.logback.classic.spi.LoggingEvent; | |
import ch.qos.logback.core.AppenderBase; | |
import io.opentelemetry.api.common.Attributes; | |
import io.opentelemetry.api.common.AttributesBuilder; | |
import io.opentelemetry.api.trace.Span; | |
import org.slf4j.Marker; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Logback appender which adds a log to current trace span event | |
*/ | |
public class LogbackSpanEventAppender extends AppenderBase<LoggingEvent> { | |
@Override | |
protected void append(LoggingEvent event) { | |
Span span = Span.current(); | |
AttributesBuilder attributesBuilder = Attributes.builder(); | |
// copy all MDC properties, you can leave it out or take only useful information to avoid storing unnecessary data | |
event.getMDCPropertyMap().forEach(attributesBuilder::put); | |
Marker marker = event.getMarker(); | |
attributesBuilder.put("marker", marker != null ? marker.getName() : null); | |
Attributes attributes = attributesBuilder.build(); | |
span.addEvent(event.getFormattedMessage(), attributes, event.getTimeStamp(), TimeUnit.MILLISECONDS); | |
} | |
} |
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
<dependency> | |
<groupId>io.opentelemetry</groupId> | |
<artifactId>opentelemetry-api</artifactId> | |
<version>1.2.0</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment