This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
feat: auto-populate metadata of log entries at write() #803
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
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
commit c1211212715d02a5791d9cb882ea568ca66b6e45
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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<>(); | ||
|
|
||
|
|
@@ -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()); | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will look into it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.