Search in sources :

Example 11 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class OcAgentExportersQuickStart method doWork.

private static void doWork(int iteration, int jobs, LongGauge gauge) {
    String childSpanName = "iteration-" + iteration;
    LabelValue value = LabelValue.create(childSpanName);
    LongPoint point = gauge.getOrCreateTimeSeries(Collections.singletonList(value));
    try (Scope scope = tracer.spanBuilder(childSpanName).startScopedSpan()) {
        for (int i = 0; i < jobs; i++) {
            String grandChildSpanName = childSpanName + "-job-" + i;
            try (Scope childScope = tracer.spanBuilder(grandChildSpanName).startScopedSpan()) {
                point.set(jobs - i);
                String line = generateRandom(random.nextInt(128));
                processLine(line);
                recordStat(M_LINES_IN, 1L);
                recordStat(M_LINE_LENGTHS, (long) line.length());
            } catch (Exception e) {
                tracer.getCurrentSpan().setStatus(Status.INTERNAL.withDescription(e.toString()));
            }
        }
    }
}
Also used : LabelValue(io.opencensus.metrics.LabelValue) Scope(io.opencensus.common.Scope) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) LongPoint(io.opencensus.metrics.LongGauge.LongPoint)

Example 12 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class DerivedLongCumulativeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DerivedLongCumulativeImpl derivedLongCumulative2 = new DerivedLongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
    derivedLongCumulative2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
    testClock.advanceTime(ONE_MINUTE);
    Timestamp endTime = testClock.now();
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_INT64, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), endTime), START_TIME);
    Metric metric = derivedLongCumulative2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedLongCumulative2.removeTimeSeries(labelValues);
    Metric metric2 = derivedLongCumulative2.getMetric(testClock);
    assertThat(metric2).isNull();
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Timestamp(io.opencensus.common.Timestamp) Test(org.junit.Test)

Example 13 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class DoubleCumulativeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DoubleCumulativeImpl doubleCumulative = new DoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
    DoublePoint doublePoint = doubleCumulative.getOrCreateTimeSeries(labelValues);
    doublePoint.add(1);
    doublePoint.add(2);
    DoublePoint defaultPoint = doubleCumulative.getDefaultTimeSeries();
    defaultPoint.add(100);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, allKeys);
    testClock.advanceTime(ONE_MINUTE);
    Timestamp endTime = testClock.now();
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
    TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.doubleValue(100), endTime), START_TIME);
    expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(3), endTime), START_TIME));
    expectedTimeSeriesList.add(defaultTimeSeries);
    Metric metric = doubleCumulative.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
    doubleCumulative.removeTimeSeries(labelValues);
    Metric metric2 = doubleCumulative.getMetric(testClock);
    assertThat(metric2).isNotNull();
    assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
Also used : TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) Timestamp(io.opencensus.common.Timestamp) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) DoublePoint(io.opencensus.metrics.DoubleCumulative.DoublePoint) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 14 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class LongCumulativeImplTest method testEquals.

@Test
public void testEquals() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LongCumulativeImpl longCumulative = new LongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS, START_TIME);
    LongPoint defaultPoint1 = longCumulative.getDefaultTimeSeries();
    LongPoint defaultPoint2 = longCumulative.getDefaultTimeSeries();
    LongPoint longPoint1 = longCumulative.getOrCreateTimeSeries(labelValues);
    LongPoint longPoint2 = longCumulative.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(longPoint1, longPoint2).testEquals();
    longCumulative.clear();
    LongPoint newDefaultPointAfterClear = longCumulative.getDefaultTimeSeries();
    LongPoint newLongPointAfterClear = longCumulative.getOrCreateTimeSeries(labelValues);
    longCumulative.removeTimeSeries(labelValues);
    LongPoint newLongPointAfterRemove = longCumulative.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(longPoint1, longPoint2).addEqualityGroup(newDefaultPointAfterClear).addEqualityGroup(newLongPointAfterClear).addEqualityGroup(newLongPointAfterRemove).testEquals();
}
Also used : LabelValue(io.opencensus.metrics.LabelValue) EqualsTester(com.google.common.testing.EqualsTester) LabelKey(io.opencensus.metrics.LabelKey) LongPoint(io.opencensus.metrics.LongCumulative.LongPoint) Test(org.junit.Test)

Example 15 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class DerivedLongGaugeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DerivedLongGaugeImpl derivedLongGauge2 = new DerivedLongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    derivedLongGauge2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_INT64, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(2), TEST_TIME), null);
    Metric metric = derivedLongGauge2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedLongGauge2.removeTimeSeries(labelValues);
    Metric metric2 = derivedLongGauge2.getMetric(testClock);
    assertThat(metric2).isNull();
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Aggregations

LabelValue (io.opencensus.metrics.LabelValue)22 LabelKey (io.opencensus.metrics.LabelKey)17 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)10 TimeSeries (io.opencensus.metrics.export.TimeSeries)10 Metric (io.opencensus.metrics.export.Metric)9 EqualsTester (com.google.common.testing.EqualsTester)4 Timestamp (io.opencensus.common.Timestamp)4 Metric (com.google.api.Metric)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)3 TimeSeries (com.google.monitoring.v3.TimeSeries)2 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)2 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)2 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)2 Type (io.opencensus.metrics.export.MetricDescriptor.Type)2 Value (io.opencensus.metrics.export.Value)2 List (java.util.List)2 Map (java.util.Map)2