Skip to content
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 format for google cloud platform #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion runtime/src/main/java/io/quarkiverse/loggingjson/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ public static class FieldsConfig {
*/
@ConfigItem
public FieldConfig errorMessage;
/**
* Options for gcpSpanId.
*/
@ConfigItem
public GcpSpanIdConfig gcpSpanId;
/**
* Options for gcpTraceId.
*/
@ConfigItem
public GcpTraceIdConfig gcpTraceId;

/**
* Options for epochSecond.
*/
@ConfigItem
public FieldConfig epochSecond;
/**
* Options for nanoSecond.
*/
@ConfigItem
public FieldConfig nanoSecond;

}

@ConfigGroup
Expand All @@ -146,6 +168,49 @@ public static class FieldConfig {
public Optional<Boolean> enabled;
}

@ConfigGroup
public static class GcpSpanIdConfig {
/**
* Used to change the json key for the field.
*/
@ConfigItem
public Optional<String> fieldName;
/**
* Enable or disable the field.
*/
@ConfigItem
public Optional<Boolean> enabled;
/**
* key to lookup from mdc.
*/
@ConfigItem
public Optional<String> mdcKey;
}

@ConfigGroup
public static class GcpTraceIdConfig {
/**
* Used to change the json key for the field.
*/
@ConfigItem
public Optional<String> fieldName;
/**
* Enable or disable the field.
*/
@ConfigItem
public Optional<Boolean> enabled;
/**
* Will write the values at the top level of the JSON log object.
*/
@ConfigItem
public Optional<String> projectId;
/**
* key to lookup from mdc.
*/
@ConfigItem
public Optional<String> mdcKey;
}

@ConfigGroup
public static class MDCConfig {
/**
Expand Down Expand Up @@ -242,6 +307,7 @@ public enum AdditionalFieldType {

public enum LogFormat {
DEFAULT,
ECS
ECS,
GCP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.quarkiverse.loggingjson.jackson.JacksonJsonFactory;
import io.quarkiverse.loggingjson.jsonb.JsonbJsonFactory;
import io.quarkiverse.loggingjson.providers.*;
import io.quarkiverse.loggingjson.providers.gcp.*;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableInstance;
import io.quarkus.runtime.RuntimeValue;
Expand All @@ -30,6 +31,8 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(Config config, bo

if (config.logFormat == Config.LogFormat.ECS) {
providers = ecsFormat(config);
} else if (config.logFormat == Config.LogFormat.GCP) {
providers = gcpFormat(config);
} else {
providers = defaultFormat(config);
}
Expand Down Expand Up @@ -87,6 +90,19 @@ private List<JsonProvider> defaultFormat(Config config) {
return providers;
}

private List<JsonProvider> gcpFormat(Config config) {
List<JsonProvider> providers = new ArrayList<>();
providers.add(new GcpEpochSecondJsonProvider(config.fields.epochSecond));
providers.add(new GcpNanoJsonProvider(config.fields.nanoSecond));
providers.add(new LoggerNameJsonProvider(config.fields.loggerName, "logger"));
providers.add(new GcpLogLevelJsonProvider(config.fields.level));
providers.add(new ThreadNameJsonProvider(config.fields.threadName, "thread"));
providers.add(new GcpMessageWithExceptionJsonProvider(config.fields.message));
providers.add(new GcpSpanIdJsonProvider(config.fields.gcpSpanId));
providers.add(new GcpTraceIdJsonProvider(config.fields.gcpTraceId));
return providers;
}

private List<JsonProvider> ecsFormat(Config config) {
List<JsonProvider> providers = new ArrayList<>();
providers.add(new TimestampJsonProvider(config.fields.timestamp, "@timestamp"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;
import java.time.Instant;

import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.*;

public class GcpEpochSecondJsonProvider implements JsonProvider, Enabled {

private final Config.FieldConfig config;
private final String fieldName;

/**
* The JSON field name for the seconds of the timestamp.
*/
public static final String TIMESTAMP_SECONDS_ATTRIBUTE = "timestampSeconds";

public GcpEpochSecondJsonProvider(Config.FieldConfig config) {
this(config, TIMESTAMP_SECONDS_ATTRIBUTE);
}

public GcpEpochSecondJsonProvider(Config.FieldConfig config, String defaultName) {
this.config = config;
this.fieldName = config.fieldName.orElse(defaultName);
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
Instant time = Instant.ofEpochMilli(event.getMillis());
JsonWritingUtils.writeNumberField(generator, fieldName, time.getEpochSecond());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.Level;

import io.quarkiverse.loggingjson.*;

public class GcpLogLevelJsonProvider implements JsonProvider, Enabled {
/**
* The JSON field name for the log level (severity).
*/
public static final String SEVERITY_ATTRIBUTE = "severity";
private static final Map<Level, String> logbackToSeverityMap;
private final String fieldName;

static {
logbackToSeverityMap = new HashMap<>();
logbackToSeverityMap.put(Level.TRACE, "DEBUG");
logbackToSeverityMap.put(Level.DEBUG, "DEBUG");
logbackToSeverityMap.put(Level.INFO, "INFO");
logbackToSeverityMap.put(Level.WARN, "WARNING");
logbackToSeverityMap.put(Level.ERROR, "ERROR");
}

private final Config.FieldConfig config;

public GcpLogLevelJsonProvider(Config.FieldConfig config) {
this(config, SEVERITY_ATTRIBUTE);
}

public GcpLogLevelJsonProvider(Config.FieldConfig config, String defaultName) {
this.config = config;
this.fieldName = config.fieldName.orElse(defaultName);
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
JsonWritingUtils.writeStringField(generator, fieldName,
logbackToSeverityMap.getOrDefault(event.getLevel(), "DEFAULT"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;
import java.io.PrintWriter;

import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.*;

public class GcpMessageWithExceptionJsonProvider extends ExtFormatter implements JsonProvider, Enabled {
private final String fieldName;
private final Config.FieldConfig config;

public GcpMessageWithExceptionJsonProvider(Config.FieldConfig config) {
this.config = config;
this.fieldName = config.fieldName.orElse("message");
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
StringBuilderWriter message = new StringBuilderWriter();
message.write(formatMessage(event));
if (event.getThrown() != null) {
message.write("\n");
event.getThrown().printStackTrace(new PrintWriter(message));
}
JsonWritingUtils.writeStringField(generator, fieldName, message.toString());
}

@Override
public String format(ExtLogRecord record) {
return null;
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;
import java.time.Instant;

import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.*;

public class GcpNanoJsonProvider implements JsonProvider, Enabled {

private final Config.FieldConfig config;

/**
* The JSON field name for the seconds of the timestamp.
*/
public static final String TIMESTAMP_NANOS_ATTRIBUTE = "timestampNanos";
private final String fieldName;

public GcpNanoJsonProvider(Config.FieldConfig config) {
this(config, TIMESTAMP_NANOS_ATTRIBUTE);
}

public GcpNanoJsonProvider(Config.FieldConfig config, String defaultName) {
this.config = config;
this.fieldName = config.fieldName.orElse(defaultName);
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
Instant time = Instant.ofEpochMilli(event.getMillis());
JsonWritingUtils.writeNumberField(generator, fieldName, time.getNano());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;

import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.*;

public class GcpSpanIdJsonProvider implements JsonProvider, Enabled {
public static final String SPAN_ID_ATTRIBUTE = "logging.googleapis.com/spanId";
private final Config.GcpSpanIdConfig config;
private final String fieldName;
private final String mdcKey;

public GcpSpanIdJsonProvider(Config.GcpSpanIdConfig config) {
this(config, SPAN_ID_ATTRIBUTE, "x-b3-spanid");
}

public GcpSpanIdJsonProvider(Config.GcpSpanIdConfig config, String defaultName, String defaultMdcKey) {
this.config = config;
this.fieldName = config.fieldName.orElse(defaultName);
this.mdcKey = config.mdcKey.orElse(defaultMdcKey);
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {

String spanId = event.getMdc(mdcKey);
if (spanId != null) {
JsonWritingUtils.writeStringField(generator, fieldName, spanId);
}
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkiverse.loggingjson.providers.gcp;

import java.io.IOException;

import org.jboss.logmanager.ExtLogRecord;

import io.quarkiverse.loggingjson.*;

public class GcpTraceIdJsonProvider implements JsonProvider, Enabled {
public static final String TRACE_ID_ATTRIBUTE = "logging.googleapis.com/trace";
private final Config.GcpTraceIdConfig config;
private final String projectId;
private final String mdcKey;
private final String fieldName;

public GcpTraceIdJsonProvider(Config.GcpTraceIdConfig config) {
this(config, TRACE_ID_ATTRIBUTE, "default", "x-b3-traceid");
}

public GcpTraceIdJsonProvider(Config.GcpTraceIdConfig config, String defaultName, String defaultProjectId,
String defaultMdcKey) {
this.config = config;
this.fieldName = config.fieldName.orElse(defaultName);
this.mdcKey = config.mdcKey.orElse(defaultMdcKey);
this.projectId = config.projectId.orElse(defaultProjectId);
}

protected String formatTraceId(final String traceId) {
// Trace IDs are either 64-bit or 128-bit, which is 16-digit hex, or 32-digit hex.
// If traceId is 64-bit (16-digit hex), then we need to prepend 0's to make a 32-digit hex.
if (traceId != null && traceId.length() == 16) {
return "0000000000000000" + traceId;
}
return traceId;
}

String composeFullTraceName(String traceId) {
return "projects/" + projectId + "/traces/" + traceId;
}

@Override
public void writeTo(JsonGenerator generator, ExtLogRecord event) throws IOException {
String traceId = event.getMdc(mdcKey);
if (traceId != null) {
JsonWritingUtils.writeStringField(generator, fieldName, composeFullTraceName(formatTraceId(traceId)));
}
}

@Override
public boolean isEnabled() {
return config.enabled.orElse(true);
}

}
Loading