public interface Tracer
traces. Traces are identified by name, e.g.
X-Trace-ID and, during an active lifecycle, they can have a value, e.g.
5dc90f18-231f-11e6-a17d-0f84f5d57f60. Values can either be provided on a per-lifecycle basis or generated
on-demand by a generator. Lifecycle events, i.e. start and stop, can be observed by registering
one or more listeners.Trace,
Generator,
TraceListener| Modifier and Type | Method and Description |
|---|---|
static TracerFactory.Builder |
builder() |
static Tracer |
create(String... names)
Creates a new tracer that will manage the given traces.
|
default <V> Callable<V> |
delegate(Callable<V> task,
Function<String,String> provider)
Creates a delegating
callable, based on the given original, that will manage the tracer
lifecycle for every invocation. |
default Runnable |
delegate(Runnable task,
Function<String,String> provider)
Creates a delegating
runnable, based on the given original, that will manage the tracer
lifecycle for every invocation. |
void |
forEach(BiConsumer<String,String> consumer)
Iterates all configured traces and allows to observe their current values.
|
Trace |
get(String name)
Retrieves the
trace with the given name. |
boolean |
isActive() |
default <V> Callable<V> |
manage(Callable<V> task)
Creates a
callable, based on the given original, that will manage a completely new lifecycle
for every invocation, |
default Runnable |
manage(Runnable task)
Creates a
runnable, based on the given original, that will manage a completely new lifecycle
for every invocation, |
default <V> Callable<V> |
preserve(Callable<V> task)
Creates a
callable, based on the given original, that will preserve the current state of
all traces for every invocation. |
default Runnable |
preserve(Runnable task)
Creates a
runnable, based on the given original, that will preserve the current state of
all traces for every invocation. |
default Map<String,String> |
snapshot()
Provides an immutable snapshot of all configured traces mapped by name to their current values.
|
default void |
start()
Starts a new trace lifecycle without transporting any trace identifiers.
|
void |
start(Function<String,String> provider)
Starts a new trace lifecycle with the possibility to transport given trace identifiers.
|
void |
stop()
Stops the current lifecycle.
|
default <V> Callable<V> |
tryPreserve(Callable<V> task)
Creates a
callable, based on the given original, that will preserve the current state of
all traces for every invocation, if a trace is currently active. |
default Runnable |
tryPreserve(Runnable task)
Creates a
runnable, based on the given original, that will preserve the current state of
all traces for every invocation, if a trace is currently active. |
default void start()
The most common use case would be to manage a trace lifecycle in a scheduled background job or during tests.
tracer.start();
try {
// do some background work here
} finally {
tracer.stop();
}
IllegalStateException - if this tracer is already startedvoid start(Function<String,String> provider)
The most common use case would be to read headers from an incoming HTTP request and hand them over to this tracer in order to persist them:
tracer.start(request::getHeader)
try {
// continue with processing the request
} finally {
tracer.stop();
}
If the provider returns null a value will be created using the configured generator.
provider - a provider for trace identifiers, will be called with individual trace namesIllegalStateException - if this tracer is already startedboolean isActive()
Trace get(String name)
trace with the given name. The returned instance is a thread-safe live-view that
allows to observe future changes to that trace's lifecycle.
This can be used to manually access a trace value, e.g. for additional audit logging:
entity.setModifiedBy(trace.getValue()); db.persist(entity);
name - the name of the traceIllegalArgumentException - if no trace was configured for the given namevoid forEach(BiConsumer<String,String> consumer)
The most common use case woule be to pass all traces onto another system by adding them to an outgoing HTTP request:
tracer.forEach(request::addHeader);
consumer - a consumer for traces, will be called with trace name-value pairsIllegalStateException - if this tracer not starteddefault Map<String,String> snapshot()
IllegalStateException - if this tracer not startedvoid stop()
IllegalStateException - if this tracer is not starteddefault Runnable manage(Runnable task)
runnable, based on the given original, that will manage a completely new lifecycle
for every invocation,task - the original runnabledelegate(Runnable, Function)default Runnable preserve(Runnable task)
runnable, based on the given original, that will preserve the current state of
all traces for every invocation.task - the original runnableIllegalStateException - if this tracer is not startedsnapshot(),
delegate(Runnable, Function)default Runnable tryPreserve(Runnable task)
runnable, based on the given original, that will preserve the current state of
all traces for every invocation, if a trace is currently active. Otherwise it will manage a completely new
lifecycle for every invocation.task - the original runnablesnapshot(),
preserve(Runnable),
manage(Runnable)default Runnable delegate(Runnable task, Function<String,String> provider)
runnable, based on the given original, that will manage the tracer
lifecycle for every invocation. Initial trace values can be seeded with the given provider.task - the original runnableprovider - a provider for trace identifiers, will be called with individual trace namesstart(Function),
manage(Runnable),
preserve(Runnable)default <V> Callable<V> manage(Callable<V> task)
callable, based on the given original, that will manage a completely new lifecycle
for every invocation,task - the original callabledelegate(Callable, Function)default <V> Callable<V> preserve(Callable<V> task)
callable, based on the given original, that will preserve the current state of
all traces for every invocation.task - the original callableIllegalStateException - if this tracer is not startedsnapshot(),
delegate(Callable, Function)default <V> Callable<V> tryPreserve(Callable<V> task)
callable, based on the given original, that will preserve the current state of
all traces for every invocation, if a trace is currently active. Otherwise it will manage a completely new
lifecycle for every invocation.task - the original callablesnapshot(),
preserve(Callable),
manage(Callable)default <V> Callable<V> delegate(Callable<V> task, Function<String,String> provider)
callable, based on the given original, that will manage the tracer
lifecycle for every invocation. Initial trace values can be seeded with the given provider.task - the original callableprovider - a provider for trace identifiers, will be called with individual trace namesstart(Function),
manage(Callable),
preserve(Callable)static Tracer create(String... names)
UUIDGenerator by
default. The returned instance will not have any listeners attached to it.names - trace names to be configuredstatic TracerFactory.Builder builder()
Copyright © 2015–2016 Zalando SE. All rights reserved.