Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aerospike client instrumentation #9836

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
faf4a92
adds aerospike instrumentation
Abhishekkr3003 Nov 8, 2023
a03903c
minor refactoring and adds testInstrumentation
Abhishekkr3003 Nov 8, 2023
ecdd747
changes muzzle fail and test case container startup check
Abhishekkr3003 Nov 9, 2023
226b9e1
checkstyle complaint package name update
Abhishekkr3003 Nov 10, 2023
58f1b46
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Nov 10, 2023
ec950dc
changes tests to include server close
Abhishekkr3003 Nov 10, 2023
14f1109
refactors tests
Abhishekkr3003 Nov 10, 2023
4133b58
removes unuseful scopes, local variables and refactors metrics
Abhishekkr3003 Nov 15, 2023
b4af69a
adds aerospike metrics test and adds check for experimental-span-attr…
Abhishekkr3003 Nov 18, 2023
d1818c1
adds muzzle assert inverse
Abhishekkr3003 Nov 18, 2023
e82bdcb
removes :test file
Abhishekkr3003 Nov 18, 2023
7c17dfc
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Nov 18, 2023
5da5b5b
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Dec 30, 2023
5059c8e
refactors metrics into javaagent and updates metrics test and removes…
Abhishekkr3003 Dec 31, 2023
93da28f
updates build.gradle
Abhishekkr3003 Dec 31, 2023
a30b6d7
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Feb 10, 2024
7f84218
minor refactoring
Abhishekkr3003 Feb 11, 2024
afda86d
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Feb 11, 2024
0ef5f40
restructure the modules to v7.0.0
Abhishekkr3003 Feb 11, 2024
3a3e509
adds readme for system properties
Abhishekkr3003 Feb 11, 2024
7d2f3a5
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Nov 26, 2024
cc92b86
refactors aerospike module
Abhishekkr3003 Nov 26, 2024
453c2b9
fixes errors
Abhishekkr3003 Nov 26, 2024
cfc0227
Merge branch 'main' into aerospike-client-instrumentation
Abhishekkr3003 Nov 26, 2024
3b6ab9b
applies spotless fixes
Abhishekkr3003 Nov 26, 2024
c34b20d
refactoring module a level up
Abhishekkr3003 Nov 26, 2024
72c35de
fixes muzzle, refactors module, updates classLoadMatcher and added doc
Abhishekkr3003 Nov 27, 2024
79765c5
removes library version check
Abhishekkr3003 Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/supported-libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These are the supported libraries and frameworks:

| Library/Framework | Auto-instrumented versions | Standalone Library Instrumentation [1] | Semantic Conventions |
|---------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| [Aerospike Client](https://github.com/aerospike/aerospike-client-java) | 7.1+ | N/A | [Database Pool Metrics] |
| [Akka Actors](https://doc.akka.io/docs/akka/current/typed/index.html) | 2.3+ | N/A | Context propagation |
| [Akka HTTP](https://doc.akka.io/docs/akka-http/current/index.html) | 10.0+ | N/A | [HTTP Client Spans], [HTTP Client Metrics], [HTTP Server Spans], [HTTP Server Metrics], Provides `http.route` [2] |
| [Alibaba Druid](https://github.com/alibaba/druid) | 1.0+ | [opentelemetry-alibaba-druid-1.0](../instrumentation/alibaba-druid-1.0/library) | [Database Pool Metrics] |
Expand Down
6 changes: 6 additions & 0 deletions instrumentation/aerospike-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Settings for the Aerospike-Client instrumentation

| System property | Type | Default | Description |
|---------------------------------------------------------------|---------|---------|----------------------------------------------------------------------|
| `otel.instrumentation.aerospike.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental Aerospike client span attributes. |
| `otel.instrumentation.aerospike.experimental-metrics` | Boolean | `false` | Enable the recording of experimental Aerospike client metrics. |
37 changes: 37 additions & 0 deletions instrumentation/aerospike-client/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("com.aerospike")
module.set("aerospike-client")
versions.set("[7.1.0,)")
assertInverse.set(true)
}
}

val latestDepTest = findProperty("testLatestDeps") as Boolean

dependencies {
library("com.aerospike:aerospike-client:7.1.0")
implementation("io.opentelemetry:opentelemetry-api-incubator")

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
}

if (latestDepTest) {
otelJava {
minJavaVersionSupported.set(JavaVersion.VERSION_21)
}
}

tasks {
test {
jvmArgs("-Djava.net.preferIPv4Stack=true")
jvmArgs("-Dotel.instrumentation.aerospike.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.aerospike.experimental-metrics=true")
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.ResultCode;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequest;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeSemanticAttributes;
import javax.annotation.Nullable;

final class AerospikeClientAttributeExtractor
implements AttributesExtractor<AerospikeRequest, Void> {

@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, AerospikeRequest aerospikeRequest) {
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, aerospikeRequest.getSet());
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
AerospikeRequest aerospikeRequest,
@Nullable Void unused,
@Nullable Throwable error) {
if (aerospikeRequest.getNode() != null) {
String nodeName = aerospikeRequest.getNode().getName();
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_NODE_NAME, nodeName);
}

if (error != null) {
if (error instanceof AerospikeException) {
AerospikeException aerospikeException = (AerospikeException) error;
attributes.put(
AerospikeSemanticAttributes.AEROSPIKE_STATUS, aerospikeException.getResultCode());
} else {
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_STATUS, ResultCode.CLIENT_ERROR);
}
} else {
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_STATUS, ResultCode.OK);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.aerospike.AerospikeSingletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.aerospike.internal.CustomElementMatcher.iterableHasAtLeastOne;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
import static net.bytebuddy.matcher.ElementMatchers.takesGenericArguments;

import com.aerospike.client.Key;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequest;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequestContext;
import java.util.Locale;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class AerospikeClientInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return hasSuperType(named("com.aerospike.client.IAerospikeClient"));
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(takesGenericArguments(iterableHasAtLeastOne(named("com.aerospike.client.Key"))))
.and(
not(
takesGenericArguments(
iterableHasAtLeastOne(named("com.aerospike.client.async.EventLoop"))))),
this.getClass().getName() + "$SyncCommandAdvice");

transformer.applyAdviceToMethod(
isMethod()
.and(takesGenericArguments(iterableHasAtLeastOne(named("com.aerospike.client.Key"))))
.and(
takesGenericArguments(
iterableHasAtLeastOne(named("com.aerospike.client.async.EventLoop")))),
this.getClass().getName() + "$AsyncCommandAdvice");
}

@SuppressWarnings("unused")
public static class SyncCommandAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startInstrumentation(
@Advice.Origin("#m") String methodName,
@Advice.AllArguments Object[] args,
@Advice.Local("AerospikeContext") AerospikeRequestContext requestContext) {
Key key = null;
for (Object object : args) {
if (object instanceof Key) {
key = (Key) object;
}
}
if (key == null) {
return;
}
Context parentContext = currentContext();
AerospikeRequest request =
AerospikeRequest.create(methodName.toUpperCase(Locale.ROOT), key.namespace, key.setName);
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
Context context = instrumenter().start(parentContext, request);
requestContext = AerospikeRequestContext.attach(request, context);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopInstrumentation(
@Advice.Thrown Throwable ex,
@Advice.Local("AerospikeContext") AerospikeRequestContext requestContext) {
requestContext.setThrowable(ex);
requestContext.detachAndEnd();
}
}

@SuppressWarnings("unused")
public static class AsyncCommandAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void startInstrumentation(
@Advice.Origin("#m") String methodName,
@Advice.AllArguments Object[] args,
@Advice.Local("AerospikeContext") AerospikeRequestContext requestContext) {
Key key = null;
for (Object object : args) {
if (object instanceof Key) {
key = (Key) object;
}
}
if (key == null) {
return;
}
Context parentContext = currentContext();
AerospikeRequest request =
AerospikeRequest.create(methodName.toUpperCase(Locale.ROOT), key.namespace, key.setName);
if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
Context context = instrumenter().start(parentContext, request);
requestContext = AerospikeRequestContext.attach(request, context);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopInstrumentaionIfError(
@Advice.Thrown Throwable ex,
@Advice.Local("AerospikeContext") AerospikeRequestContext requestContext) {
if (ex != null) {
requestContext.setThrowable(ex);
requestContext.detachAndEnd();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static java.util.Arrays.asList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumentationModule.class)
public class AerospikeClientInstrumentationModule extends InstrumentationModule {

public AerospikeClientInstrumentationModule() {
super("aerospike-client");
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("com.aerospike.client.metrics.LatencyType");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new AerospikeClientInstrumentation(),
new SyncCommandInstrumentation(),
new AsyncCommandInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequest;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeSemanticAttributes;
import javax.annotation.Nullable;

final class AerospikeDbAttributesGetter implements DbClientAttributesGetter<AerospikeRequest> {

@Override
public String getDbSystem(AerospikeRequest request) {
return AerospikeSemanticAttributes.DbSystemValues.AEROSPIKE;
}

@Override
public String getDbOperationName(AerospikeRequest request) {
return request.getOperation();
}

@Nullable
@Override
public String getDbNamespace(AerospikeRequest request) {
return request.getNamespace();
}

@Deprecated
@Nullable
@Override
public String getUser(AerospikeRequest aerospikeRequest) {
return null;
}

@Deprecated
@Nullable
@Override
public String getConnectionString(AerospikeRequest aerospikeRequest) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import com.aerospike.client.cluster.Node;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequest;
import java.net.InetSocketAddress;
import javax.annotation.Nullable;

final class AerospikeNetworkAttributesGetter
implements NetworkAttributesGetter<AerospikeRequest, Void> {

@Override
@Nullable
public InetSocketAddress getNetworkPeerInetSocketAddress(
AerospikeRequest aerospikeRequest, @Nullable Void unused) {
Node node = aerospikeRequest.getNode();
if (node != null) {
return node.getAddress();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
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.semconv.network.NetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.instrumentation.aerospike.internal.AerospikeRequest;
import io.opentelemetry.javaagent.instrumentation.aerospike.metrics.AerospikeMetrics;

public final class AerospikeSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.aerospike-client";

private static final Instrumenter<AerospikeRequest, Void> INSTRUMENTER;

static {
AerospikeDbAttributesGetter aerospikeDbAttributesGetter = new AerospikeDbAttributesGetter();
NetworkAttributesGetter<AerospikeRequest, Void> netAttributesGetter =
new AerospikeNetworkAttributesGetter();

InstrumenterBuilder<AerospikeRequest, Void> builder =
Instrumenter.<AerospikeRequest, Void>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
DbClientSpanNameExtractor.create(aerospikeDbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(aerospikeDbAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter));
InstrumentationConfig instrumentationConfig = AgentInstrumentationConfig.get();
if (instrumentationConfig.getBoolean(
"otel.instrumentation.aerospike.experimental-span-attributes", false)) {
builder.addAttributesExtractor(new AerospikeClientAttributeExtractor());
}
if (instrumentationConfig.getBoolean(
"otel.instrumentation.aerospike.experimental-metrics", false)) {
builder.addOperationMetrics(AerospikeMetrics.get());
}

INSTRUMENTER = builder.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

public static Instrumenter<AerospikeRequest, Void> instrumenter() {
return INSTRUMENTER;
}

private AerospikeSingletons() {}
}
Loading
Loading