1 | package com.github.strengthened.prometheus.healthchecks; | |
2 | ||
3 | import io.prometheus.client.Collector; | |
4 | import io.prometheus.client.GaugeMetricFamily; | |
5 | ||
6 | import java.lang.reflect.InvocationTargetException; | |
7 | import java.lang.reflect.Method; | |
8 | import java.util.Collections; | |
9 | import java.util.List; | |
10 | import java.util.Map; | |
11 | import java.util.Map.Entry; | |
12 | import java.util.NoSuchElementException; | |
13 | import java.util.SortedSet; | |
14 | import java.util.TreeSet; | |
15 | import java.util.concurrent.ConcurrentHashMap; | |
16 | import java.util.concurrent.ConcurrentMap; | |
17 | import java.util.concurrent.Executors; | |
18 | import java.util.concurrent.ScheduledExecutorService; | |
19 | import java.util.concurrent.TimeUnit; | |
20 | ||
21 | /** | |
22 | * A Collector for health checks. | |
23 | */ | |
24 | public class HealthChecksCollector extends Collector { | |
25 | final String gaugeMetricHelp; | |
26 | final String gaugeMetricName; | |
27 | final List<String> labelNames; | |
28 | ||
29 | private final ConcurrentMap<String, HealthCheck> healthChecks; | |
30 | private final ScheduledExecutorService asyncExecutorService; | |
31 | private final Object lock; | |
32 | ||
33 | private HealthChecksCollector(String gaugeMetricHelp, String gaugeMetricName, String labelName, | |
34 | ScheduledExecutorService asyncExecutorService) { | |
35 | super(); | |
36 | this.gaugeMetricHelp = gaugeMetricHelp; | |
37 | this.gaugeMetricName = gaugeMetricName; | |
38 | this.asyncExecutorService = asyncExecutorService; | |
39 | this.healthChecks = new ConcurrentHashMap<String, HealthCheck>(); | |
40 | this.labelNames = Collections.singletonList(labelName); | |
41 | this.lock = new Object(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Builds a new instance of {@code HealthChecksCollector} with default values, except for the | |
46 | * custom ScheduledExecutorService. | |
47 | * | |
48 | * @param asyncExecutorService the custom {@code ScheduledExecutorService} | |
49 | * @see Builder#setAsyncExecutorService(ScheduledExecutorService) | |
50 | * @return a new instance of {@code HealthChecksCollector}. | |
51 | */ | |
52 | public static HealthChecksCollector newInstance(ScheduledExecutorService asyncExecutorService) { | |
53 |
1
1. newInstance : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::newInstance to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Builder.of().setAsyncExecutorService(asyncExecutorService).build(); |
54 | } | |
55 | ||
56 | /** | |
57 | * Builds a new instance of {@code HealthChecksCollector} with default values, except for the | |
58 | * custom Pool Size value. | |
59 | * | |
60 | * @param asyncExecutorPoolSize the custom number of threads to keep in the pool, even if they are | |
61 | * idle. | |
62 | * @see Builder#setAsyncExecutorPoolSize(int) | |
63 | * @return a new instance of {@code HealthChecksCollector}. | |
64 | */ | |
65 | public static HealthChecksCollector newInstance(int asyncExecutorPoolSize) { | |
66 |
1
1. newInstance : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::newInstance to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Builder.of().setAsyncExecutorPoolSize(asyncExecutorPoolSize).build(); |
67 | } | |
68 | ||
69 | /** | |
70 | * Builds a new instance of {@code HealthChecksCollector} with default values. | |
71 | * | |
72 | * @see Builder#of() | |
73 | * @return a new instance of {@code HealthChecksCollector}. | |
74 | */ | |
75 | public static HealthChecksCollector newInstance() { | |
76 |
1
1. newInstance : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::newInstance to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Builder.of().build(); |
77 | } | |
78 | ||
79 | @Override | |
80 | public List<MetricFamilySamples> collect() { | |
81 |
1
1. collect : negated conditional → KILLED |
if (healthChecks.isEmpty()) { |
82 |
1
1. collect : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::collect to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Collections.emptyList(); |
83 | } else { | |
84 | GaugeMetricFamily checks = | |
85 | new GaugeMetricFamily(gaugeMetricName, gaugeMetricHelp, labelNames); | |
86 | for (Entry<String, HealthCheck> entry : healthChecks.entrySet()) { | |
87 | final String name = entry.getKey(); | |
88 | final HealthCheck healthCheck = entry.getValue(); | |
89 | final HealthStatus result = healthCheck.execute(); | |
90 | checks.addMetric(Collections.singletonList(name), result.value); | |
91 | } | |
92 |
1
1. collect : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::collect to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Collections.<MetricFamilySamples>singletonList(checks); |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Link an application {@link HealthCheck}. | |
98 | * | |
99 | * @param name the name of the health check | |
100 | * @param healthCheck the {@link HealthCheck} instance | |
101 | * @return a modified instance of this {@code HealthChecksCollector}. | |
102 | * @throws IllegalArgumentException if the {@link HealthCheck} instance is already linked | |
103 | */ | |
104 | public HealthChecksCollector addHealthCheck(String name, HealthCheck healthCheck) { | |
105 | synchronized (lock) { | |
106 |
1
1. addHealthCheck : negated conditional → KILLED |
if (healthChecks.containsKey(requireNonNull(name, "name null"))) { |
107 | throw new IllegalArgumentException("A health check named " + name + " already exists"); | |
108 | } | |
109 | HealthCheck linked = requireNonNull(healthCheck, "healthCheck null"); | |
110 |
1
1. addHealthCheck : negated conditional → KILLED |
if (healthCheck.getClass().isAnnotationPresent(Async.class)) { |
111 | linked = new AsyncHealthCheckDecorator(healthCheck, asyncExecutorService); | |
112 | } | |
113 | healthChecks.put(name, linked); | |
114 | } | |
115 |
1
1. addHealthCheck : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::addHealthCheck to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return this; |
116 | } | |
117 | ||
118 | /** | |
119 | * Link a collection of application {@link HealthCheck}. | |
120 | * | |
121 | * @param healthChecks the {@code name} - {@link HealthCheck} map | |
122 | * @return a modified instance of this {@code HealthChecksCollector}. | |
123 | * @throws IllegalArgumentException if a {@link HealthCheck} instance is already linked | |
124 | */ | |
125 | public <T extends HealthCheck> HealthChecksCollector addHealthChecks( | |
126 | Map<String, T> healthChecks) { | |
127 | for (Entry<String, T> healthCheck : requireNonNull(healthChecks, "healthChecks null") | |
128 | .entrySet()) { | |
129 | addHealthCheck(healthCheck.getKey(), healthCheck.getValue()); | |
130 | } | |
131 |
1
1. addHealthChecks : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::addHealthChecks to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return this; |
132 | } | |
133 | ||
134 | /** | |
135 | * Unlink an application {@link HealthCheck}. | |
136 | * | |
137 | * @param name the name of the health check | |
138 | * @return a modified instance of this {@code HealthChecksCollector}. | |
139 | */ | |
140 | public HealthChecksCollector removeHealthCheck(String name) { | |
141 | synchronized (lock) { | |
142 | final HealthCheck healthCheck = healthChecks.remove(name); | |
143 |
1
1. removeHealthCheck : negated conditional → KILLED |
if (healthCheck instanceof AsyncHealthCheckDecorator) { |
144 | ((AsyncHealthCheckDecorator) healthCheck).tearDown(); | |
145 | } | |
146 | } | |
147 |
1
1. removeHealthCheck : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::removeHealthCheck to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return this; |
148 | } | |
149 | ||
150 | /** | |
151 | * Unlink all the linked {@link HealthCheck}. | |
152 | * | |
153 | * @return a modified instance of this {@code HealthChecksCollector}. | |
154 | */ | |
155 | public HealthChecksCollector clear() { | |
156 | for (String key : healthChecks.keySet()) { | |
157 | removeHealthCheck(key); | |
158 | } | |
159 | shutdown(); | |
160 |
1
1. clear : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::clear to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return this; |
161 | } | |
162 | ||
163 | /** | |
164 | * Returns a set of the names of all linked health checks. | |
165 | * | |
166 | * @return the names of all linked health checks | |
167 | */ | |
168 | public SortedSet<String> getNames() { | |
169 |
1
1. getNames : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::getNames to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Collections.unmodifiableSortedSet(new TreeSet<String>(healthChecks.keySet())); |
170 | } | |
171 | ||
172 | /** | |
173 | * Returns an {@link HealthCheck} of a given name, or {@code null} if this Collector contains no | |
174 | * mapping for the name. | |
175 | * | |
176 | * @param name the name of the health check | |
177 | * @return the {@link HealthCheck} instance, or {@code null} | |
178 | */ | |
179 | public HealthCheck getHealthCheck(String name) { | |
180 |
1
1. getHealthCheck : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::getHealthCheck to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return healthChecks.get(requireNonNull(name, "name null")); |
181 | } | |
182 | ||
183 | /** | |
184 | * Runs the health check with the given name. | |
185 | * | |
186 | * @param name the health check's name | |
187 | * @return the result of the health check | |
188 | * @throws NoSuchElementException if there is no health check with the given name | |
189 | */ | |
190 | public HealthStatus runHealthCheck(String name) { | |
191 | final HealthCheck healthCheck = healthChecks.get(name); | |
192 |
1
1. runHealthCheck : negated conditional → KILLED |
if (healthCheck == null) { |
193 | throw new NoSuchElementException("No health check named " + name + " exists"); | |
194 | } | |
195 |
1
1. runHealthCheck : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::runHealthCheck to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return healthCheck.execute(); |
196 | } | |
197 | ||
198 | /** | |
199 | * Shuts down the scheduled executor for async health checks | |
200 | * | |
201 | * @return a modified instance of this {@code HealthChecksCollector}. | |
202 | */ | |
203 | public HealthChecksCollector shutdown() { | |
204 |
1
1. shutdown : removed call to java/util/concurrent/ScheduledExecutorService::shutdown → KILLED |
asyncExecutorService.shutdown(); |
205 | try { | |
206 |
1
1. shutdown : negated conditional → SURVIVED |
if (!asyncExecutorService.awaitTermination(1, TimeUnit.SECONDS)) { |
207 | asyncExecutorService.shutdownNow(); | |
208 | } | |
209 | } catch (InterruptedException ie) { | |
210 | asyncExecutorService.shutdownNow(); | |
211 |
1
1. shutdown : removed call to java/lang/Thread::interrupt → NO_COVERAGE |
Thread.currentThread().interrupt(); |
212 | } | |
213 |
1
1. shutdown : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::shutdown to ( if (x != null) null else throw new RuntimeException ) → SURVIVED |
return this; |
214 | } | |
215 | ||
216 | private static <T> T requireNonNull(T obj, String string) { | |
217 |
1
1. requireNonNull : negated conditional → KILLED |
if (obj == null) { |
218 | throw new NullPointerException(string); | |
219 | } | |
220 |
1
1. requireNonNull : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector::requireNonNull to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return obj; |
221 | } | |
222 | ||
223 | /** | |
224 | * Builds instances of type {@link HealthChecksCollector HealthChecksCollector}. Initialize | |
225 | * attributes and then invoke the {@link #build()} method to create an immutable instance. | |
226 | * | |
227 | * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or | |
228 | * collection, but instead used immediately to create instances.</em> | |
229 | */ | |
230 | public static final class Builder { | |
231 | private static final int ASYNC_EXECUTOR_POOL_SIZE = 2; | |
232 | private String gaugeMetricHelp = "Health check status results"; | |
233 | private String gaugeMetricName = "health_check_status"; | |
234 | private String gaugeMetricLabelName = "system"; | |
235 | private ScheduledExecutorService asyncExecutorService = | |
236 | createExecutorService(ASYNC_EXECUTOR_POOL_SIZE); | |
237 | ||
238 | /** | |
239 | * Creates a new {@code Builder} instance with default values. | |
240 | * <ul> | |
241 | * <li>Metric Help = {@code Health check status results}</li> | |
242 | * <li>Metric Name = {@code health_check_status}</li> | |
243 | * <li>Metric Label = {@code system}</li> | |
244 | * <li>ExecutorService = {@code ScheduledExecutorService} with thread pool size of | |
245 | * {@code 2}</li> | |
246 | * </ul> | |
247 | * | |
248 | * @return {@code this} builder for use in a chained invocation | |
249 | */ | |
250 | public static Builder of() { | |
251 |
1
1. of : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::of to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new Builder(); |
252 | } | |
253 | ||
254 | /** | |
255 | * Fill a builder with attribute values from the provided {@code HealthChecksCollector} | |
256 | * instance. | |
257 | * | |
258 | * @param instance The instance from which to copy values | |
259 | * @return {@code this} builder for use in a chained invocation | |
260 | */ | |
261 | public Builder from(HealthChecksCollector instance) { | |
262 | requireNonNull(instance, "instance"); | |
263 | setGaugeMetricHelp(instance.gaugeMetricHelp); | |
264 | setGaugeMetricName(instance.gaugeMetricName); | |
265 | setGaugeMetricLabelName(instance.labelNames.get(0)); | |
266 | setAsyncExecutorService(instance.asyncExecutorService); | |
267 |
1
1. from : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::from to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return this; |
268 | } | |
269 | ||
270 | /** | |
271 | * Builds a new {@link HealthChecksCollector HealthChecksCollector}. | |
272 | * | |
273 | * @return An instance of HealthChecksCollector | |
274 | */ | |
275 | public HealthChecksCollector build() { | |
276 |
1
1. build : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::build to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new HealthChecksCollector(gaugeMetricHelp, gaugeMetricName, gaugeMetricLabelName, |
277 | asyncExecutorService); | |
278 | } | |
279 | ||
280 | /** | |
281 | * Initializes the value for the gauge metric's help attribute. | |
282 | * | |
283 | * @param gaugeMetricHelp The value for gaugeMetricHelp | |
284 | * @return {@code this} builder for use in a chained invocation | |
285 | */ | |
286 | public Builder setGaugeMetricHelp(String gaugeMetricHelp) { | |
287 | this.gaugeMetricHelp = requireNonNull(gaugeMetricHelp, "gaugeMetricHelp null"); | |
288 |
1
1. setGaugeMetricHelp : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::setGaugeMetricHelp to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return this; |
289 | } | |
290 | ||
291 | /** | |
292 | * Initializes the value for the gauge metric's name attribute. | |
293 | * | |
294 | * @param gaugeMetricName The value for gaugeMetricName | |
295 | * @return {@code this} builder for use in a chained invocation | |
296 | */ | |
297 | public Builder setGaugeMetricName(String gaugeMetricName) { | |
298 | this.gaugeMetricName = requireNonNull(gaugeMetricName, "gaugeMetricName null"); | |
299 |
1
1. setGaugeMetricName : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::setGaugeMetricName to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return this; |
300 | } | |
301 | ||
302 | /** | |
303 | * Initializes the value for the gauge metric's label attribute. | |
304 | * | |
305 | * @param gaugeMetricLabelName The value for labelName | |
306 | * @return {@code this} builder for use in a chained invocation | |
307 | */ | |
308 | public Builder setGaugeMetricLabelName(String gaugeMetricLabelName) { | |
309 | this.gaugeMetricLabelName = requireNonNull(gaugeMetricLabelName, "gaugeMetricLabelName null"); | |
310 |
1
1. setGaugeMetricLabelName : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::setGaugeMetricLabelName to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return this; |
311 | } | |
312 | ||
313 | /** | |
314 | * Builds and initialize an {@code ScheduledExecutorService} with the specified pool size value. | |
315 | * | |
316 | * @param asyncExecutorPoolSize The value for asyncExecutorPoolSize | |
317 | * @return {@code this} builder for use in a chained invocation | |
318 | */ | |
319 | public Builder setAsyncExecutorPoolSize(int asyncExecutorPoolSize) { | |
320 | this.asyncExecutorService = createExecutorService(asyncExecutorPoolSize); | |
321 |
1
1. setAsyncExecutorPoolSize : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::setAsyncExecutorPoolSize to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return this; |
322 | } | |
323 | ||
324 | /** | |
325 | * Initializes the value for the {@code ScheduledExecutorService}. | |
326 | * | |
327 | * @param asyncExecutorService The value for asyncExecutorService | |
328 | * @return {@code this} builder for use in a chained invocation | |
329 | */ | |
330 | public Builder setAsyncExecutorService(ScheduledExecutorService asyncExecutorService) { | |
331 | this.asyncExecutorService = requireNonNull(asyncExecutorService, "asyncExecutorService null"); | |
332 |
1
1. setAsyncExecutorService : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::setAsyncExecutorService to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return this; |
333 | } | |
334 | ||
335 | private static ScheduledExecutorService createExecutorService(int corePoolSize) { | |
336 | ScheduledExecutorService asyncExecutorService = | |
337 | Executors.newScheduledThreadPool(corePoolSize, Executors.defaultThreadFactory()); | |
338 | try { | |
339 | Method method = | |
340 | asyncExecutorService.getClass().getMethod("setRemoveOnCancelPolicy", Boolean.TYPE); | |
341 | method.invoke(asyncExecutorService, true); | |
342 | } catch (NoSuchMethodException ex) { | |
343 |
1
1. createExecutorService : removed call to com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::logSetExecutorCancellationPolicyFailure → NO_COVERAGE |
logSetExecutorCancellationPolicyFailure(ex); |
344 | } catch (IllegalAccessException ex) { | |
345 |
1
1. createExecutorService : removed call to com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::logSetExecutorCancellationPolicyFailure → NO_COVERAGE |
logSetExecutorCancellationPolicyFailure(ex); |
346 | } catch (InvocationTargetException ex) { | |
347 |
1
1. createExecutorService : removed call to com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::logSetExecutorCancellationPolicyFailure → NO_COVERAGE |
logSetExecutorCancellationPolicyFailure(ex); |
348 | } | |
349 |
1
1. createExecutorService : mutated return of Object value for com/github/strengthened/prometheus/healthchecks/HealthChecksCollector$Builder::createExecutorService to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return asyncExecutorService; |
350 | } | |
351 | ||
352 | private static void logSetExecutorCancellationPolicyFailure(Exception ex) { | |
353 |
1
1. logSetExecutorCancellationPolicyFailure : removed call to java/io/PrintStream::println → NO_COVERAGE |
System.err.println(String.format( |
354 | "Tried but failed to set executor cancellation policy to remove on cancel " | |
355 | + "which has been introduced in Java 7. This could result in a memory leak " | |
356 | + "if many asynchronous health checks are registered and removed because " | |
357 | + "cancellation does not actually remove them from the executor. %s", | |
358 | ex.getMessage())); | |
359 | } | |
360 | } | |
361 | ||
362 | } | |
Mutations | ||
53 |
1.1 |
|
66 |
1.1 |
|
76 |
1.1 |
|
81 |
1.1 |
|
82 |
1.1 |
|
92 |
1.1 |
|
106 |
1.1 |
|
110 |
1.1 |
|
115 |
1.1 |
|
131 |
1.1 |
|
143 |
1.1 |
|
147 |
1.1 |
|
160 |
1.1 |
|
169 |
1.1 |
|
180 |
1.1 |
|
192 |
1.1 |
|
195 |
1.1 |
|
204 |
1.1 |
|
206 |
1.1 |
|
211 |
1.1 |
|
213 |
1.1 |
|
217 |
1.1 |
|
220 |
1.1 |
|
251 |
1.1 |
|
267 |
1.1 |
|
276 |
1.1 |
|
288 |
1.1 |
|
299 |
1.1 |
|
310 |
1.1 |
|
321 |
1.1 |
|
332 |
1.1 |
|
343 |
1.1 |
|
345 |
1.1 |
|
347 |
1.1 |
|
349 |
1.1 |
|
353 |
1.1 |