-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
MetricsFactory.java
125 lines (111 loc) · 3.81 KB
/
MetricsFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package io.dropwizard.metrics;
import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.annotation.JsonProperty;
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;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A factory for configuring the metrics sub-system for the environment.
* <p/>
* Configures an optional list of {@link com.codahale.metrics.ScheduledReporter reporters} with a
* default {@link #frequency}.
* <p/>
* <b>Configuration Parameters:</b>
* <table>
* <tr>
* <td>Name</td>
* <td>Default</td>
* <td>Description</td>
* </tr>
* <tr>
* <td>frequency</td>
* <td>1 minute</td>
* <td>The frequency to report metrics. Overridable per-reporter.</td>
* </tr>
* <tr>
* <td>reporters</td>
* <td>No reporters.</td>
* <td>A list of {@link ReporterFactory reporters} to report metrics.</td>
* </tr>
* <tr>
* <td>reportOnStop</td>
* <td>{@code false}</td>
* <td>To report metrics one last time when stopping Dropwizard.</td>
* </tr>
* </table>
*/
public class MetricsFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(MetricsFactory.class);
@Valid
@NotNull
private Duration frequency = Duration.minutes(1);
@Valid
@NotNull
private List<ReporterFactory> reporters = Collections.emptyList();
private boolean reportOnStop = false;
@JsonProperty
public List<ReporterFactory> getReporters() {
return reporters;
}
@JsonProperty
public void setReporters(List<ReporterFactory> 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.
* <p />
* 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 + '}';
}
}