Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: populate metadata on write()
  • Loading branch information
minherz committed Dec 20, 2021
commit c1211212715d02a5791d9cb882ea568ca66b6e45
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.google.cloud.logging.Logging.WriteOption.OptionType.LOG_NAME;
import static com.google.cloud.logging.Logging.WriteOption.OptionType.RESOURCE;

import com.google.api.client.util.Strings;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
Expand Down Expand Up @@ -93,6 +94,7 @@
class LoggingImpl extends BaseService<LoggingOptions> implements Logging {

private static final int FLUSH_WAIT_TIMEOUT_SECONDS = 6;
private static final String RESOURCE_NAME_FORMAT = "projects/%s/traces/%s";
private final LoggingRpc rpc;
private final Map<Object, ApiFuture<Void>> pendingWrites = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -797,6 +799,76 @@ public void write(Iterable<LogEntry> logEntries, WriteOption... options) {
inWriteCall.set(true);

try {
final Map<Option.OptionType, ?> writeOptions = optionMap(options);
final Boolean populateMetadata1 = getOptions().getAutoPopulateMetadata();
final Boolean populateMetadata2 =
WriteOption.OptionType.AUTO_POPULATE_METADATA.get(writeOptions);
if (populateMetadata2 == Boolean.TRUE
|| (populateMetadata2 == null && populateMetadata1 == Boolean.TRUE)) {
final Boolean needDebugInfo =
Iterables.any(
logEntries,
log -> log.getSeverity() == Severity.DEBUG && log.getSourceLocation() == null);
final SourceLocation sourceLocation =
needDebugInfo ? SourceLocation.fromCurrentContext(1) : null;
final MonitoredResource sharedResourceMetadata = RESOURCE.get(writeOptions);
final MonitoredResource resourceMetadata =
sharedResourceMetadata == null ? MonitoredResourceUtil.getResource(null, null) : null;
final Context context = (new ContextHandler()).getCurrentContext();
final ArrayList<LogEntry> populatedLogEntries = Lists.newArrayList();
for (LogEntry entry : logEntries) {
LogEntry.Builder builder = entry.toBuilder();
if (resourceMetadata != null && entry.getResource() == null) {
builder.setResource(resourceMetadata);
}
if (entry.getHttpRequest() == null) {
builder.setHttpRequest(context.getHttpRequest());
}
if (Strings.isNullOrEmpty(entry.getTrace())) {
// if project id can be retrieved from resource (environment) metadata
// format trace id to support grouping and correlation
if (context.getTraceId() != null) {
String projectId = null;
if (entry.getResource() != null) {
projectId =
entry
.getResource()
.getLabels()
.getOrDefault(MonitoredResourceUtil.PORJECTID_LABEL, null);
}
if (projectId == null && resourceMetadata != null) {
projectId =
resourceMetadata
.getLabels()
.getOrDefault(MonitoredResourceUtil.PORJECTID_LABEL, null);
}
if (projectId == null && sharedResourceMetadata != null) {
projectId =
sharedResourceMetadata
.getLabels()
.getOrDefault(MonitoredResourceUtil.PORJECTID_LABEL, null);
}
if (projectId != null) {
builder.setTrace(
String.format(RESOURCE_NAME_FORMAT, projectId, context.getTraceId()));
} else {
builder.setTrace(context.getTraceId());
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function becomes too bulky and hard to follow - perhaps you can export the part dealing with context into private function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Contributor Author

@minherz minherz Dec 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the trace metadata formatting is moved to a stand-alone method

if (Strings.isNullOrEmpty(entry.getSpanId())) {
builder.setSpanId(context.getSpanId());
}
if (entry.getSeverity() != null
&& entry.getSeverity() == Severity.DEBUG
&& entry.getSourceLocation() == null) {
builder.setSourceLocation(sourceLocation);
}
populatedLogEntries.add(builder.build());
}
logEntries = populatedLogEntries;
}

writeLogEntries(logEntries, options);
if (flushSeverity != null) {
for (LogEntry logEntry : logEntries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public class MonitoredResourceUtil {

private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/";
public static final String PORJECTID_LABEL = Label.ProjectId.getKey();

protected enum Label {
ClusterName("cluster_name"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ public void setUp() {
.setProjectId(PROJECT)
.setServiceRpcFactory(rpcFactoryMock)
.setRetrySettings(ServiceOptions.getNoRetrySettings())
// disable auto-population for LoggingImpl class tests
// see {@see AutoPopulationTests} for auto-population tests
.setAutoPopulateMetadata(false)
.build();

// By default when calling ListLogEntries, we append a filter of last 24 hours.
Expand Down