Skip to content

Commit

Permalink
add sampleNameFilter to collect metric
Browse files Browse the repository at this point in the history
Signed-off-by: Yaolong Liu <[email protected]>
  • Loading branch information
codings-dan committed Oct 18, 2021
1 parent 14337f7 commit 98293b1
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import io.prometheus.client.Predicate;
import io.prometheus.client.dropwizard.samplebuilder.SampleBuilder;
import io.prometheus.client.dropwizard.samplebuilder.DefaultSampleBuilder;

Expand Down Expand Up @@ -185,6 +186,38 @@ public List<MetricFamilySamples> collect() {
return new ArrayList<MetricFamilySamples>(mfSamplesMap.values());
}

@Override
public List<MetricFamilySamples> collect(Predicate<String> sampleNameFilter) {
Map<String, MetricFamilySamples> mfSamplesMap = new HashMap<String, MetricFamilySamples>();

for (SortedMap.Entry<String, Gauge> entry : registry.getGauges(metricFilter).entrySet()) {
if(sampleNameFilter.test(entry.getKey())) {
addToMap(mfSamplesMap, fromGauge(entry.getKey(), entry.getValue()));
}
}
for (SortedMap.Entry<String, Counter> entry : registry.getCounters(metricFilter).entrySet()) {
if (sampleNameFilter.test(entry.getKey())) {
addToMap(mfSamplesMap, fromCounter(entry.getKey(), entry.getValue()));
}
}
for (SortedMap.Entry<String, Histogram> entry : registry.getHistograms(metricFilter).entrySet()) {
if (sampleNameFilter.test(entry.getKey())) {
addToMap(mfSamplesMap, fromHistogram(entry.getKey(), entry.getValue()));
}
}
for (SortedMap.Entry<String, Timer> entry : registry.getTimers(metricFilter).entrySet()) {
if (sampleNameFilter.test(entry.getKey())) {
addToMap(mfSamplesMap, fromTimer(entry.getKey(), entry.getValue()));
}
}
for (SortedMap.Entry<String, Meter> entry : registry.getMeters(metricFilter).entrySet()) {
if (sampleNameFilter.test(entry.getKey())) {
addToMap(mfSamplesMap, fromMeter(entry.getKey(), entry.getValue()));
}
}
return new ArrayList<MetricFamilySamples>(mfSamplesMap.values());
}

private void addToMap(Map<String, MetricFamilySamples> mfSamplesMap, MetricFamilySamples newMfSamples)
{
if (newMfSamples != null) {
Expand Down

0 comments on commit 98293b1

Please sign in to comment.