Skip to content

Commit

Permalink
Apply both serverAttributes & networkAttributes to Lettuce 5.1 (#10197)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
  • Loading branch information
heyams and trask authored Jan 12, 2024
1 parent e843d08 commit 36ed203
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void testAsyncSubscriberWithSpecificThreadPool() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET a ?"))
.hasEventsSatisfyingExactly(
Expand All @@ -67,6 +69,8 @@ void testAsyncSubscriberWithSpecificThreadPool() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET a"))
.hasEventsSatisfyingExactly(
Expand Down

This file was deleted.

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

package io.opentelemetry.instrumentation.lettuce.v5_1;

import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesGetter;
import io.opentelemetry.instrumentation.lettuce.v5_1.OpenTelemetryTracing.OpenTelemetryEndpoint;
import java.net.InetSocketAddress;
import javax.annotation.Nullable;

class LettuceServerAttributesGetter
implements ServerAttributesGetter<OpenTelemetryEndpoint>,
NetworkAttributesGetter<OpenTelemetryEndpoint, Void> {

@Nullable
@Override
public String getServerAddress(OpenTelemetryEndpoint request) {
if (request.address != null) {
return request.address.getHostString();
}
return null;
}

@Nullable
@Override
public Integer getServerPort(OpenTelemetryEndpoint request) {
if (request.address != null) {
return request.address.getPort();
}
return null;
}

@Nullable
@Override
public InetSocketAddress getNetworkPeerInetSocketAddress(
OpenTelemetryEndpoint openTelemetryEndpoint, @Nullable Void unused) {
return openTelemetryEndpoint.address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.opentelemetry.instrumentation.api.incubator.semconv.db.RedisCommandSanitizer;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.network.ServerAttributesExtractor;
import io.opentelemetry.semconv.SemanticAttributes;
import io.opentelemetry.semconv.SemanticAttributes.DbSystemValues;
import java.net.InetSocketAddress;
Expand All @@ -39,7 +40,9 @@
final class OpenTelemetryTracing implements Tracing {

private static final AttributesExtractor<OpenTelemetryEndpoint, Void> serverAttributesExtractor =
NetworkAttributesExtractor.create(new LettuceNetworkAttributesGetter());
ServerAttributesExtractor.create(new LettuceServerAttributesGetter());
private static final AttributesExtractor<OpenTelemetryEndpoint, Void> networkAttributesExtractor =
NetworkAttributesExtractor.create(new LettuceServerAttributesGetter());
private final TracerProvider tracerProvider;

OpenTelemetryTracing(io.opentelemetry.api.trace.Tracer tracer, RedisCommandSanitizer sanitizer) {
Expand Down Expand Up @@ -204,7 +207,8 @@ public synchronized Tracer.Span remoteEndpoint(Endpoint endpoint) {
private void fillEndpoint(OpenTelemetryEndpoint endpoint) {
AttributesBuilder attributesBuilder = Attributes.builder();
Context currentContext = span == null ? context : context.with(span);
serverAttributesExtractor.onEnd(attributesBuilder, currentContext, endpoint, null, null);
serverAttributesExtractor.onStart(attributesBuilder, currentContext, endpoint);
networkAttributesExtractor.onEnd(attributesBuilder, currentContext, endpoint, null, null);
if (span != null) {
span.setAllAttributes(attributesBuilder.build());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ void testSetCommandUsingFutureGetWithTimeout() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET TESTSETKEY ?"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -189,6 +191,8 @@ void testGetCommandChainedWithThenAccept() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET TESTKEY"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -265,6 +269,8 @@ void testGetNonExistentKeyCommandWithHandleAsyncAndChainedWithThenApply() throws
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(
SemanticAttributes.DB_STATEMENT, "GET NON_EXISTENT_KEY"))
Expand Down Expand Up @@ -329,6 +335,8 @@ void testCommandWithNoArgumentsUsingBiconsumer() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -386,6 +394,8 @@ void testHashSetAndThenNestApplyToHashGetall() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(
SemanticAttributes.DB_STATEMENT,
Expand All @@ -402,6 +412,8 @@ void testHashSetAndThenNestApplyToHashGetall() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "HGETALL TESTHM"))
.hasEventsSatisfyingExactly(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ void testSetCommandWithSubscribeOnDefinedConsumer() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET TESTSETKEY ?"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -128,6 +130,8 @@ void testGetCommandWithLambdaFunction() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET TESTKEY"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -175,6 +179,8 @@ void testGetNonExistentKeyCommand() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET NON_EXISTENT_KEY"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -210,6 +216,8 @@ void testCommandWithNoArguments() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"))
.hasEventsSatisfyingExactly(
Expand All @@ -232,6 +240,8 @@ void testCommandFluxPublisher() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "COMMAND"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -275,6 +285,8 @@ void testBlockingSubscriber() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET a ?"))
.hasEventsSatisfyingExactly(
Expand All @@ -288,6 +300,8 @@ void testBlockingSubscriber() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET a"))
.hasEventsSatisfyingExactly(
Expand Down Expand Up @@ -315,6 +329,8 @@ void testAsyncSubscriber() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET a ?"))
.hasEventsSatisfyingExactly(
Expand All @@ -328,6 +344,8 @@ void testAsyncSubscriber() {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET a"))
.hasEventsSatisfyingExactly(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ void testAuthCommand() throws Exception {
equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(SemanticAttributes.SERVER_ADDRESS, "localhost"),
equalTo(SemanticAttributes.SERVER_PORT, port),
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "AUTH ?"))
.hasEventsSatisfyingExactly(
Expand Down
Loading

0 comments on commit 36ed203

Please sign in to comment.