This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
[WIP] Add support for logging based on google-cloud-logging #347
Open
meltsufin
wants to merge
2
commits into
async-support
Choose a base branch
from
async-support-alt-logging
base: async-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| <properties> | ||
| <maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format> | ||
| <appengine.api.version>1.9.40</appengine.api.version> | ||
| <gcloud.api.version>0.8.2-beta-SNAPSHOT</gcloud.api.version> | ||
|
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. Why are you using the -SNAPSHOT ?? You do need to initialize Cloud Logging and turn on the JUL hook to use it. -- I don't see that here.
Member
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. Like Greg mentioned, we don't have a non-snapshot release of it yet. |
||
| </properties> | ||
|
|
||
| <dependencies> | ||
|
|
@@ -86,7 +87,7 @@ | |
| <groupId>org.eclipse.jetty</groupId> | ||
| <artifactId>jetty-server</artifactId> | ||
| <version>${jetty.version}</version> | ||
| <scope>test</scope> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.eclipse.jetty</groupId> | ||
|
|
@@ -95,6 +96,17 @@ | |
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-logging</artifactId> | ||
| <version>${gcloud.api.version}</version> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>javax.servlet</groupId> | ||
| <artifactId>servlet-api</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
|
|
||
82 changes: 82 additions & 0 deletions
82
...ne-managed-runtime/src/main/java/com/google/apphosting/vmruntime/RequestContextScope.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,82 @@ | ||
| package com.google.apphosting.vmruntime;/* | ||
| * Copyright (C) 2016 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import com.google.cloud.logging.GaeFlexLoggingEnhancer; | ||
|
|
||
| import org.eclipse.jetty.server.Request; | ||
| import org.eclipse.jetty.server.handler.ContextHandler; | ||
| import org.eclipse.jetty.server.handler.ContextHandler.Context; | ||
| import org.eclipse.jetty.server.handler.ContextHandler.ContextScopeListener; | ||
|
|
||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * A Jetty {@link ContextScopeListener} that is called whenever | ||
| * a container managed thread enters or exits the scope of a context and/or request. | ||
| * Used to maintain {@link ThreadLocal} references to the current request and | ||
| * Google traceID, primarily for logging. | ||
| */ | ||
| public class RequestContextScope implements ContextHandler.ContextScopeListener { | ||
| static final Logger logger = Logger.getLogger(RequestContextScope.class.getName()); | ||
|
|
||
| private static final String X_CLOUD_TRACE = "x-cloud-trace-context"; | ||
| private static final ThreadLocal<Integer> contextDepth = new ThreadLocal<>(); | ||
|
|
||
| @Override | ||
| public void enterScope(Context context, Request request, Object reason) { | ||
| if (logger.isLoggable(Level.FINE)) { | ||
| logger.fine("enterScope " + context); | ||
| } | ||
| if (request != null) { | ||
| Integer depth = contextDepth.get(); | ||
| if (depth == null || depth.intValue() == 0) { | ||
| depth = 1; | ||
| String traceId = (String) request.getAttribute(X_CLOUD_TRACE); | ||
| if (traceId == null) { | ||
| traceId = request.getHeader(X_CLOUD_TRACE); | ||
| if (traceId != null) { | ||
| int slash = traceId.indexOf('/'); | ||
| if (slash >= 0) { | ||
| traceId = traceId.substring(0, slash); | ||
| } | ||
| request.setAttribute(X_CLOUD_TRACE, traceId); | ||
| GaeFlexLoggingEnhancer.setCurrentTraceId(traceId); | ||
| } | ||
| } else { | ||
| depth = depth + 1; | ||
| } | ||
| contextDepth.set(depth); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void exitScope(Context context, Request request) { | ||
| if (logger.isLoggable(Level.FINE)) { | ||
| logger.fine("exitScope " + context); | ||
| } | ||
| Integer depth = contextDepth.get(); | ||
| if (depth != null) { | ||
| if (depth > 1) { | ||
| contextDepth.set(depth - 1); | ||
| } else { | ||
| contextDepth.remove(); | ||
| GaeFlexLoggingEnhancer.setCurrentTraceId(null); | ||
| } | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably no longer used, but if it is, it should be at least 1.9.48 if not .49
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, thanks!