-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Add execution id logging to uniquely identify request logs #319
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
Merged
maemayve
merged 2 commits into
GoogleCloudPlatform:main
from
maemayve:java-execution-id
Feb 12, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
# Maven | ||
target/ | ||
dependency-reduced-pom.xml | ||
|
||
# Gradle | ||
.gradle | ||
|
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
63 changes: 63 additions & 0 deletions
63
invoker/core/src/main/java/com/google/cloud/functions/invoker/gcf/ExecutionIdUtil.java
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.google.cloud.functions.invoker.gcf; | ||
|
||
import java.util.Base64; | ||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.logging.Handler; | ||
import java.util.logging.Logger; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* A helper class that either fetches a unique execution id from request HTTP headers or generates a | ||
* random id. | ||
*/ | ||
public final class ExecutionIdUtil { | ||
private static final Logger rootLogger = Logger.getLogger(""); | ||
private static final int EXECUTION_ID_LENGTH = 12; | ||
private static final String EXECUTION_ID_HTTP_HEADER = "HTTP_FUNCTION_EXECUTION_ID"; | ||
private static final String LOG_EXECUTION_ID_ENV_NAME = "LOG_EXECUTION_ID"; | ||
|
||
private final Random random = ThreadLocalRandom.current(); | ||
|
||
/** | ||
* Add mapping to root logger from current thread id to execution id. This mapping will be used to | ||
* append the execution id to log lines. | ||
*/ | ||
public void storeExecutionId(HttpServletRequest request) { | ||
if (!executionIdLoggingEnabled()) { | ||
return; | ||
} | ||
for (Handler handler : rootLogger.getHandlers()) { | ||
if (handler instanceof JsonLogHandler) { | ||
String id = getOrGenerateExecutionId(request); | ||
((JsonLogHandler) handler).addExecutionId(Thread.currentThread().getId(), id); | ||
} | ||
} | ||
} | ||
|
||
/** Remove mapping from curent thread to request execution id */ | ||
public void removeExecutionId() { | ||
if (!executionIdLoggingEnabled()) { | ||
return; | ||
} | ||
for (Handler handler : rootLogger.getHandlers()) { | ||
if (handler instanceof JsonLogHandler) { | ||
((JsonLogHandler) handler).removeExecutionId(Thread.currentThread().getId()); | ||
} | ||
} | ||
} | ||
|
||
private String getOrGenerateExecutionId(HttpServletRequest request) { | ||
String executionId = request.getHeader(EXECUTION_ID_HTTP_HEADER); | ||
if (executionId == null) { | ||
byte[] array = new byte[EXECUTION_ID_LENGTH]; | ||
random.nextBytes(array); | ||
executionId = Base64.getEncoder().encodeToString(array); | ||
} | ||
return executionId; | ||
} | ||
|
||
private boolean executionIdLoggingEnabled() { | ||
return Boolean.parseBoolean(System.getenv().getOrDefault(LOG_EXECUTION_ID_ENV_NAME, "false")); | ||
} | ||
} |
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
Oops, something went wrong.
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.