Skip to content

Commit 35d2a00

Browse files
committed
Revert "Use separate id generator; rename attribute name."
This reverts commit 86bc0ae.
1 parent c0502b0 commit 35d2a00

File tree

3 files changed

+131
-108
lines changed

3 files changed

+131
-108
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525
import com.google.api.client.util.StringUtils;
2626

2727
import io.opencensus.common.Scope;
28-
import io.opencensus.trace.Annotation;
29-
import io.opencensus.trace.AttributeValue;
3028
import io.opencensus.trace.Span;
3129
import io.opencensus.trace.Tracer;
3230

3331
import java.io.IOException;
3432
import java.io.InputStream;
35-
import java.util.Collections;
3633
import java.util.concurrent.Callable;
3734
import java.util.concurrent.Executor;
3835
import java.util.concurrent.Executors;
@@ -869,16 +866,8 @@ public HttpResponse execute() throws IOException {
869866
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
870867
.setRecordEvents(OpenCensusUtils.isRecordEvent())
871868
.startSpan();
872-
long sentIdGenerator = 0L;
873-
long recvIdGenerator = 0L;
874-
875869
do {
876-
span.addAnnotation(
877-
Annotation.fromDescriptionAndAttributes(
878-
"retry",
879-
Collections.<String, AttributeValue>singletonMap(
880-
"number",
881-
AttributeValue.longAttributeValue(numRetries - retriesRemaining))));
870+
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
882871
// Cleanup any unneeded response from a previous iteration
883872
if (response != null) {
884873
response.ignore();
@@ -922,7 +911,7 @@ public HttpResponse execute() throws IOException {
922911
headers.setUserAgent(originalUserAgent + " " + USER_AGENT_SUFFIX);
923912
}
924913
}
925-
OpenCensusUtils.propagateTracingContext(span.getContext(), headers);
914+
OpenCensusUtils.propagateTracingContext(span, headers);
926915

927916
// headers
928917
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
@@ -1006,13 +995,11 @@ public HttpResponse execute() throws IOException {
1006995
// switch tracing scope to current span
1007996
@SuppressWarnings("MustBeClosedChecker")
1008997
Scope ws = tracer.withSpan(span);
1009-
OpenCensusUtils.recordSentMessageEvent(
1010-
span, sentIdGenerator++, lowLevelHttpRequest.getContentLength());
998+
OpenCensusUtils.recordSentMessageEvent(span, lowLevelHttpRequest.getContentLength());
1011999
try {
10121000
LowLevelHttpResponse lowLevelHttpResponse = lowLevelHttpRequest.execute();
10131001
if (lowLevelHttpResponse != null) {
1014-
OpenCensusUtils.recordReceivedMessageEvent(
1015-
span, recvIdGenerator++, lowLevelHttpResponse.getContentLength());
1002+
OpenCensusUtils.recordReceivedMessageEvent(span, lowLevelHttpResponse.getContentLength());
10161003
}
10171004
// Flag used to indicate if an exception is thrown before the response is constructed.
10181005
boolean responseConstructed = false;

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import com.google.common.annotations.VisibleForTesting;
2121

2222
import io.opencensus.contrib.http.util.HttpPropagationUtil;
23+
import io.opencensus.trace.BlankSpan;
2324
import io.opencensus.trace.EndSpanOptions;
2425
import io.opencensus.trace.NetworkEvent;
2526
import io.opencensus.trace.NetworkEvent.Type;
2627
import io.opencensus.trace.Span;
27-
import io.opencensus.trace.SpanContext;
2828
import io.opencensus.trace.Status;
2929
import io.opencensus.trace.Tracer;
3030
import io.opencensus.trace.Tracing;
@@ -57,6 +57,11 @@ public class OpenCensusUtils {
5757
*/
5858
private static Tracer tracer = Tracing.getTracer();
5959

60+
/**
61+
* Sequence id generator for message event.
62+
*/
63+
private static AtomicLong idGenerator = new AtomicLong();
64+
6065
/**
6166
* Whether spans should be recorded locally. Defaults to true.
6267
*/
@@ -132,18 +137,18 @@ public static boolean isRecordEvent() {
132137
}
133138

134139
/**
135-
* Propagate information of a given tracing context. This information will be injected into HTTP
140+
* Propagate information of current tracing context. This information will be injected into HTTP
136141
* header.
137142
*
138-
* @param spanContext the spanContext to be propagated.
143+
* @param span the span to be propagated.
139144
* @param headers the headers used in propagation.
140145
*/
141-
public static void propagateTracingContext(SpanContext spanContext, HttpHeaders headers) {
142-
Preconditions.checkArgument(spanContext != null, "spanContext should not be null.");
146+
public static void propagateTracingContext(Span span, HttpHeaders headers) {
147+
Preconditions.checkArgument(span != null, "span should not be null.");
143148
Preconditions.checkArgument(headers != null, "headers should not be null.");
144149
if (propagationTextFormat != null && propagationTextFormatSetter != null) {
145-
if (!spanContext.equals(SpanContext.INVALID)) {
146-
propagationTextFormat.inject(spanContext, headers, propagationTextFormatSetter);
150+
if (!span.equals(BlankSpan.INSTANCE)) {
151+
propagationTextFormat.inject(span.getContext(), headers, propagationTextFormatSetter);
147152
}
148153
}
149154
}
@@ -193,42 +198,39 @@ public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
193198
* represents the message size in application layer, i.e., content-length.
194199
*
195200
* @param span The {@code span} in which the send event occurs.
196-
* @param id The id for the message, It is unique within an {@link HttpRequest}.
197201
* @param size Size of the request.
198202
*/
199-
public static void recordSentMessageEvent(Span span, long id, long size) {
200-
recordMessageEvent(span, id, size, Type.SENT);
203+
public static void recordSentMessageEvent(Span span, long size) {
204+
recordMessageEvent(span, size, Type.SENT);
201205
}
202206

203207
/**
204208
* Records a new message event which contains the size of the response content. Note that the size
205209
* represents the message size in application layer, i.e., content-length.
206210
*
207211
* @param span The {@code span} in which the receive event occurs.
208-
* @param id The id for the message. It is unique within an {@link HttpRequest}.
209212
* @param size Size of the response.
210213
*/
211-
public static void recordReceivedMessageEvent(Span span, long id, long size) {
212-
recordMessageEvent(span, id, size, Type.RECV);
214+
public static void recordReceivedMessageEvent(Span span, long size) {
215+
recordMessageEvent(span, size, Type.RECV);
213216
}
214217

215218
/**
216219
* Records a message event of a certain {@link NetowrkEvent.Type}. This method is package
217220
* protected since {@link NetworkEvent} might be deprecated in future releases.
218221
*
219222
* @param span The {@code span} in which the event occurs.
220-
* @param id The id for the message.
221223
* @param size Size of the message.
222224
* @param eventType The {@code NetworkEvent.Type} of the message event.
223225
*/
224226
@VisibleForTesting
225-
static void recordMessageEvent(Span span, long id, long size, Type eventType) {
227+
static void recordMessageEvent(Span span, long size, Type eventType) {
226228
Preconditions.checkArgument(span != null, "span should not be null.");
227229
if (size < 0) {
228230
size = 0;
229231
}
230232
NetworkEvent event = NetworkEvent
231-
.builder(eventType, id)
233+
.builder(eventType, idGenerator.getAndIncrement())
232234
.setUncompressedMessageSize(size)
233235
.build();
234236
span.addNetworkEvent(event);

0 commit comments

Comments
 (0)