-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
269 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/io/opentracing/contrib/grpc/ActiveSpanContextSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2018 The OpenTracing Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.opentracing.contrib.grpc; | ||
|
||
import io.opentracing.SpanContext; | ||
|
||
/** | ||
* An interface that defines how to get the current active span context | ||
*/ | ||
public interface ActiveSpanContextSource { | ||
|
||
/** | ||
* ActiveSpanContextSource implementation that always returns | ||
* null as the active span context | ||
*/ | ||
ActiveSpanContextSource NONE = new ActiveSpanContextSource() { | ||
@Override | ||
public SpanContext getActiveSpanContext() { | ||
return null; | ||
} | ||
}; | ||
|
||
/** | ||
* ActiveSpanContextSource implementation that returns the | ||
* current span context stored in the GRPC context under | ||
* {@link OpenTracingContextKey} | ||
*/ | ||
ActiveSpanContextSource GRPC_CONTEXT = new ActiveSpanContextSource() { | ||
@Override | ||
public SpanContext getActiveSpanContext() { | ||
return OpenTracingContextKey.activeSpanContext(); | ||
} | ||
}; | ||
|
||
/** | ||
* @return the active span context | ||
*/ | ||
SpanContext getActiveSpanContext(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/io/opentracing/contrib/grpc/ActiveSpanContextSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017-2018 The OpenTracing Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.opentracing.contrib.grpc; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import io.grpc.Context; | ||
import io.opentracing.Span; | ||
import io.opentracing.mock.MockTracer; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ActiveSpanContextSourceTest { | ||
|
||
private final MockTracer tracer = new MockTracer(); | ||
|
||
@Before | ||
public void before() { | ||
tracer.reset(); | ||
} | ||
|
||
@Test | ||
public void TestDefaultNone() { | ||
ActiveSpanContextSource ss = ActiveSpanContextSource.NONE; | ||
assertNull("active span context should always be null", ss.getActiveSpanContext()); | ||
|
||
Span span = tracer.buildSpan("s0").start(); | ||
Context ctx = Context.current().withValue(OpenTracingContextKey.getKey(), span); | ||
Context previousCtx = ctx.attach(); | ||
|
||
assertNull("active span context should always be null", ss.getActiveSpanContext()); | ||
|
||
ctx.detach(previousCtx); | ||
span.finish(); | ||
} | ||
|
||
@Test | ||
public void TestDefaultGrpc() { | ||
ActiveSpanContextSource ss = ActiveSpanContextSource.GRPC_CONTEXT; | ||
assertNull("active span context should be null, no span context in OpenTracingContextKey", | ||
ss.getActiveSpanContext()); | ||
|
||
Span span = tracer.buildSpan("s0").start(); | ||
Context ctx = Context.current() | ||
.withValue(OpenTracingContextKey.getSpanContextKey(), span.context()); | ||
Context previousCtx = ctx.attach(); | ||
|
||
assertEquals("active span context should be OpenTracingContextKey.activeSpanContext()", | ||
ss.getActiveSpanContext(), span.context()); | ||
|
||
ctx.detach(previousCtx); | ||
span.finish(); | ||
|
||
assertNull("active span context should be null, no span context in OpenTracingContextKey", | ||
ss.getActiveSpanContext()); | ||
} | ||
|
||
} |
Oops, something went wrong.