Skip to content

Commit

Permalink
Stop using deprecated NetPeerAttributes outside of the deprecated pac…
Browse files Browse the repository at this point in the history
…kage (#5024)
  • Loading branch information
iNikem committed Jan 8, 2022
1 parent c75c01f commit 3144859
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.couchbase.client.core.message.CouchbaseRequest;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.api.field.VirtualField;
import io.opentelemetry.instrumentation.api.tracer.net.NetPeerAttributes;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
Expand Down Expand Up @@ -65,9 +64,12 @@ public static void addNetworkTagsToSpan(
VirtualField<CouchbaseRequest, Span> virtualField =
VirtualField.find(CouchbaseRequest.class, Span.class);

// TODO add support for peer service name
Span span = virtualField.get(request);
if (span != null) {
NetPeerAttributes.INSTANCE.setNetPeer(span, remoteHostname, null);
if (remoteHostname != null) {
span.setAttribute(SemanticAttributes.NET_PEER_NAME, remoteHostname);
}

if (remoteSocket != null) {
int splitIndex = remoteSocket.lastIndexOf(":");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.lettuce.v5_1;

import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP;

import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import io.opentelemetry.instrumentation.lettuce.v5_1.OpenTelemetryTracing.OpenTelemetryEndpoint;
import javax.annotation.Nullable;

final class LettuceNetAttributesExtractor
extends NetClientAttributesExtractor<OpenTelemetryEndpoint, Void> {

@Override
public String transport(OpenTelemetryEndpoint endpoint, @Nullable Void unused) {
return IP_TCP;
}

@Nullable
@Override
public String peerName(OpenTelemetryEndpoint endpoint, @Nullable Void unused) {
return endpoint.name;
}

@Override
public Integer peerPort(OpenTelemetryEndpoint endpoint, @Nullable Void unused) {
return endpoint.port;
}

@Nullable
@Override
public String peerIp(OpenTelemetryEndpoint endpoint, @Nullable Void unused) {
return endpoint.ip;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.instrumentation.lettuce.v5_1;

import static io.opentelemetry.instrumentation.lettuce.common.LettuceArgSplitter.splitArgs;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP;

import io.lettuce.core.output.CommandOutput;
import io.lettuce.core.protocol.CompleteableCommand;
Expand All @@ -16,14 +15,14 @@
import io.lettuce.core.tracing.Tracer;
import io.lettuce.core.tracing.TracerProvider;
import io.lettuce.core.tracing.Tracing;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.db.RedisCommandSanitizer;
import io.opentelemetry.instrumentation.api.tracer.AttributeSetter;
import io.opentelemetry.instrumentation.api.tracer.net.NetPeerAttributes;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes.DbSystemValues;
import java.net.InetSocketAddress;
Expand All @@ -35,6 +34,8 @@

final class OpenTelemetryTracing implements Tracing {

private static final LettuceNetAttributesExtractor netAttributesExtractor =
new LettuceNetAttributesExtractor();
private final TracerProvider tracerProvider;

OpenTelemetryTracing(io.opentelemetry.api.trace.Tracer tracer) {
Expand Down Expand Up @@ -108,7 +109,7 @@ public Context getSpanContext() {
}
}

private static class OpenTelemetryEndpoint implements Endpoint {
static class OpenTelemetryEndpoint implements Endpoint {
@Nullable final String ip;
final int port;
@Nullable final String name;
Expand Down Expand Up @@ -191,11 +192,7 @@ public synchronized Tracer.Span name(String name) {
@Override
public synchronized Tracer.Span remoteEndpoint(Endpoint endpoint) {
if (endpoint instanceof OpenTelemetryEndpoint) {
if (span != null) {
fillEndpoint(span::setAttribute, (OpenTelemetryEndpoint) endpoint);
} else {
fillEndpoint(spanBuilder::setAttribute, (OpenTelemetryEndpoint) endpoint);
}
fillEndpoint(span, spanBuilder, (OpenTelemetryEndpoint) endpoint);
}
return this;
}
Expand Down Expand Up @@ -316,8 +313,14 @@ private void finish(Span span) {
}
}

private static void fillEndpoint(AttributeSetter span, OpenTelemetryEndpoint endpoint) {
span.setAttribute(SemanticAttributes.NET_TRANSPORT, IP_TCP);
NetPeerAttributes.INSTANCE.setNetPeer(span, endpoint.name, endpoint.ip, endpoint.port);
private static void fillEndpoint(
@Nullable Span span, SpanBuilder spanBuilder, OpenTelemetryEndpoint endpoint) {
AttributesBuilder attributesBuilder = Attributes.builder();
netAttributesExtractor.onEnd(attributesBuilder, endpoint, null, null);
if (span != null) {
span.setAllAttributes(attributesBuilder.build());
} else {
spanBuilder.setAllAttributes(attributesBuilder.build());
}
}
}

0 comments on commit 3144859

Please sign in to comment.