8000 Revert "Enhance OpenCensus tracing instrumentation." · rchani99/google-http-java-client@36280df · GitHub
[go: up one dir, main page]

Skip to content

Commit 36280df

Browse files
committed
Revert "Enhance OpenCensus tracing instrumentation."
This reverts commit d93eb99.
1 parent 35d2a00 commit 36280df

File tree

3 files changed

+80
-230
lines changed

3 files changed

+80
-230
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
218218
/** OpenCensus tracing component. */
219219
private Tracer tracer = OpenCensusUtils.getTracer();
220220

221+
/** Prefix for tracing span name. */
222+
private static final String traceSpanNamePrefix = "Sent." + HttpRequest.class.getName() + ".";
223+
221224
/**
222225
* @param transport HTTP transport
223226
* @param requestMethod HTTP request method or {@code null} for none
@@ -862,12 +865,9 @@ public HttpResponse execute() throws IOException {
862865
Preconditions.checkNotNull(requestMethod);
863866
Preconditions.checkNotNull(url);
864867

865-
Span span = tracer
866-
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
867-
.setRecordEvents(OpenCensusUtils.isRecordEvent())
868-
.startSpan();
868+
Span span = tracer.spanBuilder(traceSpanNamePrefix + "execute").startSpan();
869869
do {
870-
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
870+
span.addAnnotation("retry #" + numRetries);
871871
// Cleanup any unneeded response from a previous iteration
872872
if (response != null) {
873873
response.ignore();
@@ -911,7 +911,7 @@ public HttpResponse execute() throws IOException {
911911
headers.setUserAgent(originalUserAgent + " " + USER_AGENT_SUFFIX);
912912
}
913913
}
914-
OpenCensusUtils.propagateTracingContext(span, headers);
914+
OpenCensusUtils.propagateTracingContext(headers);
915915

916916
// headers
917917
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
@@ -995,12 +995,8 @@ public HttpResponse execute() throws IOException {
995995
// switch tracing scope to current span
996996
@SuppressWarnings("MustBeClosedChecker")
997997
Scope ws = tracer.withSpan(span);
998-
OpenCensusUtils.recordSentMessageEvent(span, lowLevelHttpRequest.getContentLength());
999998
try {
1000999
LowLevelHttpResponse lowLevelHttpResponse = lowLevelHttpRequest.execute();
1001-
if (lowLevelHttpResponse != null) {
1002-
OpenCensusUtils.recordReceivedMessageEvent(span, lowLevelHttpResponse.getContentLength());
1003-
}
10041000
// Flag used to indicate if an exception is thrown before the response is constructed.
10051001
boolean responseConstructed = false;
10061002
try {

google-http-client/src/main/java/com/google/api/client/util/OpenCensusUtils.java

Lines changed: 16 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,53 @@
1515
package com.google.api.client.util;
1616

1717
import com.google.api.client.http.HttpHeaders;
18-
import com.google.api.client.http.HttpRequest;
18+
import com.google.api.client.http.HttpResponse;
1919
import com.google.api.client.http.HttpStatusCodes;
20-
import com.google.common.annotations.VisibleForTesting;
2120

2221
import io.opencensus.contrib.http.util.HttpPropagationUtil;
2322
import io.opencensus.trace.BlankSpan;
2423
import io.opencensus.trace.EndSpanOptions;
25-
import io.opencensus.trace.NetworkEvent;
26-
import io.opencensus.trace.NetworkEvent.Type;
2724
import io.opencensus.trace.Span;
2825
import io.opencensus.trace.Status;
2926
import io.opencensus.trace.Tracer;
3027
import io.opencensus.trace.Tracing;
3128
import io.opencensus.trace.propagation.TextFormat;
3229

33-
import java.util.Collections;
30+
import java.io.IOException;
3431
import java.util.logging.Level;
3532
import java.util.logging.Logger;
3633
import javax.annotation.Nullable;
3734

3835
/**
3936
* Utilities for Census monitoring and tracing.
4037
*
41-
* @author Hailong Wen
4238
* @since 1.24
39+
* @author Hailong Wen
4340
*/
4441
public class OpenCensusUtils {
4542

46-
private static final Logger logger = Logger.getLogger(OpenCensusUtils.class.getName());
47-
48-
/**
49-
* Span name for tracing {@link HttpRequest#execute()}.
50-
*/
51-
public static final String SPAN_NAME_HTTP_REQUEST_EXECUTE =
52-
"Sent." + HttpRequest.class.getName() + ".execute";
53-
54-
/**
55-
* OpenCensus tracing component. When no OpenCensus implementation is provided, it will return a
56-
* no-op tracer.
57-
*/
58-
private static Tracer tracer = Tracing.getTracer();
59-
60-
/**
61-
* Sequence id generator for message event.
62-
*/
63-
private static AtomicLong idGenerator = new AtomicLong();
43+
private static final Logger LOGGER = Logger.getLogger(OpenCensusUtils.class.getName());
6444

6545
/**
66-
* Whether spans should be recorded locally. Defaults to true.
46+
* OpenCensus tracing component.
47+
* When no OpenCensus implementation is provided, it will return a no-op tracer.
6748
*/
68-
private static volatile boolean isRecordEvent = true;
49+
static Tracer tracer = Tracing.getTracer();
6950

7051
/**
7152
* {@link TextFormat} used in tracing context propagation.
7253
*/
7354
@Nullable
74-
@VisibleForTesting
75-
static volatile TextFormat propagationTextFormat = null;
55+
static TextFormat propagationTextFormat = null;
7656

7757
/**
78-
* {@link TextFormat.Setter} for {@link #propagationTextFormat}.
58+
* {@link TextFormat.Setter} for {@link activeTextFormat}.
7959
*/
8060
@Nullable
81-
@VisibleForTesting
82-
static volatile TextFormat.Setter propagationTextFormatSetter = null;
61+
static TextFormat.Setter propagationTextFormatSetter = null;
8362

8463
/**
8564
* Sets the {@link TextFormat} used in context propagation.
86-
*
87-
* <p>This API allows users of google-http-client to specify other text format, or disable context
88-
* propagation by setting it to {@code null}. It should be used along with {@link
89-
* #setPropagationTextFormatSetter} for setting purpose. </p>
90-
*
9165
* @param textFormat the text format.
9266
*/
9367
public static void setPropagationTextFormat(@Nullable TextFormat textFormat) {
@@ -96,28 +70,12 @@ public static void setPropagationTextFormat(@Nullable TextFormat textFormat) {
9670

9771
/**
9872
* Sets the {@link TextFormat.Setter} used in context propagation.
99-
*
100-
* <p>This API allows users of google-http-client to specify other text format setter, or disable
101-
* context propagation by setting it to {@code null}. It should be used along with {@link
102-
* #setPropagationTextFormat} for setting purpose. </p>
103-
*
10473
* @param textFormatSetter the {@code TextFormat.Setter} for the text format.
10574
*/
10675
public static void setPropagationTextFormatSetter(@Nullable TextFormat.Setter textFormatSetter) {
10776
propagationTextFormatSetter = textFormatSetter;
10877
}
10978

110-
/**
111-
* Sets whether spans should be recorded locally.
112-
*
113-
* <p> This API allows users of google-http-client to turn on/off local span collection. </p>
114-
*
115-
* @param recordEvent record span locally if true.
116-
*/
117-
public static void setIsRecordEvent(boolean recordEvent) {
118-
isRecordEvent = recordEvent;
119-
}
120-
12179
/**
12280
* Returns the tracing component of OpenCensus.
12381
*
@@ -127,27 +85,15 @@ public static Tracer getTracer() {
12785
return tracer;
12886
}
12987

130-
/**
131-
* Returns whether spans should be recorded locally.
132-
*
133-
* @return whether spans should be recorded locally.
134-
*/
135-
public static boolean isRecordEvent() {
136-
return isRecordEvent;
137-
}
138-
13988
/**
14089
* Propagate information of current tracing context. This information will be injected into HTTP
14190
* header.
142-
*
143-
* @param span the span to be propagated.
144-
* @param headers the headers used in propagation.
14591
*/
146-
public static void propagateTracingContext(Span span, HttpHeaders headers) {
147-
Preconditions.checkArgument(span != null, "span should not be null.");
148-
Preconditions.checkArgument(headers != null, "headers should not be null.");
92+
public static void propagateTracingContext(HttpHeaders headers) {
93+
Preconditions.checkNotNull(headers);
14994
if (propagationTextFormat != null && propagationTextFormatSetter != null) {
150-
if (!span.equals(BlankSpan.INSTANCE)) {
95+
Span span = tracer.getCurrentSpan();
96+
if (span != null && !span.equals(BlankSpan.INSTANCE)) {
15197
propagationTextFormat.inject(span.getContext(), headers, propagationTextFormatSetter);
15298
}
15399
}
@@ -161,7 +107,7 @@ public static void propagateTracingContext(Span span, HttpHeaders headers) {
161107
*/
162108
public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
163109
// Always sample the span, but optionally export it.
164-
EndSpanOptions.Builder builder = EndSpanOptions.builder();
110+
EndSpanOptions.Builder builder = EndSpanOptions.builder().setSampleToLocalSpanStore(true);
165111
if (statusCode == null) {
166112
builder.setStatus(Status.UNKNOWN);
167113
} else if (!HttpStatusCodes.isSuccess(statusCode)) {
@@ -193,49 +139,6 @@ public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
193139
return builder.build();
194140
}
195141

196-
/**
197-
* Records a new message event which contains the size of the request content. Note that the size
198-
* represents the message size in application layer, i.e., content-length.
199-
*
200-
* @param span The {@code span} in which the send event occurs.
201-
* @param size Size of the request.
202-
*/
203-
public static void recordSentMessageEvent(Span span, long size) {
204-
recordMessageEvent(span, size, Type.SENT);
205-
}
206-
207-
/**
208-
* Records a new message event which contains the size of the response content. Note that the size
209-
* represents the message size in application layer, i.e., content-length.
210-
*
211-
* @param span The {@code span} in which the receive event occurs.
212-
* @param size Size of the response.
213-
*/
214-
public static void recordReceivedMessageEvent(Span span, long size) {
215-
recordMessageEvent(span, size, Type.RECV);
216-
}
217-
218-
/**
219-
* Records a message event of a certain {@link NetowrkEvent.Type}. This method is package
220-
* protected since {@link NetworkEvent} might be deprecated in future releases.
221-
*
222-
* @param span The {@code span} in which the event occurs.
223-
* @param size Size of the message.
224-
* @param eventType The {@code NetworkEvent.Type} of the message event.
225-
*/
226-
@VisibleForTesting
227-
static void recordMessageEvent(Span span, long size, Type eventType) {
228-
Preconditions.checkArgument(span != null, "span should not be null.");
229-
if (size < 0) {
230-
size = 0;
231-
}
232-
NetworkEvent event = NetworkEvent
233-
.builder(eventType, idGenerator.getAndIncrement())
234-
.setUncompressedMessageSize(size)
235-
.build();
236-
span.addNetworkEvent(event);
237-
}
238-
239142
static {
240143
try {
241144
propagationTextFormat = HttpPropagationUtil.getCloudTraceFormat();
@@ -246,16 +149,7 @@ public void put(HttpHeaders carrier, String key, String value) {
246149
}
247150
};
248151
} catch (Exception e) {
249-
logger.log(
250-
Level.WARNING, "Cannot initialize default OpenCensus HTTP propagation text format.", e);
251-
}
252-
253-
try {
254-
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(
255-
Collections.<String>singletonList(SPAN_NAME_HTTP_REQUEST_EXECUTE));
256-
} catch (Exception e) {
257-
logger.log(
258-
Level.WARNING, "Cannot register default OpenCensus span names for collection.", e);
152+
LOGGER.log(Level.WARNING, "Cannot initiate OpenCensus modules, tracing disabled", e);
259153
}
260154
}
261155

0 commit comments

Comments
 (0)
0