diff options
| author | Kristen Kozak <sebright@google.com> | 2017-07-10 17:26:13 -0700 |
|---|---|---|
| committer | Kristen Kozak <sebright@google.com> | 2017-07-11 06:41:12 -0700 |
| commit | 4a90bd0a421b1b4f4a509bec586ab9dba1c95020 (patch) | |
| tree | 41c281eff0cb654c76a30a4abbbddb48b45d81d0 /core | |
| parent | 9c0b5ee44ba47ee202fe4af040b69497eb257e36 (diff) | |
| download | platform_external_opencensus-java-4a90bd0a421b1b4f4a509bec586ab9dba1c95020.tar.gz platform_external_opencensus-java-4a90bd0a421b1b4f4a509bec586ab9dba1c95020.tar.bz2 platform_external_opencensus-java-4a90bd0a421b1b4f4a509bec586ab9dba1c95020.zip | |
Add Scope as interface for NonThrowingCloseables that work with current context.
This interface allows users to write a shorter, more descriptive type name when
using scoped tags or spans.
Diffstat (limited to 'core')
3 files changed, 10 insertions, 11 deletions
diff --git a/core/src/main/java/io/opencensus/stats/CurrentTagsUtils.java b/core/src/main/java/io/opencensus/stats/CurrentTagsUtils.java index e22f372b..96022561 100644 --- a/core/src/main/java/io/opencensus/stats/CurrentTagsUtils.java +++ b/core/src/main/java/io/opencensus/stats/CurrentTagsUtils.java @@ -14,8 +14,7 @@ package io.opencensus.stats; import io.grpc.Context; - -import io.opencensus.common.NonThrowingCloseable; +import io.opencensus.common.Scope; import io.opencensus.tags.unsafe.ContextUtils; /** @@ -48,12 +47,12 @@ final class CurrentTagsUtils { * @return An object that defines a scope where the given {@code StatsContext} is set to the * current context. */ - static NonThrowingCloseable withStatsContext(StatsContext statsContext) { + static Scope withStatsContext(StatsContext statsContext) { return new WithStatsContext(statsContext, ContextUtils.TAG_CONTEXT_KEY); } // Supports try-with-resources idiom. - private static final class WithStatsContext implements NonThrowingCloseable { + private static final class WithStatsContext implements Scope { private final Context origContext; diff --git a/core/src/main/java/io/opencensus/stats/StatsContextFactory.java b/core/src/main/java/io/opencensus/stats/StatsContextFactory.java index cee9f253..833d6d3d 100644 --- a/core/src/main/java/io/opencensus/stats/StatsContextFactory.java +++ b/core/src/main/java/io/opencensus/stats/StatsContextFactory.java @@ -15,7 +15,7 @@ package io.opencensus.stats; import static com.google.common.base.Preconditions.checkNotNull; -import io.opencensus.common.NonThrowingCloseable; +import io.opencensus.common.Scope; import java.io.IOException; import java.io.InputStream; @@ -67,7 +67,7 @@ public abstract class StatsContextFactory { * 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)) { + * try (Scope scopedStatsCtx = statsCtxFactory.withStatsContext(statsCtx)) { * doSomeOtherWork(); // Here "scopedStatsCtx" is the current StatsContext. * } * } @@ -84,7 +84,7 @@ public abstract class StatsContextFactory { * 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); + * Scope scopedStatsCtx = statsCtxFactory.withStatsContext(statsCtx); * try { * doSomeOtherWork(); // Here "scopedStatsCtx" is the current StatsContext. * } finally { @@ -98,7 +98,7 @@ public abstract class StatsContextFactory { * current Context. * @throws NullPointerException if statsContext is null. */ - public final NonThrowingCloseable withStatsContext(StatsContext statsContext) { + public final Scope withStatsContext(StatsContext statsContext) { return CurrentTagsUtils.withStatsContext(checkNotNull(statsContext, "statsContext")); } } diff --git a/core/src/test/java/io/opencensus/stats/CurrentTagsUtilsTest.java b/core/src/test/java/io/opencensus/stats/CurrentTagsUtilsTest.java index 46e5675b..0e3060d9 100644 --- a/core/src/test/java/io/opencensus/stats/CurrentTagsUtilsTest.java +++ b/core/src/test/java/io/opencensus/stats/CurrentTagsUtilsTest.java @@ -16,7 +16,7 @@ package io.opencensus.stats; import static com.google.common.truth.Truth.assertThat; import io.grpc.Context; -import io.opencensus.common.NonThrowingCloseable; +import io.opencensus.common.Scope; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -46,7 +46,7 @@ public class CurrentTagsUtilsTest { @Test public void testWithStatsContext() { assertThat(CurrentTagsUtils.getCurrentStatsContext()).isNull(); - NonThrowingCloseable scopedStatsCtx = CurrentTagsUtils.withStatsContext(statsContext); + Scope scopedStatsCtx = CurrentTagsUtils.withStatsContext(statsContext); try { assertThat(CurrentTagsUtils.getCurrentStatsContext()).isSameAs(statsContext); } finally { @@ -58,7 +58,7 @@ public class CurrentTagsUtilsTest { @Test public void testWithStatsContextUsingWrap() { Runnable runnable; - NonThrowingCloseable scopedStatsCtx = CurrentTagsUtils.withStatsContext(statsContext); + Scope scopedStatsCtx = CurrentTagsUtils.withStatsContext(statsContext); try { assertThat(CurrentTagsUtils.getCurrentStatsContext()).isSameAs(statsContext); runnable = Context.current().wrap( |
