Search in sources :

Example 6 with LabelValue

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

the class DerivedDoubleCumulativeImplTest 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);
    DerivedDoubleCumulativeImpl derivedDoubleCumulative2 = new DerivedDoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
    derivedDoubleCumulative2.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_DOUBLE, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), endTime), START_TIME);
    Metric metric = derivedDoubleCumulative2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedDoubleCumulative2.removeTimeSeries(labelValues);
    Metric metric2 = derivedDoubleCumulative2.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 7 with LabelValue

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

the class DoubleCumulativeImplTest 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"));
    DoubleCumulativeImpl doubleCumulative = new DoubleCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS, START_TIME);
    DoublePoint defaultPoint1 = doubleCumulative.getDefaultTimeSeries();
    DoublePoint defaultPoint2 = doubleCumulative.getDefaultTimeSeries();
    DoublePoint doublePoint1 = doubleCumulative.getOrCreateTimeSeries(labelValues);
    DoublePoint doublePoint2 = doubleCumulative.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).testEquals();
    doubleCumulative.clear();
    DoublePoint newDefaultPointAfterClear = doubleCumulative.getDefaultTimeSeries();
    DoublePoint newDoublePointAfterClear = doubleCumulative.getOrCreateTimeSeries(labelValues);
    doubleCumulative.removeTimeSeries(labelValues);
    DoublePoint newDoublePointAfterRemove = doubleCumulative.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).addEqualityGroup(newDefaultPointAfterClear).addEqualityGroup(newDoublePointAfterClear).addEqualityGroup(newDoublePointAfterRemove).testEquals();
}
Also used : LabelValue(io.opencensus.metrics.LabelValue) EqualsTester(com.google.common.testing.EqualsTester) DoublePoint(io.opencensus.metrics.DoubleCumulative.DoublePoint) LabelKey(io.opencensus.metrics.LabelKey) Test(org.junit.Test)

Example 8 with LabelValue

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

the class DerivedDoubleGaugeImplTest 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);
    DerivedDoubleGaugeImpl derivedDoubleGauge2 = new DerivedDoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    derivedDoubleGauge2.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_DOUBLE, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(2.5), TEST_TIME), null);
    Metric metric = derivedDoubleGauge2.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
    derivedDoubleGauge2.removeTimeSeries(labelValues);
    Metric metric2 = derivedDoubleGauge2.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)

Example 9 with LabelValue

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

the class DropWizardMetrics method collectGauge.

/**
 * Returns a {@code Metric} collected from {@link Gauge}.
 *
 * @param dropwizardName the metric name.
 * @param gauge the gauge object to collect.
 * @return a {@code Metric}.
 */
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(String dropwizardName, Gauge gauge) {
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardName, "gauge");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardName, gauge);
    // Figure out which gauge instance and call the right method to get value
    Type type;
    Value value;
    Object obj = gauge.getValue();
    if (obj instanceof Number) {
        type = Type.GAUGE_DOUBLE;
        value = Value.doubleValue(((Number) obj).doubleValue());
    } else if (obj instanceof Boolean) {
        type = Type.GAUGE_INT64;
        value = Value.longValue(((Boolean) obj) ? 1 : 0);
    } else {
        // Ignoring Gauge (gauge.getKey()) with unhandled type.
        return null;
    }
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, type, Collections.<LabelKey>emptyList());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), Point.create(value, clock.now()), null);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : Type(io.opencensus.metrics.export.MetricDescriptor.Type) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) Value(io.opencensus.metrics.export.Value) Nullable(javax.annotation.Nullable)

Example 10 with LabelValue

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

the class DropWizardMetrics method collectGauge.

/**
 * Returns a {@code Metric} collected from {@link Gauge}.
 *
 * @param dropwizardMetric the metric name.
 * @param gauge the gauge object to collect.
 * @return a {@code Metric}.
 */
@SuppressWarnings("rawtypes")
@Nullable
private Metric collectGauge(MetricName dropwizardMetric, Gauge gauge) {
    // TODO cache dropwizard MetricName -> OC MetricDescriptor, Labels conversion
    String metricName = DropWizardUtils.generateFullMetricName(dropwizardMetric.getKey(), "gauge");
    String metricDescription = DropWizardUtils.generateFullMetricDescription(dropwizardMetric.getKey(), gauge);
    AbstractMap.SimpleImmutableEntry<List<LabelKey>, List<LabelValue>> labels = DropWizardUtils.generateLabels(dropwizardMetric);
    // Figure out which gauge instance and call the right method to get value
    Type type;
    Value value;
    Object obj = gauge.getValue();
    if (obj instanceof Number) {
        type = Type.GAUGE_DOUBLE;
        value = Value.doubleValue(((Number) obj).doubleValue());
    } else if (obj instanceof Boolean) {
        type = Type.GAUGE_INT64;
        value = Value.longValue(((Boolean) obj) ? 1 : 0);
    } else {
        // Ignoring Gauge (gauge.getKey()) with unhandled type.
        return null;
    }
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, DEFAULT_UNIT, type, labels.getKey());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(labels.getValue(), Point.create(value, clock.now()), null);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : AbstractMap(java.util.AbstractMap) Type(io.opencensus.metrics.export.MetricDescriptor.Type) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) Value(io.opencensus.metrics.export.Value) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(javax.annotation.Nullable)

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