Skip to content

Commit 866ba33

Browse files
committed
Add server address and port for Spymemcached
1 parent 186960f commit 866ba33

File tree

3 files changed

+158
-27
lines changed

3 files changed

+158
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.spymemcached;
7+
8+
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
9+
import java.net.InetSocketAddress;
10+
import java.net.SocketAddress;
11+
import java.util.Collection;
12+
import javax.annotation.Nullable;
13+
import net.spy.memcached.MemcachedConnection;
14+
import net.spy.memcached.MemcachedNode;
15+
16+
final class SpymemcachedNetworkAttributesGetter
17+
implements ServerAttributesGetter<SpymemcachedRequest> {
18+
19+
@Nullable
20+
@Override
21+
public String getServerAddress(SpymemcachedRequest request) {
22+
MemcachedConnection connection = request.getConnection();
23+
if (connection != null) {
24+
Collection<MemcachedNode> nodes = connection.getLocator().getAll();
25+
if (!nodes.isEmpty()) {
26+
SocketAddress socketAddress = nodes.iterator().next().getSocketAddress();
27+
if (socketAddress instanceof InetSocketAddress) {
28+
return ((InetSocketAddress) socketAddress).getHostString();
29+
}
30+
}
31+
}
32+
return null;
33+
}
34+
35+
@Nullable
36+
@Override
37+
public Integer getServerPort(SpymemcachedRequest request) {
38+
MemcachedConnection connection = request.getConnection();
39+
if (connection != null) {
40+
Collection<MemcachedNode> nodes = connection.getLocator().getAll();
41+
if (!nodes.isEmpty()) {
42+
SocketAddress socketAddress = nodes.iterator().next().getSocketAddress();
43+
if (socketAddress instanceof InetSocketAddress) {
44+
return ((InetSocketAddress) socketAddress).getPort();
45+
}
46+
}
47+
}
48+
return null;
49+
}
50+
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedSingletons.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
1212
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1313
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
14+
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
1415

1516
public final class SpymemcachedSingletons {
1617
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spymemcached-2.12";
@@ -19,13 +20,15 @@ public final class SpymemcachedSingletons {
1920

2021
static {
2122
SpymemcachedAttributesGetter dbAttributesGetter = new SpymemcachedAttributesGetter();
23+
SpymemcachedNetworkAttributesGetter netAttributesGetter = new SpymemcachedNetworkAttributesGetter();
2224

2325
INSTRUMENTER =
2426
Instrumenter.builder(
2527
GlobalOpenTelemetry.get(),
2628
INSTRUMENTATION_NAME,
2729
DbClientSpanNameExtractor.create(dbAttributesGetter))
2830
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
31+
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
2932
.addOperationMetrics(DbClientMetrics.get())
3033
.buildInstrumenter(SpanKindExtractor.alwaysClient());
3134
}

0 commit comments

Comments
 (0)