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)));
}
}
}
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);
}
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);
}
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();
}
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);
}
Aggregations