Skip to content
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
1 change: 0 additions & 1 deletion dd-java-agent-ittests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<name>dd-java-agent-ittests</name>
<description>Datadog Java Agent integration tests</description>
<url>https://github.com/datadog/dd-trace-java</url>
<packaging>pom</packaging>


<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.datadoghq.trace.agent;

import com.datadoghq.trace.DDTags;
import com.datadoghq.trace.Trace;
import io.opentracing.tag.StringTag;
import io.opentracing.util.GlobalTracer;

public class SayTracedHello {

@Trace(operationName="SAY_HELLO",tagsKV={"service-name","test"})
@Trace(operationName="SAY_HELLO")
public static String sayHello(){
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test");
return "hello!";
}

@Trace(operationName="SAY_HA",tagsKV={"service-name","test","span-type","DB"})
@Trace(operationName="SAY_HA")
public static String sayHA(){
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test");
new StringTag(DDTags.SPAN_TYPE).set(GlobalTracer.get().activeSpan(), "DB");
return "HA!!";
}

@Trace(operationName="NEW_TRACE",tagsKV={"service-name","test2"})
@Trace(operationName="NEW_TRACE")
public static String sayHELLOsayHA(){
new StringTag(DDTags.SERVICE_NAME).set(GlobalTracer.get().activeSpan(), "test2");
return sayHello()+sayHA();
}

Expand Down
4 changes: 2 additions & 2 deletions dd-java-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public void myMethod() throws InterruptedException{

By default, the operation name attached to the spawn span will be the name of the method and no meta tags will be attached.

You can use the `operationName` and `tagsKV` attributes to customize your trace:
You can use the `operationName` customize your trace:

```java
@Trace(operationName="Before DB",tagsKV={"mytag","myvalue"})
@Trace(operationName="Before DB")
public void myMethod() throws InterruptedException{
....
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public static void loadAnnotationsRules(String... scannedPackages) {
String ruleText =
CURRENT_SPAN_EXISTS+
buildSpan(javassistMethod)+
buildWithTags(javassistMethod)+
START;
RuleScript script = createRuleScript("Start Active Span ",cc, javassistMethod, Location.create(LocationType.ENTRY,""),ruleText);
generatedScripts.append(script).append("\n");
Expand Down Expand Up @@ -264,26 +263,4 @@ private static String buildSpan(CtMethod javassistMethod){
}
return BUILD_SPAN+javassistMethod.getName()+CLOSE_PARENTHESIS;
}

private static String buildWithTags(CtMethod javassistMethod){
try {
Trace trace = (Trace) javassistMethod.getAnnotation(Trace.class);
if(trace.tagsKV()!=null && trace.tagsKV().length>0){
if(trace.tagsKV().length%2==0){
StringBuilder sb = new StringBuilder();
for(int i = 0;i<trace.tagsKV().length;i=i+2){
sb.append(".withTag(\"")
.append(trace.tagsKV()[i]).append("\",\"").append(trace.tagsKV()[i+1])
.append(CLOSE_PARENTHESIS);
}
return sb.toString();
}else{
throw new IllegalArgumentException("The 'tagsKV' annotation attribute must define Key/Value pairs only");
}
}
} catch (Exception e) {
log.log(Level.WARNING, "Error when building injection rule on method " + javassistMethod + ". Fallback on default value.", e);
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,4 @@
* The operation name to set. By default it takes the method's name
*/
String operationName() default "";

/**
* Tags Key/Values. Null by default, it sets only K/V String pairs such as {"service-name", "my-service", etc...}
*/
String[] tagsKV() default {};

}
12 changes: 12 additions & 0 deletions dd-trace-examples/dropwizard-mongo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<version>${dd-trace.version}</version>
</dependency>

<!-- These dependencies are required for adding tags -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>${opentracing.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>${opentracing.version}</version>
</dependency>

<!-- Application dependencies -->
<dependency>
<groupId>io.dropwizard</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;

import com.datadoghq.trace.Trace;
import io.opentracing.tag.StringTag;
import io.opentracing.util.GlobalTracer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -21,8 +23,9 @@ public static void main(String[] args) throws Exception{
System.out.println("After execute");
}

@Trace(tagsKV={"service-name","TracedClient"})
@Trace
private static void executeCall() throws IOException {
new StringTag("service-name").set(GlobalTracer.get().activeSpan(), "TracedClient");
OkHttpClient client = new OkHttpClient().newBuilder().build();

Request request = new Request.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import io.opentracing.tag.StringTag;
import io.opentracing.util.GlobalTracer;
import org.bson.Document;

import javax.ws.rs.GET;
Expand Down Expand Up @@ -101,24 +103,24 @@ public List<Book> getBooks() throws InterruptedException {
}

/**
* The beforeDB is traced using the annotation @trace
* Tags, the operation name can be configured using tagsKV and operationName options
* The beforeDB is traced using the annotation @trace with a custom operationName and a custom tag.
*
* @throws InterruptedException
*/
@Trace(operationName = "Before DB", tagsKV = {"mytag", "myvalue"})
@Trace(operationName = "Before DB")
public void beforeDB() throws InterruptedException {
new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue");
Thread.sleep(333);
}

/**
* The beforeDB is traced using the annotation @trace
* Tags, the operation name can be configured using tagsKV and operationName options
* The beforeDB is traced using the annotation @trace with a custom operationName and a custom tag.
*
* @throws InterruptedException
*/
@Trace(operationName = "After DB", tagsKV = {"mytag", "myvalue"})
@Trace(operationName = "After DB")
public void afterDB() throws InterruptedException {
new StringTag("mytag").set(GlobalTracer.get().activeSpan(), "myvalue");
Thread.sleep(111);
}

Expand Down