Skip to content

Commit

Permalink
feat: enable using custom logic scheduling for graphite
Browse files Browse the repository at this point in the history
  • Loading branch information
github-louis-fruleux authored and joschi committed Oct 3, 2024
1 parent c281f60 commit 6714710
Showing 1 changed file with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
package com.codahale.metrics.graphite;

import com.codahale.metrics.Clock;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metered;
import com.codahale.metrics.MetricAttribute;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import com.codahale.metrics.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -75,6 +64,8 @@ public static class Builder {
private boolean addMetricAttributesAsTags;
private DoubleFunction<String> floatingPointFormatter;

private GetScheduledFuture<?> getScheduledFuture;

private Builder(MetricRegistry registry) {
this.registry = registry;
this.clock = Clock.defaultClock();
Expand All @@ -87,6 +78,7 @@ private Builder(MetricRegistry registry) {
this.disabledMetricAttributes = Collections.emptySet();
this.addMetricAttributesAsTags = false;
this.floatingPointFormatter = DEFAULT_FP_FORMATTER;
this.getScheduledFuture = null;
}

/**
Expand Down Expand Up @@ -211,6 +203,18 @@ public Builder withFloatingPointFormatter(DoubleFunction<String> floatingPointFo
return this;
}

/**
* Use custom schedule logic.
* By default, logic is scheduledWithFixedDelay
*
* @param getScheduledFuture a method to schedule next update
* @return {@code this}
*/
public Builder withScheduledFuture(GetScheduledFuture<?> getScheduledFuture) {
this.getScheduledFuture = getScheduledFuture;
return this;
}

/**
* Builds a {@link GraphiteReporter} with the given properties, sending metrics using the
* given {@link GraphiteSender}.
Expand Down Expand Up @@ -243,7 +247,8 @@ public GraphiteReporter build(GraphiteSender graphite) {
shutdownExecutorOnStop,
disabledMetricAttributes,
addMetricAttributesAsTags,
floatingPointFormatter);
floatingPointFormatter,
getScheduledFuture);
}
}

Expand Down Expand Up @@ -318,7 +323,7 @@ protected GraphiteReporter(MetricRegistry registry,
Set<MetricAttribute> disabledMetricAttributes,
boolean addMetricAttributesAsTags) {
this(registry, graphite, clock, prefix, rateUnit, durationUnit, filter, executor, shutdownExecutorOnStop,
disabledMetricAttributes, addMetricAttributesAsTags, DEFAULT_FP_FORMATTER);
disabledMetricAttributes, addMetricAttributesAsTags, DEFAULT_FP_FORMATTER, null);
}

/**
Expand All @@ -338,6 +343,7 @@ protected GraphiteReporter(MetricRegistry registry,
* @param disabledMetricAttributes do not report specific metric attributes
* @param addMetricAttributesAsTags if true, then add metric attributes as tags instead of suffixes
* @param floatingPointFormatter custom floating point formatter
* @param getScheduledFuture custom scheduling logic (may be null).
*/
protected GraphiteReporter(MetricRegistry registry,
GraphiteSender graphite,
Expand All @@ -350,9 +356,11 @@ protected GraphiteReporter(MetricRegistry registry,
boolean shutdownExecutorOnStop,
Set<MetricAttribute> disabledMetricAttributes,
boolean addMetricAttributesAsTags,
DoubleFunction<String> floatingPointFormatter) {
DoubleFunction<String> floatingPointFormatter,
GetScheduledFuture<?> getScheduledFuture
) {
super(registry, "graphite-reporter", filter, rateUnit, durationUnit, executor, shutdownExecutorOnStop,
disabledMetricAttributes, null);
disabledMetricAttributes, getScheduledFuture);
this.graphite = graphite;
this.clock = clock;
this.prefix = prefix;
Expand Down

0 comments on commit 6714710

Please sign in to comment.