Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Percentile filter #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<name>Cassandra Exporter Common</name>

<properties>
<version.picocli>3.6.1</version.picocli>
<version.picocli>3.9.5</version.picocli>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.zegelin.cassandra.exporter;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.zegelin.function.FloatFloatFunction;
import com.zegelin.cassandra.exporter.collector.dynamic.FunctionalMetricFamilyCollector.CollectorFunction;
import com.zegelin.cassandra.exporter.collector.dynamic.FunctionalMetricFamilyCollector.LabeledObjectGroup;
import com.zegelin.prometheus.domain.*;
import com.zegelin.prometheus.domain.Interval.Quantile;

import org.apache.cassandra.metrics.CassandraMetricsRegistry.JmxCounterMBean;
import org.apache.cassandra.metrics.CassandraMetricsRegistry.JmxGaugeMBean;
import org.apache.cassandra.metrics.CassandraMetricsRegistry.JmxMeterMBean;
import org.apache.cassandra.utils.EstimatedHistogram;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class CollectorFunctions {
Expand Down Expand Up @@ -124,7 +133,8 @@ public static CollectorFunction<JmxGaugeMBean> numericGaugeAsCounter() {
/**
* Collect a {@link JmxGaugeMBean} with a Cassandra {@link EstimatedHistogram} value as a Prometheus summary
*/
public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary(final FloatFloatFunction bucketScaleFunction) {
public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary(final FloatFloatFunction bucketScaleFunction,Set<Quantile> excludeQuantiles) {
podile marked this conversation as resolved.
Show resolved Hide resolved
Set<Quantile> includedQuantiles =excludeQuantiles==null?Interval.Quantile.STANDARD_PERCENTILES:Sets.difference(Interval.Quantile.STANDARD_PERCENTILES,excludeQuantiles);
return group -> {
final Stream<SummaryMetricFamily.Summary> summaryStream = group.labeledObjects().entrySet().stream()
.map(e -> new Object() {
Expand All @@ -135,12 +145,11 @@ public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary(final Flo
final long[] bucketData = (long[]) e.gauge.getValue();

if (bucketData.length == 0) {
return new SummaryMetricFamily.Summary(e.labels, Float.NaN, Float.NaN, Interval.asIntervals(Interval.Quantile.STANDARD_PERCENTILES, q -> Float.NaN));
return new SummaryMetricFamily.Summary(e.labels, Float.NaN, Float.NaN, Interval.asIntervals(includedQuantiles, q -> Float.NaN));
}

final EstimatedHistogram histogram = new EstimatedHistogram(bucketData);

final Iterable<Interval> quantiles = Interval.asIntervals(Interval.Quantile.STANDARD_PERCENTILES, q -> bucketScaleFunction.apply((float) histogram.percentile(q.value)));
final Iterable<Interval> quantiles = Interval.asIntervals(includedQuantiles, q -> bucketScaleFunction.apply((float) histogram.percentile(q.value)));

return new SummaryMetricFamily.Summary(e.labels, Float.NaN, histogram.count(), quantiles);
});
Expand All @@ -149,31 +158,41 @@ public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary(final Flo
};
}

public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary() {
return histogramGaugeAsSummary(l -> l);
public static CollectorFunction<JmxGaugeMBean> histogramGaugeAsSummary(Set<Quantile> excludeQuantiles) {
return histogramGaugeAsSummary((l -> l),excludeQuantiles);
podile marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Collect a {@link SamplingCounting} as a Prometheus summary
*/
protected static CollectorFunction<SamplingCounting> samplingAndCountingAsSummary(final FloatFloatFunction quantileScaleFunction) {
protected static CollectorFunction<SamplingCounting> samplingAndCountingAsSummary(final FloatFloatFunction quantileScaleFunction,Set<Quantile> excludeQuantiles) {
podile marked this conversation as resolved.
Show resolved Hide resolved
return group -> {
final Stream<SummaryMetricFamily.Summary> summaryStream = group.labeledObjects().entrySet().stream()
.map(e -> new Object() {
final Labels labels = e.getKey();
final SamplingCounting samplingCounting = e.getValue();
})
.map(e -> {
final Iterable<Interval> quantiles = Iterables.transform(e.samplingCounting.getIntervals(), i -> i.transform(quantileScaleFunction));


Iterator<Interval> itr = e.samplingCounting.getIntervals().iterator();
ArrayList<Interval> filtered = Lists.newArrayList();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new list here isn't necessary. Lets just use Iterables.filter(...).

Copy link
Author

@podile podile Mar 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you suggested, I will apply filter it in TextFormatMetricFormatWriter and revert the above change.

while(itr.hasNext()) {
Interval interval = itr.next();
if(excludeQuantiles.contains(interval.quantile)) {
continue;
}
filtered.add(interval);
}

final Iterable<Interval> quantiles = Iterables.transform(filtered, i -> i.transform(quantileScaleFunction));
return new SummaryMetricFamily.Summary(e.labels, Float.NaN, e.samplingCounting.getCount(), quantiles);
});

return Stream.of(new SummaryMetricFamily(group.name(), group.help(), summaryStream));
};
}

public static CollectorFunction<SamplingCounting> samplingAndCountingAsSummary() {
return samplingAndCountingAsSummary(FloatFloatFunction.identity());
public static CollectorFunction<SamplingCounting> samplingAndCountingAsSummary(Set<Quantile> excludeQuantiles) {
return samplingAndCountingAsSummary(FloatFloatFunction.identity(),excludeQuantiles);
}
}
Loading