Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 6b49392

Browse files
Sebrucksongy23
authored andcommitted
Map http attributes to the stackdriver format, resolves #1153 (#1183)
(cherry picked from commit a1a6cf1)
1 parent bc7344c commit 6b49392

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.trace.v2.TraceServiceClient;
2424
import com.google.cloud.trace.v2.TraceServiceSettings;
2525
import com.google.common.annotations.VisibleForTesting;
26+
import com.google.common.collect.ImmutableMap;
2627
import com.google.common.io.BaseEncoding;
2728
import com.google.devtools.cloudtrace.v2.AttributeValue;
2829
import com.google.devtools.cloudtrace.v2.ProjectName;
@@ -77,6 +78,16 @@ final class StackdriverV2ExporterHandler extends SpanExporter.Handler {
7778
.setStringValue(toTruncatableStringProto(AGENT_LABEL_VALUE_STRING))
7879
.build();
7980

81+
private static final ImmutableMap<String, String> HTTP_ATTRIBUTE_MAPPING =
82+
ImmutableMap.<String, String>builder()
83+
.put("http.host", "/http/host")
84+
.put("http.method", "/http/method")
85+
.put("http.path", "/http/path")
86+
.put("http.route", "/http/route")
87+
.put("http.user_agent", "/http/user_agent")
88+
.put("http.status_code", "/http/status_code")
89+
.build();
90+
8091
private final String projectId;
8192
private final TraceServiceClient traceServiceClient;
8293
private final ProjectName projectName;
@@ -222,11 +233,20 @@ private static Attributes.Builder toAttributesBuilderProto(
222233
Attributes.Builder attributesBuilder =
223234
Attributes.newBuilder().setDroppedAttributesCount(droppedAttributesCount);
224235
for (Map.Entry<String, io.opencensus.trace.AttributeValue> label : attributes.entrySet()) {
225-
attributesBuilder.putAttributeMap(label.getKey(), toAttributeValueProto(label.getValue()));
236+
attributesBuilder.putAttributeMap(
237+
mapKey(label.getKey()), toAttributeValueProto(label.getValue()));
226238
}
227239
return attributesBuilder;
228240
}
229241

242+
private static String mapKey(String key) {
243+
if (HTTP_ATTRIBUTE_MAPPING.containsKey(key)) {
244+
return HTTP_ATTRIBUTE_MAPPING.get(key);
245+
} else {
246+
return key;
247+
}
248+
}
249+
230250
private static Status toStatusProto(io.opencensus.trace.Status status) {
231251
Status.Builder statusBuilder = Status.newBuilder().setCode(status.getCanonicalCode().value());
232252
if (status.getDescription() != null) {

exporters/trace/stackdriver/src/test/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandlerProtoTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
@RunWith(JUnit4.class)
5555
public final class StackdriverV2ExporterHandlerProtoTest {
56+
5657
private static final Credentials FAKE_CREDENTIALS =
5758
GoogleCredentials.newBuilder().setAccessToken(new AccessToken("fake", new Date(100))).build();
5859
// OpenCensus constants
@@ -141,7 +142,6 @@ public void generateSpan() {
141142
CHILD_SPAN_COUNT,
142143
status,
143144
endTimestamp);
144-
145145
TimeEvent annotationTimeEvent1 =
146146
TimeEvent.newBuilder()
147147
.setAnnotation(
@@ -255,4 +255,55 @@ public void generateSpan() {
255255
assertThat(span.getChildSpanCount())
256256
.isEqualTo(Int32Value.newBuilder().setValue(CHILD_SPAN_COUNT).build());
257257
}
258+
259+
@Test
260+
public void mapHttpAttributes() {
261+
Map<String, io.opencensus.trace.AttributeValue> attributesMap =
262+
new HashMap<String, io.opencensus.trace.AttributeValue>();
263+
264+
attributesMap.put("http.host", io.opencensus.trace.AttributeValue.stringAttributeValue("host"));
265+
attributesMap.put(
266+
"http.method", io.opencensus.trace.AttributeValue.stringAttributeValue("method"));
267+
attributesMap.put("http.path", io.opencensus.trace.AttributeValue.stringAttributeValue("path"));
268+
attributesMap.put(
269+
"http.route", io.opencensus.trace.AttributeValue.stringAttributeValue("route"));
270+
attributesMap.put(
271+
"http.user_agent", io.opencensus.trace.AttributeValue.stringAttributeValue("user_agent"));
272+
attributesMap.put(
273+
"http.status_code", io.opencensus.trace.AttributeValue.longAttributeValue(200L));
274+
SpanData.Attributes httpAttributes = SpanData.Attributes.create(attributesMap, 0);
275+
276+
SpanData spanData =
277+
SpanData.create(
278+
spanContext,
279+
parentSpanId,
280+
/* hasRemoteParent= */ true,
281+
SPAN_NAME,
282+
startTimestamp,
283+
httpAttributes,
284+
annotations,
285+
networkEvents,
286+
links,
287+
CHILD_SPAN_COUNT,
288+
status,
289+
endTimestamp);
290+
291+
Span span = handler.generateSpan(spanData);
292+
Map<String, AttributeValue> attributes = span.getAttributes().getAttributeMapMap();
293+
294+
assertThat(attributes).containsEntry("/http/host", toStringValue("host"));
295+
assertThat(attributes).containsEntry("/http/method", toStringValue("method"));
296+
assertThat(attributes).containsEntry("/http/path", toStringValue("path"));
297+
assertThat(attributes).containsEntry("/http/route", toStringValue("route"));
298+
assertThat(attributes).containsEntry("/http/user_agent", toStringValue("user_agent"));
299+
assertThat(attributes)
300+
.containsEntry("/http/status_code", AttributeValue.newBuilder().setIntValue(200L).build());
301+
}
302+
303+
private static AttributeValue toStringValue(String value) {
304+
return AttributeValue.newBuilder()
305+
.setStringValue(
306+
TruncatableString.newBuilder().setValue(value).setTruncatedByteCount(0).build())
307+
.build();
308+
}
258309
}

0 commit comments

Comments
 (0)