Skip to content

Commit

Permalink
OpenTracing Shim: Handle unsupported types when setting Attributes. (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#4939)

* OpenTracing Shim: Handle unsupported types when setting Attributes.

This happens through:
* SpanBuilder.withTag()
* Span.setTag()

* Simplify BigInteger conversion assertions.
  • Loading branch information
carlosalberto authored and dmarkwat committed Dec 30, 2022
1 parent 9085860 commit fc69ffa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public SpanBuilder withTag(String key, Number value) {
if (value == null) {
return this;
}
// TODO - Verify only the 'basic' types are supported/used.

if (value instanceof Integer
|| value instanceof Long
|| value instanceof Short
Expand All @@ -175,7 +175,8 @@ public SpanBuilder withTag(String key, Number value) {
this.spanBuilderAttributeKeys.add(doubleKey(key));
this.spanBuilderAttributeValues.add(value.doubleValue());
} else {
throw new IllegalArgumentException("Number type not supported");
this.spanBuilderAttributeKeys.add(stringKey(key));
this.spanBuilderAttributeValues.add(value.toString());
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Span setTag(String key, Number value) {
if (value == null) {
return this;
}
// TODO - Verify only the 'basic' types are supported/used.

if (value instanceof Integer
|| value instanceof Long
|| value instanceof Short
Expand All @@ -121,7 +121,7 @@ public Span setTag(String key, Number value) {
} else if (value instanceof Float || value instanceof Double) {
span.setAttribute(key, value.doubleValue());
} else {
throw new IllegalArgumentException("Number type not supported");
span.setAttribute(key, value.toString());
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentracing.References;
import java.math.BigInteger;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -296,6 +297,20 @@ void withStartTimestamp() {
assertThat(spanData.getStartEpochNanos()).isEqualTo(micros * 1000L);
}

@Test
void setAttribute_unrecognizedType() {
SpanShim span =
(SpanShim)
new SpanBuilderShim(telemetryInfo, SPAN_NAME).withTag("foo", BigInteger.TEN).start();
try {
SpanData spanData = ((ReadableSpan) span.getSpan()).toSpanData();
assertThat(spanData.getAttributes().size()).isEqualTo(1);
assertThat(spanData.getAttributes().get(AttributeKey.stringKey("foo"))).isEqualTo("10");
} finally {
span.finish();
}
}

@Test
void setAttributes_beforeSpanStart() {
SdkTracerProvider tracerSdkFactory =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import io.opentracing.log.Fields;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -59,6 +60,16 @@ void context_simple() {
assertThat(contextShim.baggageItems().iterator().hasNext()).isFalse();
}

@Test
void setAttribute_unrecognizedType() {
SpanShim spanShim = new SpanShim(telemetryInfo, span);
spanShim.setTag("foo", BigInteger.ONE);

SpanData spanData = ((ReadableSpan) span).toSpanData();
assertThat(spanData.getAttributes().size()).isEqualTo(1);
assertThat(spanData.getAttributes().get(AttributeKey.stringKey("foo"))).isEqualTo("1");
}

@Test
void baggage() {
SpanShim spanShim = new SpanShim(telemetryInfo, span);
Expand Down

0 comments on commit fc69ffa

Please sign in to comment.