Spring Trace Id and Span ID in GCP Cloud Run request log

775 views Asked by At

I am using spring cloud GCP logging and trace in my GCP cloud run application

<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/cloud/gcp/logging/logback-appender.xml"/>

  <root level="INFO">
    <appender-ref ref="STACKDRIVER"/>
  </root>

</configuration>

After adding this I can see that logs are grouped

enter image description here

Now I have two questions

  1. Can we have the trace ID and and span ID printed in the request log it self.
  2. After adding stack driver, the spring default console pattern is not shown. All the logs are printed in raw format. They do not have thread-name, span id, appName etc.

Can these two be achieved?

Update Trying to enhance the log

@Component
public class NukTraceIdLoggingEnhancer extends TraceIdLoggingEnhancer {

  @Override
  public void enhanceLogEntry(LogEntry.Builder builder) {
    super.enhanceLogEntry(builder);

    LogEntry.Builder newDummyBuilder = builder;

    LogEntry logEntry = newDummyBuilder.build();
    Payload<?> payload = logEntry.getPayload();
    if (logEntry.getPayload() instanceof JsonPayload) {
      JsonPayload jsonPayload = (JsonPayload) payload;
      String content = (String) ((JsonPayload) payload).getDataAsMap().get("message");

      content = "abc" + logEntry.getSpanId() + ", " + logEntry.getTrace() + content;

      jsonPayload.getDataAsMap().replace("message", content);

      builder.setPayload(jsonPayload);
    }
  }

  @Override
  public void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent e) {
    super.enhanceLogEntry(builder, e);

    LogEntry.Builder newDummyBuilder = builder;

    LogEntry logEntry = newDummyBuilder.build();
    Payload<?> payload = logEntry.getPayload();
    if (logEntry.getPayload() instanceof JsonPayload) {
      JsonPayload jsonPayload = (JsonPayload) payload;
      String content = (String) ((JsonPayload) payload).getDataAsMap().get("message");

      content = "XYZ" + logEntry.getSpanId() + ", " + logEntry.getTrace() + content;

      jsonPayload.getDataAsMap().replace("message", content);

      builder.setPayload(jsonPayload);
    }
  }


}

logback-spring.xml

        <configuration>
          <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
          <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

            

<property name="STACKDRIVER_LOG_NAME" value="${STACKDRIVER_LOG_NAME:-spring.log}"/>
                <property name="STACKDRIVER_LOG_FLUSH_LEVEL" value="${STACKDRIVER_LOG_FLUSH_LEVEL:-WARN}"/>

                <appender name="MY_STACKDRIVER" class="com.google.cloud.spring.logging.LoggingAppender">
                  <log>${STACKDRIVER_LOG_NAME}</log> <!-- Optional : default spring.log -->
                  <loggingEventEnhancer>abc.configuration.NukTraceIdLoggingEnhancer</loggingEventEnhancer>
                  <flushLevel>INFO</flushLevel> <!-- Optional : default ERROR -->
                </appender>

            <root level="INFO">
              <appender-ref ref="MY_STACKDRIVER"/>
            </root>
        </configuration>
0

There are 0 answers