forked from micrometer-metrics/micrometer
/
ServiceLevelObjective.java
716 lines (593 loc) · 27.5 KB
/
ServiceLevelObjective.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/**
* Copyright 2020 VMware, Inc.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.micrometer.health;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.MeterBinder;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.ValueAtPercentile;
import io.micrometer.core.instrument.search.Search;
import io.micrometer.core.lang.Nullable;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import static io.micrometer.health.QueryUtils.MAX_OR_NAN;
import static io.micrometer.health.QueryUtils.SUM_OR_NAN;
/**
* Service level objective.
*
* @author Jon Schneider
* @author Johnny Lim
* @since 1.6.0
*/
public abstract class ServiceLevelObjective {
private static final ThreadLocal<DecimalFormat> WHOLE_OR_SHORT_DECIMAL = ThreadLocal.withInitial(() -> {
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
return new DecimalFormat("##0.##",otherSymbols);
});
private final String name;
private final Tags tags;
@Nullable
private final String baseUnit;
/**
* Describes what it means for this service level objective to not be met.
*/
@Nullable
private final String failedMessage;
protected ServiceLevelObjective(String name, Tags tags, @Nullable String baseUnit, @Nullable String failedMessage) {
this.name = name;
this.tags = tags;
this.baseUnit = baseUnit;
this.failedMessage = failedMessage;
}
public String getName() {
return name;
}
public Iterable<Tag> getTags() {
return tags;
}
@Nullable
public String getBaseUnit() {
return baseUnit;
}
public Meter.Id getId() {
return new Meter.Id(name, tags, baseUnit, failedMessage, Meter.Type.GAUGE);
}
@Nullable
public String getFailedMessage() {
return failedMessage;
}
public abstract Collection<MeterBinder> getRequires();
public abstract Collection<MeterFilter> getAcceptFilters();
public abstract void tick(MeterRegistry registry);
public abstract boolean healthy(MeterRegistry registry);
public static MultipleIndicator.Builder compose(String name, ServiceLevelObjective... objectives) {
return new MultipleIndicator.Builder(name, objectives);
}
public static SingleIndicator.Builder build(String name) {
return new SingleIndicator.Builder(name);
}
public static class SingleIndicator extends ServiceLevelObjective {
private final NumericQuery query;
private final Collection<MeterBinder> requires;
private final String testDescription;
private final Predicate<Double> test;
protected SingleIndicator(NumericQuery query, String testDescription, Predicate<Double> test) {
super(query.name, query.tags, query.baseUnit, query.failedMessage);
this.query = query;
this.requires = query.requires;
this.testDescription = testDescription;
this.test = test;
}
@Override
public boolean healthy(MeterRegistry registry) {
Double v = query.getValue(registry);
return v.isNaN() || test.test(v);
}
@Override
public void tick(MeterRegistry registry) {
query.tick(registry);
}
@Override
public Collection<MeterBinder> getRequires() {
return requires;
}
@Override
public Collection<MeterFilter> getAcceptFilters() {
return query.acceptFilters();
}
public double getValue(MeterRegistry registry) {
return query.getValue(registry);
}
public String getValueAsString(MeterRegistry registry) {
double value = getValue(registry);
return Double.isNaN(value) ? "no value available" :
getBaseUnit() != null && getBaseUnit().toLowerCase().contains("percent") ?
WHOLE_OR_SHORT_DECIMAL.get().format(value * 100) + "%" :
WHOLE_OR_SHORT_DECIMAL.get().format(value);
}
public String getTestDescription() {
return testDescription;
}
static SingleIndicator testNumeric(NumericQuery query, String testDescription, Predicate<Double> test) {
return new SingleIndicator(query, testDescription, test);
}
static SingleIndicator testDuration(NumericQuery query, String testDescription, Predicate<Duration> test) {
return new SingleIndicator(query, testDescription, valueNanos -> valueNanos.isNaN() || test.test(Duration.ofNanos(valueNanos.longValue())));
}
public static class Builder {
private final String name;
private Tags tags = Tags.empty();
@Nullable
private String baseUnit;
@Nullable
private String failedMessage;
private final Collection<MeterBinder> requires;
Builder(String name) {
this.name = name;
this.requires = new ArrayList<>();
}
Builder(String name, @Nullable String failedMessage, Collection<MeterBinder> requires) {
this.name = name;
this.failedMessage = failedMessage;
this.requires = requires;
}
public final Builder failedMessage(@Nullable String failedMessage) {
this.failedMessage = failedMessage;
return this;
}
public final Builder requires(MeterBinder... requires) {
this.requires.addAll(Arrays.asList(requires));
return this;
}
public final Builder baseUnit(@Nullable String baseUnit) {
this.baseUnit = baseUnit;
return this;
}
/**
* @param tags Must be an even number of arguments representing key/value pairs of tags.
* @return This builder.
*/
public final Builder tags(String... tags) {
return tags(Tags.of(tags));
}
/**
* @param tags Tags to add to the eventual timer.
* @return The builder with added tags.
*/
public final Builder tags(Iterable<Tag> tags) {
this.tags = this.tags.and(tags);
return this;
}
/**
* @param key The tag key.
* @param value The tag value.
* @return The timer builder with a single added tag.
*/
public final Builder tag(String key, String value) {
this.tags = tags.and(key, value);
return this;
}
public final NumericQuery count(Function<Search, Search> search) {
return new Instant(name, tags, baseUnit, failedMessage, requires, search, s -> s.meters().stream()
.map(m -> {
if (m instanceof Counter) {
return ((Counter) m).count();
} else if (m instanceof Timer) {
return (double) ((Timer) m).count();
} else if (m instanceof FunctionTimer) {
return ((FunctionTimer) m).count();
} else if (m instanceof FunctionCounter) {
return ((FunctionCounter) m).count();
} else if (m instanceof LongTaskTimer) {
return (double) ((LongTaskTimer) m).activeTasks();
}
return Double.NaN;
})
.reduce(Double.NaN, SUM_OR_NAN)
);
}
public NumericQuery errorRatio(Function<Search, Search> searchAll,
Function<Search, Search> searchErrors) {
return count(searchAll.andThen(searchErrors))
.dividedBy(over -> over.count(searchAll));
}
public final NumericQuery total(Function<Search, Search> search) {
return new Instant(name, tags, baseUnit, failedMessage, requires, search, s -> s.meters().stream()
.map(m -> {
if (m instanceof DistributionSummary) {
return ((DistributionSummary) m).totalAmount();
} else if (m instanceof Timer) {
return ((Timer) m).totalTime(TimeUnit.NANOSECONDS);
} else if (m instanceof LongTaskTimer) {
return ((LongTaskTimer) m).duration(TimeUnit.NANOSECONDS);
}
return Double.NaN;
})
.reduce(Double.NaN, SUM_OR_NAN)
);
}
public final NumericQuery maxPercentile(Function<Search, Search> search, double percentile) {
return new Instant(name, tags, baseUnit, failedMessage, requires, search, s -> s.meters().stream()
.map(m -> {
ValueAtPercentile[] valueAtPercentiles = new ValueAtPercentile[0];
if (m instanceof DistributionSummary) {
valueAtPercentiles = ((DistributionSummary) m).takeSnapshot().percentileValues();
} else if (m instanceof Timer) {
valueAtPercentiles = ((Timer) m).takeSnapshot().percentileValues();
} else if (m instanceof LongTaskTimer) {
valueAtPercentiles = ((LongTaskTimer) m).takeSnapshot().percentileValues();
}
return Arrays.stream(valueAtPercentiles)
.filter(vap -> vap.percentile() == percentile)
.map(ValueAtPercentile::value)
.findAny()
.orElse(Double.NaN);
})
.reduce(Double.NaN, MAX_OR_NAN)
);
}
public final NumericQuery max(Function<Search, Search> search) {
return new Instant(name, tags, baseUnit, failedMessage, requires, search, s -> s.meters().stream()
.map(m -> {
if (m instanceof DistributionSummary) {
return ((DistributionSummary) m).max();
} else if (m instanceof Timer) {
return ((Timer) m).max(TimeUnit.NANOSECONDS);
} else if (m instanceof LongTaskTimer) {
return ((LongTaskTimer) m).max(TimeUnit.NANOSECONDS);
}
return Double.NaN;
})
.reduce(Double.NaN, MAX_OR_NAN)
);
}
/**
* @param search The search criteria for a {@link Gauge}.
* @return The value of the first matching gauge time series.
*/
public final NumericQuery value(Function<Search, Search> search) {
return new Instant(name, tags, baseUnit, failedMessage, requires, search, s -> s.meters().stream()
.map(m -> {
if (m instanceof TimeGauge) {
return ((TimeGauge) m).value(TimeUnit.NANOSECONDS);
} else if (m instanceof Gauge) {
return ((Gauge) m).value();
}
return Double.NaN;
})
.filter(n -> !Double.isNaN(n))
.findAny()
.orElse(Double.NaN)
);
}
}
public static abstract class NumericQuery {
protected final String name;
private final Tags tags;
@Nullable
private final String baseUnit;
@Nullable
private final String failedMessage;
private final Collection<MeterBinder> requires;
NumericQuery(String name, Tags tags, @Nullable String baseUnit,
@Nullable String failedMessage, Collection<MeterBinder> requires) {
this.name = name;
this.tags = tags;
this.baseUnit = baseUnit;
this.failedMessage = failedMessage;
this.requires = requires;
}
abstract Double getValue(MeterRegistry registry);
private String thresholdString(double threshold) {
return baseUnit != null && baseUnit.toLowerCase().contains("percent") ?
WHOLE_OR_SHORT_DECIMAL.get().format(threshold * 100) + "%" :
WHOLE_OR_SHORT_DECIMAL.get().format(threshold);
}
public final SingleIndicator isLessThan(double threshold) {
return SingleIndicator.testNumeric(this,
"<" + thresholdString(threshold),
v -> v < threshold);
}
public final SingleIndicator isLessThanOrEqualTo(double threshold) {
return SingleIndicator.testNumeric(this,
"<=" + thresholdString(threshold),
v -> v <= threshold);
}
public final SingleIndicator isGreaterThan(double threshold) {
return SingleIndicator.testNumeric(this,
">" + thresholdString(threshold),
v -> v > threshold);
}
public final SingleIndicator isGreaterThanOrEqualTo(double threshold) {
return SingleIndicator.testNumeric(this,
">=" + thresholdString(threshold),
v -> v >= threshold);
}
public final SingleIndicator isEqualTo(double threshold) {
return SingleIndicator.testNumeric(this,
"==" + thresholdString(threshold),
v -> v == threshold);
}
public final SingleIndicator isLessThan(Duration threshold) {
return SingleIndicator.testDuration(this,
"<" + threshold,
v -> v.compareTo(threshold) < 0);
}
public final SingleIndicator isLessThanOrEqualTo(Duration threshold) {
return SingleIndicator.testDuration(this,
"<=" + threshold,
v -> v.compareTo(threshold) <= 0);
}
public final SingleIndicator isGreaterThan(Duration threshold) {
return SingleIndicator.testDuration(this,
">" + threshold,
v -> v.compareTo(threshold) > 0);
}
public final SingleIndicator isGreaterThanOrEqualTo(Duration threshold) {
return SingleIndicator.testDuration(this,
">=" + threshold,
v -> v.compareTo(threshold) >= 0);
}
public final SingleIndicator isEqualTo(Duration threshold) {
return SingleIndicator.testDuration(this,
"==" + threshold,
v -> v.compareTo(threshold) == 0);
}
public final SingleIndicator test(String thresholdDescription, Predicate<Double> threshold) {
return SingleIndicator.testNumeric(this, thresholdDescription, threshold);
}
public final SingleIndicator testDuration(String thresholdDescription, Predicate<Duration> threshold) {
return SingleIndicator.testDuration(this, thresholdDescription, threshold);
}
public final NumericQuery dividedBy(Function<SingleIndicator.Builder, NumericQuery> over) {
return new ArithmeticOp(this, over.apply(new Builder(name, failedMessage, requires)), (v1, v2) -> v1 / v2);
}
public final NumericQuery multipliedBy(Function<SingleIndicator.Builder, NumericQuery> by) {
return new ArithmeticOp(this, by.apply(new Builder(name, failedMessage, requires)), (v1, v2) -> v1 * v2);
}
public final NumericQuery plus(Function<SingleIndicator.Builder, NumericQuery> with) {
return new ArithmeticOp(this, with.apply(new Builder(name, failedMessage, requires)), Double::sum);
}
public final NumericQuery minus(Function<SingleIndicator.Builder, NumericQuery> with) {
return new ArithmeticOp(this, with.apply(new Builder(name, failedMessage, requires)), (v1, v2) -> v1 - v2);
}
public final NumericQuery combineWith(Function<SingleIndicator.Builder, NumericQuery> with,
BinaryOperator<Double> combiner) {
return new ArithmeticOp(this, with.apply(new Builder(name, failedMessage, requires)), combiner);
}
public final NumericQuery maxOver(Duration interval) {
return new OverInterval(this, interval, vs -> vs.max().orElse(Double.NaN));
}
public final NumericQuery minOver(Duration interval) {
return new OverInterval(this, interval, vs -> vs.min().orElse(Double.NaN));
}
public final NumericQuery sumOver(Duration interval) {
return new OverInterval(this, interval, DoubleStream::sum);
}
public final NumericQuery averageOver(Duration interval) {
return new OverInterval(this, interval, vs -> vs.average().orElse(Double.NaN));
}
abstract Collection<MeterFilter> acceptFilters();
abstract void tick(MeterRegistry registry);
}
static class Instant extends NumericQuery {
private static final CompositeMeterRegistry NOOP_REGISTRY = new CompositeMeterRegistry(Clock.SYSTEM);
private final Function<Search, Search> search;
private final Function<Search, Double> toValue;
Instant(String name, Tags tags, @Nullable String baseUnit,
@Nullable String failedMessage, Collection<MeterBinder> requires,
Function<Search, Search> search, Function<Search, Double> toValue) {
super(name, tags, baseUnit, failedMessage, requires);
this.search = search;
this.toValue = toValue;
}
protected Double getValue(MeterRegistry registry) {
return toValue.apply(search.apply(Search.in(registry)));
}
@Override
public Collection<MeterFilter> acceptFilters() {
return Collections.singleton(search.apply(Search.in(NOOP_REGISTRY)).acceptFilter());
}
@Override
public void tick(MeterRegistry registry) {
// do nothing because the value is always determined from the current instant
}
}
static class ArithmeticOp extends NumericQuery {
private final NumericQuery left;
private final NumericQuery right;
private BinaryOperator<Double> combiner;
ArithmeticOp(NumericQuery left, NumericQuery right, BinaryOperator<Double> combiner) {
super(left.name, left.tags, left.baseUnit, left.failedMessage, left.requires);
this.left = left;
this.right = right;
this.combiner = combiner;
}
@Override
protected Double getValue(MeterRegistry registry) {
return combiner.apply(left.getValue(registry), right.getValue(registry));
}
@Override
public Collection<MeterFilter> acceptFilters() {
List<MeterFilter> filters = new ArrayList<>();
filters.addAll(left.acceptFilters());
filters.addAll(right.acceptFilters());
return filters;
}
@Override
public void tick(MeterRegistry registry) {
left.tick(registry);
right.tick(registry);
}
}
static class OverInterval extends NumericQuery {
private final Deque<Sample> samples = new ConcurrentLinkedDeque<>();
private final NumericQuery numericQuery;
private final Duration interval;
private final Function<DoubleStream, Double> collector;
OverInterval(NumericQuery q, Duration interval, Function<DoubleStream, Double> collector) {
super(q.name, q.tags, q.baseUnit, q.failedMessage, q.requires);
this.numericQuery = q;
this.interval = interval;
this.collector = collector;
}
private static class Sample {
private final long tick;
private final double sample;
private Sample(long tick, double sample) {
this.tick = tick;
this.sample = sample;
}
}
@Override
protected Double getValue(MeterRegistry registry) {
return collector.apply(samples.stream()
.mapToDouble(s -> s.sample)
.filter(n -> !Double.isNaN(n)));
}
@Override
public Collection<MeterFilter> acceptFilters() {
return numericQuery.acceptFilters();
}
@Override
public void tick(MeterRegistry registry) {
long time = registry.config().clock().monotonicTime();
Sample first = samples.peekFirst();
if (first != null && Duration.ofNanos(time - first.tick).compareTo(interval) > 0) {
samples.removeFirst();
}
samples.addLast(new Sample(time, numericQuery.getValue(registry)));
}
}
}
public static class MultipleIndicator extends ServiceLevelObjective {
private final ServiceLevelObjective[] objectives;
private final BinaryOperator<Boolean> combiner;
MultipleIndicator(String name, Tags tags, @Nullable String failedMessage,
ServiceLevelObjective[] objectives,
BinaryOperator<Boolean> combiner) {
super(name, tags, null, failedMessage);
this.objectives = objectives;
this.combiner = combiner;
}
@Override
public boolean healthy(MeterRegistry registry) {
return Arrays.stream(objectives).map(o -> o.healthy(registry)).reduce(combiner).orElse(true);
}
@Override
public Collection<MeterBinder> getRequires() {
return Arrays.stream(objectives)
.flatMap(o -> o.getRequires().stream())
.collect(Collectors.toList());
}
public ServiceLevelObjective[] getObjectives() {
return objectives;
}
@Override
public Collection<MeterFilter> getAcceptFilters() {
return Arrays.stream(objectives)
.flatMap(o -> o.getAcceptFilters().stream())
.collect(Collectors.toList());
}
@Override
public void tick(MeterRegistry registry) {
for (ServiceLevelObjective objective : objectives) {
objective.tick(registry);
}
}
public static class Builder {
private final String name;
private Tags tags = Tags.empty();
private final ServiceLevelObjective[] objectives;
@Nullable
private String failedMessage;
Builder(String name, ServiceLevelObjective[] objectives) {
this.name = name;
this.objectives = objectives;
}
public final Builder failedMessage(@Nullable String failedMessage) {
this.failedMessage = failedMessage;
return this;
}
/**
* @param tags Must be an even number of arguments representing key/value pairs of tags.
* @return This builder.
*/
public Builder tags(String... tags) {
return tags(Tags.of(tags));
}
/**
* @param tags Tags to add to the eventual timer.
* @return The builder with added tags.
*/
public Builder tags(Iterable<Tag> tags) {
this.tags = this.tags.and(tags);
return this;
}
/**
* @param key The tag key.
* @param value The tag value.
* @return The builder with a single added tag.
*/
public Builder tag(String key, String value) {
this.tags = tags.and(key, value);
return this;
}
public final MultipleIndicator and() {
return new MultipleIndicator(name, tags, failedMessage, objectives, (o1, o2) -> o1 && o2);
}
public final MultipleIndicator or() {
return new MultipleIndicator(name, tags, failedMessage, objectives, (o1, o2) -> o1 || o2);
}
public final MultipleIndicator combine(BinaryOperator<Boolean> combiner, ServiceLevelObjective... objectives) {
return new MultipleIndicator(name, tags, failedMessage, objectives, combiner);
}
}
}
static class FilteredServiceLevelObjective extends ServiceLevelObjective {
private final ServiceLevelObjective delegate;
FilteredServiceLevelObjective(Meter.Id id, ServiceLevelObjective delegate) {
super(id.getName(), Tags.of(id.getTags()), id.getBaseUnit(), id.getDescription());
this.delegate = delegate;
}
@Override
public Collection<MeterBinder> getRequires() {
return delegate.getRequires();
}
@Override
public Collection<MeterFilter> getAcceptFilters() {
return delegate.getAcceptFilters();
}
@Override
public void tick(MeterRegistry registry) {
delegate.tick(registry);
}
@Override
public boolean healthy(MeterRegistry registry) {
return delegate.healthy(registry);
}
}
}