Search in sources :

Example 21 with LabelKey

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

the class StackdriverExportUtilsTest method convertSummaryMetricWithNullSum.

@Test
public void convertSummaryMetricWithNullSum() {
    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);
    List<LabelKey> labelKeys = new ArrayList<>(LABEL_KEY);
    labelKeys.add(PERCENTILE_LABEL_KEY);
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor2 = 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));
    LabelValue existingLabelValues = LABEL_VALUE.get(0);
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries2 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("50.0")), Point.create(Value.doubleValue(6), TIMESTAMP), null));
    List<io.opencensus.metrics.export.Metric> metrics = StackdriverExportUtils.convertSummaryMetric(SUMMARY_METRIC_NULL_SUM);
    assertThat(metrics).isNotEmpty();
    assertThat(metrics.size()).isEqualTo(2);
    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()).isNotEmpty();
    assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor2);
    assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries2);
}
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 22 with LabelKey

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

the class StackdriverExportUtilsTest method createMetricDescriptor_WithCustomConstantLabels.

@Test
public void createMetricDescriptor_WithCustomConstantLabels() {
    Map<LabelKey, LabelValue> constantLabels = Collections.singletonMap(LabelKey.create("my_key", "desc"), LabelValue.create("value"));
    MetricDescriptor metricDescriptor = StackdriverExportUtils.createMetricDescriptor(METRIC_DESCRIPTOR, PROJECT_ID, "custom.googleapis.com/myorg/", "myorg/", constantLabels);
    assertThat(metricDescriptor.getLabelsList()).containsExactly(LabelDescriptor.newBuilder().setKey(LABEL_KEY.get(0).getKey()).setDescription(LABEL_KEY.get(0).getDescription()).setValueType(ValueType.STRING).build(), LabelDescriptor.newBuilder().setKey("my_key").setDescription("desc").setValueType(ValueType.STRING).build());
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) LabelValue(io.opencensus.metrics.LabelValue) LabelKey(io.opencensus.metrics.LabelKey) Test(org.junit.Test)

Example 23 with LabelKey

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

the class LongGaugeImplTest method setDefaultLabelValues.

@Test
public void setDefaultLabelValues() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    LongGaugeImpl longGauge = new LongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS);
    LongPoint defaultPoint = longGauge.getDefaultTimeSeries();
    defaultPoint.set(-230);
    Metric metric = longGauge.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metric.getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList().get(0).getLabelValues().get(0)).isEqualTo(UNSET_VALUE);
    assertThat(metric.getTimeSeriesList().get(0).getLabelValues().get(1)).isEqualTo(UNSET_VALUE);
}
Also used : LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) Test(org.junit.Test)

Example 24 with LabelKey

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

the class LongGaugeImplTest 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"));
    LongGaugeImpl longGauge = new LongGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS);
    LongPoint defaultPoint1 = longGauge.getDefaultTimeSeries();
    LongPoint defaultPoint2 = longGauge.getDefaultTimeSeries();
    LongPoint longPoint1 = longGauge.getOrCreateTimeSeries(labelValues);
    LongPoint longPoint2 = longGauge.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(longPoint1, longPoint2).testEquals();
    longGauge.clear();
    LongPoint newDefaultPointAfterClear = longGauge.getDefaultTimeSeries();
    LongPoint newLongPointAfterClear = longGauge.getOrCreateTimeSeries(labelValues);
    longGauge.removeTimeSeries(labelValues);
    LongPoint newLongPointAfterRemove = longGauge.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.LongGauge.LongPoint) Test(org.junit.Test)

Example 25 with LabelKey

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

the class StackdriverExportUtils method createMetricDescriptor.

// Convert a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor
static MetricDescriptor createMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor, String projectId, String domain, String displayNamePrefix, Map<LabelKey, LabelValue> constantLabels) {
    MetricDescriptor.Builder builder = MetricDescriptor.newBuilder();
    String type = generateType(metricDescriptor.getName(), domain);
    // Name format refers to
    // cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors/create
    builder.setName("projects/" + projectId + "/metricDescriptors/" + type);
    builder.setType(type);
    builder.setDescription(metricDescriptor.getDescription());
    builder.setDisplayName(createDisplayName(metricDescriptor.getName(), displayNamePrefix));
    for (LabelKey labelKey : metricDescriptor.getLabelKeys()) {
        builder.addLabels(createLabelDescriptor(labelKey));
    }
    for (LabelKey labelKey : constantLabels.keySet()) {
        builder.addLabels(createLabelDescriptor(labelKey));
    }
    builder.setUnit(metricDescriptor.getUnit());
    builder.setMetricKind(createMetricKind(metricDescriptor.getType()));
    builder.setValueType(createValueType(metricDescriptor.getType()));
    return builder.build();
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) LabelKey(io.opencensus.metrics.LabelKey) ByteString(com.google.protobuf.ByteString)

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