Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenTracing Shim: Handle unsupported types when setting Attributes. #4939

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,21 @@ 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(BigInteger.TEN.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there's any value in being explicit about the expected string? I.e. .isEqualTo("10")?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assertions now compare against literals directly ;)

} 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,17 @@ 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(BigInteger.ONE.toString());
}

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