Search in sources :

Example 51 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class ViewManagerImplTest method testMultipleViews_DifferentMeasures.

private void testMultipleViews_DifferentMeasures(Measure measure1, Measure measure2, double value1, double value2) {
    final View view1 = createCumulativeView(VIEW_NAME, measure1, DISTRIBUTION, Arrays.asList(KEY));
    final View view2 = createCumulativeView(VIEW_NAME_2, measure2, DISTRIBUTION, Arrays.asList(KEY));
    clock.setTime(Timestamp.create(1, 0));
    viewManager.registerView(view1);
    clock.setTime(Timestamp.create(2, 0));
    viewManager.registerView(view2);
    TagContext tags = tagger.emptyBuilder().put(KEY, VALUE).build();
    MeasureMap measureMap = statsRecorder.newMeasureMap();
    putToMeasureMap(measureMap, measure1, value1);
    putToMeasureMap(measureMap, measure2, value2);
    measureMap.record(tags);
    clock.setTime(Timestamp.create(3, 0));
    ViewData viewData1 = viewManager.getView(VIEW_NAME);
    clock.setTime(Timestamp.create(4, 0));
    ViewData viewData2 = viewManager.getView(VIEW_NAME_2);
    assertThat(viewData1.getWindowData()).isEqualTo(CumulativeData.create(Timestamp.create(1, 0), Timestamp.create(3, 0)));
    StatsTestUtil.assertAggregationMapEquals(viewData1.getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(DISTRIBUTION, measure1, value1)), EPSILON);
    assertThat(viewData2.getWindowData()).isEqualTo(CumulativeData.create(Timestamp.create(2, 0), Timestamp.create(4, 0)));
    StatsTestUtil.assertAggregationMapEquals(viewData2.getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(DISTRIBUTION, measure2, value2)), EPSILON);
}
Also used : TagContext(io.opencensus.tags.TagContext) StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) View(io.opencensus.stats.View) MeasureMap(io.opencensus.stats.MeasureMap)

Example 52 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class ViewManagerImplTest method testRecordInterval.

private final void testRecordInterval(Measure measure, Aggregation aggregation, double[] initialValues, /* There are 5 initial values recorded before we call getView(). */
double value6, double value7, AggregationData expectedValues1, AggregationData expectedValues2, AggregationData expectedValues3) {
    // The interval is 10 seconds, i.e. values should expire after 10 seconds.
    // Each bucket has a duration of 2.5 seconds.
    View view = View.create(VIEW_NAME, VIEW_DESCRIPTION, measure, aggregation, Arrays.asList(KEY), Interval.create(TEN_SECONDS));
    // start at 30s
    long startTimeMillis = 30 * MILLIS_PER_SECOND;
    clock.setTime(Timestamp.fromMillis(startTimeMillis));
    viewManager.registerView(view);
    TagContext tags = tagger.emptyBuilder().put(KEY, VALUE).build();
    for (int i = 1; i <= 5; i++) {
        /*
       * Add each value in sequence, at 31s, 32s, 33s, etc.
       * 1st and 2nd values should fall into the first bucket [30.0, 32.5),
       * 3rd and 4th values should fall into the second bucket [32.5, 35.0),
       * 5th value should fall into the third bucket [35.0, 37.5).
       */
        clock.setTime(Timestamp.fromMillis(startTimeMillis + i * MILLIS_PER_SECOND));
        putToMeasureMap(statsRecorder.newMeasureMap(), measure, initialValues[i - 1]).record(tags);
    }
    clock.setTime(Timestamp.fromMillis(startTimeMillis + 8 * MILLIS_PER_SECOND));
    // 38s, no values should have expired
    StatsTestUtil.assertAggregationMapEquals(viewManager.getView(VIEW_NAME).getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(aggregation, measure, initialValues)), EPSILON);
    clock.setTime(Timestamp.fromMillis(startTimeMillis + 11 * MILLIS_PER_SECOND));
    // 41s, 40% of the values in the first bucket should have expired (1 / 2.5 = 0.4).
    StatsTestUtil.assertAggregationMapEquals(viewManager.getView(VIEW_NAME).getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), expectedValues1), EPSILON);
    clock.setTime(Timestamp.fromMillis(startTimeMillis + 12 * MILLIS_PER_SECOND));
    // 42s, add a new value value1, should fall into bucket [40.0, 42.5)
    putToMeasureMap(statsRecorder.newMeasureMap(), measure, value6).record(tags);
    clock.setTime(Timestamp.fromMillis(startTimeMillis + 17 * MILLIS_PER_SECOND));
    // 47s, values in the first and second bucket should have expired, and 80% of values in the
    // third bucket should have expired. The new value should persist.
    StatsTestUtil.assertAggregationMapEquals(viewManager.getView(VIEW_NAME).getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), expectedValues2), EPSILON);
    clock.setTime(Timestamp.fromMillis(60 * MILLIS_PER_SECOND));
    // 60s, all previous values should have expired, add another value value2
    putToMeasureMap(statsRecorder.newMeasureMap(), measure, value7).record(tags);
    StatsTestUtil.assertAggregationMapEquals(viewManager.getView(VIEW_NAME).getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), expectedValues3), EPSILON);
    clock.setTime(Timestamp.fromMillis(100 * MILLIS_PER_SECOND));
    // 100s, all values should have expired
    assertThat(viewManager.getView(VIEW_NAME).getAggregationMap()).isEmpty();
}
Also used : TagContext(io.opencensus.tags.TagContext) View(io.opencensus.stats.View)

Example 53 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class ScopedTagMapTest method createBuilderFromCurrentTags.

@Test
public void createBuilderFromCurrentTags() {
    TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1).build();
    Scope scope = tagger.withTagContext(scopedTags);
    try {
        TagContext newTags = tagger.currentBuilder().put(KEY_2, VALUE_2).build();
        assertThat(tagContextToList(newTags)).containsExactly(Tag.create(KEY_1, VALUE_1), Tag.create(KEY_2, VALUE_2));
        assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
    } finally {
        scope.close();
    }
}
Also used : Scope(io.opencensus.common.Scope) TagContext(io.opencensus.tags.TagContext) Test(org.junit.Test)

Example 54 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class ScopedTagMapTest method addToCurrentTagsWithBuilder.

@Test
public void addToCurrentTagsWithBuilder() {
    TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1).build();
    Scope scope1 = tagger.withTagContext(scopedTags);
    try {
        Scope scope2 = tagger.currentBuilder().put(KEY_2, VALUE_2).buildScoped();
        try {
            assertThat(tagContextToList(tagger.getCurrentTagContext())).containsExactly(Tag.create(KEY_1, VALUE_1), Tag.create(KEY_2, VALUE_2));
        } finally {
            scope2.close();
        }
        assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
    } finally {
        scope1.close();
    }
}
Also used : Scope(io.opencensus.common.Scope) TagContext(io.opencensus.tags.TagContext) Test(org.junit.Test)

Example 55 with TagContext

use of io.opencensus.tags.TagContext in project instrumentation-java by census-instrumentation.

the class ScopedTagMapTest method multiScopeTagMapWithMetadata.

@Test
public void multiScopeTagMapWithMetadata() {
    TagContext scopedTags = tagger.emptyBuilder().put(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION).put(KEY_2, VALUE_2, METADATA_UNLIMITED_PROPAGATION).build();
    Scope scope1 = tagger.withTagContext(scopedTags);
    try {
        // Scope 1
        Scope scope2 = tagger.currentBuilder().put(KEY_3, VALUE_3, METADATA_NO_PROPAGATION).put(KEY_2, VALUE_4, METADATA_NO_PROPAGATION).buildScoped();
        try {
            // Scope 2
            assertThat(tagContextToList(tagger.getCurrentTagContext())).containsExactly(Tag.create(KEY_1, VALUE_1, METADATA_UNLIMITED_PROPAGATION), Tag.create(KEY_2, VALUE_4, METADATA_NO_PROPAGATION), Tag.create(KEY_3, VALUE_3, METADATA_NO_PROPAGATION));
        } finally {
            // Close Scope 2
            scope2.close();
        }
        assertThat(tagger.getCurrentTagContext()).isSameInstanceAs(scopedTags);
    } finally {
        scope1.close();
    }
}
Also used : Scope(io.opencensus.common.Scope) TagContext(io.opencensus.tags.TagContext) Test(org.junit.Test)

Aggregations

TagContext (io.opencensus.tags.TagContext)76 Test (org.junit.Test)56 Tag (io.opencensus.tags.Tag)10 TagContextBuilder (io.opencensus.tags.TagContextBuilder)9 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)7 Context (io.grpc.Context)6 Scope (io.opencensus.common.Scope)5 StatsTestUtil.createEmptyViewData (io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData)5 View (io.opencensus.stats.View)5 ViewData (io.opencensus.stats.ViewData)5 Metadata (io.grpc.Metadata)3 StatsTestUtils (io.grpc.internal.testing.StatsTestUtils)3 MeasureMap (io.opencensus.stats.MeasureMap)3 TagValue (io.opencensus.tags.TagValue)3 SpanContext (io.opencensus.trace.SpanContext)3 HashMap (java.util.HashMap)3 ServerStreamTracer (io.grpc.ServerStreamTracer)2 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)2 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)2 SimpleTagContext (io.opencensus.implcore.stats.StatsTestUtil.SimpleTagContext)2