private <M extends Metered> MetricFamilySamples fromMeter(List<Map.Entry<MetricName, M>> metersWithSameName, String suffix) { final Map.Entry<MetricName, M> first = metersWithSameName.get(0); final MetricName firstName = first.getKey(); ArrayList<MetricFamilySamples.Sample> sampleList = new ArrayList<MetricFamilySamples.Sample>(metersWithSameName.size()); final String name = firstName.getName() + suffix; for (Map.Entry<MetricName, M> entry : metersWithSameName) { final M metered = entry.getValue(); sampleList.add(new MetricFamilySamples.Sample(name + "_total", entry.getKey().getTagKeys(), entry.getKey().getTagValues(), metered.getCount())); sampleList.add(new MetricFamilySamples.Sample(name + "_m1", entry.getKey().getTagKeys(), entry.getKey().getTagValues(), metered.getOneMinuteRate())); sampleList.add(new MetricFamilySamples.Sample(name + "_m5", entry.getKey().getTagKeys(), entry.getKey().getTagValues(), metered.getFiveMinuteRate())); sampleList.add(new MetricFamilySamples.Sample(name + "_m15", entry.getKey().getTagKeys(), entry.getKey().getTagValues(), metered.getFifteenMinuteRate())); } return new MetricFamilySamples(name, Type.UNTYPED, getHelpMessage(firstName, first.getValue()), sampleList); }
void addSample(MetricFamilySamples.Sample sample, Type type, String help) { MetricFamilySamples mfs = metricFamilySamplesMap.get(sample.name); if (mfs == null) { // JmxScraper.MBeanReceiver is only called from one thread, // so there's no race here. mfs = new MetricFamilySamples(sample.name, type, help, new ArrayList<MetricFamilySamples.Sample>()); metricFamilySamplesMap.put(sample.name, mfs); } mfs.samples.add(sample); }
public List<MetricFamilySamples> describe() { List<MetricFamilySamples> sampleFamilies = new ArrayList<MetricFamilySamples>(); sampleFamilies.add(new MetricFamilySamples("jmx_scrape_duration_seconds", Type.GAUGE, "Time this JMX scrape took, in seconds.", new ArrayList<MetricFamilySamples.Sample>())); sampleFamilies.add(new MetricFamilySamples("jmx_scrape_error", Type.GAUGE, "Non-zero if this scrape failed.", new ArrayList<MetricFamilySamples.Sample>())); return sampleFamilies; }
public List<MetricFamilySamples> describe() { List<MetricFamilySamples> sampleFamilies = new ArrayList<MetricFamilySamples>(); sampleFamilies.add(new MetricFamilySamples("jmx_scrape_duration_seconds", Type.GAUGE, "Time this JMX scrape took, in seconds.", new ArrayList<MetricFamilySamples.Sample>())); sampleFamilies.add(new MetricFamilySamples("jmx_scrape_error", Type.GAUGE, "Non-zero if this scrape failed.", new ArrayList<MetricFamilySamples.Sample>())); return sampleFamilies; }
void addSample(MetricFamilySamples.Sample sample, Type type, String help) { MetricFamilySamples mfs = metricFamilySamplesMap.get(sample.name); if (mfs == null) { // JmxScraper.MBeanReceiver is only called from one thread, // so there's no race here. mfs = new MetricFamilySamples(sample.name, type, help, new ArrayList<MetricFamilySamples.Sample>()); metricFamilySamplesMap.put(sample.name, mfs); } mfs.samples.add(sample); }
@Override public List<MetricFamilySamples> describe() { return Collections.singletonList( new MetricFamilySamples(fullname, Type.HISTOGRAM, help, Collections.<MetricFamilySamples.Sample>emptyList())); }
@Test public void testDescribe() { PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager); assertThat(collector.describe()) .containsExactly( new MetricFamilySamples( METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList())); }
samples.add(new MetricFamilySamples.Sample( "jmx_scrape_duration_seconds", new ArrayList<String>(), new ArrayList<String>(), (System.nanoTime() - start) / 1.0E9)); mfsList.add(new MetricFamilySamples("jmx_scrape_duration_seconds", Type.GAUGE, "Time this JMX scrape took, in seconds.", samples)); mfsList.add(new MetricFamilySamples("jmx_scrape_error", Type.GAUGE, "Non-zero if this scrape failed.", samples)); return mfsList;
/** * Export a histogram snapshot as a prometheus SUMMARY. * * @param dropwizardName metric name. * @param snapshot the histogram snapshot. * @param count the total sample count for this snapshot. * @param factor a factor to apply to histogram values. */ MetricFamilySamples fromSnapshotAndCount(String dropwizardName, Snapshot snapshot, long count, double factor, String helpMessage) { List<MetricFamilySamples.Sample> samples = Arrays.asList( sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.5"), snapshot.getMedian() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.75"), snapshot.get75thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.95"), snapshot.get95thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.98"), snapshot.get98thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.99"), snapshot.get99thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "", Arrays.asList("quantile"), Arrays.asList("0.999"), snapshot.get999thPercentile() * factor), sampleBuilder.createSample(dropwizardName, "_count", new ArrayList<String>(), new ArrayList<String>(), count) ); return new MetricFamilySamples(samples.get(0).name, Type.SUMMARY, helpMessage, samples); }
/** * Export gauge as a prometheus gauge. */ MetricFamilySamples fromGauge(String dropwizardName, Gauge gauge) { Object obj = gauge.getValue(); double value; if (obj instanceof Number) { value = ((Number) obj).doubleValue(); } else if (obj instanceof Boolean) { value = ((Boolean) obj) ? 1 : 0; } else { LOGGER.log(Level.FINE, String.format("Invalid type for Gauge %s: %s", sanitizeMetricName(dropwizardName), obj == null ? "null" : obj.getClass().getName())); return null; } MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "", new ArrayList<String>(), new ArrayList<String>(), value); return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, gauge), Arrays.asList(sample)); }
@Override public List<MetricFamilySamples> describe() { List<MetricFamilySamples> familySamples = new ArrayList<>(); if (globalRegistry == null) { return familySamples; } List<Sample> samples = new ArrayList<>(); for (Registry registry : globalRegistry.getRegistries()) { for (Meter meter : registry) { meter.measure().forEach(measurement -> { Sample sample = convertMeasurementToSample(measurement); samples.add(sample); }); } } familySamples.add(new MetricFamilySamples("ServiceComb_Metrics", Type.UNTYPED, "ServiceComb Metrics", samples)); return familySamples; }
@Override public List<MetricFamilySamples> collect() { ArrayList<MetricFamilySamples> samples = new ArrayList<MetricFamilySamples>(); for (PublicMetrics publicMetrics : this.publicMetrics) { for (Metric<?> metric : publicMetrics.metrics()) { String name = Collector.sanitizeMetricName(metric.getName()); double value = metric.getValue().doubleValue(); MetricFamilySamples metricFamilySamples = new MetricFamilySamples( name, Type.GAUGE, name, Collections.singletonList( new MetricFamilySamples.Sample(name, Collections.<String>emptyList(), Collections.<String>emptyList(), value))); samples.add(metricFamilySamples); } } return samples; }
private MetricFamilySamples buildStatusCounter() { String name = "jetty_responses_total"; return new MetricFamilySamples( name, Type.COUNTER, "Number of requests with response status", Arrays.asList( buildStatusSample(name, "1xx", statisticsHandler.getResponses1xx()), buildStatusSample(name, "2xx", statisticsHandler.getResponses2xx()), buildStatusSample(name, "3xx", statisticsHandler.getResponses3xx()), buildStatusSample(name, "4xx", statisticsHandler.getResponses4xx()), buildStatusSample(name, "5xx", statisticsHandler.getResponses5xx()) ) ); }
private void addToMap(Map<String, MetricFamilySamples> mfSamplesMap, MetricFamilySamples newMfSamples) { if (newMfSamples != null) { MetricFamilySamples currentMfSamples = mfSamplesMap.get(newMfSamples.name); if (currentMfSamples == null) { mfSamplesMap.put(newMfSamples.name, newMfSamples); } else { List<MetricFamilySamples.Sample> samples = new ArrayList<MetricFamilySamples.Sample>(currentMfSamples.samples); samples.addAll(newMfSamples.samples); mfSamplesMap.put(newMfSamples.name, new MetricFamilySamples(newMfSamples.name, currentMfSamples.type, currentMfSamples.help, samples)); } } }
/** * Export a Meter as as prometheus COUNTER. */ MetricFamilySamples fromMeter(String dropwizardName, Meter meter) { final MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "_total", new ArrayList<String>(), new ArrayList<String>(), meter.getCount()); return new MetricFamilySamples(sample.name, Type.COUNTER, getHelpMessage(dropwizardName, meter), Arrays.asList(sample)); }
/** * Export counter as Prometheus <a href="https://prometheus.io/docs/concepts/metric_types/#gauge">Gauge</a>. */ MetricFamilySamples fromCounter(String dropwizardName, Counter counter) { MetricFamilySamples.Sample sample = sampleBuilder.createSample(dropwizardName, "", new ArrayList<String>(), new ArrayList<String>(), new Long(counter.getCount()).doubleValue()); return new MetricFamilySamples(sample.name, Type.GAUGE, getHelpMessage(dropwizardName, counter), Arrays.asList(sample)); }
@Override public List<MetricFamilySamples> describe() { return Collections.singletonList( new MetricFamilySamples(fullname, Type.HISTOGRAM, help, Collections.<MetricFamilySamples.Sample>emptyList())); }
protected List<MetricFamilySamples> familySamplesList(Collector.Type type, List<MetricFamilySamples.Sample> samples) { MetricFamilySamples mfs = new MetricFamilySamples(fullname, type, help, samples); List<MetricFamilySamples> mfsList = new ArrayList<MetricFamilySamples>(1); mfsList.add(mfs); return mfsList; }
private static MetricFamilySamples buildGauge(String name, String help, double value) { return new MetricFamilySamples( name, Type.GAUGE, help, Collections.singletonList(new MetricFamilySamples.Sample(name, EMPTY_LIST, EMPTY_LIST, value))); }
private static MetricFamilySamples buildCounter(String name, String help, double value) { return new MetricFamilySamples( name, Type.COUNTER, help, Collections.singletonList(new MetricFamilySamples.Sample(name, EMPTY_LIST, EMPTY_LIST, value))); }