Span metadata must be string values.

This commit is contained in:
Tyler Benson 2019-02-08 11:52:37 -08:00
parent 0e1dce868c
commit a59eec223f
4 changed files with 20 additions and 7 deletions

View File

@ -99,7 +99,7 @@ public interface Span {
/**
* Get all the metadata attached to this span.
*
* @return
* @return immutable map of span metadata.
*/
Map<String, Object> getMeta();

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@ -13,6 +14,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -213,9 +215,19 @@ class SpanImpl implements Span {
}
@Override
@JsonGetter("meta")
@JsonIgnore
public synchronized Map<String, Object> getMeta() {
final Map<String, Object> result = new HashMap<>(meta.size());
return Collections.unmodifiableMap(meta);
}
/**
* The agent expects meta's values to be strings.
*
* @return a copy of meta with all values converted to strings.
*/
@JsonGetter("meta")
synchronized Map<String, String> getMetaString() {
final Map<String, String> result = new HashMap<>(meta.size());
for (final Map.Entry<String, Object> entry : meta.entrySet()) {
result.put(entry.getKey(), String.valueOf(entry.getValue()));
}

View File

@ -43,7 +43,7 @@ class JsonSpan {
@JsonCreator
JsonSpan() {}
JsonSpan(Span span) {
JsonSpan(SpanImpl span) {
traceId = new BigInteger(span.getContext().getTraceId())
parentId = new BigInteger(span.getContext().getParentId())
spanId = new BigInteger(span.getContext().getSpanId())
@ -58,6 +58,6 @@ class JsonSpan {
error = span.isErrored() ? 1 : 0
meta = span.getMeta()
meta = span.getMetaString()
}
}

View File

@ -129,7 +129,8 @@ class SpanImplTest extends Specification {
span.setMeta("boolean.key", true)
then:
span.getMeta() == ["number.key": "123", "string.key": "meta string", "boolean.key": "true"]
span.getMeta() == ["number.key": 123, "string.key": "meta string", "boolean.key": true]
span.getMetaString() == ["number.key": "123", "string.key": "meta string", "boolean.key": "true"]
}
def "test meta setter on finished span for #key"() {
@ -227,7 +228,7 @@ class SpanImplTest extends Specification {
when: "finish/finalize span"
span."$method"(*methodArgs)
then: "interceptors called"
interceptors.reverseEach({ interceptor ->
then: