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

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

There is a newer version: 0.40.13
Show newest version
package io.dropwizard.metrics;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 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.
*/ public class MetricsFactory { private static final Logger LOGGER = LoggerFactory.getLogger(MetricsFactory.class); @Valid @NotNull private Duration frequency = Duration.minutes(1); @Valid @NotNull private ImmutableList reporters = ImmutableList.of(); @JsonProperty public ImmutableList getReporters() { return reporters; } @JsonProperty public void setReporters(ImmutableList reporters) { this.reporters = reporters; } @JsonProperty public Duration getFrequency() { return frequency; } @JsonProperty public void setFrequency(Duration frequency) { this.frequency = frequency; } /** * 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)); environment.manage(manager); } catch (Exception e) { LOGGER.warn("Failed to create reporter, metrics may not be properly reported.", e); } } } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("frequency", frequency) .add("reporters", reporters) .toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy