Search in sources :

Example 61 with Metric

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

the class CreateTimeSeriesExporter method export.

@Override
public void export(Collection<Metric> metrics) {
    List<TimeSeries> timeSeriesList = new ArrayList<>(metrics.size());
    for (Metric metric : metrics) {
        timeSeriesList.addAll(StackdriverExportUtils.createTimeSeriesList(metric, monitoredResource, domain, projectName.getProject(), constantLabels));
    }
    Span span = tracer.getCurrentSpan();
    for (List<TimeSeries> batchedTimeSeries : Lists.partition(timeSeriesList, MAX_BATCH_EXPORT_SIZE)) {
        span.addAnnotation("Export Stackdriver TimeSeries.");
        try {
            CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(projectName.toString()).addAllTimeSeries(batchedTimeSeries).build();
            metricServiceClient.createTimeSeries(request);
            span.addAnnotation("Finish exporting TimeSeries.");
        } catch (ApiException e) {
            logger.log(Level.WARNING, "ApiException thrown when exporting TimeSeries.", e);
            span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Exception thrown when exporting TimeSeries.", e);
            span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when exporting TimeSeries: " + StackdriverExportUtils.exceptionMessage(e)));
        }
    }
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) Metric(io.opencensus.metrics.export.Metric) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Example 62 with Metric

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

the class MetricsProtoUtilsTests method toMetricProto_Distribution.

@Test
public void toMetricProto_Distribution() {
    Metric metric = Metric.create(DESCRIPTOR_1, Collections.singletonList(TIME_SERIES_1));
    io.opencensus.proto.metrics.v1.Metric expected = io.opencensus.proto.metrics.v1.Metric.newBuilder().setMetricDescriptor(io.opencensus.proto.metrics.v1.MetricDescriptor.newBuilder().setName(METRIC_NAME_1).setDescription(METRIC_DESCRIPTION).setUnit(UNIT).setType(io.opencensus.proto.metrics.v1.MetricDescriptor.Type.CUMULATIVE_DISTRIBUTION).addLabelKeys(io.opencensus.proto.metrics.v1.LabelKey.newBuilder().setKey(KEY_1.getKey()).setDescription(KEY_1.getDescription()).build()).build()).setResource(io.opencensus.proto.resource.v1.Resource.newBuilder().setType(RESOURCE.getType()).putAllLabels(RESOURCE.getLabels()).build()).addTimeseries(io.opencensus.proto.metrics.v1.TimeSeries.newBuilder().setStartTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_4)).addLabelValues(io.opencensus.proto.metrics.v1.LabelValue.newBuilder().setHasValue(true).setValue(VALUE_1.getValue()).build()).addPoints(io.opencensus.proto.metrics.v1.Point.newBuilder().setTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_2)).setDistributionValue(DistributionValue.newBuilder().setCount(5).setSum(24.0).setSumOfSquaredDeviation(321.5).setBucketOptions(DistributionValue.BucketOptions.newBuilder().setExplicit(Explicit.newBuilder().addAllBounds(Arrays.<Double>asList(5.0, 10.0, 15.0)).build()).build()).addBuckets(DistributionValue.Bucket.newBuilder().setCount(2).build()).addBuckets(DistributionValue.Bucket.newBuilder().setCount(1).build()).addBuckets(DistributionValue.Bucket.newBuilder().setCount(2).setExemplar(DistributionValue.Exemplar.newBuilder().setTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_1)).setValue(11).build()).build()).addBuckets(DistributionValue.Bucket.newBuilder().setCount(0).build()).build()).build()).build()).build();
    io.opencensus.proto.metrics.v1.Metric actual = MetricsProtoUtils.toMetricProto(metric, RESOURCE);
    assertThat(actual).isEqualTo(expected);
}
Also used : Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 63 with Metric

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

the class MetricsProtoUtilsTests method toMetricProto_Summary.

@Test
public void toMetricProto_Summary() {
    Metric metric = Metric.create(DESCRIPTOR_2, Collections.singletonList(TIME_SERIES_2));
    io.opencensus.proto.metrics.v1.Metric expected = io.opencensus.proto.metrics.v1.Metric.newBuilder().setMetricDescriptor(io.opencensus.proto.metrics.v1.MetricDescriptor.newBuilder().setName(METRIC_NAME_2).setDescription(METRIC_DESCRIPTION).setUnit(UNIT).setType(io.opencensus.proto.metrics.v1.MetricDescriptor.Type.SUMMARY).addLabelKeys(io.opencensus.proto.metrics.v1.LabelKey.newBuilder().setKey(KEY_1.getKey()).setDescription(KEY_1.getDescription()).build()).addLabelKeys(io.opencensus.proto.metrics.v1.LabelKey.newBuilder().setKey(KEY_2.getKey()).setDescription(KEY_2.getDescription()).build()).build()).addTimeseries(io.opencensus.proto.metrics.v1.TimeSeries.newBuilder().setStartTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_5)).addLabelValues(io.opencensus.proto.metrics.v1.LabelValue.newBuilder().setHasValue(true).setValue(VALUE_2.getValue()).build()).addLabelValues(io.opencensus.proto.metrics.v1.LabelValue.newBuilder().setHasValue(false).build()).addPoints(io.opencensus.proto.metrics.v1.Point.newBuilder().setTimestamp(MetricsProtoUtils.toTimestampProto(TIMESTAMP_3)).setSummaryValue(SummaryValue.newBuilder().setCount(Int64Value.of(5)).setSum(DoubleValue.of(45.0)).setSnapshot(SummaryValue.Snapshot.newBuilder().setCount(Int64Value.of(3)).setSum(DoubleValue.of(25.0)).addPercentileValues(SummaryValue.Snapshot.ValueAtPercentile.newBuilder().setValue(11).setPercentile(0.4).build()).build()).build()).build()).build()).build();
    io.opencensus.proto.metrics.v1.Metric actual = MetricsProtoUtils.toMetricProto(metric, null);
    assertThat(actual).isEqualTo(expected);
}
Also used : Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 64 with Metric

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

the class LongGaugeImplTest method getOrCreateTimeSeries_WithNegativePointValues.

@Test
public void getOrCreateTimeSeries_WithNegativePointValues() {
    LongPoint point = longGaugeMetric.getOrCreateTimeSeries(LABEL_VALUES);
    point.add(-100);
    point.add(-33);
    Metric metric = longGaugeMetric.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metric.getTimeSeriesList().get(0).getPoints().size()).isEqualTo(1);
    assertThat(metric.getTimeSeriesList().get(0).getPoints().get(0).getValue()).isEqualTo(Value.longValue(-133));
    assertThat(metric.getTimeSeriesList().get(0).getPoints().get(0).getTimestamp()).isEqualTo(TEST_TIME);
    assertThat(metric.getTimeSeriesList().get(0).getStartTimestamp()).isNull();
}
Also used : Metric(io.opencensus.metrics.export.Metric) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) Test(org.junit.Test)

Example 65 with Metric

use of io.opencensus.metrics.export.Metric 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)

Aggregations

Metric (io.opencensus.metrics.export.Metric)73 Test (org.junit.Test)68 ArrayList (java.util.ArrayList)26 Timestamp (io.opencensus.common.Timestamp)19 TimeSeries (io.opencensus.metrics.export.TimeSeries)16 LabelKey (io.opencensus.metrics.LabelKey)15 LabelValue (io.opencensus.metrics.LabelValue)9 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)9 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)8 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)8 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)6 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)6 DerivedLongGauge (io.opencensus.metrics.DerivedLongGauge)4 DerivedDoubleGauge (io.opencensus.metrics.DerivedDoubleGauge)3 LongGauge (io.opencensus.metrics.LongGauge)3 MetricName (io.dropwizard.metrics5.MetricName)2 DoubleGauge (io.opencensus.metrics.DoubleGauge)2 MetricProducer (io.opencensus.metrics.export.MetricProducer)2 Span (io.opencensus.trace.Span)2 Counter (com.codahale.metrics.Counter)1