Do not create a baggage HashMap each time

This commit is contained in:
Guillaume Polaert 2017-05-24 10:50:44 +02:00
parent 1cd1183f07
commit 405e5a5410
2 changed files with 12 additions and 13 deletions

View File

@ -1,18 +1,13 @@
package com.datadoghq.trace; package com.datadoghq.trace;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.datadoghq.trace.integration.DDSpanContextDecorator; import com.datadoghq.trace.integration.DDSpanContextDecorator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import io.opentracing.Span; import io.opentracing.Span;
import io.opentracing.tag.Tags; import io.opentracing.tag.Tags;
import java.util.*;
/** /**
* SpanContext represents Span state that must propagate to descendant Spans and across process boundaries. * SpanContext represents Span state that must propagate to descendant Spans and across process boundaries.
* <p> * <p>
@ -81,8 +76,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.spanId = spanId; this.spanId = spanId;
this.parentId = parentId; this.parentId = parentId;
if (baggageItems == null) { if (baggageItems == null) {
this.baggageItems = new HashMap<String, String>(); this.baggageItems = Collections.emptyMap();
} else { } else {
this.baggageItems = baggageItems; this.baggageItems = baggageItems;
} }
@ -121,7 +117,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
} }
public String getResourceName() { public String getResourceName() {
return this.resourceName == null || this.resourceName.isEmpty() ? this.operationName : this.resourceName; return this.resourceName == null || this.resourceName.isEmpty() ? this.operationName : this.resourceName;
} }
public boolean getErrorFlag() { public boolean getErrorFlag() {
@ -134,6 +130,9 @@ public class DDSpanContext implements io.opentracing.SpanContext {
} }
public void setBaggageItem(String key, String value) { public void setBaggageItem(String key, String value) {
if (this.baggageItems.isEmpty()) {
this.baggageItems = new HashMap<String, String>();
}
this.baggageItems.put(key, value); this.baggageItems.put(key, value);
} }
@ -176,11 +175,11 @@ public class DDSpanContext implements io.opentracing.SpanContext {
this.tags.put(tag, value); this.tags.put(tag, value);
//Call decorators //Call decorators
for(DDSpanContextDecorator decorator:tracer.getSpanContextDecorators()){ for (DDSpanContextDecorator decorator : tracer.getSpanContextDecorators()) {
decorator.afterSetTag(this, tag, value); decorator.afterSetTag(this, tag, value);
} }
//Error management //Error management
if(Tags.ERROR.getKey().equals(tag) && Boolean.TRUE.equals(value)){ if (Tags.ERROR.getKey().equals(tag) && Boolean.TRUE.equals(value)) {
this.errorFlag = true; this.errorFlag = true;
} }
} }
@ -191,7 +190,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
@Override @Override
public String toString() { public String toString() {
return "Span [ "+traceId+" ] [ "+spanId+" | "+parentId+" ] [ "+getServiceName()+" | "+getOperationName()+" | "+getResourceName()+" ]"; return "Span [ " + traceId + " ] [ " + spanId + " | " + parentId + " ] [ " + getServiceName() + " | " + getOperationName() + " | " + getResourceName() + " ]";
} }
public void setOperationName(String operationName) { public void setOperationName(String operationName) {

View File

@ -304,7 +304,7 @@ public class DDTracer implements io.opentracing.Tracer {
serviceName, serviceName,
operationName, operationName,
this.resourceName, this.resourceName,
this.parent == null ? new HashMap<String, String>() : p.getBaggageItems(), this.parent == null ? null : p.getBaggageItems(),
errorFlag, errorFlag,
spanType, spanType,
this.tags, this.tags,