Skip to content

Commit

Permalink
Map http attributes to the stackdriver format, resolves census-instru…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebruck committed May 7, 2018
1 parent 1513015 commit a2ea0b7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.trace.v2.TraceServiceClient;
import com.google.cloud.trace.v2.TraceServiceSettings;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import com.google.devtools.cloudtrace.v2.AttributeValue;
import com.google.devtools.cloudtrace.v2.ProjectName;
Expand Down Expand Up @@ -77,6 +78,16 @@ final class StackdriverV2ExporterHandler extends SpanExporter.Handler {
.setStringValue(toTruncatableStringProto(AGENT_LABEL_VALUE_STRING))
.build();

private static final ImmutableMap<String, String> HTTP_ATTRIBUTE_MAPPING =
ImmutableMap.<String, String>builder()
.put("http.host", "/http/host")
.put("http.method", "/http/method")
.put("http.path", "/http/path")
.put("http.route", "/http/route")
.put("http.user_agent", "/http/user_agent")
.put("http.status_code", "/http/status_code")
.build();

private final String projectId;
private final TraceServiceClient traceServiceClient;
private final ProjectName projectName;
Expand Down Expand Up @@ -222,11 +233,20 @@ private static Attributes.Builder toAttributesBuilderProto(
Attributes.Builder attributesBuilder =
Attributes.newBuilder().setDroppedAttributesCount(droppedAttributesCount);
for (Map.Entry<String, io.opencensus.trace.AttributeValue> label : attributes.entrySet()) {
attributesBuilder.putAttributeMap(label.getKey(), toAttributeValueProto(label.getValue()));
attributesBuilder.putAttributeMap(
mapKey(label.getKey()), toAttributeValueProto(label.getValue()));
}
return attributesBuilder;
}

private static String mapKey(String key) {
if (HTTP_ATTRIBUTE_MAPPING.containsKey(key)) {
return HTTP_ATTRIBUTE_MAPPING.get(key);
} else {
return key;
}
}

private static Status toStatusProto(io.opencensus.trace.Status status) {
Status.Builder statusBuilder = Status.newBuilder().setCode(status.getCanonicalCode().value());
if (status.getDescription() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

@RunWith(JUnit4.class)
public final class StackdriverV2ExporterHandlerProtoTest {

private static final Credentials FAKE_CREDENTIALS =
GoogleCredentials.newBuilder().setAccessToken(new AccessToken("fake", new Date(100))).build();
// OpenCensus constants
Expand Down Expand Up @@ -141,7 +142,6 @@ public void generateSpan() {
CHILD_SPAN_COUNT,
status,
endTimestamp);

TimeEvent annotationTimeEvent1 =
TimeEvent.newBuilder()
.setAnnotation(
Expand Down Expand Up @@ -255,4 +255,55 @@ public void generateSpan() {
assertThat(span.getChildSpanCount())
.isEqualTo(Int32Value.newBuilder().setValue(CHILD_SPAN_COUNT).build());
}

@Test
public void mapHttpAttributes() {
Map<String, io.opencensus.trace.AttributeValue> attributesMap =
new HashMap<String, io.opencensus.trace.AttributeValue>();

attributesMap.put("http.host", io.opencensus.trace.AttributeValue.stringAttributeValue("host"));
attributesMap.put(
"http.method", io.opencensus.trace.AttributeValue.stringAttributeValue("method"));
attributesMap.put("http.path", io.opencensus.trace.AttributeValue.stringAttributeValue("path"));
attributesMap.put(
"http.route", io.opencensus.trace.AttributeValue.stringAttributeValue("route"));
attributesMap.put(
"http.user_agent", io.opencensus.trace.AttributeValue.stringAttributeValue("user_agent"));
attributesMap.put(
"http.status_code", io.opencensus.trace.AttributeValue.longAttributeValue(200L));
SpanData.Attributes httpAttributes = SpanData.Attributes.create(attributesMap, 0);

SpanData spanData =
SpanData.create(
spanContext,
parentSpanId,
/* hasRemoteParent= */ true,
SPAN_NAME,
startTimestamp,
httpAttributes,
annotations,
networkEvents,
links,
CHILD_SPAN_COUNT,
status,
endTimestamp);

Span span = handler.generateSpan(spanData);
Map<String, AttributeValue> attributes = span.getAttributes().getAttributeMapMap();

assertThat(attributes).containsEntry("/http/host", toStringValue("host"));
assertThat(attributes).containsEntry("/http/method", toStringValue("method"));
assertThat(attributes).containsEntry("/http/path", toStringValue("path"));
assertThat(attributes).containsEntry("/http/route", toStringValue("route"));
assertThat(attributes).containsEntry("/http/user_agent", toStringValue("user_agent"));
assertThat(attributes)
.containsEntry("/http/status_code", AttributeValue.newBuilder().setIntValue(200L).build());
}

private static AttributeValue toStringValue(String value) {
return AttributeValue.newBuilder()
.setStringValue(
TruncatableString.newBuilder().setValue(value).setTruncatedByteCount(0).build())
.build();
}
}

0 comments on commit a2ea0b7

Please sign in to comment.