Skip to content

Commit

Permalink
Elasticsearch rest client low cardinality span name (open-telemetry#5584
Browse files Browse the repository at this point in the history
)
  • Loading branch information
laurit authored and RashmiRam committed May 23, 2022
1 parent 6363c4e commit d088d7e
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestInstrumenterFactory;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import org.elasticsearch.client.Response;

public final class ElasticsearchRest5Singletons {

private static final Instrumenter<String, Response> INSTRUMENTER =
private static final Instrumenter<ElasticsearchRestRequest, Response> INSTRUMENTER =
ElasticsearchRestInstrumenterFactory.create("io.opentelemetry.elasticsearch-rest-5.0");

public static Instrumenter<String, Response> instrumenter() {
public static Instrumenter<ElasticsearchRestRequest, Response> instrumenter() {
return INSTRUMENTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.RestResponseListener;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -48,13 +49,13 @@ public static class PerformRequestAsyncAdvice {
public static void onEnter(
@Advice.Argument(0) String method,
@Advice.Argument(1) String endpoint,
@Advice.Local("otelRequest") String request,
@Advice.Local("otelRequest") ElasticsearchRestRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Argument(value = 5, readOnly = false) ResponseListener responseListener) {

Context parentContext = currentContext();
request = method + " " + endpoint;
request = ElasticsearchRestRequest.create(method, endpoint);
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
Expand All @@ -70,7 +71,7 @@ public static void onEnter(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") String request,
@Advice.Local("otelRequest") ElasticsearchRestRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ class ElasticsearchRest5Test extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 1) {
span(0) {
name "GET _cluster/health"
name "GET"
kind CLIENT
hasNoParent()
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down Expand Up @@ -130,12 +131,13 @@ class ElasticsearchRest5Test extends AgentInstrumentationSpecification {
hasNoParent()
}
span(1) {
name "GET _cluster/health"
name "GET"
kind CLIENT
childOf(span(0))
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestInstrumenterFactory;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import org.elasticsearch.client.Response;

public final class ElasticsearchRest6Singletons {

private static final Instrumenter<String, Response> INSTRUMENTER =
private static final Instrumenter<ElasticsearchRestRequest, Response> INSTRUMENTER =
ElasticsearchRestInstrumenterFactory.create("io.opentelemetry.elasticsearch-rest-6.4");

public static Instrumenter<String, Response> instrumenter() {
public static Instrumenter<ElasticsearchRestRequest, Response> instrumenter() {
return INSTRUMENTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.RestResponseListener;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -47,12 +48,12 @@ public static class PerformRequestAsyncAdvice {
public static void onEnter(
@Advice.Argument(0) Request request,
@Advice.Argument(value = 1, readOnly = false) ResponseListener responseListener,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Context parentContext = currentContext();
otelRequest = request.getMethod() + " " + request.getEndpoint();
otelRequest = ElasticsearchRestRequest.create(request.getMethod(), request.getEndpoint());
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
return;
}
Expand All @@ -68,7 +69,7 @@ public static void onEnter(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ class ElasticsearchRest6Test extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 1) {
span(0) {
name "GET _cluster/health"
name "GET"
kind CLIENT
hasNoParent()
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down Expand Up @@ -124,12 +125,13 @@ class ElasticsearchRest6Test extends AgentInstrumentationSpecification {
hasNoParent()
}
span(1) {
name "GET _cluster/health"
name "GET"
kind CLIENT
childOf(span(0))
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestInstrumenterFactory;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import org.elasticsearch.client.Response;

public final class ElasticsearchRest7Singletons {

private static final Instrumenter<String, Response> INSTRUMENTER =
private static final Instrumenter<ElasticsearchRestRequest, Response> INSTRUMENTER =
ElasticsearchRestInstrumenterFactory.create("io.opentelemetry.elasticsearch-rest-7.0");

public static Instrumenter<String, Response> instrumenter() {
public static Instrumenter<ElasticsearchRestRequest, Response> instrumenter() {
return INSTRUMENTER;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.ElasticsearchRestRequest;
import io.opentelemetry.javaagent.instrumentation.elasticsearch.rest.RestResponseListener;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -53,12 +54,12 @@ public static class PerformRequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(0) Request request,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Context parentContext = currentContext();
otelRequest = request.getMethod() + " " + request.getEndpoint();
otelRequest = ElasticsearchRestRequest.create(request.getMethod(), request.getEndpoint());
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
return;
}
Expand All @@ -71,7 +72,7 @@ public static void onEnter(
public static void stopSpan(
@Advice.Return(readOnly = false) Response response,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand All @@ -91,12 +92,12 @@ public static class PerformRequestAsyncAdvice {
public static void onEnter(
@Advice.Argument(0) Request request,
@Advice.Argument(value = 1, readOnly = false) ResponseListener responseListener,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Context parentContext = currentContext();
otelRequest = request.getMethod() + " " + request.getEndpoint();
otelRequest = ElasticsearchRestRequest.create(request.getMethod(), request.getEndpoint());
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
return;
}
Expand All @@ -112,7 +113,7 @@ public static void onEnter(
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") String otelRequest,
@Advice.Local("otelRequest") ElasticsearchRestRequest otelRequest,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ class ElasticsearchRest7Test extends AgentInstrumentationSpecification {
assertTraces(1) {
trace(0, 1) {
span(0) {
name "GET _cluster/health"
name "GET"
kind CLIENT
hasNoParent()
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down Expand Up @@ -123,12 +124,13 @@ class ElasticsearchRest7Test extends AgentInstrumentationSpecification {
hasNoParent()
}
span(1) {
name "GET _cluster/health"
name "GET"
kind CLIENT
childOf(span(0))
attributes {
"$SemanticAttributes.DB_SYSTEM" "elasticsearch"
"$SemanticAttributes.DB_OPERATION" "GET _cluster/health"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.DB_STATEMENT" "GET _cluster/health"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_PEER_NAME" httpHost.hostName
"$SemanticAttributes.NET_PEER_PORT" httpHost.port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ plugins {

dependencies {
compileOnly("org.elasticsearch.client:rest:5.0.0")
compileOnly("com.google.auto.value:auto-value-annotations")

annotationProcessor("com.google.auto.value:auto-value")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,41 @@
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import javax.annotation.Nullable;

final class ElasticsearchRestAttributesGetter implements DbClientAttributesGetter<String> {
final class ElasticsearchRestAttributesGetter
implements DbClientAttributesGetter<ElasticsearchRestRequest> {

@Override
public String system(String operation) {
public String system(ElasticsearchRestRequest request) {
return SemanticAttributes.DbSystemValues.ELASTICSEARCH;
}

@Override
@Nullable
public String user(String operation) {
public String user(ElasticsearchRestRequest request) {
return null;
}

@Override
@Nullable
public String name(String operation) {
public String name(ElasticsearchRestRequest request) {
return null;
}

@Override
@Nullable
public String connectionString(String operation) {
public String connectionString(ElasticsearchRestRequest request) {
return null;
}

@Override
@Nullable
public String statement(String operation) {
return null;
public String statement(ElasticsearchRestRequest request) {
return request.getMethod() + " " + request.getOperation();
}

@Override
@Nullable
public String operation(String operation) {
return operation;
public String operation(ElasticsearchRestRequest request) {
return request.getMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

public final class ElasticsearchRestInstrumenterFactory {

public static Instrumenter<String, Response> create(String instrumentationName) {
public static Instrumenter<ElasticsearchRestRequest, Response> create(
String instrumentationName) {
ElasticsearchRestAttributesGetter dbClientAttributesGetter =
new ElasticsearchRestAttributesGetter();
ElasticsearchRestNetResponseAttributesGetter netAttributesGetter =
new ElasticsearchRestNetResponseAttributesGetter();

return Instrumenter.<String, Response>builder(
return Instrumenter.<ElasticsearchRestRequest, Response>builder(
GlobalOpenTelemetry.get(),
instrumentationName,
DbClientSpanNameExtractor.create(dbClientAttributesGetter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.elasticsearch.client.Response;

final class ElasticsearchRestNetResponseAttributesGetter
implements NetClientAttributesGetter<String, Response> {
implements NetClientAttributesGetter<ElasticsearchRestRequest, Response> {

@Override
public String transport(String operation, Response response) {
public String transport(ElasticsearchRestRequest request, Response response) {
return SemanticAttributes.NetTransportValues.IP_TCP;
}

@Override
@Nullable
public String peerName(String operation, @Nullable Response response) {
public String peerName(ElasticsearchRestRequest request, @Nullable Response response) {
if (response != null) {
return response.getHost().getHostName();
}
Expand All @@ -29,7 +29,7 @@ public String peerName(String operation, @Nullable Response response) {

@Override
@Nullable
public Integer peerPort(String operation, @Nullable Response response) {
public Integer peerPort(ElasticsearchRestRequest request, @Nullable Response response) {
if (response != null) {
return response.getHost().getPort();
}
Expand All @@ -38,7 +38,7 @@ public Integer peerPort(String operation, @Nullable Response response) {

@Override
@Nullable
public String peerIp(String operation, @Nullable Response response) {
public String peerIp(ElasticsearchRestRequest request, @Nullable Response response) {
if (response != null && response.getHost().getAddress() != null) {
return response.getHost().getAddress().getHostAddress();
}
Expand Down
Loading

0 comments on commit d088d7e

Please sign in to comment.