Skip to content

Commit 61ca5fd

Browse files
author
Guillaume Polaert
committed
[PR #1] Migrating all things to the span context (and add setters, and do not instantiate HashMap if it's not needed)
1 parent 1d388dd commit 61ca5fd

7 files changed

Lines changed: 163 additions & 96 deletions

File tree

src/main/java/com/datadoghq/trace/impl/DDSpan.java

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.datadoghq.trace.impl;
22

3+
import com.fasterxml.jackson.annotation.JsonGetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
5+
import io.opentracing.Span;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
39
import java.util.HashMap;
410
import java.util.List;
511
import java.util.Map;
612
import java.util.Map.Entry;
713
import java.util.concurrent.TimeUnit;
814

9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
12-
import com.fasterxml.jackson.annotation.JsonGetter;
13-
import com.fasterxml.jackson.annotation.JsonIgnore;
14-
15-
import io.opentracing.Span;
16-
1715
/**
1816
* Represents an in-flight span in the opentracing system.
1917
* <p>
@@ -22,14 +20,7 @@
2220
*/
2321
public class DDSpan implements io.opentracing.Span {
2422

25-
/**
26-
* Each span have an operation name describing the current span
27-
*/
28-
private String operationName;
29-
/**
30-
* Tags are associated to the current span, they will not propagate to the children span
31-
*/
32-
private Map<String, Object> tags;
23+
3324
/**
3425
* StartTime stores the creation time of the span in milliseconds
3526
*/
@@ -53,19 +44,13 @@ public class DDSpan implements io.opentracing.Span {
5344
* A simple constructor.
5445
* Currently, users have
5546
*
56-
* @param operationName the operation name associated to the span
57-
* @param tags Tags attached to the span
5847
* @param timestampMicro if set, use this time instead of the auto-generated time
5948
* @param context the context
6049
*/
6150
protected DDSpan(
62-
String operationName,
63-
Map<String, Object> tags,
6451
long timestampMicro,
6552
DDSpanContext context) {
6653

67-
this.operationName = operationName;
68-
this.tags = tags;
6954
this.context = context;
7055

7156
// record the start time in nano (current milli + nano delta)
@@ -79,11 +64,6 @@ protected DDSpan(
7964
// track each span of the trace
8065
this.context.getTrace().add(this);
8166

82-
// check DD attributes required
83-
// FIXME Remove IAE
84-
if (this.context.getServiceName() == null) {
85-
throw new IllegalArgumentException("No ServiceName provided");
86-
}
8767
}
8868

8969
/* (non-Javadoc)
@@ -145,34 +125,26 @@ private boolean isRootSpan() {
145125
* @see io.opentracing.Span#setTag(java.lang.String, java.lang.String)
146126
*/
147127
public Span setTag(String tag, String value) {
148-
return setTag(tag, (Object) value);
128+
this.context().setTag(tag, (Object) value);
129+
return this;
149130
}
150131

151132
/* (non-Javadoc)
152133
* @see io.opentracing.Span#setTag(java.lang.String, boolean)
153134
*/
154135
public Span setTag(String tag, boolean value) {
155-
return setTag(tag, (Object) value);
136+
this.context().setTag(tag, (Object) value);
137+
return this;
156138
}
157139

158140
/* (non-Javadoc)
159141
* @see io.opentracing.Span#setTag(java.lang.String, java.lang.Number)
160142
*/
161143
public Span setTag(String tag, Number value) {
162-
return this.setTag(tag, (Object) value);
144+
this.context().setTag(tag, (Object) value);
145+
return this;
163146
}
164147

165-
/**
166-
* Add a tag to the span. Tags are not propagated to the children
167-
*
168-
* @param tag the tag-name
169-
* @param value the value of the value
170-
* @return the builder instance
171-
*/
172-
private Span setTag(String tag, Object value) {
173-
tags.put(tag, value);
174-
return this;
175-
}
176148

177149
/* (non-Javadoc)
178150
* @see io.opentracing.Span#context()
@@ -200,11 +172,7 @@ public String getBaggageItem(String key) {
200172
* @see io.opentracing.Span#setOperationName(java.lang.String)
201173
*/
202174
public Span setOperationName(String operationName) {
203-
// FIXME operationName is in each constructor --> always IAE
204-
if (this.operationName != null) {
205-
throw new IllegalArgumentException("The operationName is already assigned.");
206-
}
207-
this.operationName = operationName;
175+
this.context().setOperationName(operationName);
208176
return this;
209177
}
210178

@@ -258,15 +226,6 @@ public Span log(long l, String s, Object o) {
258226

259227

260228
//Getters and JSON serialisation instructions
261-
@JsonGetter("name")
262-
public String getOperationName() {
263-
return operationName;
264-
}
265-
266-
@JsonIgnore
267-
public Map<String, Object> getTags() {
268-
return this.tags;
269-
}
270229

271230
/**
272231
* Meta merges baggage and tags (stringified values)
@@ -295,8 +254,8 @@ public long getDurationNano() {
295254
return durationNano;
296255
}
297256

298-
@JsonGetter
299-
public String getService() {
257+
@JsonGetter("service")
258+
public String getServiceName() {
300259
return context.getServiceName();
301260
}
302261

@@ -317,9 +276,18 @@ public long getParentId() {
317276

318277
@JsonGetter("resource")
319278
public String getResourceName() {
320-
return context.getResourceName() == null ? this.operationName : context.getResourceName();
279+
return context.getResourceName() == null ? context.getOperationName() : context.getResourceName();
280+
}
281+
282+
@JsonGetter("name")
283+
public String getOperationName() {
284+
return this.context().getOperationName();
321285
}
322286

287+
@JsonIgnore
288+
public Map<String, Object> getTags() {
289+
return this.context().getTags();
290+
}
323291
@JsonGetter
324292
public String getType() {
325293
return context.getSpanType();
@@ -335,4 +303,19 @@ public String toString() {
335303
return context.toString();
336304
}
337305

306+
307+
public Span setServiceName(String serviceName) {
308+
this.context().setServiceName(serviceName);
309+
return this;
310+
}
311+
312+
public Span setResourceName(String resourceName) {
313+
this.context().setResourceName(resourceName);
314+
return this;
315+
}
316+
317+
public Span setType(String type) {
318+
this.context().setType(type);
319+
return this;
320+
}
338321
}

src/main/java/com/datadoghq/trace/impl/DDSpanContext.java

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.datadoghq.trace.impl;
22

33

4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
4+
import java.util.*;
85

96
import com.fasterxml.jackson.annotation.JsonIgnore;
107

@@ -23,29 +20,37 @@ public class DDSpanContext implements io.opentracing.SpanContext {
2320
private final long traceId;
2421
private final long spanId;
2522
private final long parentId;
26-
private final Map<String, String> baggageItems;
23+
private Map<String, String> baggageItems;
2724

2825
// DD attributes
2926
/**
3027
* The service name is required, otherwise the span are dropped by the agent
3128
*/
32-
private final String serviceName;
29+
private String serviceName;
3330
/**
3431
* The resource associated to the service (server_web, database, etc.)
3532
*/
36-
private final String resourceName;
33+
private String resourceName;
3734
/**
3835
* True indicates that the span reports an error
3936
*/
4037
private final boolean errorFlag;
4138
/**
4239
* The type of the span. If null, the Datadog Agent will report as a custom
4340
*/
44-
private final String spanType;
41+
private String spanType;
4542
/**
4643
* The collection of all span related to this one
4744
*/
4845
private final List<Span> trace;
46+
/**
47+
* Each span have an operation name describing the current span
48+
*/
49+
private String operationName;
50+
/**
51+
* Tags are associated to the current span, they will not propagate to the children span
52+
*/
53+
private Map<String, Object> tags;
4954
// Others attributes
5055
/**
5156
* For technical reasons, the ref to the original tracer
@@ -57,10 +62,12 @@ public DDSpanContext(
5762
long spanId,
5863
long parentId,
5964
String serviceName,
65+
String operationName,
6066
String resourceName,
6167
Map<String, String> baggageItems,
6268
boolean errorFlag,
6369
String spanType,
70+
Map<String, Object> tags,
6471
List<Span> trace,
6572
DDTracer tracer) {
6673

@@ -69,15 +76,19 @@ public DDSpanContext(
6976
this.parentId = parentId;
7077

7178
if (baggageItems == null) {
72-
this.baggageItems = new HashMap<String, String>();
79+
this.baggageItems = Collections.emptyMap();
7380
} else {
7481
this.baggageItems = baggageItems;
7582
}
83+
7684
this.serviceName = serviceName;
85+
this.operationName = operationName;
7786
this.resourceName = resourceName;
7887
this.errorFlag = errorFlag;
7988
this.spanType = spanType;
8089

90+
this.tags = tags;
91+
8192
if (trace == null) {
8293
this.trace = new ArrayList<Span>();
8394
} else {
@@ -117,6 +128,9 @@ public String getSpanType() {
117128
}
118129

119130
public void setBaggageItem(String key, String value) {
131+
if (this.baggageItems.isEmpty()) {
132+
this.baggageItems = new HashMap<String, String>();
133+
}
120134
this.baggageItems.put(key, value);
121135
}
122136

@@ -145,11 +159,48 @@ public DDTracer getTracer() {
145159
return this.tracer;
146160
}
147161

162+
/**
163+
* Add a tag to the span. Tags are not propagated to the children
164+
*
165+
* @param tag the tag-name
166+
* @param value the value of the value
167+
* @return the builder instance
168+
*/
169+
public void setTag(String tag, Object value) {
170+
if (this.tags.isEmpty()) {
171+
this.tags = new HashMap<String, Object>();
172+
}
173+
this.tags.put(tag, value);
174+
}
175+
148176
@Override
149177
public String toString() {
150178
return "Span [traceId=" + traceId
151179
+ ", spanId=" + spanId
152180
+ ", parentId=" + parentId + "]";
153181
}
154182

183+
public void setOperationName(String operationName) {
184+
this.operationName = operationName;
185+
}
186+
187+
public String getOperationName() {
188+
return operationName;
189+
}
190+
191+
public Map<String, Object> getTags() {
192+
return tags;
193+
}
194+
195+
public void setServiceName(String serviceName) {
196+
this.serviceName = serviceName;
197+
}
198+
199+
public void setResourceName(String resourceName) {
200+
this.resourceName = resourceName;
201+
}
202+
203+
public void setType(String type) {
204+
this.spanType = type;
205+
}
155206
}

0 commit comments

Comments
 (0)