public abstract class StatsContextFactory extends Object
StatsContext.| Constructor and Description |
|---|
StatsContextFactory() |
| Modifier and Type | Method and Description |
|---|---|
abstract StatsContext |
deserialize(InputStream input)
Creates a
StatsContext from the given on-the-wire encoded representation. |
StatsContext |
getCurrentStatsContext()
Get current StatsContext from current gRPC Context.
|
abstract StatsContext |
getDefault()
Returns the default
StatsContext. |
NonThrowingCloseable |
withStatsContext(StatsContext statsContext)
Enters the scope of code where the given
StatsContext is in the current Context, and
returns an object that represents that scope. |
public abstract StatsContext deserialize(InputStream input) throws IOException
StatsContext from the given on-the-wire encoded representation.
Should be the inverse of StatsContext.serialize(java.io.OutputStream). The
serialized representation should be based on the StatsContext binary representation.
input - on-the-wire representation of a StatsContextStatsContext deserialized from inputIOExceptionpublic abstract StatsContext getDefault()
StatsContext.public final StatsContext getCurrentStatsContext()
StatsContext from io.grpc.Context, or the default
StatsContext if there's no StatsContext associated with current
io.grpc.Context.public final NonThrowingCloseable withStatsContext(StatsContext statsContext)
StatsContext is in the current Context, and
returns an object that represents that scope. The scope is exited when the returned object is
closed.
Supports try-with-resource idiom.
Example of usage:
private final StatsContextFactory statsCtxFactory =
checkNotNull(Stats.getStatsContextFactory(), "statsCtxFactory");
void doWork() {
// Construct a new StatsContext with required tags to be set into current context.
StatsContext statsCtx = statsCtxFactory.getCurrentStatsContext().with(tagKey, tagValue);
try (NonThrowingCloseable scopedStatsCtx = statsCtxFactory.withStatsContext(statsCtx)) {
doSomeOtherWork(); // Here "scopedStatsCtx" is the current StatsContext.
}
}
Prior to Java SE 7, you can use a finally block to ensure that a resource is closed regardless of whether the try statement completes normally or abruptly.
Example of usage prior to Java SE7:
private final StatsContextFactory statsCtxFactory =
checkNotNull(Stats.getStatsContextFactory(), "statsCtxFactory");
void doWork() {
// Construct a new StatsContext with required tags to be set into current context.
StatsContext statsCtx = statsCtxFactory.getCurrentStatsContext().with(tagKey, tagValue);
NonThrowingCloseable scopedStatsCtx = statsCtxFactory.withStatsContext(statsCtx);
try {
doSomeOtherWork(); // Here "scopedStatsCtx" is the current StatsContext.
} finally {
scopedStatsCtx.close();
}
}
statsContext - The StatsContext to be set to the current Context.StatsContext will be set to the
current Context.NullPointerException - if statsContext is null.