Skip to content

Commit 88a7781

Browse files
Log invalid arguments rather than throwing exceptions. (#5012)
1 parent f490ba8 commit 88a7781

File tree

6 files changed

+51
-36
lines changed

6 files changed

+51
-36
lines changed

opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ScopeManagerShim.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public Span activeSpan() {
5050
@Override
5151
@SuppressWarnings("MustBeClosedChecker")
5252
public Scope activate(@Nullable Span span) {
53-
if (span == null) {
53+
SpanShim spanShim = ShimUtil.getSpanShim(span);
54+
if (spanShim == null) {
5455
return new ScopeShim(Context.current().with(NOOP_SPANSHIM).makeCurrent());
5556
}
56-
return new ScopeShim(Context.current().with(ShimUtil.getSpanShim(span)).makeCurrent());
57+
return new ScopeShim(Context.current().with(spanShim).makeCurrent());
5758
}
5859
}

opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/ShimUtil.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,40 @@
77

88
import io.opentracing.Span;
99
import io.opentracing.SpanContext;
10+
import io.opentracing.noop.NoopSpan;
1011
import java.util.function.Supplier;
12+
import java.util.logging.Level;
13+
import java.util.logging.Logger;
1114
import javax.annotation.Nullable;
1215

1316
class ShimUtil {
17+
private static final Logger logger = Logger.getLogger(ShimUtil.class.getName());
1418

1519
private ShimUtil() {}
1620

17-
static SpanContextShim getContextShim(SpanContext context) {
21+
@Nullable
22+
static SpanContextShim getContextShim(@Nullable SpanContext context) {
23+
if (context == null) {
24+
return null;
25+
}
26+
1827
if (!(context instanceof SpanContextShim)) {
19-
throw new IllegalArgumentException(
20-
"context is not a valid SpanContextShim object: " + className(context));
28+
logger.log(
29+
Level.INFO,
30+
"Expected to have an OpenTelemetry SpanContext but got {0}",
31+
className(context));
32+
return null;
2133
}
2234

2335
return (SpanContextShim) context;
2436
}
2537

26-
static SpanShim getSpanShim(Span span) {
38+
@Nullable
39+
static SpanShim getSpanShim(@Nullable Span span) {
40+
if (span == null || span instanceof NoopSpan) {
41+
return null;
42+
}
43+
2744
if (!(span instanceof SpanShim)) {
2845
if (span instanceof Supplier<?>) {
2946
// allow libraries to implement a delegate span,
@@ -32,11 +49,13 @@ static SpanShim getSpanShim(Span span) {
3249
if (wrapped instanceof Span) {
3350
return getSpanShim((Span) wrapped);
3451
} else {
35-
throw new IllegalArgumentException(
36-
"span wrapper didn't return a span: " + className(wrapped));
52+
logger.log(Level.INFO, "Span wrapper didn't return a span: {0}", className(wrapped));
53+
return null;
3754
}
3855
}
39-
throw new IllegalArgumentException("span is not a valid SpanShim object: " + className(span));
56+
57+
logger.log(Level.INFO, "Expected to have an OpenTelemetry Span but got {0}", className(span));
58+
return null;
4059
}
4160

4261
return (SpanShim) span;

opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.opentracing.Span;
2424
import io.opentracing.SpanContext;
2525
import io.opentracing.Tracer.SpanBuilder;
26-
import io.opentracing.noop.NoopSpan;
2726
import io.opentracing.tag.Tag;
2827
import io.opentracing.tag.Tags;
2928
import java.util.ArrayList;
@@ -64,11 +63,12 @@ public SpanBuilderShim(TelemetryInfo telemetryInfo, String spanName) {
6463

6564
@Override
6665
public SpanBuilder asChildOf(Span parent) {
67-
if (parent == null || parent instanceof NoopSpan) {
68-
return this;
69-
}
7066
SpanShim spanShim = ShimUtil.getSpanShim(parent);
71-
return addReference(References.CHILD_OF, spanShim.context());
67+
if (spanShim != null) {
68+
addReference(References.CHILD_OF, spanShim.context());
69+
}
70+
71+
return this;
7272
}
7373

7474
@Override
@@ -78,7 +78,8 @@ public SpanBuilder asChildOf(SpanContext parent) {
7878

7979
@Override
8080
public SpanBuilder addReference(@Nullable String referenceType, SpanContext referencedContext) {
81-
if (referencedContext == null) {
81+
SpanContextShim contextShim = ShimUtil.getContextShim(referencedContext);
82+
if (contextShim == null) {
8283
return this;
8384
}
8485

@@ -92,8 +93,6 @@ public SpanBuilder addReference(@Nullable String referenceType, SpanContext refe
9293
return this;
9394
}
9495

95-
SpanContextShim contextShim = ShimUtil.getContextShim(referencedContext);
96-
9796
// Optimization for 99% situations, when there is only one parent.
9897
if (allParents.size() == 0) {
9998
allParents =

opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/TracerShim.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public <C> void inject(SpanContext context, Format<C> format, C carrier) {
6161
return;
6262
}
6363

64-
SpanContextShim contextShim = getContextShim(context);
64+
SpanContextShim contextShim = ShimUtil.getContextShim(context);
65+
if (contextShim == null) {
66+
return;
67+
}
6568

6669
if (format == Format.Builtin.TEXT_MAP
6770
|| format == Format.Builtin.TEXT_MAP_INJECT
@@ -94,12 +97,4 @@ public <C> SpanContext extract(Format<C> format, C carrier) {
9497
public void close() {
9598
isClosed = true;
9699
}
97-
98-
static SpanContextShim getContextShim(SpanContext context) {
99-
if (!(context instanceof SpanContextShim)) {
100-
throw new IllegalArgumentException("context is not a valid SpanContextShim object");
101-
}
102-
103-
return (SpanContextShim) context;
104-
}
105100
}

opentracing-shim/src/test/java/io/opentelemetry/opentracingshim/ShimUtilTest.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.opentracingshim;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
109

1110
import io.opentelemetry.api.trace.Span;
1211
import io.opentelemetry.api.trace.Tracer;
@@ -40,20 +39,14 @@ void spanWrapper() {
4039
SpanShim shim = new SpanShim(telemetryInfo, span);
4140
assertThat(ShimUtil.getSpanShim(shim)).isEqualTo(shim);
4241
assertThat(ShimUtil.getSpanShim(new SpanWrapper(shim))).isEqualTo(shim);
43-
assertThatThrownBy(() -> ShimUtil.getSpanShim(new SpanWrapper("not a span")))
44-
.isInstanceOf(IllegalArgumentException.class)
45-
.hasMessage("span wrapper didn't return a span: java.lang.String");
46-
assertThatThrownBy(() -> ShimUtil.getSpanShim(null))
47-
.isInstanceOf(IllegalArgumentException.class)
48-
.hasMessage("span is not a valid SpanShim object: null");
42+
assertThat(ShimUtil.getSpanShim(new SpanWrapper("not a span"))).isNull();
43+
assertThat(ShimUtil.getSpanShim(null)).isNull();
4944
}
5045

5146
@Test
5247
void getContextShim() {
5348
SpanContextShim contextShim = new SpanContextShim(new SpanShim(telemetryInfo, span));
5449
assertThat(ShimUtil.getContextShim(contextShim)).isEqualTo(contextShim);
55-
assertThatThrownBy(() -> ShimUtil.getContextShim(null))
56-
.isInstanceOf(IllegalArgumentException.class)
57-
.hasMessage("context is not a valid SpanContextShim object: null");
50+
assertThat(ShimUtil.getContextShim(null)).isNull();
5851
}
5952
}

opentracing-shim/src/test/java/io/opentelemetry/opentracingshim/TracerShimTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.opentracingshim;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.Mockito.mock;
910

1011
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
1112
import io.opentelemetry.api.trace.Tracer;
@@ -262,6 +263,13 @@ void inject_nullContext() {
262263
assertThat(map).isEmpty();
263264
}
264265

266+
@Test
267+
void inject_invalid() {
268+
Map<String, String> map = new HashMap<>();
269+
tracerShim.inject(mock(SpanContext.class), Format.Builtin.TEXT_MAP, new TextMapAdapter(map));
270+
assertThat(map).isEmpty();
271+
}
272+
265273
@Test
266274
void inject_textMap() {
267275
Map<String, String> map = new HashMap<>();

0 commit comments

Comments
 (0)