Skip to content

Commit

Permalink
implement
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Nov 10, 2024
1 parent 39e461d commit 2d7cb9f
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.instrumentation.api.internal.SpanKey;
import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.internal.InternalNetworkAttributesExtractor;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -44,6 +45,7 @@ public final class DbClientAttributesExtractor<REQUEST, RESPONSE>

private final DbClientAttributesGetter<REQUEST, RESPONSE> getter;
private final InternalNetworkAttributesExtractor<REQUEST, RESPONSE> internalNetworkExtractor;
private final ServerAttributesExtractor<REQUEST, RESPONSE> serverAttributesExtractor;

/** Creates the database client attributes extractor with default configuration. */
public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
Expand All @@ -56,6 +58,7 @@ public static <REQUEST, RESPONSE> AttributesExtractor<REQUEST, RESPONSE> create(
internalNetworkExtractor =
new InternalNetworkAttributesExtractor<>(
getter, SemconvStability.emitOldDatabaseSemconv(), false);
serverAttributesExtractor = ServerAttributesExtractor.create(getter);
}

@Override
Expand All @@ -74,6 +77,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
internalSet(attributes, DB_STATEMENT, getter.getDbQueryText(request));
internalSet(attributes, DB_OPERATION, getter.getDbOperationName(request));
}
serverAttributesExtractor.onStart(attributes, parentContext, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.api.incubator.semconv.db;

import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
import javax.annotation.Nullable;

/**
Expand All @@ -20,7 +21,7 @@
* OpenTelemetry specification.
*/
public interface DbClientAttributesGetter<REQUEST, RESPONSE>
extends NetworkAttributesGetter<REQUEST, RESPONSE> {
extends NetworkAttributesGetter<REQUEST, RESPONSE>, ServerAttributesGetter<REQUEST> {

@Deprecated
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class DbClientSpanNameExtractor<REQUEST> implements SpanNameExtr
* @see DbClientAttributesGetter#getDbNamespace(Object) used to extract {@code <db.namespace>}.
*/
public static <REQUEST> SpanNameExtractor<REQUEST> create(
DbClientAttributesGetter<REQUEST, Void> getter) {
DbClientAttributesGetter<REQUEST, ?> getter) {
return new GenericDbClientSpanNameExtractor<>(getter);
}

Expand Down Expand Up @@ -65,9 +65,9 @@ protected String computeSpanName(String dbName, String operation, String mainIde
private static final class GenericDbClientSpanNameExtractor<REQUEST>
extends DbClientSpanNameExtractor<REQUEST> {

private final DbClientAttributesGetter<REQUEST, Void> getter;
private final DbClientAttributesGetter<REQUEST, ?> getter;

private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, Void> getter) {
private GenericDbClientSpanNameExtractor(DbClientAttributesGetter<REQUEST, ?> getter) {
this.getter = getter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.annotation.Nullable;

final class CouchbaseAttributesGetter
Expand Down Expand Up @@ -48,4 +50,15 @@ public String getDbQueryText(CouchbaseRequestInfo couchbaseRequest) {
public String getDbOperationName(CouchbaseRequestInfo couchbaseRequest) {
return couchbaseRequest.operation();
}

@Nullable
@Override
public InetSocketAddress getNetworkPeerInetSocketAddress(
CouchbaseRequestInfo couchbaseRequest, @Nullable Void unused) {
SocketAddress peerAddress = couchbaseRequest.getPeerAddress();
if (peerAddress instanceof InetSocketAddress) {
return (InetSocketAddress) peerAddress;
}
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;

public final class CouchbaseSingletons {
Expand All @@ -25,13 +24,11 @@ public final class CouchbaseSingletons {
CouchbaseAttributesGetter couchbaseAttributesGetter = new CouchbaseAttributesGetter();
SpanNameExtractor<CouchbaseRequestInfo> spanNameExtractor =
new CouchbaseSpanNameExtractor(DbClientSpanNameExtractor.create(couchbaseAttributesGetter));
CouchbaseNetworkAttributesGetter netAttributesGetter = new CouchbaseNetworkAttributesGetter();

InstrumenterBuilder<CouchbaseRequestInfo, Void> builder =
Instrumenter.<CouchbaseRequestInfo, Void>builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor)
.addAttributesExtractor(DbClientAttributesExtractor.create(couchbaseAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
.addContextCustomizer(
(context, couchbaseRequest, startAttributes) ->
CouchbaseRequestInfo.init(context, couchbaseRequest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.annotation.Nullable;

final class JedisDbAttributesGetter implements DbClientAttributesGetter<JedisRequest, Void> {
Expand Down Expand Up @@ -43,4 +45,15 @@ public String getDbQueryText(JedisRequest request) {
public String getDbOperationName(JedisRequest request) {
return request.getOperation();
}

@Override
@Nullable
public InetSocketAddress getNetworkPeerInetSocketAddress(
JedisRequest jedisRequest, @Nullable Void unused) {
SocketAddress socketAddress = jedisRequest.getRemoteSocketAddress();
if (socketAddress instanceof InetSocketAddress) {
return (InetSocketAddress) socketAddress;
}
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;

public final class JedisSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.jedis-4.0";
Expand All @@ -19,15 +18,13 @@ public final class JedisSingletons {

static {
JedisDbAttributesGetter dbAttributesGetter = new JedisDbAttributesGetter();
JedisNetworkAttributesGetter netAttributesGetter = new JedisNetworkAttributesGetter();

INSTRUMENTER =
Instrumenter.<JedisRequest, Void>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
DbClientSpanNameExtractor.create(dbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -46,6 +45,7 @@ public Instrumenter<DbExecution, Void> build(
Function<SpanNameExtractor<DbExecution>, ? extends SpanNameExtractor<? super DbExecution>>
spanNameExtractorTransformer,
boolean statementSanitizationEnabled) {

SpanNameExtractor<? super DbExecution> spanNameExtractor =
spanNameExtractorTransformer.apply(
DbClientSpanNameExtractor.create(R2dbcSqlAttributesGetter.INSTANCE));
Expand All @@ -56,7 +56,6 @@ public Instrumenter<DbExecution, Void> build(
SqlClientAttributesExtractor.builder(R2dbcSqlAttributesGetter.INSTANCE)
.setStatementSanitizationEnabled(statementSanitizationEnabled)
.build())
.addAttributesExtractor(ServerAttributesExtractor.create(R2dbcNetAttributesGetter.INSTANCE))
.addAttributesExtractors(additionalExtractors)
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public enum R2dbcSqlAttributesGetter implements SqlClientAttributesGetter<DbExecution> {
public enum R2dbcSqlAttributesGetter implements SqlClientAttributesGetter<DbExecution, Void> {
INSTANCE;

@Override
Expand Down Expand Up @@ -45,4 +45,16 @@ public String getConnectionString(DbExecution request) {
public String getRawQueryText(DbExecution request) {
return request.getRawQueryText();
}

@Nullable
@Override
public String getServerAddress(DbExecution request) {
return request.getHost();
}

@Nullable
@Override
public Integer getServerPort(DbExecution request) {
return request.getPort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
import java.net.InetSocketAddress;
import javax.annotation.Nullable;

final class RedissonDbAttributesGetter implements DbClientAttributesGetter<RedissonRequest, Void> {
Expand Down Expand Up @@ -45,4 +46,10 @@ public String getDbQueryText(RedissonRequest request) {
public String getDbOperationName(RedissonRequest request) {
return request.getOperation();
}

@Override
public InetSocketAddress getNetworkPeerInetSocketAddress(
RedissonRequest request, @Nullable Void unused) {
return request.getAddress();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;

public final class RedissonInstrumenterFactory {

public static Instrumenter<RedissonRequest, Void> createInstrumenter(String instrumentationName) {
RedissonDbAttributesGetter dbAttributesGetter = new RedissonDbAttributesGetter();
RedissonNetAttributesGetter netAttributesGetter = new RedissonNetAttributesGetter();

return Instrumenter.<RedissonRequest, Void>builder(
GlobalOpenTelemetry.get(),
instrumentationName,
DbClientSpanNameExtractor.create(dbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down

This file was deleted.

0 comments on commit 2d7cb9f

Please sign in to comment.