All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.metrics.common.MetricsFactory Maven / Gradle / Ivy

There is a newer version: 5.0.0-rc.3
Show newest version
package io.dropwizard.metrics.common;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.util.Duration;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * A factory for configuring the metrics sub-system for the environment.
 * 

* Configures an optional list of {@link com.codahale.metrics.ScheduledReporter reporters} with a * default {@link #frequency}. *

* Configuration Parameters: *

* * * * * * * * * * * * * * * * * * * * *
NameDefaultDescription
frequency1 minuteThe frequency to report metrics. Overridable per-reporter.
reportersNo reporters.A list of {@link ReporterFactory reporters} to report metrics.
reportOnStop{@code false}To report metrics one last time when stopping Dropwizard.
*/ public class MetricsFactory { private static final Logger LOGGER = LoggerFactory.getLogger(MetricsFactory.class); @Valid @NotNull private Duration frequency = Duration.minutes(1); @Valid @NotNull private List reporters = Collections.emptyList(); private boolean reportOnStop = false; @JsonProperty public List getReporters() { return reporters; } @JsonProperty public void setReporters(List reporters) { this.reporters = new ArrayList<>(reporters); } @JsonProperty public Duration getFrequency() { return frequency; } @JsonProperty public void setFrequency(Duration frequency) { this.frequency = frequency; } /** * @since 2.0 */ @JsonProperty public boolean isReportOnStop() { return reportOnStop; } /** * @since 2.0 */ @JsonProperty public void setReportOnStop(boolean reportOnStop) { this.reportOnStop = reportOnStop; } /** * Configures the given lifecycle with the {@link com.codahale.metrics.ScheduledReporter * reporters} configured for the given registry. *

* The reporters are tied in to the given lifecycle, such that their {@link #getFrequency() * frequency} for reporting metrics begins when the lifecycle {@link * io.dropwizard.lifecycle.Managed#start() starts}, and stops when the lifecycle * {@link io.dropwizard.lifecycle.Managed#stop() stops}. * * @param environment the lifecycle to manage the reporters. * @param registry the metric registry to report metrics from. */ public void configure(LifecycleEnvironment environment, MetricRegistry registry) { for (ReporterFactory reporter : reporters) { try { final ScheduledReporterManager manager = new ScheduledReporterManager(reporter.build(registry), reporter.getFrequency().orElseGet(this::getFrequency), isReportOnStop()); environment.manage(manager); } catch (Exception e) { LOGGER.warn("Failed to create reporter, metrics may not be properly reported.", e); } } } @Override public String toString() { return "MetricsFactory{frequency=" + frequency + ", reporters=" + reporters + ", reportOnStop=" + reportOnStop + '}'; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy