Search in sources :

Example 1 with Metric

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

the class MetricReader method readAndExport.

/**
 * Reads the metrics from the {@link MetricProducerManager} and exports them to the {@code
 * metricExporter}.
 *
 * @param metricExporter the exporter called to export the metrics read.
 * @since 0.19
 */
public void readAndExport(MetricExporter metricExporter) {
    Span span = tracer.spanBuilder(spanName).setRecordEvents(true).setSampler(probabilitySampler).startSpan();
    Scope scope = tracer.withSpan(span);
    try {
        ArrayList<Metric> metricsList = new ArrayList<>();
        for (MetricProducer metricProducer : metricProducerManager.getAllMetricProducer()) {
            metricsList.addAll(metricProducer.getMetrics());
        }
        metricExporter.export(metricsList);
    } catch (Throwable e) {
        logger.log(Level.WARNING, "Exception thrown by the metrics exporter.", e);
        span.setStatus(Status.UNKNOWN.withDescription("Exception when export metrics: " + exceptionMessage(e)));
    } finally {
        scope.close();
        span.end();
    }
}
Also used : Scope(io.opencensus.common.Scope) ArrayList(java.util.ArrayList) MetricProducer(io.opencensus.metrics.export.MetricProducer) Metric(io.opencensus.metrics.export.Metric) Span(io.opencensus.trace.Span)

Example 2 with Metric

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

the class OcAgentMetricsExporterWorker method export.

// Polls MetricProducerManager from Metrics library for all registered MetricDescriptors,
// converts them to proto, then exports them to OC-Agent.
private void export() {
    if (exportRpcHandler == null || exportRpcHandler.isCompleted()) {
        return;
    }
    ArrayList<Metric> metricsList = Lists.newArrayList();
    for (MetricProducer metricProducer : metricProducerManager.getAllMetricProducer()) {
        metricsList.addAll(metricProducer.getMetrics());
    }
    List<io.opencensus.proto.metrics.v1.Metric> metricProtos = Lists.newArrayList();
    for (Metric metric : metricsList) {
        // TODO(songya): determine if we should make the optimization on not sending already-existed
        // MetricDescriptors.
        // boolean registered = true;
        // if (!registeredDescriptors.contains(metric.getMetricDescriptor())) {
        // registered = false;
        // registeredDescriptors.add(metric.getMetricDescriptor());
        // }
        metricProtos.add(MetricsProtoUtils.toMetricProto(metric, null));
    }
    exportRpcHandler.onExport(// mutate after the initial message.
    ExportMetricsServiceRequest.newBuilder().addAllMetrics(metricProtos).build());
}
Also used : MetricProducer(io.opencensus.metrics.export.MetricProducer) Metric(io.opencensus.metrics.export.Metric)

Example 3 with Metric

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

the class LongGaugeImplTest method clear.

@Test
public void clear() {
    LongPoint longPoint = longGaugeMetric.getOrCreateTimeSeries(LABEL_VALUES);
    longPoint.add(-11);
    LongPoint defaultPoint = longGaugeMetric.getDefaultTimeSeries();
    defaultPoint.set(100);
    Metric metric = longGaugeMetric.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    longGaugeMetric.clear();
    assertThat(longGaugeMetric.getMetric(testClock)).isNull();
}
Also used : Metric(io.opencensus.metrics.export.Metric) LongPoint(io.opencensus.metrics.LongGauge.LongPoint) Test(org.junit.Test)

Example 4 with Metric

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

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

the class LongGaugeImplTest method getOrCreateTimeSeries.

@Test
public void getOrCreateTimeSeries() {
    LongPoint point = longGaugeMetric.getOrCreateTimeSeries(LABEL_VALUES);
    point.add(100);
    LongPoint point1 = longGaugeMetric.getOrCreateTimeSeries(LABEL_VALUES);
    point1.set(500);
    Metric metric = longGaugeMetric.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric).isEqualTo(Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(LABEL_VALUES, Point.create(Value.longValue(500), TEST_TIME), null)));
    assertThat(point).isSameInstanceAs(point1);
}
Also used : 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