Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.spymemcached;

import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collection;
import javax.annotation.Nullable;
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.MemcachedNode;

final class SpymemcachedNetworkAttributesGetter
implements ServerAttributesGetter<SpymemcachedRequest> {

@Nullable
@Override
public String getServerAddress(SpymemcachedRequest request) {
MemcachedConnection connection = request.getConnection();
if (connection != null) {
Collection<MemcachedNode> nodes = connection.getLocator().getAll();
if (!nodes.isEmpty()) {
SocketAddress socketAddress = nodes.iterator().next().getSocketAddress();
if (socketAddress instanceof InetSocketAddress) {
return ((InetSocketAddress) socketAddress).getHostString();
}
}
}
return null;
}

@Nullable
@Override
public Integer getServerPort(SpymemcachedRequest request) {
MemcachedConnection connection = request.getConnection();
if (connection != null) {
Collection<MemcachedNode> nodes = connection.getLocator().getAll();
if (!nodes.isEmpty()) {
SocketAddress socketAddress = nodes.iterator().next().getSocketAddress();
if (socketAddress instanceof InetSocketAddress) {
return ((InetSocketAddress) socketAddress).getPort();
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;

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

static {
SpymemcachedAttributesGetter dbAttributesGetter = new SpymemcachedAttributesGetter();
SpymemcachedNetworkAttributesGetter netAttributesGetter = new SpymemcachedNetworkAttributesGetter();

INSTRUMENTER =
Instrumenter.builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
DbClientSpanNameExtractor.create(dbAttributesGetter))
.addAttributesExtractor(DbClientAttributesExtractor.create(dbAttributesGetter))
.addAttributesExtractor(ServerAttributesExtractor.create(netAttributesGetter))
.addOperationMetrics(DbClientMetrics.get())
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}
Expand Down
Loading
Loading