@Override public void export(Collection<Metric> metrics) { ArrayList<Metric> registeredMetrics = new ArrayList<>(metrics.size()); for (Metric metric : metrics) { final io.opencensus.metrics.export.MetricDescriptor metricDescriptor = metric.getMetricDescriptor(); if (metricDescriptor.getType() == Type.SUMMARY) { List<Metric> convertedMetrics = StackdriverExportUtils.convertSummaryMetric(metric); registeredMetrics.ensureCapacity(registeredMetrics.size() + convertedMetrics.size()); for (Metric convertedMetric : convertedMetrics) { if (registerMetricDescriptor(convertedMetric.getMetricDescriptor())) { registeredMetrics.add(convertedMetric); } } } else { if (registerMetricDescriptor(metricDescriptor)) { registeredMetrics.add(metric); } } } nextExporter.export(registeredMetrics); } }
@Test public void readAndExport() { Set<MetricProducer> metricProducerSet = new HashSet<>(); metricProducerSet.add(metricProducer); when(metricProducer.getMetrics()).thenReturn(Collections.<Metric>emptyList()); when(metricProducerManager.getAllMetricProducer()).thenReturn(metricProducerSet); MetricReader metricReader = MetricReader.create( Options.builder().setMetricProducerManager(metricProducerManager).build()); metricReader.readAndExport(metricExporter); verify(metricExporter).export(eq(Collections.<Metric>emptyList())); } }
/** * 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(); } }
/** * 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(); } }