Skip to content

Commit ea09fd5

Browse files
authored
revert: "feat: remove opencensus (#834)" (#837)
This reverts commit f16c777.
1 parent f16c777 commit ea09fd5

File tree

6 files changed

+744
-0
lines changed

6 files changed

+744
-0
lines changed

google-http-client/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
</description>
1717

1818
<build>
19+
<pluginManagement>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-dependency-plugin</artifactId>
24+
<version>3.1.1</version>
25+
<configuration>
26+
<ignoredUnusedDeclaredDependencies>io.opencensus:opencensus-impl</ignoredUnusedDeclaredDependencies>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</pluginManagement>
1931
<plugins>
2032
<plugin>
2133
<groupId>org.apache.maven.plugins</groupId>
@@ -97,6 +109,10 @@
97109
<groupId>org.apache.httpcomponents</groupId>
98110
<artifactId>httpcore</artifactId>
99111
</dependency>
112+
<dependency>
113+
<groupId>com.google.code.findbugs</groupId>
114+
<artifactId>jsr305</artifactId>
115+
</dependency>
100116
<dependency>
101117
<groupId>com.google.guava</groupId>
102118
<artifactId>guava</artifactId>
@@ -105,6 +121,14 @@
105121
<groupId>com.google.j2objc</groupId>
106122
<artifactId>j2objc-annotations</artifactId>
107123
</dependency>
124+
<dependency>
125+
<groupId>io.opencensus</groupId>
126+
<artifactId>opencensus-api</artifactId>
127+
</dependency>
128+
<dependency>
129+
<groupId>io.opencensus</groupId>
130+
<artifactId>opencensus-contrib-http-util</artifactId>
131+
</dependency>
108132

109133
<dependency>
110134
<groupId>com.google.guava</groupId>
@@ -126,5 +150,15 @@
126150
<artifactId>mockito-all</artifactId>
127151
<scope>test</scope>
128152
</dependency>
153+
<dependency>
154+
<groupId>io.opencensus</groupId>
155+
<artifactId>opencensus-impl</artifactId>
156+
<scope>test</scope>
157+
</dependency>
158+
<dependency>
159+
<groupId>io.opencensus</groupId>
160+
<artifactId>opencensus-testing</artifactId>
161+
<scope>test</scope>
162+
</dependency>
129163
</dependencies>
130164
</project>

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import com.google.api.client.util.StreamingContent;
2424
import com.google.api.client.util.StringUtils;
2525
import com.google.common.util.concurrent.ThreadFactoryBuilder;
26+
import io.opencensus.common.Scope;
27+
import io.opencensus.contrib.http.util.HttpTraceAttributeConstants;
28+
import io.opencensus.trace.AttributeValue;
29+
import io.opencensus.trace.Span;
30+
import io.opencensus.trace.Tracer;
2631
import java.io.IOException;
2732
import java.io.InputStream;
2833
import java.util.Properties;
@@ -192,6 +197,9 @@ public final class HttpRequest {
192197
/** Sleeper. */
193198
private Sleeper sleeper = Sleeper.DEFAULT;
194199

200+
/** OpenCensus tracing component. */
201+
private final Tracer tracer = OpenCensusUtils.getTracer();
202+
195203
/**
196204
* Determines whether {@link HttpResponse#getContent()} of this request should return raw input
197205
* stream or not.
@@ -835,7 +843,13 @@ public HttpResponse execute() throws IOException {
835843
Preconditions.checkNotNull(requestMethod);
836844
Preconditions.checkNotNull(url);
837845

846+
Span span =
847+
tracer
848+
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
849+
.setRecordEvents(OpenCensusUtils.isRecordEvent())
850+
.startSpan();
838851
do {
852+
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
839853
// Cleanup any unneeded response from a previous iteration
840854
if (response != null) {
841855
response.ignore();
@@ -850,6 +864,10 @@ public HttpResponse execute() throws IOException {
850864
}
851865
// build low-level HTTP request
852866
String urlString = url.build();
867+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_METHOD, requestMethod);
868+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_HOST, url.getHost());
869+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_PATH, url.getRawPath());
870+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_URL, urlString);
853871

854872
LowLevelHttpRequest lowLevelHttpRequest = transport.buildRequest(requestMethod, urlString);
855873
Logger logger = HttpTransport.LOGGER;
@@ -879,11 +897,14 @@ public HttpResponse execute() throws IOException {
879897
if (!suppressUserAgentSuffix) {
880898
if (originalUserAgent == null) {
881899
headers.setUserAgent(USER_AGENT_SUFFIX);
900+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, USER_AGENT_SUFFIX);
882901
} else {
883902
String newUserAgent = originalUserAgent + " " + USER_AGENT_SUFFIX;
884903
headers.setUserAgent(newUserAgent);
904+
addSpanAttribute(span, HttpTraceAttributeConstants.HTTP_USER_AGENT, newUserAgent);
885905
}
886906
}
907+
OpenCensusUtils.propagateTracingContext(span, headers);
887908

888909
// headers
889910
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
@@ -967,8 +988,15 @@ public HttpResponse execute() throws IOException {
967988
lowLevelHttpRequest.setTimeout(connectTimeout, readTimeout);
968989
lowLevelHttpRequest.setWriteTimeout(writeTimeout);
969990

991+
// switch tracing scope to current span
992+
@SuppressWarnings("MustBeClosedChecker")
993+
Scope ws = tracer.withSpan(span);
994+
OpenCensusUtils.recordSentMessageEvent(span, lowLevelHttpRequest.getContentLength());
970995
try {
971996
LowLevelHttpResponse lowLevelHttpResponse = lowLevelHttpRequest.execute();
997+
if (lowLevelHttpResponse != null) {
998+
OpenCensusUtils.recordReceivedMessageEvent(span, lowLevelHttpResponse.getContentLength());
999+
}
9721000
// Flag used to indicate if an exception is thrown before the response is constructed.
9731001
boolean responseConstructed = false;
9741002
try {
@@ -986,13 +1014,17 @@ public HttpResponse execute() throws IOException {
9861014
if (!retryOnExecuteIOException
9871015
&& (ioExceptionHandler == null
9881016
|| !ioExceptionHandler.handleIOException(this, retryRequest))) {
1017+
// static analysis shows response is always null here
1018+
span.end(OpenCensusUtils.getEndSpanOptions(null));
9891019
throw e;
9901020
}
9911021
// Save the exception in case the retries do not work and we need to re-throw it later.
9921022
executeException = e;
9931023
if (loggable) {
9941024
logger.log(Level.WARNING, "exception thrown while executing request", e);
9951025
}
1026+
} finally {
1027+
ws.close();
9961028
}
9971029

9981030
// Flag used to indicate if an exception is thrown before the response has completed
@@ -1049,6 +1081,8 @@ public HttpResponse execute() throws IOException {
10491081
}
10501082
}
10511083
} while (retryRequest);
1084+
span.end(OpenCensusUtils.getEndSpanOptions(response == null ? null : response.getStatusCode()));
1085+
10521086
if (response == null) {
10531087
// Retries did not help resolve the execute exception, re-throw it.
10541088
throw executeException;
@@ -1163,6 +1197,12 @@ public HttpRequest setSleeper(Sleeper sleeper) {
11631197
return this;
11641198
}
11651199

1200+
private static void addSpanAttribute(Span span, String key, String value) {
1201+
if (value != null) {
1202+
span.putAttribute(key, AttributeValue.stringAttributeValue(value));
1203+
}
1204+
}
1205+
11661206
private static String getVersion() {
11671207
String version = HttpRequest.class.getPackage().getImplementationVersion();
11681208
// in a non-packaged environment (local), there's no implementation version to read

0 commit comments

Comments
 (0)