diff options
| author | Sebastian Bruckner <Sebruck@users.noreply.github.com> | 2018-05-07 18:27:03 +0200 |
|---|---|---|
| committer | sebright <sebright@google.com> | 2018-05-07 09:27:03 -0700 |
| commit | a1a6cf16ec5e6ec3c1e08e96b1e8dd9a45dce13e (patch) | |
| tree | cf5596447edee63813e5c60c125b55b3797a4142 /exporters | |
| parent | 1513015cf93843bba6215faa02228b9e462abb30 (diff) | |
| download | platform_external_opencensus-java-a1a6cf16ec5e6ec3c1e08e96b1e8dd9a45dce13e.tar.gz platform_external_opencensus-java-a1a6cf16ec5e6ec3c1e08e96b1e8dd9a45dce13e.tar.bz2 platform_external_opencensus-java-a1a6cf16ec5e6ec3c1e08e96b1e8dd9a45dce13e.zip | |
Map http attributes to the stackdriver format, resolves #1153 (#1183)
Diffstat (limited to 'exporters')
2 files changed, 73 insertions, 2 deletions
diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java index 7aea093e..2e339400 100644 --- a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java @@ -23,6 +23,7 @@ import com.google.auth.Credentials; 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; @@ -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; @@ -222,11 +233,20 @@ final class StackdriverV2ExporterHandler extends SpanExporter.Handler { 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) { diff --git a/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandlerProtoTest.java b/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandlerProtoTest.java index 9f012d2a..904de820 100644 --- a/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandlerProtoTest.java +++ b/exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandlerProtoTest.java @@ -53,6 +53,7 @@ import org.junit.runners.JUnit4; @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 @@ -141,7 +142,6 @@ public final class StackdriverV2ExporterHandlerProtoTest { CHILD_SPAN_COUNT, status, endTimestamp); - TimeEvent annotationTimeEvent1 = TimeEvent.newBuilder() .setAnnotation( @@ -255,4 +255,55 @@ public final class StackdriverV2ExporterHandlerProtoTest { 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(); + } } |
