Search in sources :

Example 1 with MetricProducer

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

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

Aggregations

Metric (io.opencensus.metrics.export.Metric)2 MetricProducer (io.opencensus.metrics.export.MetricProducer)2 Scope (io.opencensus.common.Scope)1 Span (io.opencensus.trace.Span)1 ArrayList (java.util.ArrayList)1