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 7 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,115 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.db;

import static io.opentelemetry.instrumentation.api.instrumenter.db.DbMessageSizeUtil.getMessageSize;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.LongUpDownCounter;
import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public final class AerospikeMetrics implements OperationListener {
Copy link
Contributor

Choose a reason for hiding this comment

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

Usually we place framework specific instrumentation classes with the framework instrumentation not here

Copy link
Author

Choose a reason for hiding this comment

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

Moved it to the library in framework instrumentation.

private static final double NANOS_PER_MS = TimeUnit.MILLISECONDS.toNanos(1);

private static final ContextKey<State> AEROSPIKE_CLIENT_METRICS_STATE =
ContextKey.named("aerospike-client-metrics-state");

private static final Logger logger = Logger.getLogger(AerospikeMetrics.class.getName());

private final LongCounter requestCounter;
private final LongCounter responseCounter;
private final LongUpDownCounter concurrencyUpDownCounter;
private final DoubleHistogram clientLatencyHistogram;

@SuppressWarnings("unused")
private final DoubleHistogram recordSizeHistogram;

private AerospikeMetrics(Meter meter) {
LongCounterBuilder requestCounterBuilder =
meter.counterBuilder("aerospike.requests").setDescription("Aerospike Calls");
AerospikeMetricsAdvice.applyRequestCounterAdvice(requestCounterBuilder);
requestCounter = requestCounterBuilder.build();
LongCounterBuilder responseCounterBuilder =
meter.counterBuilder("aerospike.response").setDescription("Aerospike Responses");
AerospikeMetricsAdvice.applyResponseCounterAdvice(responseCounterBuilder);
responseCounter = responseCounterBuilder.build();
LongUpDownCounterBuilder concurrencyUpDownCounterBuilder =
meter
.upDownCounterBuilder("aerospike.concurrreny")
.setDescription("Aerospike Concurrent Requests");
AerospikeMetricsAdvice.applyConcurrencyUpDownCounterAdvice(concurrencyUpDownCounterBuilder);
concurrencyUpDownCounter = concurrencyUpDownCounterBuilder.build();
DoubleHistogramBuilder durationBuilder =
meter
.histogramBuilder("aerospike.client.duration")
.setDescription("Aerospike Response Latency")
.setUnit("ms");
AerospikeMetricsAdvice.applyClientDurationAdvice(durationBuilder);
clientLatencyHistogram = durationBuilder.build();
DoubleHistogramBuilder recordSizeHistogramBuilder =
meter
.histogramBuilder("aerospike.record.size")
.setDescription("Aerospike Record Size")
.setUnit("By");
AerospikeMetricsAdvice.applyRecordSizeAdvice(recordSizeHistogramBuilder);
recordSizeHistogram = recordSizeHistogramBuilder.build();
}

public static OperationMetrics get() {
return AerospikeMetrics::new;
}

@Override
public Context onStart(Context context, Attributes startAttributes, long startNanos) {
requestCounter.add(1, startAttributes, context);
concurrencyUpDownCounter.add(1, startAttributes, context);
return context.with(
AEROSPIKE_CLIENT_METRICS_STATE,
new AutoValue_AerospikeMetrics_State(startAttributes, startNanos));
}

@Override
public void onEnd(Context context, Attributes endAttributes, long endNanos) {
State state = context.get(AEROSPIKE_CLIENT_METRICS_STATE);
if (state == null) {
logger.log(
FINE,
"No state present when ending context {0}. Cannot record Aerospike End Call metrics.",
context);
return;
}
concurrencyUpDownCounter.add(-1, state.startAttributes(), context);
Attributes mergedAttributes = state.startAttributes().toBuilder().putAll(endAttributes).build();
responseCounter.add(1, mergedAttributes, context);
clientLatencyHistogram.record(
(endNanos - state.startTimeNanos()) / NANOS_PER_MS, mergedAttributes, context);
Long requestBodySize = getMessageSize(mergedAttributes);
if (requestBodySize != null) {
recordSizeHistogram.record(requestBodySize, mergedAttributes, context);
}
}

@AutoValue
abstract static class State {

abstract Attributes startAttributes();

abstract long startTimeNanos();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.db;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.LongUpDownCounterBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedLongCounterBuilder;
import io.opentelemetry.extension.incubator.metrics.ExtendedLongUpDownCounterBuilder;
import io.opentelemetry.instrumentation.api.internal.SemconvStability;
import io.opentelemetry.semconv.SemanticAttributes;
import java.util.ArrayList;
import java.util.List;

final class AerospikeMetricsAdvice {
private AerospikeMetricsAdvice() {}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
static void applyRequestCounterAdvice(LongCounterBuilder builder) {
if (!(builder instanceof ExtendedLongCounterBuilder)) {
return;
}

List<AttributeKey<?>> attributes = new ArrayList<>();
attributes.add(SemanticAttributes.DB_SYSTEM);
attributes.add(SemanticAttributes.DB_OPERATION);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY);
if (SemconvStability.emitStableHttpSemconv()) {
attributes.add(SemanticAttributes.NETWORK_TYPE);
attributes.add(SemanticAttributes.NETWORK_TRANSPORT);
attributes.add(SemanticAttributes.SERVER_ADDRESS);
attributes.add(SemanticAttributes.SERVER_PORT);
}
if (SemconvStability.emitOldHttpSemconv()) {
attributes.add(SemanticAttributes.NET_SOCK_PEER_ADDR);
attributes.add(SemanticAttributes.NET_SOCK_PEER_NAME);
attributes.add(SemanticAttributes.NET_SOCK_PEER_PORT);
}

((ExtendedLongCounterBuilder) builder).setAttributesAdvice(attributes);
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
static void applyConcurrencyUpDownCounterAdvice(LongUpDownCounterBuilder builder) {
if (!(builder instanceof ExtendedLongUpDownCounterBuilder)) {
return;
}

List<AttributeKey<?>> attributes = new ArrayList<>();
attributes.add(SemanticAttributes.DB_SYSTEM);
attributes.add(SemanticAttributes.DB_OPERATION);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY);
if (SemconvStability.emitStableHttpSemconv()) {
attributes.add(SemanticAttributes.NETWORK_TYPE);
attributes.add(SemanticAttributes.NETWORK_TRANSPORT);
attributes.add(SemanticAttributes.SERVER_ADDRESS);
attributes.add(SemanticAttributes.SERVER_PORT);
}
if (SemconvStability.emitOldHttpSemconv()) {
attributes.add(SemanticAttributes.NET_SOCK_PEER_ADDR);
attributes.add(SemanticAttributes.NET_SOCK_PEER_NAME);
attributes.add(SemanticAttributes.NET_SOCK_PEER_PORT);
}

((ExtendedLongUpDownCounterBuilder) builder).setAttributesAdvice(attributes);
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
static void applyResponseCounterAdvice(LongCounterBuilder builder) {
if (!(builder instanceof ExtendedLongCounterBuilder)) {
return;
}

List<AttributeKey<?>> attributes = new ArrayList<>();
attributes.add(SemanticAttributes.DB_SYSTEM);
attributes.add(SemanticAttributes.DB_OPERATION);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_STATUS);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE);
if (SemconvStability.emitStableHttpSemconv()) {
attributes.add(SemanticAttributes.NETWORK_TYPE);
attributes.add(SemanticAttributes.NETWORK_TRANSPORT);
attributes.add(SemanticAttributes.SERVER_ADDRESS);
attributes.add(SemanticAttributes.SERVER_PORT);
}
if (SemconvStability.emitOldHttpSemconv()) {
attributes.add(SemanticAttributes.NET_SOCK_PEER_ADDR);
attributes.add(SemanticAttributes.NET_SOCK_PEER_NAME);
attributes.add(SemanticAttributes.NET_SOCK_PEER_PORT);
}

((ExtendedLongCounterBuilder) builder).setAttributesAdvice(attributes);
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
static void applyClientDurationAdvice(DoubleHistogramBuilder builder) {
if (!(builder instanceof ExtendedDoubleHistogramBuilder)) {
return;
}

List<AttributeKey<?>> attributes = new ArrayList<>();
attributes.add(SemanticAttributes.DB_SYSTEM);
attributes.add(SemanticAttributes.DB_OPERATION);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_STATUS);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE);
if (SemconvStability.emitStableHttpSemconv()) {
attributes.add(SemanticAttributes.NETWORK_TYPE);
attributes.add(SemanticAttributes.NETWORK_TRANSPORT);
attributes.add(SemanticAttributes.SERVER_ADDRESS);
attributes.add(SemanticAttributes.SERVER_PORT);
}
if (SemconvStability.emitOldHttpSemconv()) {
attributes.add(SemanticAttributes.NET_SOCK_PEER_ADDR);
attributes.add(SemanticAttributes.NET_SOCK_PEER_NAME);
attributes.add(SemanticAttributes.NET_SOCK_PEER_PORT);
}

((ExtendedDoubleHistogramBuilder) builder).setAttributesAdvice(attributes);
}

@SuppressWarnings("deprecation") // until old http semconv are dropped in 2.0
static void applyRecordSizeAdvice(DoubleHistogramBuilder builder) {
if (!(builder instanceof ExtendedDoubleHistogramBuilder)) {
return;
}

List<AttributeKey<?>> attributes = new ArrayList<>();
attributes.add(SemanticAttributes.DB_SYSTEM);
attributes.add(SemanticAttributes.DB_OPERATION);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_NAMESPACE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_SET_NAME);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_USER_KEY);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_ERROR_CODE);
attributes.add(AerospikeSemanticAttributes.AEROSPIKE_STATUS);
if (SemconvStability.emitStableHttpSemconv()) {
attributes.add(SemanticAttributes.NETWORK_TYPE);
attributes.add(SemanticAttributes.NETWORK_TRANSPORT);
attributes.add(SemanticAttributes.SERVER_ADDRESS);
attributes.add(SemanticAttributes.SERVER_PORT);
}
if (SemconvStability.emitOldHttpSemconv()) {
attributes.add(SemanticAttributes.NET_SOCK_PEER_ADDR);
attributes.add(SemanticAttributes.NET_SOCK_PEER_NAME);
attributes.add(SemanticAttributes.NET_SOCK_PEER_PORT);
}

((ExtendedDoubleHistogramBuilder) builder).setAttributesAdvice(attributes);
}
}
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.instrumentation.api.instrumenter.db;

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,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.instrumenter.db;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import javax.annotation.Nullable;

final class DbMessageSizeUtil {

@Nullable
static Long getMessageSize(Attributes... attributesList) {
return getAttribute(AerospikeSemanticAttributes.AEROSPIKE_TRANSFER_SIZE, attributesList);
}

@Nullable
private static <T> T getAttribute(AttributeKey<T> key, Attributes... attributesList) {
for (Attributes attributes : attributesList) {
T value = attributes.get(key);
if (value != null) {
return value;
}
}
return null;
}

private DbMessageSizeUtil() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id("otel.javaagent-instrumentation")
}

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

Choose a reason for hiding this comment

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

usually we also add assertInverse.set(true) to verify that the instrumentation does not pass on older versions

Copy link
Author

Choose a reason for hiding this comment

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

Added, assertInverse

}
}

dependencies {
library("com.aerospike:aerospike-client:7.1.0")

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
testInstrumentation(project(":instrumentation:aerospike-client:aerospike-client-7.1:javaagent"))
Copy link
Contributor

Choose a reason for hiding this comment

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

should be added automatically

Copy link
Author

Choose a reason for hiding this comment

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

Yes, changed it to use the framework instrumentation library.

}

tasks {
test {
jvmArgs("-Djava.net.preferIPv4Stack=true")
usesService(gradle.sharedServices.registrations["testcontainersBuildService"].service)
}
}
Loading