-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetricsUtil.java
More file actions
217 lines (192 loc) · 6.94 KB
/
MetricsUtil.java
File metadata and controls
217 lines (192 loc) · 6.94 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
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.common.util;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.Timer;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* In GreptimeDB client, metrics are required. As for whether to output (log) metrics
* results, you decide.
*/
public final class MetricsUtil {
private static final Logger LOG = LoggerFactory.getLogger(MetricsUtil.class);
private static final MetricRegistry METRIC_REGISTRY = new MetricRegistry();
private static final ScheduledReporter SCHEDULED_REPORTER;
static {
ScheduledExecutorService scheduledPool = ThreadPoolUtil.newScheduledBuilder()
.enableMetric(true)
.coreThreads(1)
.poolName("metrics.reporter")
.threadFactory(new NamedThreadFactory("metrics.reporter", true))
.build();
SCHEDULED_REPORTER = createReporter(scheduledPool);
}
private static ScheduledReporter createReporter(ScheduledExecutorService scheduledPool) {
try {
return Slf4jReporter.forRegistry(MetricsUtil.METRIC_REGISTRY)
.withLoggingLevel(Slf4jReporter.LoggingLevel.INFO)
.outputTo(LOG)
.scheduleOn(scheduledPool)
.shutdownExecutorOnStop(true)
.build();
} catch (Throwable ex) {
LOG.warn("Fail to create metrics reporter.", ex);
return null;
}
}
public static void startScheduledReporter(long period, TimeUnit unit) {
if (SCHEDULED_REPORTER != null) {
LOG.info("Starting the metrics scheduled reporter.");
SCHEDULED_REPORTER.start(period, unit);
}
}
public static void stopScheduledReporterAndDestroy() {
if (SCHEDULED_REPORTER != null) {
LOG.info("Stopping the metrics scheduled reporter.");
SCHEDULED_REPORTER.stop();
}
}
public static void reportImmediately() {
SCHEDULED_REPORTER.report();
}
/**
* Gets the global registry of metric instances.
*
* @return the global registry of metric instances
*/
public static MetricRegistry metricRegistry() {
return METRIC_REGISTRY;
}
/**
* Gets the {@link Meter} registered under this name; or create
* and register a new {@link Meter} if none is registered.
*
* @param name the name of the metric
* @return the {@link Meter} registered under this name
*/
public static Meter meter(Object name) {
return METRIC_REGISTRY.meter(named(name));
}
/**
* Gets the {@link Meter} registered under this name; or create
* and register a new {@link Meter} if none is registered.
*
* @param names the names of the metric
* @return the {@link Meter} registered under this name
*/
public static Meter meter(Object... names) {
return METRIC_REGISTRY.meter(named(names));
}
/**
* Gets the {@link Timer} registered under this name; or create
* and register a new {@link Timer} if none is registered.
*
* @param name the name of the metric
* @return the {@link Timer} registered under this name
*/
public static Timer timer(Object name) {
return METRIC_REGISTRY.timer(named(name));
}
/**
* Gets the {@link Timer} registered under this name; or create
* and register a new {@link Timer} if none is registered.
*
* @param names the names of the metric
* @return the {@link Timer} registered under this name
*/
public static Timer timer(Object... names) {
return METRIC_REGISTRY.timer(named(names));
}
/**
* Gets the {@link Counter} registered under this name; or create
* and register a new {@link Counter} if none is registered.
*
* @param name the name of the metric
* @return the {@link Counter} registered under this name
*/
public static Counter counter(Object name) {
return METRIC_REGISTRY.counter(named(name));
}
/**
* Gets the {@link Counter} registered under this name; or create
* and register a new {@link Counter} if none is registered.
*
* @param names the names of the metric
* @return the {@link Counter} registered under this name
*/
public static Counter counter(Object... names) {
return METRIC_REGISTRY.counter(named(names));
}
/**
* Gets the {@link Histogram} registered under this name; or create
* and register a new {@link Histogram} if none is registered.
*
* @param name the name of the metric
* @return the {@link Histogram} registered under this name
*/
public static Histogram histogram(Object name) {
return METRIC_REGISTRY.histogram(named(name));
}
/**
* Gets the {@link Histogram} registered under this name; or create
* and register a new {@link Histogram} if none is registered.
*
* @param names the names of the metric
* @return the {@link Histogram} registered under this name
*/
public static Histogram histogram(Object... names) {
return METRIC_REGISTRY.histogram(named(names));
}
public static String named(Object name) {
return String.valueOf(name);
}
public static String namedById(String id, String... names) {
StringBuilder buf = StringBuilderHelper.get();
buf.append(id);
named0(buf, names);
return buf.toString();
}
public static String named(Object... names) {
StringBuilder buf = StringBuilderHelper.get();
named0(buf, names);
return buf.toString();
}
private static void named0(StringBuilder buf, Object... names) {
for (Object name : names) {
if (buf.length() > 0) {
buf.append('_');
}
buf.append(name);
}
}
private static void named0(StringBuilder buf, String... names) {
for (String name : names) {
if (buf.length() > 0) {
buf.append('_');
}
buf.append(name);
}
}
private MetricsUtil() {}
}