-
Notifications
You must be signed in to change notification settings - Fork 452
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
Re-add OpenCensus integration #545
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f08873
Revert "Revert "Add OpenCensus tracing instrument.""
chingor13 41651a9
Revert "Revert "Enhance OpenCensus tracing instrumentation.""
chingor13 cf77d68
Update opencensus version and use dependencyManagement section
chingor13 89a725f
Fix missing import
chingor13 bf72917
Set span attributes
chingor13 fabb6d7
Fix checkstyle and NPE for missing attributes
chingor13 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 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 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
263 changes: 263 additions & 0 deletions
263
google-http-client/src/main/java/com/google/api/client/util/OpenCensusUtils.java
This file contains 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,263 @@ | ||
/* | ||
* Copyright (c) 2018 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. | ||
*/ | ||
|
||
package com.google.api.client.util; | ||
|
||
import com.google.api.client.http.HttpHeaders; | ||
import com.google.api.client.http.HttpRequest; | ||
import com.google.api.client.http.HttpStatusCodes; | ||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import io.opencensus.contrib.http.util.HttpPropagationUtil; | ||
import io.opencensus.trace.BlankSpan; | ||
import io.opencensus.trace.EndSpanOptions; | ||
import io.opencensus.trace.NetworkEvent; | ||
import io.opencensus.trace.NetworkEvent.Type; | ||
import io.opencensus.trace.Span; | ||
import io.opencensus.trace.Status; | ||
import io.opencensus.trace.Tracer; | ||
import io.opencensus.trace.Tracing; | ||
import io.opencensus.trace.propagation.TextFormat; | ||
|
||
import java.util.Collections; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Utilities for Census monitoring and tracing. | ||
* | ||
* @author Hailong Wen | ||
* @since 1.24 | ||
*/ | ||
public class OpenCensusUtils { | ||
|
||
private static final Logger logger = Logger.getLogger(OpenCensusUtils.class.getName()); | ||
|
||
/** | ||
* Span name for tracing {@link HttpRequest#execute()}. | ||
*/ | ||
public static final String SPAN_NAME_HTTP_REQUEST_EXECUTE = | ||
"Sent." + HttpRequest.class.getName() + ".execute"; | ||
|
||
/** | ||
* OpenCensus tracing component. When no OpenCensus implementation is provided, it will return a | ||
* no-op tracer. | ||
*/ | ||
private static Tracer tracer = Tracing.getTracer(); | ||
|
||
/** | ||
* Sequence id generator for message event. | ||
*/ | ||
private static AtomicLong idGenerator = new AtomicLong(); | ||
|
||
/** | ||
* Whether spans should be recorded locally. Defaults to true. | ||
*/ | ||
private static volatile boolean isRecordEvent = true; | ||
|
||
/** | ||
* {@link TextFormat} used in tracing context propagation. | ||
*/ | ||
@Nullable | ||
@VisibleForTesting | ||
static volatile TextFormat propagationTextFormat = null; | ||
|
||
/** | ||
* {@link TextFormat.Setter} for {@link #propagationTextFormat}. | ||
*/ | ||
@Nullable | ||
@VisibleForTesting | ||
static volatile TextFormat.Setter propagationTextFormatSetter = null; | ||
|
||
/** | ||
* Sets the {@link TextFormat} used in context propagation. | ||
* | ||
* <p>This API allows users of google-http-client to specify other text format, or disable context | ||
* propagation by setting it to {@code null}. It should be used along with {@link | ||
* #setPropagationTextFormatSetter} for setting purpose. </p> | ||
* | ||
* @param textFormat the text format. | ||
*/ | ||
public static void setPropagationTextFormat(@Nullable TextFormat textFormat) { | ||
propagationTextFormat = textFormat; | ||
} | ||
|
||
/** | ||
* Sets the {@link TextFormat.Setter} used in context propagation. | ||
* | ||
* <p>This API allows users of google-http-client to specify other text format setter, or disable | ||
* context propagation by setting it to {@code null}. It should be used along with {@link | ||
* #setPropagationTextFormat} for setting purpose. </p> | ||
* | ||
* @param textFormatSetter the {@code TextFormat.Setter} for the text format. | ||
*/ | ||
public static void setPropagationTextFormatSetter(@Nullable TextFormat.Setter textFormatSetter) { | ||
propagationTextFormatSetter = textFormatSetter; | ||
} | ||
|
||
/** | ||
* Sets whether spans should be recorded locally. | ||
* | ||
* <p> This API allows users of google-http-client to turn on/off local span collection. </p> | ||
* | ||
* @param recordEvent record span locally if true. | ||
*/ | ||
public static void setIsRecordEvent(boolean recordEvent) { | ||
isRecordEvent = recordEvent; | ||
} | ||
|
||
/** | ||
* Returns the tracing component of OpenCensus. | ||
* | ||
* @return the tracing component of OpenCensus. | ||
*/ | ||
public static Tracer getTracer() { | ||
return tracer; | ||
} | ||
|
||
/** | ||
* Returns whether spans should be recorded locally. | ||
* | ||
* @return whether spans should be recorded locally. | ||
*/ | ||
public static boolean isRecordEvent() { | ||
return isRecordEvent; | ||
} | ||
|
||
/** | ||
* Propagate information of current tracing context. This information will be injected into HTTP | ||
* header. | ||
* | ||
* @param span the span to be propagated. | ||
* @param headers the headers used in propagation. | ||
*/ | ||
public static void propagateTracingContext(Span span, HttpHeaders headers) { | ||
Preconditions.checkArgument(span != null, "span should not be null."); | ||
Preconditions.checkArgument(headers != null, "headers should not be null."); | ||
if (propagationTextFormat != null && propagationTextFormatSetter != null) { | ||
if (!span.equals(BlankSpan.INSTANCE)) { | ||
propagationTextFormat.inject(span.getContext(), headers, propagationTextFormatSetter); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns an {@link EndSpanOptions} to end a http span according to the status code. | ||
* | ||
* @param statusCode the status code, can be null to represent no valid response is returned. | ||
* @return an {@code EndSpanOptions} that best suits the status code. | ||
*/ | ||
public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) { | ||
// Always sample the span, but optionally export it. | ||
EndSpanOptions.Builder builder = EndSpanOptions.builder(); | ||
if (statusCode == null) { | ||
builder.setStatus(Status.UNKNOWN); | ||
} else if (!HttpStatusCodes.isSuccess(statusCode)) { | ||
switch (statusCode) { | ||
case HttpStatusCodes.STATUS_CODE_BAD_REQUEST: | ||
builder.setStatus(Status.INVALID_ARGUMENT); | ||
break; | ||
case HttpStatusCodes.STATUS_CODE_UNAUTHORIZED: | ||
builder.setStatus(Status.UNAUTHENTICATED); | ||
break; | ||
case HttpStatusCodes.STATUS_CODE_FORBIDDEN: | ||
builder.setStatus(Status.PERMISSION_DENIED); | ||
break; | ||
case HttpStatusCodes.STATUS_CODE_NOT_FOUND: | ||
builder.setStatus(Status.NOT_FOUND); | ||
break; | ||
case HttpStatusCodes.STATUS_CODE_PRECONDITION_FAILED: | ||
builder.setStatus(Status.FAILED_PRECONDITION); | ||
break; | ||
case HttpStatusCodes.STATUS_CODE_SERVER_ERROR: | ||
builder.setStatus(Status.UNAVAILABLE); | ||
break; | ||
default: | ||
builder.setStatus(Status.UNKNOWN); | ||
} | ||
} else { | ||
builder.setStatus(Status.OK); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Records a new message event which contains the size of the request content. Note that the size | ||
* represents the message size in application layer, i.e., content-length. | ||
* | ||
* @param span The {@code span} in which the send event occurs. | ||
* @param size Size of the request. | ||
*/ | ||
public static void recordSentMessageEvent(Span span, long size) { | ||
recordMessageEvent(span, size, Type.SENT); | ||
} | ||
|
||
/** | ||
* Records a new message event which contains the size of the response content. Note that the size | ||
* represents the message size in application layer, i.e., content-length. | ||
* | ||
* @param span The {@code span} in which the receive event occurs. | ||
* @param size Size of the response. | ||
*/ | ||
public static void recordReceivedMessageEvent(Span span, long size) { | ||
recordMessageEvent(span, size, Type.RECV); | ||
} | ||
|
||
/** | ||
* Records a message event of a certain {@link NetowrkEvent.Type}. This method is package | ||
* protected since {@link NetworkEvent} might be deprecated in future releases. | ||
* | ||
* @param span The {@code span} in which the event occurs. | ||
* @param size Size of the message. | ||
* @param eventType The {@code NetworkEvent.Type} of the message event. | ||
*/ | ||
@VisibleForTesting | ||
static void recordMessageEvent(Span span, long size, Type eventType) { | ||
Preconditions.checkArgument(span != null, "span should not be null."); | ||
if (size < 0) { | ||
size = 0; | ||
} | ||
NetworkEvent event = NetworkEvent | ||
.builder(eventType, idGenerator.getAndIncrement()) | ||
.setUncompressedMessageSize(size) | ||
.build(); | ||
span.addNetworkEvent(event); | ||
} | ||
|
||
static { | ||
try { | ||
propagationTextFormat = HttpPropagationUtil.getCloudTraceFormat(); | ||
propagationTextFormatSetter = new TextFormat.Setter<HttpHeaders>() { | ||
@Override | ||
public void put(HttpHeaders carrier, String key, String value) { | ||
carrier.set(key, value); | ||
} | ||
}; | ||
} catch (Exception e) { | ||
logger.log( | ||
Level.WARNING, "Cannot initialize default OpenCensus HTTP propagation text format.", e); | ||
} | ||
|
||
try { | ||
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection( | ||
Collections.<String>singletonList(SPAN_NAME_HTTP_REQUEST_EXECUTE)); | ||
} catch (Exception e) { | ||
logger.log( | ||
Level.WARNING, "Cannot register default OpenCensus span names for collection.", e); | ||
} | ||
} | ||
|
||
private OpenCensusUtils() {} | ||
} |
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.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.