Skip to content

Commit

Permalink
Revert "Use separate id generator; rename attribute name."
Browse files Browse the repository at this point in the history
This reverts commit 86bc0ae.
  • Loading branch information
jeanbza committed Jun 25, 2018
1 parent c0502b0 commit 35d2a00
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
import com.google.api.client.util.StringUtils;

import io.opencensus.common.Scope;
import io.opencensus.trace.Annotation;
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.Collections;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -869,16 +866,8 @@ public HttpResponse execute() throws IOException {
.spanBuilder(OpenCensusUtils.SPAN_NAME_HTTP_REQUEST_EXECUTE)
.setRecordEvents(OpenCensusUtils.isRecordEvent())
.startSpan();
long sentIdGenerator = 0L;
long recvIdGenerator = 0L;

do {
span.addAnnotation(
Annotation.fromDescriptionAndAttributes(
"retry",
Collections.<String, AttributeValue>singletonMap(
"number",
AttributeValue.longAttributeValue(numRetries - retriesRemaining))));
span.addAnnotation("retry #" + (numRetries - retriesRemaining));
// Cleanup any unneeded response from a previous iteration
if (response != null) {
response.ignore();
Expand Down Expand Up @@ -922,7 +911,7 @@ public HttpResponse execute() throws IOException {
headers.setUserAgent(originalUserAgent + " " + USER_AGENT_SUFFIX);
}
}
OpenCensusUtils.propagateTracingContext(span.getContext(), headers);
OpenCensusUtils.propagateTracingContext(span, headers);

// headers
HttpHeaders.serializeHeaders(headers, logbuf, curlbuf, logger, lowLevelHttpRequest);
Expand Down Expand Up @@ -1006,13 +995,11 @@ public HttpResponse execute() throws IOException {
// switch tracing scope to current span
@SuppressWarnings("MustBeClosedChecker")
Scope ws = tracer.withSpan(span);
OpenCensusUtils.recordSentMessageEvent(
span, sentIdGenerator++, lowLevelHttpRequest.getContentLength());
OpenCensusUtils.recordSentMessageEvent(span, lowLevelHttpRequest.getContentLength());
try {
LowLevelHttpResponse lowLevelHttpResponse = lowLevelHttpRequest.execute();
if (lowLevelHttpResponse != null) {
OpenCensusUtils.recordReceivedMessageEvent(
span, recvIdGenerator++, lowLevelHttpResponse.getContentLength());
OpenCensusUtils.recordReceivedMessageEvent(span, lowLevelHttpResponse.getContentLength());
}
// Flag used to indicate if an exception is thrown before the response is constructed.
boolean responseConstructed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import com.google.common.annotations.VisibleForTesting;

import io.opencensus.contrib.http.util.HttpPropagationUtil;
import io.opencensus.trace.BlankSpan;
import io.opencensus.trace.EndSpanOptions;
import io.opencensus.trace.NetworkEvent;
import io.opencensus.trace.NetworkEvent.Type;
import io.opencensus.trace.Span;
import io.opencensus.trace.SpanContext;
import io.opencensus.trace.Status;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
Expand Down Expand Up @@ -57,6 +57,11 @@ public class OpenCensusUtils {
*/
private static Tracer tracer = Tracing.getTracer();

/**
* Sequence id generator for message event.
*/
private static AtomicLong idGenerator = new AtomicLong();

/**
* Whether spans should be recorded locally. Defaults to true.
*/
Expand Down Expand Up @@ -132,18 +137,18 @@ public static boolean isRecordEvent() {
}

/**
* Propagate information of a given tracing context. This information will be injected into HTTP
* Propagate information of current tracing context. This information will be injected into HTTP
* header.
*
* @param spanContext the spanContext to be propagated.
* @param span the span to be propagated.
* @param headers the headers used in propagation.
*/
public static void propagateTracingContext(SpanContext spanContext, HttpHeaders headers) {
Preconditions.checkArgument(spanContext != null, "spanContext should not be null.");
public static void propagateTracingContext(Span span, HttpHeaders headers) {
Preconditions.checkArgument(span != null, "span should not be null.");
Preconditions.checkArgument(headers != null, "headers should not be null.");
if (propagationTextFormat != null && propagationTextFormatSetter != null) {
if (!spanContext.equals(SpanContext.INVALID)) {
propagationTextFormat.inject(spanContext, headers, propagationTextFormatSetter);
if (!span.equals(BlankSpan.INSTANCE)) {
propagationTextFormat.inject(span.getContext(), headers, propagationTextFormatSetter);
}
}
}
Expand Down Expand Up @@ -193,42 +198,39 @@ public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
* represents the message size in application layer, i.e., content-length.
*
* @param span The {@code span} in which the send event occurs.
* @param id The id for the message, It is unique within an {@link HttpRequest}.
* @param size Size of the request.
*/
public static void recordSentMessageEvent(Span span, long id, long size) {
recordMessageEvent(span, id, size, Type.SENT);
public static void recordSentMessageEvent(Span span, long size) {
recordMessageEvent(span, size, Type.SENT);
}

/**
* Records a new message event which contains the size of the response content. Note that the size
* represents the message size in application layer, i.e., content-length.
*
* @param span The {@code span} in which the receive event occurs.
* @param id The id for the message. It is unique within an {@link HttpRequest}.
* @param size Size of the response.
*/
public static void recordReceivedMessageEvent(Span span, long id, long size) {
recordMessageEvent(span, id, size, Type.RECV);
public static void recordReceivedMessageEvent(Span span, long size) {
recordMessageEvent(span, size, Type.RECV);
}

/**
* Records a message event of a certain {@link NetowrkEvent.Type}. This method is package
* protected since {@link NetworkEvent} might be deprecated in future releases.
*
* @param span The {@code span} in which the event occurs.
* @param id The id for the message.
* @param size Size of the message.
* @param eventType The {@code NetworkEvent.Type} of the message event.
*/
@VisibleForTesting
static void recordMessageEvent(Span span, long id, long size, Type eventType) {
static void recordMessageEvent(Span span, long size, Type eventType) {
Preconditions.checkArgument(span != null, "span should not be null.");
if (size < 0) {
size = 0;
}
NetworkEvent event = NetworkEvent
.builder(eventType, id)
.builder(eventType, idGenerator.getAndIncrement())
.setUncompressedMessageSize(size)
.build();
span.addNetworkEvent(event);
Expand Down
Loading

0 comments on commit 35d2a00

Please sign in to comment.