Search in sources :

Example 1 with LabelKey

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

the class StackdriverExportUtils method createMetric.

// Create a Metric using the LabelKeys and LabelValues.
@VisibleForTesting
static Metric createMetric(io.opencensus.metrics.export.MetricDescriptor metricDescriptor, List<LabelValue> labelValues, String domain, Map<LabelKey, LabelValue> constantLabels) {
    Metric.Builder builder = Metric.newBuilder();
    builder.setType(generateType(metricDescriptor.getName(), domain));
    Map<String, String> stringTagMap = Maps.newHashMap();
    List<LabelKey> labelKeys = metricDescriptor.getLabelKeys();
    for (int i = 0; i < labelValues.size(); i++) {
        String value = labelValues.get(i).getValue();
        if (value == null) {
            continue;
        }
        stringTagMap.put(labelKeys.get(i).getKey(), value);
    }
    for (Map.Entry<LabelKey, LabelValue> constantLabel : constantLabels.entrySet()) {
        String constantLabelKey = constantLabel.getKey().getKey();
        String constantLabelValue = constantLabel.getValue().getValue();
        constantLabelValue = constantLabelValue == null ? "" : constantLabelValue;
        stringTagMap.put(constantLabelKey, constantLabelValue);
    }
    builder.putAllLabels(stringTagMap);
    return builder.build();
}
Also used : LabelValue(io.opencensus.metrics.LabelValue) LabelKey(io.opencensus.metrics.LabelKey) Metric(com.google.api.Metric) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Point(com.google.monitoring.v3.Point) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with LabelKey

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

the class StackdriverExportUtilsTest method convertSummaryMetric.

@Test
public void convertSummaryMetric() {
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor1 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SUMMARY_SUFFIX_COUNT, METRIC_DESCRIPTION, METRIC_UNIT_2, Type.CUMULATIVE_INT64, LABEL_KEY);
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor2 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SUMMARY_SUFFIX_SUM, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_DOUBLE, LABEL_KEY);
    List<LabelKey> labelKeys = new ArrayList<>(LABEL_KEY);
    labelKeys.add(PERCENTILE_LABEL_KEY);
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor3 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SNAPSHOT_SUFFIX_PERCENTILE, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, labelKeys);
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries1 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, Point.create(Value.longValue(22), TIMESTAMP), null));
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries2 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, Point.create(Value.doubleValue(74.8), TIMESTAMP), null));
    LabelValue existingLabelValues = LABEL_VALUE.get(0);
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries3 = Arrays.asList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("50.0")), Point.create(Value.doubleValue(6), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("75.0")), Point.create(Value.doubleValue(10.2), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("98.0")), Point.create(Value.doubleValue(4.6), TIMESTAMP), null), io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("99.0")), Point.create(Value.doubleValue(1.2), TIMESTAMP), null));
    List<io.opencensus.metrics.export.Metric> metrics = StackdriverExportUtils.convertSummaryMetric(SUMMARY_METRIC);
    assertThat(metrics).isNotEmpty();
    assertThat(metrics.size()).isEqualTo(3);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor1);
    assertThat(metrics.get(0).getTimeSeriesList()).isNotEmpty();
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries1);
    assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries2);
    assertThat(metrics.get(1).getTimeSeriesList()).isNotEmpty();
    assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor2);
    assertThat(metrics.get(2).getTimeSeriesList()).isNotEmpty();
    assertThat(metrics.get(2).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor3);
    assertThat(metrics.get(2).getTimeSeriesList().size()).isEqualTo(4);
    assertThat(metrics.get(2).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries3);
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(com.google.api.Metric) Test(org.junit.Test)

Example 3 with LabelKey

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

the class LongGaugeImplTest 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);
    LongGaugeImpl longGauge = new LongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    LongPoint longPoint = longGauge.getOrCreateTimeSeries(labelValues);
    longPoint.add(1);
    longPoint.add(2);
    LongPoint defaultPoint = longGauge.getDefaultTimeSeries();
    defaultPoint.set(100);
    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);
    List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
    TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.longValue(100), TEST_TIME), null);
    expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), TEST_TIME), null));
    expectedTimeSeriesList.add(defaultTimeSeries);
    Metric metric = longGauge.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
    longGauge.removeTimeSeries(labelValues);
    Metric metric2 = longGauge.getMetric(testClock);
    assertThat(metric2).isNotNull();
    assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
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) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) Test(org.junit.Test)

Example 4 with LabelKey

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

the class DropWizardMetricsTest method collect_Histogram.

@Test
public void collect_Histogram() {
    Histogram resultCounts = metricRegistry.histogram("result");
    resultCounts.update(200);
    ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
    assertThat(metrics.size()).isEqualTo(1);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("codahale_result_histogram", "Collected from codahale (metric=result, type=com.codahale.metrics.Histogram)", DEFAULT_UNIT, Type.SUMMARY, Collections.<LabelKey>emptyList()));
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.summaryValue(Summary.create(1L, 0.0, Snapshot.create(1L, 0.0, Arrays.asList(ValueAtPercentile.create(50.0, 200.0), ValueAtPercentile.create(75.0, 200.0), ValueAtPercentile.create(98.0, 200.0), ValueAtPercentile.create(99.0, 200.0), ValueAtPercentile.create(99.9, 200.0))))));
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getStartTimestamp()).isNotNull();
}
Also used : Histogram(com.codahale.metrics.Histogram) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 5 with LabelKey

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

the class DropWizardMetricsTest method collect_Counter.

@Test
public void collect_Counter() {
    // create dropwizard metrics
    Counter evictions = metricRegistry.counter("cache_evictions");
    evictions.inc();
    evictions.inc(3);
    evictions.dec();
    evictions.dec(2);
    ArrayList<Metric> metrics = new ArrayList<>(dropWizardMetrics.getMetrics());
    assertThat(metrics.size()).isEqualTo(1);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(MetricDescriptor.create("codahale_cache_evictions_counter", "Collected from codahale (metric=cache_evictions, " + "type=com.codahale.metrics.Counter)", DEFAULT_UNIT, Type.GAUGE_INT64, Collections.<LabelKey>emptyList()));
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(0);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(1));
    assertThat(metrics.get(0).getTimeSeriesList().get(0).getStartTimestamp()).isNull();
}
Also used : Counter(com.codahale.metrics.Counter) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Aggregations

LabelKey (io.opencensus.metrics.LabelKey)25 Test (org.junit.Test)22 LabelValue (io.opencensus.metrics.LabelValue)17 ArrayList (java.util.ArrayList)16 Metric (io.opencensus.metrics.export.Metric)15 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)8 TimeSeries (io.opencensus.metrics.export.TimeSeries)8 EqualsTester (com.google.common.testing.EqualsTester)4 Timestamp (io.opencensus.common.Timestamp)4 Metric (com.google.api.Metric)3 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)3 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)3 MetricDescriptor (com.google.api.MetricDescriptor)2 TimeSeries (com.google.monitoring.v3.TimeSeries)2 ByteString (com.google.protobuf.ByteString)2 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)2 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)2 Map (java.util.Map)2 Counter (com.codahale.metrics.Counter)1 Histogram (com.codahale.metrics.Histogram)1