|
1 | 1 | package com.datadoghq.trace; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Collections; |
5 | | -import java.util.HashMap; |
6 | | -import java.util.List; |
7 | | -import java.util.Map; |
8 | | - |
9 | | -import org.slf4j.Logger; |
10 | | -import org.slf4j.LoggerFactory; |
11 | | - |
12 | 3 | import com.datadoghq.trace.integration.DDSpanContextDecorator; |
13 | 4 | import com.datadoghq.trace.propagation.Codec; |
14 | 5 | import com.datadoghq.trace.propagation.HTTPCodec; |
15 | 6 | import com.datadoghq.trace.sampling.AllSampler; |
16 | 7 | import com.datadoghq.trace.sampling.Sampler; |
17 | 8 | import com.datadoghq.trace.writer.LoggingWriter; |
18 | 9 | import com.datadoghq.trace.writer.Writer; |
19 | | - |
20 | | -import io.opentracing.ActiveSpan; |
21 | 10 | import io.opentracing.BaseSpan; |
22 | 11 | import io.opentracing.Span; |
23 | 12 | import io.opentracing.SpanContext; |
24 | 13 | import io.opentracing.propagation.Format; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.util.*; |
25 | 18 |
|
26 | 19 |
|
27 | 20 | /** |
@@ -152,50 +145,50 @@ public void write(List<DDBaseSpan<?>> trace) { |
152 | 145 | public void close() { |
153 | 146 | writer.close(); |
154 | 147 | } |
155 | | - |
| 148 | + |
156 | 149 | private final ThreadLocal<DDActiveSpan> currentActiveSpan = new ThreadLocal<DDActiveSpan>(); |
157 | 150 |
|
158 | 151 | @Override |
159 | 152 | public DDActiveSpan activeSpan() { |
160 | 153 | return currentActiveSpan.get(); |
161 | 154 | } |
162 | | - |
| 155 | + |
163 | 156 | /** |
164 | 157 | * Set the newly created active span as the active one from the Tracer's perspective |
165 | | - * |
| 158 | + * |
166 | 159 | * @param activeSpan |
167 | 160 | */ |
168 | | - protected void makeActive(DDActiveSpan activeSpan){ |
| 161 | + protected void makeActive(DDActiveSpan activeSpan) { |
169 | 162 | //We cannot make active a preably deactivated span |
170 | | - if(activeSpan!=null && activeSpan.isDeactivated()) |
| 163 | + if (activeSpan != null && activeSpan.isDeactivated()) |
171 | 164 | currentActiveSpan.set(null); |
172 | 165 | else |
173 | 166 | currentActiveSpan.set(activeSpan); |
174 | 167 | } |
175 | | - |
| 168 | + |
176 | 169 | /** |
177 | 170 | * Deactivate the current span (if active) and make the parent active (again) |
178 | | - * |
| 171 | + * |
179 | 172 | * @param activeSpan |
180 | 173 | */ |
181 | | - protected void deactivate(DDActiveSpan activeSpan){ |
| 174 | + protected void deactivate(DDActiveSpan activeSpan) { |
182 | 175 | DDActiveSpan current = activeSpan(); |
183 | | - if(current==activeSpan){ |
| 176 | + if (current == activeSpan) { |
184 | 177 | //The parent becomes the active span |
185 | 178 | makeActive(activeSpan.getParent()); |
186 | 179 | } |
187 | 180 | } |
188 | 181 |
|
189 | 182 | @Override |
190 | 183 | public DDActiveSpan makeActive(Span span) { |
191 | | - if(!(span instanceof DDSpan)) |
192 | | - throw new IllegalArgumentException("Cannot transform a non DDSpan into a DDActiveSpan. Provided class: "+span.getClass()); |
193 | | - |
| 184 | + if (!(span instanceof DDSpan)) |
| 185 | + throw new IllegalArgumentException("Cannot transform a non DDSpan into a DDActiveSpan. Provided class: " + span.getClass()); |
| 186 | + |
194 | 187 | //Wrap the provided manual span into an active one with the current parent |
195 | | - DDActiveSpan activeSpan = new DDActiveSpan(activeSpan(),(DDSpan)span); |
196 | | - |
| 188 | + DDActiveSpan activeSpan = new DDActiveSpan(activeSpan(), (DDSpan) span); |
| 189 | + |
197 | 190 | makeActive(activeSpan); |
198 | | - |
| 191 | + |
199 | 192 | return activeSpan; |
200 | 193 | } |
201 | 194 |
|
@@ -229,22 +222,22 @@ public SpanBuilder ignoreActiveSpan() { |
229 | 222 | public DDActiveSpan startActive() { |
230 | 223 | //Set the active span as parent if ignoreActiveSpan==true |
231 | 224 | DDActiveSpan activeParent = null; |
232 | | - if(!ignoreActiveSpan){ |
| 225 | + if (!ignoreActiveSpan) { |
233 | 226 | DDActiveSpan current = activeSpan(); |
234 | | - if(current!=null){ |
| 227 | + if (current != null) { |
235 | 228 | activeParent = current; |
236 | | - |
| 229 | + |
237 | 230 | //Ensure parent inheritance |
238 | 231 | asChildOf(activeParent); |
239 | 232 | } |
240 | 233 | } |
241 | | - |
| 234 | + |
242 | 235 | //Create the active span |
243 | | - DDActiveSpan activeSpan = new DDActiveSpan(activeParent,this.timestamp, buildSpanContext()); |
| 236 | + DDActiveSpan activeSpan = new DDActiveSpan(activeParent, this.timestamp, buildSpanContext()); |
244 | 237 | logger.debug("{} - Starting a new active span.", activeSpan); |
245 | | - |
| 238 | + |
246 | 239 | makeActive(activeSpan); |
247 | | - |
| 240 | + |
248 | 241 | return activeSpan; |
249 | 242 | } |
250 | 243 |
|
@@ -284,7 +277,7 @@ public DDTracer.DDSpanBuilder withTag(String tag, boolean bool) { |
284 | 277 | return withTag(tag, (Object) bool); |
285 | 278 | } |
286 | 279 |
|
287 | | - |
| 280 | + |
288 | 281 | public DDSpanBuilder(String operationName) { |
289 | 282 | this.operationName = operationName; |
290 | 283 | } |
@@ -380,25 +373,32 @@ private DDSpanContext buildSpanContext() { |
380 | 373 | } |
381 | 374 | } |
382 | 375 |
|
| 376 | + |
383 | 377 | String operationName = this.operationName != null ? this.operationName : this.resourceName; |
384 | 378 |
|
385 | 379 | //this.operationName, this.tags, |
386 | 380 |
|
387 | 381 | // some attributes are inherited from the parent |
388 | 382 | context = new DDSpanContext( |
389 | 383 | this.parent == null ? generatedId : p.getTraceId(), |
390 | | - generatedId, |
391 | | - this.parent == null ? 0L : p.getSpanId(), |
392 | | - serviceName, |
393 | | - operationName, |
394 | | - this.resourceName, |
395 | | - this.parent == null ? null : p.getBaggageItems(), |
396 | | - errorFlag, |
397 | | - spanType, |
398 | | - this.tags, |
399 | | - this.parent == null ? null : p.getTrace(), |
400 | | - DDTracer.this |
401 | | - ); |
| 384 | + generatedId, |
| 385 | + this.parent == null ? 0L : p.getSpanId(), |
| 386 | + serviceName, |
| 387 | + operationName, |
| 388 | + this.resourceName, |
| 389 | + this.parent == null ? null : p.getBaggageItems(), |
| 390 | + errorFlag, |
| 391 | + spanType, |
| 392 | + this.tags, |
| 393 | + this.parent == null ? null : p.getTrace(), |
| 394 | + DDTracer.this |
| 395 | + ); |
| 396 | + |
| 397 | + |
| 398 | + // Force the lang meta |
| 399 | + if (context.getBaggageItem(DDSpanContext.LANGUAGE_FIELDNAME) == null) { |
| 400 | + context.setBaggageItem(DDSpanContext.LANGUAGE_FIELDNAME, "java"); |
| 401 | + } |
402 | 402 |
|
403 | 403 | return context; |
404 | 404 | } |
|
0 commit comments