Skip to content

Commit faca859

Browse files
author
Guillaume Polaert
authored
Merge pull request #20 from DataDog/dev
Deleting dev
2 parents 4605b8b + d9dc044 commit faca859

File tree

12 files changed

+249
-35
lines changed

12 files changed

+249
-35
lines changed

dd-java-agent-ittests/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@
169169
<scope>test</scope>
170170
</dependency>
171171

172+
173+
<!--JMS -->
174+
<dependency>
175+
<groupId>javax.jms</groupId>
176+
<artifactId>javax.jms-api</artifactId>
177+
<version>2.0.1</version>
178+
<scope>test</scope>
179+
</dependency>
180+
<dependency>
181+
<groupId>org.apache.activemq.tooling</groupId>
182+
<artifactId>activemq-junit</artifactId>
183+
<version>5.14.5</version>
184+
<scope>test</scope>
185+
</dependency>
186+
<dependency>
187+
<groupId>org.apache.activemq</groupId>
188+
<artifactId>activemq-broker</artifactId>
189+
<version>5.14.5</version>
190+
<scope>test</scope>
191+
</dependency>
192+
193+
172194
</dependencies>
173195
<build>
174196
<plugins>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.datadoghq.trace.instrument;
2+
3+
import io.opentracing.contrib.jms.TracingMessageProducer;
4+
import io.opentracing.contrib.jms.common.TracingMessageConsumer;
5+
import org.apache.activemq.ActiveMQConnectionFactory;
6+
import org.apache.activemq.command.ActiveMQQueue;
7+
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
8+
import org.junit.BeforeClass;
9+
import org.junit.ClassRule;
10+
import org.junit.Test;
11+
12+
import javax.jms.*;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
public class JMSInstrumentationTest {
17+
18+
19+
@ClassRule
20+
public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();
21+
private static Session session;
22+
private static ActiveMQQueue destination;
23+
24+
@BeforeClass
25+
public static void start() throws JMSException {
26+
27+
broker.start();
28+
ActiveMQConnectionFactory connectionFactory = broker.createConnectionFactory();
29+
30+
destination = new ActiveMQQueue("someQueue");
31+
Connection connection = connectionFactory.createConnection();
32+
connection.start();
33+
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
34+
35+
}
36+
37+
@Test
38+
public void test() throws Exception {
39+
40+
41+
MessageProducer producer = session.createProducer(destination);
42+
MessageConsumer consumer = session.createConsumer(destination);
43+
44+
assertThat(producer).isInstanceOf(TracingMessageProducer.class);
45+
assertThat(consumer).isInstanceOf(TracingMessageConsumer.class);
46+
}
47+
48+
}

dd-java-agent/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<ot.contrib.httpclient.version>0.0.2</ot.contrib.httpclient.version>
2828
<ot.contrib.cassandra.version>0.0.2</ot.contrib.cassandra.version>
2929
<ot.contrib.elasticsearch.version>0.0.2</ot.contrib.elasticsearch.version>
30+
<ot.contrib.jms.version>0.0.3</ot.contrib.jms.version>
3031
<!-- Others versions -->
3132
<java.version>1.7</java.version>
3233
</properties>
@@ -139,6 +140,42 @@
139140
<scope>provided</scope>
140141
</dependency>
141142

143+
<!-- JMS instrumentation -->
144+
<!--<dependency>-->
145+
<!--<groupId>io.opentracing.contrib</groupId>-->
146+
<!--<artifactId>opentracing-jms-1</artifactId>-->
147+
<!--<version>${ot.contrib.jms.version}</version>-->
148+
<!--<exclusions>-->
149+
<!--<exclusion>-->
150+
<!--<groupId>javax.jms</groupId>-->
151+
<!--<artifactId>jms-api</artifactId>-->
152+
<!--</exclusion>-->
153+
<!--</exclusions>-->
154+
<!--</dependency>-->
155+
<!--<dependency>-->
156+
<!--<groupId>javax.jms</groupId>-->
157+
<!--<artifactId>jms-api</artifactId>-->
158+
<!--<version>1.1-rev-1</version>-->
159+
<!--<scope>provided</scope>-->
160+
<!--</dependency>-->
161+
<dependency>
162+
<groupId>io.opentracing.contrib</groupId>
163+
<artifactId>opentracing-jms-2</artifactId>
164+
<version>${ot.contrib.jms.version}</version>
165+
<exclusions>
166+
<exclusion>
167+
<groupId>javax.jms</groupId>
168+
<artifactId>javax.jms-api</artifactId>
169+
</exclusion>
170+
</exclusions>
171+
</dependency>
172+
<dependency>
173+
<groupId>javax.jms</groupId>
174+
<artifactId>javax.jms-api</artifactId>
175+
<version>2.0.1</version>
176+
<scope>provided</scope>
177+
</dependency>
178+
142179
<!-- AWS SDK instrumentation -->
143180
<dependency>
144181
<groupId>io.opentracing.contrib</groupId>

dd-java-agent/src/main/java/io/opentracing/contrib/agent/helper/DDAgentTracingHelper.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
/**
1212
* This class provides helpfull stuff in order to easy patch object using Byteman rules
1313
*
14-
* @param <ObjectType> The type of the object to patch
14+
* @param <T> The type of the object to patch
1515
*/
16-
public abstract class DDAgentTracingHelper<ObjectType> extends OpenTracingHelper {
16+
public abstract class DDAgentTracingHelper<T> extends OpenTracingHelper {
1717

1818
private static final Logger LOGGER = Logger.getLogger(DDAgentTracingHelper.class.getCanonicalName());
1919

@@ -48,15 +48,22 @@ public abstract class DDAgentTracingHelper<ObjectType> extends OpenTracingHelper
4848
* @param args The object to patch, the type is defined by the subclass instantiation
4949
* @return The object patched
5050
*/
51-
public ObjectType patch(ObjectType args) {
51+
public T patch(T args) {
5252

53-
info("Try to patch " + args.getClass().getName());
54-
ObjectType patched;
53+
if (args == null) {
54+
info("Skipping " + rule.getName() + "' rule because the input arg is null");
55+
return args;
56+
}
57+
58+
String className = args.getClass().getName();
59+
info("Try to patch " + className);
60+
61+
T patched;
5562
try {
5663
patched = doPatch(args);
57-
info(args.getClass().getName() + " patched");
64+
info(className + " patched");
5865
} catch (Throwable e) {
59-
warning("Failed to patch" + args.getClass().getName() + ", reason: " + e.getMessage());
66+
warning("Failed to patch" + className + ", reason: " + e.getMessage());
6067
logStackTrace(e.getMessage(), e);
6168
patched = args;
6269
}
@@ -70,7 +77,7 @@ public ObjectType patch(ObjectType args) {
7077
* @return the object patched
7178
* @throws Exception The exceptions are managed directly to the patch method
7279
*/
73-
abstract protected ObjectType doPatch(ObjectType obj) throws Exception;
80+
abstract protected T doPatch(T obj) throws Exception;
7481

7582

7683
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.opentracing.contrib.agent.helper;
2+
3+
import io.opentracing.contrib.jms.common.TracingMessageConsumer;
4+
import org.jboss.byteman.rule.Rule;
5+
6+
import javax.jms.MessageConsumer;
7+
8+
public class JMSMessageConsumerHelper extends DDAgentTracingHelper<MessageConsumer> {
9+
10+
public JMSMessageConsumerHelper(Rule rule) {
11+
super(rule);
12+
}
13+
14+
15+
@Override
16+
public MessageConsumer patch(MessageConsumer args) {
17+
return super.patch(args);
18+
}
19+
20+
/**
21+
* Strategy: Wrapper the instance into a new one.
22+
*
23+
* @param consumer The JMS instance
24+
* @return A new instance with the old one wrapped
25+
* @throws Exception
26+
*/
27+
protected MessageConsumer doPatch(MessageConsumer consumer) throws Exception {
28+
if (consumer instanceof TracingMessageConsumer) {
29+
return consumer;
30+
}
31+
return new TracingMessageConsumer(consumer, tracer);
32+
}
33+
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.opentracing.contrib.agent.helper;
2+
3+
import io.opentracing.contrib.jms.TracingMessageProducer;
4+
import org.jboss.byteman.rule.Rule;
5+
6+
import javax.jms.MessageProducer;
7+
8+
public class JMSMessageProducerHelper extends DDAgentTracingHelper<MessageProducer> {
9+
10+
public JMSMessageProducerHelper(Rule rule) {
11+
super(rule);
12+
}
13+
14+
@Override
15+
public MessageProducer patch(MessageProducer args) {
16+
return super.patch(args);
17+
}
18+
19+
/**
20+
* Strategy: Wrapper the instance into a new one.
21+
*
22+
* @param producer The JMS instance
23+
* @return A new instance with the old one wrapped
24+
* @throws Exception
25+
*/
26+
protected MessageProducer doPatch(MessageProducer producer) throws Exception {
27+
if (producer instanceof TracingMessageProducer) {
28+
return producer;
29+
}
30+
return new TracingMessageProducer(producer, tracer);
31+
}
32+
}

dd-java-agent/src/main/resources/otarules.btm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,28 @@ IF TRUE
102102
DO
103103
patch($this)
104104
ENDRULE
105+
106+
107+
# Instrument JMS
108+
# ===========================
109+
RULE jms producer
110+
INTERFACE javax.jms.Session
111+
METHOD createProducer
112+
HELPER io.opentracing.contrib.agent.helper.JMSMessageProducerHelper
113+
AT EXIT
114+
IF TRUE
115+
DO
116+
$! = patch($!);
117+
ENDRULE
118+
119+
120+
RULE jms consumer
121+
INTERFACE javax.jms.Session
122+
METHOD createConsumer
123+
HELPER io.opentracing.contrib.agent.helper.JMSMessageConsumerHelper
124+
AT EXIT
125+
IF TRUE
126+
DO
127+
$! = patch($!);
128+
ENDRULE
129+

dd-trace/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
<description>Datadog core library</description>
1515
<url>https://github.com/datadog/dd-trace-java</url>
1616

17-
<properties>
18-
<java.version>1.6</java.version>
19-
</properties>
20-
2117
<dependencies>
2218

2319
<!-- Opentracing core -->

dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.datadoghq.trace.integration.DDSpanContextDecorator;
55
import com.fasterxml.jackson.annotation.JsonIgnore;
6+
import com.google.common.collect.Maps;
67
import io.opentracing.tag.Tags;
78

89
import java.util.*;
@@ -21,6 +22,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
2122
private final long traceId;
2223
private final long spanId;
2324
private final long parentId;
25+
private final String threadName = Thread.currentThread().getName();
26+
private final long threadId = Thread.currentThread().getId();
2427
private Map<String, String> baggageItems;
2528

2629
// DD attributes
@@ -126,7 +129,7 @@ public String getResourceName() {
126129
public boolean getErrorFlag() {
127130
return errorFlag;
128131
}
129-
132+
130133
public void setErrorFlag(boolean errorFlag) {
131134
this.errorFlag = errorFlag;
132135
}
@@ -193,6 +196,11 @@ public synchronized void setTag(String tag, Object value) {
193196
}
194197

195198
public synchronized Map<String, Object> getTags() {
199+
if(tags.isEmpty()) {
200+
tags = Maps.newHashMapWithExpectedSize(2);
201+
}
202+
tags.put(DDTags.THREAD_NAME, threadName);
203+
tags.put(DDTags.THREAD_ID, threadId);
196204
return Collections.unmodifiableMap(tags);
197205
}
198206

dd-trace/src/main/java/com/datadoghq/trace/DDTags.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ public class DDTags {
44
public static final String SPAN_TYPE = "span-type";
55
public static final String SERVICE_NAME = "service-name";
66
public static final String RESOURCE_NAME = "resource-name";
7+
public static final String THREAD_NAME = "thread-name";
8+
public static final String THREAD_ID = "thread-id";
79
}

0 commit comments

Comments
 (0)