-
Notifications
You must be signed in to change notification settings - Fork 828
Expand file tree
/
Copy pathPrometheusProperties.java
More file actions
269 lines (232 loc) · 9.59 KB
/
PrometheusProperties.java
File metadata and controls
269 lines (232 loc) · 9.59 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package io.prometheus.metrics.config;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import javax.annotation.Nullable;
/**
* The Prometheus Java client library can be configured at runtime (e.g. using a properties file).
*
* <p>This class represents the runtime configuration.
*/
public class PrometheusProperties {
private static final PrometheusProperties instance = PrometheusPropertiesLoader.load();
private final MetricsProperties defaultMetricsProperties;
private final MetricPropertiesMap metricProperties;
private final ExemplarsProperties exemplarProperties;
private final ExporterProperties exporterProperties;
private final ExporterFilterProperties exporterFilterProperties;
private final ExporterHttpServerProperties exporterHttpServerProperties;
private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties;
private final ExporterPushgatewayProperties exporterPushgatewayProperties;
private final OpenMetrics2Properties openMetrics2Properties;
/**
* Map that stores metric-specific properties keyed by metric name in exposition format
* (underscores instead of dots).
*
* <p>This wrapper makes it explicit that metric names are normalized to underscore format for
* storage, so that environment variables and properties with dots in metric names can be
* correctly looked up using normalized names.
*/
static class MetricPropertiesMap {
private final Map<String, MetricsProperties> map = new HashMap<>();
void set(Map<String, MetricsProperties> properties) {
map.clear();
properties.forEach(this::put);
}
void put(String metricName, MetricsProperties properties) {
map.put(normalize(metricName), properties);
}
/**
* Get metric properties by metric name.
*
* <p>Accepts metric names in any format (with dots or underscores) and automatically converts
* them to the normalized underscore format used for storage.
*
* @param metricName the metric name (dots will be converted to underscores)
* @return the metric properties, or null if not configured
*/
@Nullable
MetricsProperties get(String metricName) {
return map.get(normalize(metricName));
}
// copied from PrometheusNaming - but we can't reuse that class here because it's in a module
// that
// depends on PrometheusProperties, which would create a circular dependency.
private static String normalize(String name) {
StringBuilder escaped = new StringBuilder();
for (int i = 0; i < name.length(); ) {
int c = name.codePointAt(i);
if (isValidLegacyChar(c, i)) {
escaped.appendCodePoint(c);
} else {
escaped.append('_');
}
i += Character.charCount(c);
}
return escaped.toString();
}
}
private static boolean isValidLegacyChar(int c, int i) {
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_'
|| c == ':'
|| (c >= '0' && c <= '9' && i > 0);
}
/**
* Get the properties instance. When called for the first time, {@code get()} loads the properties
* from the following locations:
*
* <ul>
* <li>{@code prometheus.properties} file found in the classpath.
* <li>Properties file specified in the {@code PROMETHEUS_CONFIG} environment variable or the
* {@code prometheus.config} system property.
* <li>Individual properties from system properties.
* </ul>
*/
public static PrometheusProperties get() throws PrometheusPropertiesException {
return instance;
}
public static Builder builder() {
return new Builder();
}
// Package-private constructor for PrometheusPropertiesLoader and Builder
PrometheusProperties(
MetricsProperties defaultMetricsProperties,
MetricPropertiesMap metricProperties,
ExemplarsProperties exemplarProperties,
ExporterProperties exporterProperties,
ExporterFilterProperties exporterFilterProperties,
ExporterHttpServerProperties httpServerConfig,
ExporterPushgatewayProperties pushgatewayProperties,
ExporterOpenTelemetryProperties otelConfig,
OpenMetrics2Properties openMetrics2Properties) {
this.defaultMetricsProperties = defaultMetricsProperties;
this.metricProperties = metricProperties;
this.exemplarProperties = exemplarProperties;
this.exporterProperties = exporterProperties;
this.exporterFilterProperties = exporterFilterProperties;
this.exporterHttpServerProperties = httpServerConfig;
this.exporterPushgatewayProperties = pushgatewayProperties;
this.exporterOpenTelemetryProperties = otelConfig;
this.openMetrics2Properties = openMetrics2Properties;
}
/**
* The default metric properties apply for metrics where {@link #getMetricProperties(String)} is
* {@code null}.
*/
public MetricsProperties getDefaultMetricProperties() {
return defaultMetricsProperties;
}
/**
* Properties specific for one metric. Should be merged with {@link
* #getDefaultMetricProperties()}. May return {@code null} if no metric-specific properties are
* configured for a metric name.
*
* @param metricName the metric name (dots will be automatically converted to underscores to match
* exposition format)
*/
@Nullable
public MetricsProperties getMetricProperties(String metricName) {
return metricProperties.get(metricName);
}
public ExemplarsProperties getExemplarProperties() {
return exemplarProperties;
}
public ExporterProperties getExporterProperties() {
return exporterProperties;
}
public ExporterFilterProperties getExporterFilterProperties() {
return exporterFilterProperties;
}
public ExporterHttpServerProperties getExporterHttpServerProperties() {
return exporterHttpServerProperties;
}
public ExporterPushgatewayProperties getExporterPushgatewayProperties() {
return exporterPushgatewayProperties;
}
public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
return exporterOpenTelemetryProperties;
}
public OpenMetrics2Properties getOpenMetrics2Properties() {
return openMetrics2Properties;
}
public static class Builder {
private MetricsProperties defaultMetricsProperties = MetricsProperties.builder().build();
private final MetricPropertiesMap metricProperties = new MetricPropertiesMap();
private ExemplarsProperties exemplarProperties = ExemplarsProperties.builder().build();
private ExporterProperties exporterProperties = ExporterProperties.builder().build();
private ExporterFilterProperties exporterFilterProperties =
ExporterFilterProperties.builder().build();
private ExporterHttpServerProperties exporterHttpServerProperties =
ExporterHttpServerProperties.builder().build();
private ExporterPushgatewayProperties pushgatewayProperties =
ExporterPushgatewayProperties.builder().build();
private ExporterOpenTelemetryProperties otelConfig =
ExporterOpenTelemetryProperties.builder().build();
private OpenMetrics2Properties openMetrics2Properties =
OpenMetrics2Properties.builder().build();
private Builder() {}
public Builder defaultMetricsProperties(MetricsProperties defaultMetricsProperties) {
this.defaultMetricsProperties = defaultMetricsProperties;
return this;
}
public Builder metricProperties(Map<String, MetricsProperties> metricProperties) {
this.metricProperties.set(metricProperties);
return this;
}
/** Convenience for adding a single named MetricsProperties */
public Builder putMetricProperty(String name, MetricsProperties props) {
this.metricProperties.put(name, props);
return this;
}
public Builder exemplarProperties(ExemplarsProperties exemplarProperties) {
this.exemplarProperties = exemplarProperties;
return this;
}
public Builder exporterProperties(ExporterProperties exporterProperties) {
this.exporterProperties = exporterProperties;
return this;
}
public Builder exporterFilterProperties(ExporterFilterProperties exporterFilterProperties) {
this.exporterFilterProperties = exporterFilterProperties;
return this;
}
public Builder exporterHttpServerProperties(
ExporterHttpServerProperties exporterHttpServerProperties) {
this.exporterHttpServerProperties = exporterHttpServerProperties;
return this;
}
public Builder pushgatewayProperties(ExporterPushgatewayProperties pushgatewayProperties) {
this.pushgatewayProperties = pushgatewayProperties;
return this;
}
public Builder exporterOpenTelemetryProperties(
ExporterOpenTelemetryProperties exporterOpenTelemetryProperties) {
this.otelConfig = exporterOpenTelemetryProperties;
return this;
}
public Builder enableOpenMetrics2(Consumer<OpenMetrics2Properties.Builder> configurator) {
OpenMetrics2Properties.Builder openMetrics2Builder = OpenMetrics2Properties.builder();
configurator.accept(openMetrics2Builder);
this.openMetrics2Properties = openMetrics2Builder.build();
return this;
}
public Builder openMetrics2Properties(OpenMetrics2Properties openMetrics2Properties) {
this.openMetrics2Properties = openMetrics2Properties;
return this;
}
public PrometheusProperties build() {
return new PrometheusProperties(
defaultMetricsProperties,
metricProperties,
exemplarProperties,
exporterProperties,
exporterFilterProperties,
exporterHttpServerProperties,
pushgatewayProperties,
otelConfig,
openMetrics2Properties);
}
}
}