-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
694 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...pentelemetry/runtime/tracing/intrumentation/vertx/RedisClientInstrumenterVertxTracer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package io.quarkus.opentelemetry.runtime.tracing.intrumentation.vertx; | ||
|
||
import static io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig.INSTRUMENTATION_NAME; | ||
|
||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientAttributesGetter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientSpanNameExtractor; | ||
import io.opentelemetry.semconv.SemanticAttributes; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.spi.tracing.SpanKind; | ||
import io.vertx.core.spi.tracing.TagExtractor; | ||
import io.vertx.core.tracing.TracingPolicy; | ||
|
||
public class RedisClientInstrumenterVertxTracer implements | ||
InstrumenterVertxTracer<RedisClientInstrumenterVertxTracer.CommandTrace, RedisClientInstrumenterVertxTracer.CommandTrace> { | ||
private final Instrumenter<CommandTrace, CommandTrace> redisClientInstrumenter; | ||
|
||
public RedisClientInstrumenterVertxTracer(final OpenTelemetry openTelemetry) { | ||
RedisClientAttributesGetter redisClientAttributesGetter = new RedisClientAttributesGetter(); | ||
|
||
InstrumenterBuilder<CommandTrace, CommandTrace> clientInstrumenterBuilder = Instrumenter.builder( | ||
openTelemetry, | ||
INSTRUMENTATION_NAME, DbClientSpanNameExtractor.create(redisClientAttributesGetter)); | ||
|
||
this.redisClientInstrumenter = clientInstrumenterBuilder | ||
.addAttributesExtractor(DbClientAttributesExtractor.create(redisClientAttributesGetter)) | ||
.buildInstrumenter(SpanKindExtractor.alwaysClient()); | ||
} | ||
|
||
@Override | ||
public <R> boolean canHandle(R request, TagExtractor<R> tagExtractor) { | ||
if (request instanceof CommandTrace) { | ||
return true; | ||
} | ||
|
||
return "redis".equals(tagExtractor.extract(request).get("db.type")); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <R> OpenTelemetryVertxTracer.SpanOperation sendRequest( | ||
final Context context, | ||
final SpanKind kind, | ||
final TracingPolicy policy, | ||
final R request, | ||
final String operation, | ||
final BiConsumer<String, String> headers, | ||
final TagExtractor<R> tagExtractor) { | ||
|
||
R commandTrace = (R) CommandTrace.commandTrace(tagExtractor.extract(request)); | ||
return InstrumenterVertxTracer.super.sendRequest(context, kind, policy, commandTrace, operation, headers, tagExtractor); | ||
} | ||
|
||
@Override | ||
public <R> void receiveResponse( | ||
final Context context, | ||
final R response, | ||
final OpenTelemetryVertxTracer.SpanOperation spanOperation, | ||
final Throwable failure, | ||
final TagExtractor<R> tagExtractor) { | ||
|
||
InstrumenterVertxTracer.super.receiveResponse(context, response, spanOperation, failure, tagExtractor); | ||
} | ||
|
||
@Override | ||
public Instrumenter<CommandTrace, CommandTrace> getReceiveRequestInstrumenter() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Instrumenter<CommandTrace, CommandTrace> getSendResponseInstrumenter() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Instrumenter<CommandTrace, CommandTrace> getSendRequestInstrumenter() { | ||
return redisClientInstrumenter; | ||
} | ||
|
||
@Override | ||
public Instrumenter<CommandTrace, CommandTrace> getReceiveResponseInstrumenter() { | ||
return redisClientInstrumenter; | ||
} | ||
|
||
// From io.vertx.redis.client.impl.CommandReporter | ||
static class CommandTrace { | ||
private final Map<String, String> attributes; | ||
|
||
CommandTrace(final Map<String, String> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
static CommandTrace commandTrace(final Map<String, String> attributes) { | ||
return new CommandTrace(attributes); | ||
} | ||
|
||
public String operation() { | ||
return attributes.get("db.statement"); | ||
} | ||
|
||
public String user() { | ||
return attributes.get("db.user"); | ||
} | ||
|
||
public String peerAddress() { | ||
return attributes.get("peer.address"); | ||
} | ||
|
||
public String dbInstance() { | ||
return attributes.get("db.instance"); | ||
} | ||
} | ||
|
||
static class RedisClientAttributesGetter implements DbClientAttributesGetter<CommandTrace> { | ||
@Override | ||
public String getStatement(final CommandTrace commandTrace) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getOperation(final CommandTrace commandTrace) { | ||
return commandTrace.operation(); | ||
} | ||
|
||
@Override | ||
public String getSystem(final CommandTrace commandTrace) { | ||
return SemanticAttributes.DbSystemValues.REDIS; | ||
} | ||
|
||
@Override | ||
public String getUser(final CommandTrace commandTrace) { | ||
return commandTrace.user(); | ||
} | ||
|
||
@Override | ||
public String getName(final CommandTrace commandTrace) { | ||
return commandTrace.dbInstance(); | ||
} | ||
|
||
@Override | ||
public String getConnectionString(final CommandTrace commandTrace) { | ||
return commandTrace.peerAddress(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
integration-tests/opentelemetry-redis-instrumentation/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# OpenTelemetry Redis Instrumentation example | ||
|
||
## Running the tests | ||
|
||
By default, the tests of this module are disabled. To activate the test, use the `-Dtest-containers` option. | ||
|
||
NB: Tests in this module will attempt a connection to a local Redis listening on the default port. | ||
If you have specific requirements, you can define a specific connection URL with `-Dquarkus.redis.hosts=host:port`. | ||
Or, you can use the `-Dstart-containers` option to start the Redis Server container automatically. | ||
|
||
### Running tests in JVM mode | ||
|
||
To run, you can run the following command: | ||
|
||
``` | ||
mvn clean install -Dtest-containers | ||
``` | ||
|
||
Alternatively, to run the tests in JVM mode with Redis Server started as a Docker container, you can run the following command: | ||
|
||
``` | ||
mvn clean install -Dtest-containers -Dstart-containers | ||
``` | ||
|
||
### Running tests in native mode | ||
|
||
Additionally, you can generate a native image and run the tests for this native image by adding `-Dnative`: | ||
|
||
``` | ||
mvn clean install -Dtest-containers -Dnative | ||
``` | ||
|
||
You can also use the `-Dstart-containers` system property (as shown below), if you want the Redis Server container to be started automatically. | ||
|
||
``` | ||
mvn clean install -Dtest-containers -Dnative -Dstart-containers | ||
``` |
Oops, something went wrong.