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 option to configure GCP logging extension's logging target #455

Merged
merged 3 commits into from
Jul 5, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.Optional;

import com.google.cloud.logging.LoggingHandler.LogTarget;
import com.google.cloud.logging.Severity;
import com.google.cloud.logging.Synchronicity;

Expand Down Expand Up @@ -74,6 +75,14 @@ public class LoggingConfiguration {
@ConfigItem
public StructuredConfig structured;

/**
* Configures if logs should be written to stdout or stderr instead of using Google Cloud Operations API.
* Useful if app is deployed to managed Google Cloud Platform environment with installed logger agent.
* Possible values: STDOUT, STDERR and CLOUD_LOGGING.
*/
@ConfigItem(defaultValue = "CLOUD_LOGGING")
public LogTarget logTarget;
loicmathieu marked this conversation as resolved.
Show resolved Hide resolved

@ConfigGroup
public static class StructuredConfig {

Expand Down Expand Up @@ -198,4 +207,5 @@ public enum LogFormat {
TEXT,
JSON
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ public void doPublish(ExtLogRecord record) {
TraceInfo trace = traceExtractor.extract(record);
LogEntry logEntry = transform(record, trace);
if (logEntry != null) {
l.write(ImmutableList.of(logEntry), defaultWriteOptions);
switch (config.logTarget) {
case STDOUT:
System.out.println(logEntry.toStructuredJsonString());
break;
case STDERR:
System.err.println(logEntry.toStructuredJsonString());
break;
case CLOUD_LOGGING:
l.write(ImmutableList.of(logEntry), defaultWriteOptions);
break;
}
}
} catch (Exception ex) {
getErrorManager().error("Failed to publish record to GCP", ex, ErrorManager.WRITE_FAILURE);
getErrorManager().error("Failed to write logs", ex, ErrorManager.WRITE_FAILURE);
}
}

Expand Down Expand Up @@ -138,7 +148,7 @@ private void initDefaultWriteOptions() {
private MonitoredResource createMonitoredResource() {
MonitoredResource.Builder b = MonitoredResource.newBuilder(this.config.resource.type);
if (this.config.resource.label != null) {
this.config.resource.label.forEach((k, v) -> b.addLabel(k, v));
this.config.resource.label.forEach(b::addLabel);
}
return b.build();
}
Expand Down