io.dropwizard.metrics.common.BaseReporterFactory Maven / Gradle / Ivy
package io.dropwizard.metrics.common;
import com.codahale.metrics.MetricAttribute;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.ScheduledReporter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.util.Duration;
import io.dropwizard.validation.MinDuration;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.valueextraction.Unwrapping;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* A base {@link ReporterFactory} for configuring metric reporters.
*
* Configures options common to all {@link ScheduledReporter}s.
*
* Configuration Parameters:
*
*
* Name
* Default
* Description
*
*
* durationUnit
* milliseconds
* The unit to report durations as. Overrides per-metric duration units.
*
*
* rateUnit
* seconds
* The unit to report rates as. Overrides per-metric rate units.
*
*
* excludes
* No excluded metrics.
* Metrics to exclude from reports, by name. When defined, matching metrics will not be
* reported. See {@link #getFilter()}.
*
*
* includes
* All metrics included.
* Metrics to include in reports, by name. When defined, only these metrics will be
* reported. See {@link #getFilter()}. Exclusion rules (excludes) take precedence,
* so if a name matches both excludes and includes, it is excluded.
*
*
* excludesAttributes
* No excluded attributes.
* Metric attributes to exclude from reports, by name (e.g `p98`, `m15_rate`, `stddev`).
* When defined, matching metrics attributes will not be reported. See {@link MetricAttribute}
*
*
* includesAttributes
* All metrics attributes.
* Metrics attributes to include in reports, by name (e.g `p98`, `m15_rate`, `stddev`).
* When defined, only these attributes will be reported. See {@link MetricAttribute}.
* Exclusion rules (excludes) take precedence, so if an attribute matches both includesAttributes
* and excludesAttributes, it is excluded.
*
*
* useRegexFilters
* false
* Indicates whether the values of the 'includes' and 'excludes' fields should be
* treated as regular expressions or not.
*
*
* frequency
* none
* The frequency to report metrics. Overrides the {@link
* MetricsFactory#getFrequency() default}.
*
*
*/
public abstract class BaseReporterFactory implements ReporterFactory {
private static final DefaultStringMatchingStrategy DEFAULT_STRING_MATCHING_STRATEGY =
new DefaultStringMatchingStrategy();
private static final RegexStringMatchingStrategy REGEX_STRING_MATCHING_STRATEGY =
new RegexStringMatchingStrategy();
private static final SubstringMatchingStrategy SUBSTRING_MATCHING_STRATEGY =
new SubstringMatchingStrategy();
@NotNull
private TimeUnit durationUnit = TimeUnit.MILLISECONDS;
@NotNull
private TimeUnit rateUnit = TimeUnit.SECONDS;
@NotNull
private Set excludes = Collections.emptySet();
@NotNull
private Set includes = Collections.emptySet();
@Valid
@MinDuration(value = 0, payload = Unwrapping.Unwrap.class)
private Optional frequency = Optional.empty();
private boolean useRegexFilters = false;
private boolean useSubstringMatching = false;
private EnumSet excludesAttributes = EnumSet.noneOf(MetricAttribute.class);
private EnumSet includesAttributes = EnumSet.allOf(MetricAttribute.class);
public TimeUnit getDurationUnit() {
return durationUnit;
}
@JsonProperty
public void setDurationUnit(TimeUnit durationUnit) {
this.durationUnit = durationUnit;
}
@JsonProperty
public TimeUnit getRateUnit() {
return rateUnit;
}
@JsonProperty
public void setRateUnit(final TimeUnit rateUnit) {
this.rateUnit = rateUnit;
}
@JsonProperty
public Set getIncludes() {
return includes;
}
@JsonProperty
public void setIncludes(Set includes) {
this.includes = new HashSet<>(includes);
}
@JsonProperty
public Set getExcludes() {
return excludes;
}
@JsonProperty
public void setExcludes(Set excludes) {
this.excludes = new HashSet<>(excludes);
}
@Override
@JsonProperty
public Optional getFrequency() {
return frequency;
}
@JsonProperty
public void setFrequency(Optional frequency) {
this.frequency = frequency;
}
@JsonProperty
public boolean getUseRegexFilters() {
return useRegexFilters;
}
@JsonProperty
public void setUseRegexFilters(boolean useRegexFilters) {
this.useRegexFilters = useRegexFilters;
}
@JsonProperty
public boolean getUseSubstringMatching() {
return useSubstringMatching;
}
@JsonProperty
public void setUseSubstringMatching(boolean useSubstringMatching) {
this.useSubstringMatching = useSubstringMatching;
}
@JsonProperty
public EnumSet getExcludesAttributes() {
return excludesAttributes;
}
@JsonProperty
public void setExcludesAttributes(EnumSet excludesAttributes) {
this.excludesAttributes = EnumSet.copyOf(excludesAttributes);
}
@JsonProperty
public EnumSet getIncludesAttributes() {
return includesAttributes;
}
@JsonProperty
public void setIncludesAttributes(EnumSet includesAttributes) {
this.includesAttributes = EnumSet.copyOf(includesAttributes);
}
/**
* Gets a {@link MetricFilter} that specifically includes and excludes configured metrics.
*
* Filtering works in 4 ways:
*
* - unfiltered
* - All metrics are reported
* - excludes-only
* - All metrics are reported, except those whose name is listed in excludes.
* - includes-only
* - Only metrics whose name is listed in includes are reported.
* - mixed (both includes and excludes
* - Only metrics whose name is listed in includes and
* not listed in excludes are reported;
* excludes takes precedence over includes.
*
*
* @return the filter for selecting metrics based on the configured excludes/includes.
* @see #getIncludes()
* @see #getExcludes()
*/
@JsonIgnore
public MetricFilter getFilter() {
final StringMatchingStrategy stringMatchingStrategy = getUseRegexFilters() ?
REGEX_STRING_MATCHING_STRATEGY : (getUseSubstringMatching() ? SUBSTRING_MATCHING_STRATEGY : DEFAULT_STRING_MATCHING_STRATEGY);
// Include the metric if its name is not excluded and its name is included
// Where, by default, with no includes setting, all names are included.
return (name, metric) -> !stringMatchingStrategy.containsMatch(getExcludes(), name) &&
(getIncludes().isEmpty() || stringMatchingStrategy.containsMatch(getIncludes(), name));
}
protected Set getDisabledAttributes() {
final EnumSet metricAttributes = EnumSet.complementOf(getIncludesAttributes());
metricAttributes.addAll(getExcludesAttributes());
return metricAttributes;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy