forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetricsTestOperationBuilder.java
137 lines (120 loc) · 4.36 KB
/
MetricsTestOperationBuilder.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.metrics;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.metrics.BoundDoubleCounter;
import io.opentelemetry.api.metrics.BoundDoubleHistogram;
import io.opentelemetry.api.metrics.BoundLongCounter;
import io.opentelemetry.api.metrics.BoundLongHistogram;
import io.opentelemetry.api.metrics.DoubleCounter;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongHistogram;
import io.opentelemetry.api.metrics.Meter;
import java.util.concurrent.ThreadLocalRandom;
/**
* This enum allows for iteration over all of the operations that we want to benchmark. To ensure
* that the enum cannot change state, each enum holds a builder function- passing a meter in will
* return a wrapper for both bound and unbound versions of that operation which can then be used in
* a benchmark.
*/
@SuppressWarnings("ImmutableEnumChecker")
public enum MetricsTestOperationBuilder {
LongCounterAdd(
meter -> {
return new Operation() {
final LongCounter metric = meter.counterBuilder("long_counter").build();
final BoundLongCounter boundMetric =
meter
.counterBuilder("bound_long_counter")
.build()
.bind(Attributes.builder().put("KEY", "VALUE").build());
@Override
public void perform(Attributes labels) {
metric.add(5L, labels);
}
@Override
public void performBound() {
boundMetric.add(5L);
}
};
}),
DoubleCounterAdd(
meter -> {
return new Operation() {
final DoubleCounter metric = meter.counterBuilder("double_counter").ofDoubles().build();
final BoundDoubleCounter boundMetric =
meter
.counterBuilder("bound_double_counter")
.ofDoubles()
.build()
.bind(Attributes.builder().put("KEY", "VALUE").build());
@Override
public void perform(Attributes labels) {
metric.add(5.0d, labels);
}
@Override
public void performBound() {
boundMetric.add(5.0d);
}
};
}),
DoubleHistogramRecord(
meter -> {
return new Operation() {
final DoubleHistogram metric =
meter.histogramBuilder("double_histogram_recorder").build();
final BoundDoubleHistogram boundMetric =
meter
.histogramBuilder("bound_double_histogram_recorder")
.build()
.bind(Attributes.builder().put("KEY", "VALUE").build());
@Override
public void perform(Attributes labels) {
// We record different values to try to hit more areas of the histogram buckets.
metric.record(ThreadLocalRandom.current().nextDouble(0, 20_000d), labels);
}
@Override
public void performBound() {
boundMetric.record(ThreadLocalRandom.current().nextDouble(0, 20_000d));
}
};
}),
LongHistogramRecord(
meter -> {
return new Operation() {
final LongHistogram metric =
meter.histogramBuilder("long_value_recorder").ofLongs().build();
final BoundLongHistogram boundMetric =
meter
.histogramBuilder("bound_long_value_recorder")
.ofLongs()
.build()
.bind(Attributes.builder().put("KEY", "VALUE").build());
@Override
public void perform(Attributes labels) {
metric.record(ThreadLocalRandom.current().nextLong(0, 20_000L), labels);
}
@Override
public void performBound() {
boundMetric.record(ThreadLocalRandom.current().nextLong(0, 20_000L));
}
};
});
private final OperationBuilder builder;
MetricsTestOperationBuilder(final OperationBuilder builder) {
this.builder = builder;
}
public Operation build(Meter meter) {
return this.builder.build(meter);
}
private interface OperationBuilder {
Operation build(Meter meter);
}
interface Operation {
void perform(Attributes labels);
void performBound();
}
}