Java Code Examples for io.opencensus.tags.unsafe.ContextUtils

The following examples show how to use io.opencensus.tags.unsafe.ContextUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: opencensus-java   Source File: StatsRecorderImplTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void record_CurrentContextSet() {
  View view =
      View.create(
          VIEW_NAME,
          "description",
          MEASURE_DOUBLE,
          Sum.create(),
          Arrays.asList(KEY),
          Cumulative.create());
  viewManager.registerView(view);
  TagContext tags = new SimpleTagContext(Tag.create(KEY, VALUE));
  Context orig = ContextUtils.withValue(Context.current(), tags).attach();
  try {
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record();
  } finally {
    Context.current().detach(orig);
  }
  ViewData viewData = viewManager.getView(VIEW_NAME);

  // record() should have used the given TagContext.
  assertThat(viewData.getAggregationMap().keySet()).containsExactly(Arrays.asList(VALUE));
}
 
Example 2
Source Project: opencensus-java   Source File: CurrentTagMapUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCurrentTagMap_ContextSetToNull() {
  Context orig = ContextUtils.withValue(Context.current(), null).attach();
  try {
    TagContext tags = CurrentTagMapUtils.getCurrentTagMap();
    assertThat(tags).isNotNull();
    assertThat(tagContextToList(tags)).isEmpty();
  } finally {
    Context.current().detach(orig);
  }
}
 
Example 3
Source Project: opencensus-java   Source File: TaggerImplTest.java    License: Apache License 2.0 5 votes vote down vote up
private TagContextBuilder getResultOfCurrentBuilder(TagContext tagsToSet) {
  Context orig = ContextUtils.withValue(Context.current(), tagsToSet).attach();
  try {
    return tagger.currentBuilder();
  } finally {
    Context.current().detach(orig);
  }
}
 
Example 4
Source Project: opencensus-java   Source File: TaggerImplTest.java    License: Apache License 2.0 5 votes vote down vote up
private TagContext getResultOfGetCurrentTagContext(TagContext tagsToSet) {
  Context orig = ContextUtils.withValue(Context.current(), tagsToSet).attach();
  try {
    return tagger.getCurrentTagContext();
  } finally {
    Context.current().detach(orig);
  }
}
 
Example 5
Source Project: opencensus-java   Source File: TaggerImplTest.java    License: Apache License 2.0 5 votes vote down vote up
private TagContext getResultOfWithTagContext(TagContext tagsToSet) {
  Scope scope = tagger.withTagContext(tagsToSet);
  try {
    return ContextUtils.getValue(Context.current());
  } finally {
    scope.close();
  }
}
 
Example 6
Source Project: grpc-java   Source File: CensusStatsModule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Context filterContext(Context context) {
  if (!module.tagger.empty().equals(parentCtx)) {
    return ContextUtils.withValue(context, parentCtx);
  }
  return context;
}
 
Example 7
Source Project: grpc-nebula-java   Source File: StatsTestUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TagContext getCurrentTagContext() {
  return ContextUtils.TAG_CONTEXT_KEY.get();
}
 
Example 8
Source Project: opencensus-java   Source File: MeasureMapImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void record() {
  // Use the context key directly, to avoid depending on the tags implementation.
  record(ContextUtils.getValue(Context.current()));
}
 
Example 9
Source Project: grpc-java   Source File: StatsTestUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TagContext getCurrentTagContext() {
  return ContextUtils.getValue(Context.current());
}
 
Example 10
Source Project: opencensus-java   Source File: CurrentTagMapUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link TagContext} from the current context.
 *
 * @return the {@code TagContext} from the current context.
 */
static TagContext getCurrentTagMap() {
  return ContextUtils.getValue(Context.current());
}
 
Example 11
Source Project: opencensus-java   Source File: CurrentTagMapUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a new {@link WithTagMap}.
 *
 * @param tags the {@code TagContext} to be added to the current {@code Context}.
 */
private WithTagMap(TagContext tags) {
  orig = ContextUtils.withValue(Context.current(), tags).attach();
}