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

Convert RabbitMQ to Instrumenter #4463

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions instrumentation/rabbitmq-2.7/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ muzzle {
dependencies {
library("com.rabbitmq:amqp-client:2.7.0")

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

testLibrary("org.springframework.amqp:spring-rabbit:1.1.0.RELEASE") {
exclude("com.rabbitmq", "amqp-client")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.rabbitmq;

import com.google.auto.value.AutoValue;
import com.rabbitmq.client.Channel;

@AutoValue
public abstract class ChannelAndMethod {

public static ChannelAndMethod create(Channel channel, String method) {
return new AutoValue_ChannelAndMethod(channel, method);
}

abstract Channel getChannel();

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

package io.opentelemetry.javaagent.instrumentation.rabbitmq;

import com.google.auto.value.AutoValue;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;

@AutoValue
abstract class DeliveryRequest {

static DeliveryRequest create(
String queue, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
return new AutoValue_DeliveryRequest(queue, envelope, properties, body);
}

abstract String getQueue();

abstract Envelope getEnvelope();

abstract AMQP.BasicProperties getProperties();

@SuppressWarnings("mutable")
abstract byte[] getBody();

String spanName() {
String queue = getQueue();
if (queue == null || queue.isEmpty()) {
return "<default> process";
} else if (queue.startsWith("amq.gen-")) {
return "<generated> process";
} else {
return queue + " process";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.rabbitmq;

import io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation;
import io.opentelemetry.instrumentation.api.instrumenter.messaging.MessagingAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import javax.annotation.Nullable;

public class RabbitChannelAttributesExtractor
extends MessagingAttributesExtractor<ChannelAndMethod, Void> {
@Override
public MessageOperation operation() {
return MessageOperation.SEND;
}

@Override
protected String system(ChannelAndMethod channelAndMethod) {
return "rabbitmq";
}

@Override
protected String destinationKind(ChannelAndMethod channelAndMethod) {
return SemanticAttributes.MessagingDestinationKindValues.QUEUE;
}

@Nullable
@Override
protected String destination(ChannelAndMethod channelAndMethod) {
return null;
}

@Override
protected boolean temporaryDestination(ChannelAndMethod channelAndMethod) {
return false;
}

@Nullable
@Override
protected String protocol(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected String protocolVersion(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected String url(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected String conversationId(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected Long messagePayloadSize(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected Long messagePayloadCompressedSize(ChannelAndMethod channelAndMethod) {
return null;
}

@Nullable
@Override
protected String messageId(ChannelAndMethod channelAndMethod, @Nullable Void unused) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitCommandInstrumentation.SpanHolder.CURRENT_RABBIT_CONTEXT;
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitTracer.tracer;
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitInstrumenterHelper.helper;
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitSingletons.channelInstrumenter;
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitSingletons.receiveInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.canThrow;
import static net.bytebuddy.matcher.ElementMatchers.isGetter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand Down Expand Up @@ -94,13 +96,21 @@ public static void onEnter(
@Advice.Origin("Channel.#m") String method,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelRequest") ChannelAndMethod request) {
callDepth = CallDepth.forClass(Channel.class);
if (callDepth.getAndIncrement() > 0) {
return;
}

context = tracer().startSpan(method, channel.getConnection());
Context parentContext = Java8BytecodeBridge.currentContext();
request = ChannelAndMethod.create(channel, method);

if (!channelInstrumenter().shouldStart(parentContext, request)) {
return;
}

context = channelInstrumenter().start(parentContext, request);
CURRENT_RABBIT_CONTEXT.set(context);
scope = context.makeCurrent();
}
Expand All @@ -110,19 +120,16 @@ public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
@Advice.Local("otelScope") Scope scope,
@Advice.Local("otelRequest") ChannelAndMethod request) {
if (callDepth.decrementAndGet() > 0) {
return;
}

scope.close();

CURRENT_RABBIT_CONTEXT.remove();
if (throwable != null) {
tracer().endExceptionally(context, throwable);
} else {
tracer().end(context);
}
channelInstrumenter().end(context, request, null, throwable);
}
}

Expand All @@ -139,7 +146,7 @@ public static void setSpanNameAddHeaders(
Span span = Java8BytecodeBridge.spanFromContext(context);

if (span.getSpanContext().isValid()) {
tracer().onPublish(span, exchange, routingKey);
helper().onPublish(span, exchange, routingKey);
if (body != null) {
span.setAttribute(
SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES, (long) body.length);
Expand All @@ -149,13 +156,13 @@ public static void setSpanNameAddHeaders(
if (props == null) {
props = MessageProperties.MINIMAL_BASIC;
}
tracer().onProps(span, props);
helper().onProps(span, props);

// We need to copy the BasicProperties and provide a header map we can modify
Map<String, Object> headers = props.getHeaders();
headers = (headers == null) ? new HashMap<>() : new HashMap<>(headers);

tracer().inject(context, headers, MapSetter.INSTANCE);
helper().inject(context, headers, MapSetter.INSTANCE);

props =
new AMQP.BasicProperties(
Expand All @@ -181,32 +188,37 @@ public static void setSpanNameAddHeaders(
public static class ChannelGetAdvice {

@Advice.OnMethodEnter
public static long takeTimestamp(@Advice.Local("otelCallDepth") CallDepth callDepth) {
public static void takeTimestamp(
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelTimer") Timer timer) {
callDepth = CallDepth.forClass(Channel.class);
callDepth.getAndIncrement();
return System.currentTimeMillis();
timer = Timer.start();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void extractAndStartSpan(
@Advice.This Channel channel,
@Advice.Argument(0) String queue,
@Advice.Enter long startTime,
@Advice.Return GetResponse response,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelTimer") Timer timer) {
if (callDepth.decrementAndGet() > 0) {
return;
}

Context parentContext = Java8BytecodeBridge.currentContext();
ReceiveRequest request =
ReceiveRequest.create(queue, timer, response, channel.getConnection());
if (!receiveInstrumenter().shouldStart(parentContext, request)) {
return;
}

// can't create span and put into scope in method enter above, because can't add parent after
// span creation
Context context = tracer().startGetSpan(queue, startTime, response, channel.getConnection());
if (throwable != null) {
tracer().endExceptionally(context, throwable);
} else {
tracer().end(context);
}
Context context = receiveInstrumenter().start(parentContext, request);
receiveInstrumenter().end(context, request, null, throwable);
}
}

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

package io.opentelemetry.javaagent.instrumentation.rabbitmq;

import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import javax.annotation.Nullable;

public class RabbitChannelNetAttributesExtractor
extends NetClientAttributesExtractor<ChannelAndMethod, Void> {
@Nullable
@Override
public String transport(ChannelAndMethod channelAndMethod, @Nullable Void unused) {
return null;
}

@Nullable
@Override
public String peerName(ChannelAndMethod channelAndMethod, @Nullable Void unused) {
return channelAndMethod.getChannel().getConnection().getAddress().getHostName();
}

@Nullable
@Override
public Integer peerPort(ChannelAndMethod channelAndMethod, @Nullable Void unused) {
return channelAndMethod.getChannel().getConnection().getPort();
}

@Nullable
@Override
public String peerIp(ChannelAndMethod channelAndMethod, @Nullable Void unused) {
return channelAndMethod.getChannel().getConnection().getAddress().getHostAddress();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void setSpanNameAddHeaders(@Advice.This Command command) {

Context context = CURRENT_RABBIT_CONTEXT.get();
if (context != null && command.getMethod() != null) {
RabbitTracer.onCommand(Java8BytecodeBridge.spanFromContext(context), command);
RabbitInstrumenterHelper.onCommand(Java8BytecodeBridge.spanFromContext(context), command);
}
}
}
Expand Down
Loading