Skip to content

Commit

Permalink
revert: "feat: remove opencensus (googleapis#834)" (googleapis#837)
Browse files Browse the repository at this point in the history
This reverts commit f16c777.
  • Loading branch information
chingor13 authored Oct 11, 2019
1 parent f16c777 commit ea09fd5
Show file tree
Hide file tree
Showing 6 changed files with 744 additions and 0 deletions.
34 changes: 34 additions & 0 deletions google-http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
</description>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<ignoredUnusedDeclaredDependencies>io.opencensus:opencensus-impl</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -97,6 +109,10 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand All @@ -105,6 +121,14 @@
<groupId>com.google.j2objc</groupId>
<artifactId>j2objc-annotations</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-http-util</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -126,5 +150,15 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
import com.google.api.client.util.StreamingContent;
import com.google.api.client.util.StringUtils;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.opencensus.common.Scope;
import io.opencensus.contrib.http.util.HttpTraceAttributeConstants;
import io.opencensus.trace.AttributeValue;
import io.opencensus.trace.Span;
import io.opencensus.trace.Tracer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand Down Expand Up @@ -192,6 +197,9 @@ public final class HttpRequest {
/** Sleeper. */
private Sleeper sleeper = Sleeper.DEFAULT;

/** OpenCensus tracing component. */
private final Tracer tracer = OpenCensusUtils.getTracer();

/**
* Determines whether {@link HttpResponse#getContent()} of this request should return raw input
* stream or not.
Expand Down Expand Up @@ -835,7 +843,13 @@ public HttpResponse execute() throws IOException {
Preconditions.checkNotNull(requestMethod);
Preconditions.checkNotNull(url);

Span span =
tracer
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
.setRecordEvents(OpenCensusUtils.isRecordEvent())
.startSpan();
do {
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
// Cleanup any unneeded response from a previous iteration
if (response != null) {
response.ignore();
Expand All @@ -850,6 +864,10 @@ public HttpResponse execute() throws IOException {
}
// build low-level HTTP request
String urlString = url.build();
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_METHOD, requestMethod);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_HOST, url.getHost());
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_PATH, url.getRawPath());
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_URL, urlString);

LowLevelHttpRequest lowLevelHttpRequest = transport.buildRequest(requestMethod, urlString);
Logger logger = HttpTransport.LOGGER;
Expand Down Expand Up @@ -879,11 +897,14 @@ public HttpResponse execute() throws IOException {
if (!suppressUserAgentSuffix) {
if (originalUserAgent == null) {
headers.setUserAgent(USER_AGENT_SUFFIX);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, USER_AGENT_SUFFIX);
} else {
String newUserAgent = originalUserAgent + " " + USER_AGENT_SUFFIX;
headers.setUserAgent(newUserAgent);
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, newUserAgent);
}
}
OpenCensusUtils.propagateTracingContext(span, headers);

// headers
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
Expand Down Expand Up @@ -967,8 +988,15 @@ public HttpResponse execute() throws IOException {
lowLevelHttpRequest.setTimeout(connectTimeout, readTimeout);
lowLevelHttpRequest.setWriteTimeout(writeTimeout);

// switch tracing scope to current span
@SuppressWarnings("MustBeClosedChecker")
Scope ws = tracer.withSpan(span);
OpenCensusUtils.recordSentMessageEvent(span, lowLevelHttpRequest.getContentLength());
try {
LowLevelHttpResponse lowLevelHttpResponse = lowLevelHttpRequest.execute();
if (lowLevelHttpResponse != null) {
OpenCensusUtils.recordReceivedMessageEvent(span, lowLevelHttpResponse.getContentLength());
}
// Flag used to indicate if an exception is thrown before the response is constructed.
boolean responseConstructed = false;
try {
Expand All @@ -986,13 +1014,17 @@ public HttpResponse execute() throws IOException {
if (!retryOnExecuteIOException
&& (ioExceptionHandler == null
|| !ioExceptionHandler.handleIOException(this, retryRequest))) {
// static analysis shows response is always null here
span.end(OpenCensusUtils.getEndSpanOptions(null));
throw e;
}
// Save the exception in case the retries do not work and we need to re-throw it later.
executeException = e;
if (loggable) {
logger.log(Level.WARNING, "exception thrown while executing request", e);
}
} finally {
ws.close();
}

// Flag used to indicate if an exception is thrown before the response has completed
Expand Down Expand Up @@ -1049,6 +1081,8 @@ public HttpResponse execute() throws IOException {
}
}
} while (retryRequest);
span.end(OpenCensusUtils.getEndSpanOptions(response == null ? null : response.getStatusCode()));

if (response == null) {
// Retries did not help resolve the execute exception, re-throw it.
throw executeException;
Expand Down Expand Up @@ -1163,6 +1197,12 @@ public HttpRequest setSleeper(Sleeper sleeper) {
return this;
}

private static void addSpanAttribute(Span span, String key, String value) {
if (value != null) {
span.putAttribute(key, AttributeValue.stringAttributeValue(value));
}
}

private static String getVersion() {
String version = HttpRequest.class.getPackage().getImplementationVersion();
// in a non-packaged environment (local), there's no implementation version to read
Expand Down
Loading

0 comments on commit ea09fd5

Please sign in to comment.