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 15 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("com.aerospike")
module.set("aerospike-client")
versions.set("[4.4.9,)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you want this instrumentation to work starting from 7.1.0 and don't care about earlier versions? The idea here is to have the muzzle version the same as the earliest supported version to avoid the situation where instrumentation applies to some earlier version but does not work correctly. You could bring the minimum version down to 5.0.0 where tests seem to pass with a small modification or I guess you could artificially restrict the version where the instrumentation works with something like

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return hasClassesNamed("redis.clients.jedis.CommandArguments");
}
where you enable the instrumentation only when a class that was added in 7.1.0 is present (of course this only works when such a class exists).

assertInverse.set(true)
}
}

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

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

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

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

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 javax.annotation.Nullable;

final class AerospikeClientAttributeExtractor
implements AttributesExtractor<AerospikeRequest, Void> {

@Override
public void onStart(
AttributesBuilder attributes, Context parentContext, AerospikeRequest aerospikeRequest) {
attributes.put(
AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE, aerospikeRequest.getNamespace());
laurit marked this conversation as resolved.
Show resolved Hide resolved
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME, aerospikeRequest.getSet());
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY, aerospikeRequest.getUserKey());
}

@Override
public void onEnd(
AttributesBuilder attributes,
Context context,
AerospikeRequest aerospikeRequest,
@Nullable Void unused,
@Nullable Throwable error) {
attributes.put(
AerospikeSemanticAttributes.AEROSPIKE_STATUS, aerospikeRequest.getStatus().name());
if (error != null) {
if (error instanceof AerospikeException) {
AerospikeException aerospikeException = (AerospikeException) error;
attributes.put(
AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, aerospikeException.getResultCode());
} else {
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, ResultCode.CLIENT_ERROR);
}
} else {
attributes.put(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE, ResultCode.OK);
if (aerospikeRequest.getSize() != null) {
attributes.put(
AerospikeSemanticAttributes.AEROSPIKE_TRANSFER_SIZE, aerospikeRequest.getSize());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

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", "aerospike-client-7.1.0");
}

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

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new SyncCommandInstrumentation(),
new SocketInstrumentation(),
new AsyncCommandInstrumentation(),
new AsyncHandlerInstrumentation(),
new AsyncScanAllCommandInstrumentation());
}

@Override
public boolean isIndyModule() {
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isIndyModule is an experimental feature you don't need to add this method unless you know that what is done in this instrumentation fails with indy for some reason

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientAttributesGetter;
import javax.annotation.Nullable;

final class AerospikeDbAttributesGetter implements DbClientAttributesGetter<AerospikeRequest> {

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

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

@Override
public String getName(AerospikeRequest request) {
return null;
}

@Override
public String getConnectionString(AerospikeRequest request) {
return null;
}

@Override
public String getStatement(AerospikeRequest request) {
return null;
}

@Override
public String getOperation(AerospikeRequest request) {
return request.getOperation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

import com.aerospike.client.cluster.Node;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
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,60 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

import com.aerospike.client.Key;
import com.aerospike.client.cluster.Node;
import com.google.auto.value.AutoValue;
import javax.annotation.Nullable;

@AutoValue
public abstract class AerospikeRequest {
private Node node;
private Integer size;
private Status status;

public static AerospikeRequest create(String operation, Key key) {
return new AutoValue_AerospikeRequest(
operation, key.namespace, key.setName, key.userKey.toString());
}

public static AerospikeRequest create(String operation, String namespace, String set) {
return new AutoValue_AerospikeRequest(operation, namespace, set, null);
}

public abstract String getOperation();

public abstract String getNamespace();

public abstract String getSet();

@Nullable
public abstract String getUserKey();

public void setNode(Node node) {
this.node = node;
}

public Node getNode() {
return this.node;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;

public final class AerospikeRequestContext {
private static final ThreadLocal<AerospikeRequestContext> contextThreadLocal =
new ThreadLocal<>();
private AerospikeRequest request;
private Context context;

private AerospikeRequestContext() {}

public static AerospikeRequestContext attach(AerospikeRequest request, Context context) {

AerospikeRequestContext requestContext = new AerospikeRequestContext();
requestContext.request = request;
requestContext.context = context;
contextThreadLocal.set(requestContext);
return requestContext;
}

public void detachAndEnd() {
contextThreadLocal.remove();
}

@SuppressWarnings("unchecked")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the suppress warnings really necessary?

public static AerospikeRequestContext current() {
return contextThreadLocal.get();
}

public void endSpan(
Instrumenter<AerospikeRequest, Void> instrumenter,
Context context,
AerospikeRequest request,
Throwable throwable) {
instrumenter.end(context, request, null, throwable);
}

public AerospikeRequest getRequest() {
return request;
}

public Context getContext() {
return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.aerospike.v7_1;

import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;

public final class AerospikeSemanticAttributes {
private AerospikeSemanticAttributes() {}

public static final AttributeKey<String> AEROSPIKE_STATUS = stringKey("aerospike.status");
public static final AttributeKey<Long> AEROSPIKE_ERROR_CODE = longKey("aerospike.error.code");
public static final AttributeKey<String> AEROSPIKE_NAMESPACE = stringKey("aerospike.namespace");
public static final AttributeKey<String> AEROSPIKE_SET_NAME = stringKey("aerospike.set.name");
public static final AttributeKey<String> AEROSPIKE_USER_KEY = stringKey("aerospike.user.key");
public static final AttributeKey<Long> AEROSPIKE_TRANSFER_SIZE =
longKey("aerospike.transfer.size");

public static final class DbSystemValues {
public static final String AEROSPIKE = "aerospike";

private DbSystemValues() {}
}
}
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.v7_1;

import io.opentelemetry.api.GlobalOpenTelemetry;
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.InstrumentationConfig;
import io.opentelemetry.javaagent.instrumentation.aerospike.v7_1.metrics.AerospikeMetrics;

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

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))
.addOperationMetrics(AerospikeMetrics.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly to experimental span attributes experimental metrics should be behind otel.instrumentation.aerospike.experimental-metrics flag

if (InstrumentationConfig.get()
.getBoolean("otel.instrumentation.aerospike.experimental-span-attributes", false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder.addAttributesExtractor(new AerospikeClientAttributeExtractor());
}

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

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

private AersopikeSingletons() {}
}
Loading
Loading