-
Notifications
You must be signed in to change notification settings - Fork 453
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
Add OpenCensus tracing instrumentation in google-http-client module. #393
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* 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.HttpResponse; | ||
import com.google.api.client.http.HttpStatusCodes; | ||
|
||
import io.opencensus.contrib.http.util.HttpPropagationUtil; | ||
import io.opencensus.trace.BlankSpan; | ||
import io.opencensus.trace.EndSpanOptions; | ||
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.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Utilities for Census monitoring and tracing. | ||
* | ||
* @since 1.24 | ||
* @author Hailong Wen | ||
*/ | ||
public class OpenCensusUtils { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(OpenCensusUtils.class.getName()); | ||
|
||
/** | ||
* OpenCensus tracing component. | ||
* When no OpenCensus implementation is provided, it will return a no-op tracer. | ||
*/ | ||
static Tracer tracer = Tracing.getTracer(); | ||
|
||
/** | ||
* {@link TextFormat} used in tracing context propagation. | ||
*/ | ||
@Nullable | ||
static TextFormat propagationTextFormat = null; | ||
|
||
/** | ||
* {@link TextFormat.Setter} for {@link activeTextFormat}. | ||
*/ | ||
@Nullable | ||
static TextFormat.Setter propagationTextFormatSetter = null; | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
/** | ||
* Sets the {@link TextFormat} used in context propagation. | ||
* @param textFormat the text format. | ||
*/ | ||
public static void setPropagationTextFormat(@Nullable TextFormat textFormat) { | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
propagationTextFormat = textFormat; | ||
} | ||
|
||
/** | ||
* Sets the {@link TextFormat.Setter} used in context propagation. | ||
* @param textFormatSetter the {@code TextFormat.Setter} for the text format. | ||
*/ | ||
public static void setPropagationTextFormatSetter(@Nullable TextFormat.Setter textFormatSetter) { | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
propagationTextFormatSetter = textFormatSetter; | ||
} | ||
|
||
/** | ||
* Returns the tracing component of OpenCensus. | ||
* | ||
* @return the tracing component of OpenCensus. | ||
*/ | ||
public static Tracer getTracer() { | ||
return tracer; | ||
} | ||
|
||
/** | ||
* Propagate information of current tracing context. This information will be injected into HTTP | ||
* header. | ||
*/ | ||
public static void propagateTracingContext(HttpHeaders headers) { | ||
Preconditions.checkNotNull(headers); | ||
if (propagationTextFormat != null && propagationTextFormatSetter != null) { | ||
Span span = tracer.getCurrentSpan(); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
if (span != null && !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().setSampleToLocalSpanStore(true); | ||
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(); | ||
} | ||
|
||
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 initiate OpenCensus modules, tracing disabled", e); | ||
} | ||
} | ||
|
||
private OpenCensusUtils() {} | ||
} |
This comment was marked as spam.
Sorry, something went wrong.