Skip to content

Commit

Permalink
Use names specified by OpenTelemetry for tags (#81)
Browse files Browse the repository at this point in the history
See #69

Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
  • Loading branch information
tsegismont authored Sep 27, 2024
1 parent 2fd4f9c commit 427f3eb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,62 +169,8 @@ private <T> Span reportTagsAndStart(SpanBuilder span, T obj, TagExtractor<T> tag
private static <T> Attributes processTags(T obj, TagExtractor<T> tagExtractor, boolean client) {
AttributesBuilder builder = Attributes.builder();
int len = tagExtractor.len(obj);
boolean receivedDbSystem = false;
for (int idx = 0; idx < len; idx++) {
String name = tagExtractor.name(obj, idx);
String value = tagExtractor.value(obj, idx);
switch (name) {
case "peer.address":
builder.put("network.peer.address", value);
builder.put(name, value);
break;
case "peer.port":
builder.put("network.peer.port", value);
builder.put(name, value);
break;
case "message_bus.destination":
builder.put("messaging.destination.name", value);
builder.put(name, value);
break;
case "message_bus.system":
builder.put("messaging.system", value);
break;
case "message_bus.operation":
builder.put("messaging.operation", value);
break;
case "db.type":
if (!receivedDbSystem) {
builder.put("db.system", value);
}
builder.put(name, value);
break;
case "db.system":
receivedDbSystem = true;
builder.put(name, value);
break;
case "http.method":
builder.put("http.request.method", value);
builder.put(name, value);
break;
case "http.url":
if (client) {
builder.put("url.full", value);
}
builder.put(name, value);
break;
case "http.status_code":
builder.put("http.response.status_code", value);
builder.put(name, value);
break;
case "http.path":
builder.put("url.path", value);
break;
case "http.query":
builder.put("url.query", value);
break;
default:
builder.put(name, value);
}
builder.put(tagExtractor.name(obj, idx), tagExtractor.value(obj, idx));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ private void testSend(VertxTestContext ctx, TracingPolicy policy, int expected)
count++;
assertThat(operationName)
.isEqualTo("send");
assertThat(data.getAttributes().get(AttributeKey.stringKey("message_bus.destination")))
.isEqualTo(ADDRESS);
assertThat(data.getAttributes().get(AttributeKey.stringKey("messaging.destination.name")))
.isEqualTo(ADDRESS);
}
Expand Down Expand Up @@ -156,8 +154,6 @@ private void testPublish(VertxTestContext ctx, TracingPolicy policy, int expecte
count++;
assertThat(operationName)
.isEqualTo("publish");
assertThat(data.getAttributes().get(AttributeKey.stringKey("message_bus.destination")))
.isEqualTo(ADDRESS);
assertThat(data.getAttributes().get(AttributeKey.stringKey("messaging.destination.name")))
.isEqualTo(ADDRESS);
}
Expand Down Expand Up @@ -228,7 +224,7 @@ private void testRequestReply(VertxTestContext ctx, TracingPolicy policy, boolea
count++;
assertThat(operationName)
.isEqualTo("send");
assertThat(data.getAttributes().get(AttributeKey.stringKey("message_bus.destination")))
assertThat(data.getAttributes().get(AttributeKey.stringKey("messaging.destination.name")))
.isEqualTo(ADDRESS);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void testEventBus(VertxTestContext ctx) throws Exception {
otelTesting.assertTraces().anySatisfy(spanDataList ->
assertThat(spanDataList)
.anySatisfy(spanData ->
assertThat(spanData.getAttributes().get(AttributeKey.stringKey("message_bus.destination")))
assertThat(spanData.getAttributes().get(AttributeKey.stringKey("messaging.destination.name")))
.isEqualTo("the-address")
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,16 @@ public void testPreparedQuery(VertxTestContext ctx) throws Exception {
assertEquals(3, spans.size());
SpanData requestSpan = spans.get(1);
assertEquals("GET", requestSpan.getName());
assertEquals("GET", requestSpan.getAttributes().get(AttributeKey.stringKey("http.method")));
assertEquals("GET", requestSpan.getAttributes().get(AttributeKey.stringKey("http.request.method")));
assertEquals("http://localhost:8080/", requestSpan.getAttributes().get(AttributeKey.stringKey("http.url")));
assertEquals("200", requestSpan.getAttributes().get(AttributeKey.stringKey("http.status_code")));
assertEquals("200", requestSpan.getAttributes().get(AttributeKey.stringKey("http.response.status_code")));
assertTrue(MILLISECONDS.convert(requestSpan.getEndEpochNanos() - requestSpan.getStartEpochNanos(), NANOSECONDS) > baseDurationInMs);
SpanData querySpan = spans.get(0);
assertEquals("Query", querySpan.getName());
assertEquals("client", querySpan.getAttributes().get(AttributeKey.stringKey("span.kind")));
assertEquals("SELECT $1 \"VAL\"", querySpan.getAttributes().get(AttributeKey.stringKey("db.statement")));
assertEquals("sql", querySpan.getAttributes().get(AttributeKey.stringKey("db.type")));
assertEquals("SELECT $1 \"VAL\"", querySpan.getAttributes().get(AttributeKey.stringKey("db.query.text")));
assertEquals("postgres", querySpan.getAttributes().get(AttributeKey.stringKey("db.user")));
assertEquals("postgres", querySpan.getAttributes().get(AttributeKey.stringKey("db.instance")));
assertEquals("postgres", querySpan.getAttributes().get(AttributeKey.stringKey("db.namespace")));
assertEquals("postgresql", querySpan.getAttributes().get(AttributeKey.stringKey("db.system")));
assertEquals(querySpan.getParentSpanId(), requestSpan.getSpanId());
assertEquals(querySpan.getTraceId(), requestSpan.getTraceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/
package io.vertx.tracing.opentracing;

import static io.vertx.tracing.opentracing.OpenTracingUtil.ACTIVE_SPAN;

import io.jaegertracing.Configuration;
import io.opentracing.Span;
import io.opentracing.SpanContext;
Expand All @@ -30,6 +28,8 @@
import java.util.Map;
import java.util.function.BiConsumer;

import static io.vertx.tracing.opentracing.OpenTracingUtil.ACTIVE_SPAN;

/**
* - https://github.com/opentracing/specification/blob/master/semantic_conventions.md
* - https://github.com/opentracing/specification/blob/master/specification.md
Expand Down Expand Up @@ -163,7 +163,57 @@ public <R> void receiveResponse(Context context, R response, Span span, Throwabl
private <T> void reportTags(Span span, T obj, TagExtractor<T> tagExtractor) {
int len = tagExtractor.len(obj);
for (int idx = 0; idx < len; idx++) {
span.setTag(tagExtractor.name(obj, idx), tagExtractor.value(obj, idx));
String name = tagExtractor.name(obj, idx);
String value = tagExtractor.value(obj, idx);
switch (name) {
case "server.address":
case "network.peer.address":
span.setTag("peer.address", value);
break;
case "server.port":
case "network.peer.port":
span.setTag("peer.port", value);
break;
case "messaging.destination.name":
span.setTag("message_bus.destination", value);
break;
case "messaging.system":
span.setTag("message_bus.system", value);
break;
case "messaging.operation":
span.setTag("message_bus.operation", value);
break;
case "db.namespace":
span.setTag("db.instance", value);
break;
case "db.query.text":
case "db.operation.name":
span.setTag("db.statement", value);
break;
case "db.system":
span.setTag("db.type", value);
break;
case "http.request.method":
span.setTag("http.method", value);
break;
case "http.response.status_code":
span.setTag("http.status_code", value);
break;
case "url.full":
span.setTag("http.url", value);
break;
case "url.scheme":
span.setTag("http.scheme", value);
break;
case "url.path":
span.setTag("http.path", value);
break;
case "url.query":
span.setTag("http.query", value);
break;
default:
span.setTag(name, value);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,21 @@ private static <R> void reportTags(R request, TagExtractor<R> tagExtractor, Span
String name = tagExtractor.name(request, i);
String value = tagExtractor.value(request, i);
switch (name) {
case "db.statement":
case "db.query.text":
span.tag("sql.query", value);
break;
case "db.instance":
case "db.namespace":
case "messaging.destination.name":
span.remoteServiceName(value);
break;
case "peer.address":
case "network.peer.address":
Matcher matcher = P.matcher(value);
if (matcher.matches()) {
String host = matcher.group(1);
int port = Integer.parseInt(matcher.group(2));
span.remoteIpAndPort(host, port);
}
break;
case "message_bus.destination":
span.remoteServiceName(value);
break;
}
}
}
Expand Down

0 comments on commit 427f3eb

Please sign in to comment.