Skip to content

Commit

Permalink
674: add span/sampled values to LogEntry, add source log info labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Boshier committed Sep 20, 2024
1 parent e200f31 commit 304c2e4
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.logging.ErrorManager;
import java.util.logging.Level;

import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -11,6 +12,7 @@
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.Payload;
import com.google.cloud.logging.Severity;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

Expand All @@ -25,6 +27,10 @@

public class LoggingHandler extends ExtHandler {

private static final String LEVEL_NAME_KEY = "levelName";
private static final String LEVEL_VALUE_KEY = "levelValue";
private static final String LOGGER_NAME_KEY = "loggerName";

private final LoggingConfiguration config;

// lazy values, they depend on the gcp config which in turn
Expand Down Expand Up @@ -79,11 +85,24 @@ public void doPublish(ExtLogRecord record) {
private LogEntry transform(ExtLogRecord record, TraceInfo trace) {
Payload<?> payload = internalHandler.transform(record, trace);
if (payload != null) {
Level level = record.getLevel();
Severity severity = LevelTransformer.toSeverity(level);
com.google.cloud.logging.LogEntry.Builder builder = LogEntry.newBuilder(payload)
.setSeverity(LevelTransformer.toSeverity(record.getLevel()))
.setTimestamp(record.getInstant());
.setSeverity(severity)
.setTimestamp(record.getInstant())
.addLabel(LEVEL_NAME_KEY, level.toString())
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.intValue()));

String loggerName = record.getSourceClassName();
if (!Strings.isNullOrEmpty(loggerName)) {
builder = builder.addLabel(LOGGER_NAME_KEY, loggerName);
}

if (this.config.gcpTracing().enabled() && trace != null && !Strings.isNullOrEmpty(trace.getTraceId())) {
builder = builder.setTrace(composeTraceString(trace.getTraceId()));
builder = builder
.setTrace(composeTraceString(trace.getTraceId()))
.setSpanId(trace.getSpanId())
.setTraceSampled(true);
}
return builder.build();
} else {
Expand Down

0 comments on commit 304c2e4

Please sign in to comment.