Search code examples
javaspring-bootopen-telemetrygoogle-cloud-loggingopen-telemetry-java

Is there a way to send logs directly to google cloud logging using opentelemetry in a spring boot application?


I am trying to send the application logs directly to google cloud logging using opentelemetry.

With the below changes, the logs are sent to OpenTelemetry collector which is up on port 4318

pom.xml

<dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-spring-boot-starter</artifactId>
</dependency>

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="com/google/cloud/spring/logging/logback-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <appender name="OpenTelemetry"
              class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender">
    </appender>

    <root level="info">
        <appender-ref ref="OpenTelemetry"/>
    </root>
</configuration>

How can I send those logs to google cloud logging instead of the Opentelemetry collector ?


Solution

  • By using the com.google.cloud.opentelemetry library I am able to send the log/trace data directly to Google Cloud Log/Trace without using the Opentelemetry collector.

    The developer is reponsible to create the span and end it when needed, for more information check GoogleCloudPlatform example

    The creation/end of the span can be centralized in 1 place by putting the relevant code in an interceptor.

    • Spring framework way: create custom interceptor that implements org.springframework.web.servlet.HandlerInterceptor
    • Servlets way: create a custom interceptor that implements jakarta.servlet.Filter or javax.servlet.Filter