Skip to content

Commit 5723d26

Browse files
author
Rishabh
authored
Controlled logging RabbitMq Resolver (#127)
1 parent 6d07b5a commit 5723d26

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

hypertrace-trace-enricher/hypertrace-trace-enricher-impl/src/main/java/org/hypertrace/traceenricher/enrichment/enrichers/resolver/backend/RabbitMqBackendResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class RabbitMqBackendResolver extends AbstractBackendResolver {
1717

1818
@Override
1919
public Optional<Entity> resolveEntity(Event event, StructuredTraceGraph structuredTraceGraph) {
20+
if (!MessagingSemanticConventionUtils.isRabbitMqBackend(event)) {
21+
return Optional.empty();
22+
}
2023
Optional<String> routingKey = MessagingSemanticConventionUtils.getRabbitMqRoutingKey(event);
2124

2225
if (routingKey.isEmpty() || StringUtils.isEmpty(routingKey.get())) {

semantic-convention-utils/src/main/java/org/hypertrace/semantic/convention/utils/messaging/MessagingSemanticConventionUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ public class MessagingSemanticConventionUtils {
2626
* @return Routing key for Rabbit mq messaging system
2727
*/
2828
public static Optional<String> getRabbitMqRoutingKey(Event event) {
29+
if (!isRabbitMqBackend(event)) {
30+
return Optional.empty();
31+
}
2932
return Optional.ofNullable(SpanAttributeUtils.getFirstAvailableStringAttribute(
3033
event, RABBITMQ_ROUTING_KEYS));
3134
}
35+
36+
public static boolean isRabbitMqBackend(Event event) {
37+
return SpanAttributeUtils.containsAttributeKey(event, RawSpanConstants.getValue(RabbitMq.RABBIT_MQ_ROUTING_KEY))
38+
|| SpanAttributeUtils.containsAttributeKey(event, OtelMessagingSemanticConventions.RABBITMQ_ROUTING_KEY.getValue());
39+
}
3240
}

semantic-convention-utils/src/test/java/org/hypertrace/semantic/convention/utils/messaging/OtelMessagingSemanticConventionUtilsTest.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import java.util.Map;
44
import java.util.Optional;
55
import org.hypertrace.core.semantic.convention.constants.messaging.OtelMessagingSemanticConventions;
6-
import org.hypertrace.semantic.convention.utils.SemanticConventionTestUtil;
76
import org.hypertrace.core.datamodel.Attributes;
87
import org.hypertrace.core.datamodel.Event;
98
import org.hypertrace.core.span.constants.RawSpanConstants;
109
import org.hypertrace.core.span.constants.v1.RabbitMq;
10+
import org.junit.jupiter.api.Assertions;
1111
import org.junit.jupiter.api.Test;
1212

13+
import static org.hypertrace.semantic.convention.utils.SemanticConventionTestUtil.buildAttributeValue;
14+
import static org.hypertrace.semantic.convention.utils.SemanticConventionTestUtil.buildAttributes;
1315
import static org.junit.jupiter.api.Assertions.assertEquals;
1416
import static org.junit.jupiter.api.Assertions.assertTrue;
1517
import static org.mockito.Mockito.mock;
@@ -25,26 +27,49 @@ public void testGetRabbitMqRoutingKey() {
2527
Event e = mock(Event.class);
2628
// otel format
2729
String routingKey = "otelRoutingKey";
28-
Attributes attributes = SemanticConventionTestUtil.buildAttributes(
29-
Map.of(OtelMessagingSemanticConventions.RABBITMQ_ROUTING_KEY.getValue(), SemanticConventionTestUtil.buildAttributeValue(routingKey)));
30+
Attributes attributes = buildAttributes(
31+
Map.of(OtelMessagingSemanticConventions.RABBITMQ_ROUTING_KEY.getValue(), buildAttributeValue(routingKey)));
3032
when(e.getAttributes()).thenReturn(attributes);
3133
Optional<String> v = MessagingSemanticConventionUtils.getRabbitMqRoutingKey(e);
3234
assertEquals(routingKey, v.get());
3335

3436
// other format
3537
routingKey = "otherRoutingKey";
36-
attributes = SemanticConventionTestUtil.buildAttributes(
37-
Map.of(RawSpanConstants.getValue(RabbitMq.RABBIT_MQ_ROUTING_KEY), SemanticConventionTestUtil
38-
.buildAttributeValue(routingKey)));
38+
attributes = buildAttributes(
39+
Map.of(RawSpanConstants.getValue(RabbitMq.RABBIT_MQ_ROUTING_KEY), buildAttributeValue(routingKey)));
3940
when(e.getAttributes()).thenReturn(attributes);
4041
v = MessagingSemanticConventionUtils.getRabbitMqRoutingKey(e);
4142
assertEquals(routingKey, v.get());
4243

4344
// routing key absent
44-
attributes = SemanticConventionTestUtil.buildAttributes(
45-
Map.of("span.kind", SemanticConventionTestUtil.buildAttributeValue("client")));
45+
attributes = buildAttributes(
46+
Map.of("span.kind", buildAttributeValue("client")));
4647
when(e.getAttributes()).thenReturn(attributes);
4748
v = MessagingSemanticConventionUtils.getRabbitMqRoutingKey(e);
4849
assertTrue(v.isEmpty());
4950
}
51+
52+
@Test
53+
public void testIsRabbitMqBackend() {
54+
Event e = mock(Event.class);
55+
// otel format
56+
String routingKey = "otelRoutingKey";
57+
Attributes attributes = buildAttributes(
58+
Map.of(OtelMessagingSemanticConventions.RABBITMQ_ROUTING_KEY.getValue(), buildAttributeValue(routingKey)));
59+
when(e.getAttributes()).thenReturn(attributes);
60+
boolean v = MessagingSemanticConventionUtils.isRabbitMqBackend(e);
61+
assertTrue(v);
62+
// other format
63+
routingKey = "otherRoutingKey";
64+
attributes = buildAttributes(
65+
Map.of(RawSpanConstants.getValue(RabbitMq.RABBIT_MQ_ROUTING_KEY), buildAttributeValue(routingKey)));
66+
when(e.getAttributes()).thenReturn(attributes);
67+
v = MessagingSemanticConventionUtils.isRabbitMqBackend(e);
68+
assertTrue(v);
69+
// not present
70+
attributes = buildAttributes(Map.of());
71+
when(e.getAttributes()).thenReturn(attributes);
72+
v = MessagingSemanticConventionUtils.isRabbitMqBackend(e);
73+
Assertions.assertFalse(v);
74+
}
5075
}

0 commit comments

Comments
 (0)