-
Notifications
You must be signed in to change notification settings - Fork 344
Remove default finish behaviour for activate()
#219
Changes from all commits
cd26a39
1bb2e14
be7ac02
8cb95fb
6423db2
1a1a0ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,14 +10,6 @@ In order to understand the Java platform API, one must first be familiar with | |
the [OpenTracing project](http://opentracing.io) and | ||
[terminology](http://opentracing.io/documentation/pages/spec.html) more specifically. | ||
|
||
## Status | ||
|
||
This project has a working design of interfaces for the OpenTracing API. There | ||
is a [MockTracer](https://github.com/opentracing/opentracing-java/tree/master/opentracing-mock) | ||
to facilitate unit-testing of OpenTracing Java instrumentation. | ||
|
||
Packages are deployed to Maven Central under the `io.opentracing` group. | ||
|
||
## Usage | ||
|
||
### Initialization | ||
|
@@ -51,67 +43,48 @@ if (scope != null) { | |
|
||
### Starting a new Span | ||
|
||
The common case starts an `Scope` that's automatically registered for intra-process propagation via `ScopeManager`. The best practice is to use a try-with-resources pattern which handles Exceptions and early returns: | ||
The common case starts a `Scope` that's automatically registered for intra-process propagation via `ScopeManager`. | ||
|
||
```java | ||
io.opentracing.Tracer tracer = ...; | ||
... | ||
try (Scope scope = tracer.buildSpan("someWork").startActive()) { | ||
// Do things. | ||
// | ||
// If we create async work, `scope.span()` allows us to pass the `Span` along as well. | ||
} | ||
``` | ||
|
||
The above is semantically equivalent to the more explicit try-finally version: | ||
Note that `startActive(true)` finishes the span on `Scope.close()`. | ||
Use it carefully because the `try-with-resources` construct finishes the span before | ||
the `catch` or `finally` blocks are executed, which makes logging exceptions and | ||
setting tags impossible. It is recommended to start the span and activate it later in `try-with-resources`. | ||
This makes the span available in catch and finally blocks. | ||
|
||
```java | ||
io.opentracing.Tracer tracer = ...; | ||
... | ||
Scope scope = tracer.buildSpan("someWork").startActive(); | ||
try { | ||
Span span = tracer.buildSpan("someWork").start(); | ||
try (Scope scope = tracer.scopeManager().activate(span, false)) | ||
// Do things. | ||
} catch { | ||
Tags.ERROR.set(scope.span(), true); | ||
} finally { | ||
scope.deactivate(); | ||
span.finish(); | ||
} | ||
``` | ||
|
||
To manually step around the `ScopeManager` registration, use `startManual()`, like this: | ||
The following code uses `try-with-resources` to finish the span. | ||
|
||
```java | ||
io.opentracing.Tracer tracer = ...; | ||
... | ||
Span span = tracer.buildSpan("someWork").startManual(); | ||
try { | ||
// (do things / record data to `span`) | ||
} finally { | ||
span.finish(); | ||
try (Scope scope = tracer.buildSpan("someWork").startActive(true)) { | ||
// Do things. | ||
// | ||
// `scope.span()` allows us to pass the `Span` to newly created threads. | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be good to add a catch clause with a comment
|
||
``` | ||
|
||
**If there is an `Scope`, it will act as the parent to any newly started `Span`** unless the programmer invokes `ignoreActiveSpan()` at `buildSpan()` time, like so: | ||
**If there is a `Scope`, it will act as the parent to any newly started `Span`** unless | ||
the programmer invokes `ignoreActiveSpan()` at `buildSpan()` time or specified parent context explicitly: | ||
|
||
```java | ||
io.opentracing.Tracer tracer = ...; | ||
... | ||
Scope scope = tracer.buildSpan("someWork").ignoreActiveSpan().startActive(); | ||
``` | ||
|
||
#### Explicit `finish()`ing via `Scope.activate(boolean)` | ||
|
||
When an `Scope` is created (either via `Tracer.SpanBuilder#startActive` or `ScopeManager#activate`), it's possible to specify whether the `Span` will be finished upon `Scope.deactivate()`, through a method overload taking a `finishSpanOnClose` parameter, which defaults to true. | ||
|
||
```java | ||
io.opentracing.Tracer tracer = ...; | ||
... | ||
try (Scope scope = tracer.buildSpan("someWork").startActive(false)) { | ||
// false was passed to `startActive()`, so the `Span` will not be finished | ||
// upon Scope deactivation | ||
} | ||
``` | ||
|
||
This is specially useful when a `Span` needs to be passed to a another thread or callback, reactivated, and then passed to the next thread or callback, and only be finished when the task end is reached. | ||
|
||
### Deferring asynchronous work | ||
|
||
Consider the case where a `Span`'s lifetime logically starts in one thread and ends in another. For instance, the Span's own internal timing breakdown might look like this: | ||
|
@@ -158,16 +131,20 @@ Observe that passing `Scope` to another thread or callback is not supported. Onl | |
|
||
In practice, all of this is most fluently accomplished through the use of an OpenTracing-aware `ExecutorService` and/or `Runnable`/`Callable` adapter; they factor out most of the typing. | ||
|
||
# Development | ||
## Instrumentation Tests | ||
|
||
This is a maven project, and provides a wrapper, `./mvnw` to pin a consistent | ||
version. For example, `./mvnw clean install`. | ||
This project has a working design of interfaces for the OpenTracing API. There | ||
is a [MockTracer](https://github.com/opentracing/opentracing-java/tree/master/opentracing-mock) | ||
to facilitate unit-testing of OpenTracing Java instrumentation. | ||
|
||
This wrapper was generated by `mvn -N io.takari:maven:wrapper -Dmaven=3.5.0` | ||
Packages are deployed to Maven Central under the `io.opentracing` group. | ||
|
||
## Development | ||
|
||
## Building | ||
This is a maven project, and provides a wrapper, `./mvnw` to pin a consistent | ||
version. Run `./mvnw clean install` to build, run tests, and create jars. | ||
|
||
Execute `./mvnw clean install` to build, run tests, and create jars. | ||
This wrapper was generated by `mvn -N io.takari:maven:wrapper -Dmaven=3.5.0` | ||
|
||
## Contributing | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,22 +15,13 @@ | |
|
||
/** | ||
* The {@link ScopeManager} interface abstracts both the activation of {@link Span} instances (via | ||
* {@link ScopeManager#activate(Span)}) and access to an active {@link Span}/{@link Scope} | ||
* {@link ScopeManager#activate(Span, boolean)}) and access to an active {@link Span}/{@link Scope} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: parentheses around |
||
* (via {@link ScopeManager#active()}). | ||
* | ||
* @see Scope | ||
* @see Tracer#scopeManager() | ||
*/ | ||
public interface ScopeManager { | ||
/** | ||
* Make a {@link Span} instance active. | ||
* | ||
* @param span the {@link Span} that should become the {@link #active()} | ||
* @return a {@link Scope} instance to control the end of the active period for the {@link Span}. | ||
* Span will automatically be finished when {@link Scope#close()} is called. It is a | ||
* programming error to neglect to call {@link Scope#close()} on the returned instance. | ||
*/ | ||
Scope activate(Span span); | ||
|
||
/** | ||
* Make a {@link Span} instance active. | ||
|
@@ -48,7 +39,7 @@ public interface ScopeManager { | |
* | ||
* <p> | ||
* If there is an {@link Scope non-null scope}, its wrapped {@link Span} becomes an implicit parent of any | ||
* newly-created {@link Span} at {@link Tracer.SpanBuilder#startActive()} time (rather than at | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. live above: |
||
* newly-created {@link Span} at {@link Tracer.SpanBuilder#startActive(boolean)} time (rather than at | ||
* {@link Tracer#buildSpan(String)} time). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @return the {@link Scope active scope}, or null if none could be found. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scope.span()
is inaccessible here according to the comment