/
MetricAppBean.java
228 lines (175 loc) · 7.33 KB
/
MetricAppBean.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
/*
**********************************************************************
* Copyright (c) 2017, 2019 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
package org.eclipse.microprofile.metrics.test;
import java.util.function.Supplier;
import org.eclipse.microprofile.metrics.Counter;
import org.eclipse.microprofile.metrics.Gauge;
import org.eclipse.microprofile.metrics.Histogram;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricID;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.Timer;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Metric;
import org.eclipse.microprofile.metrics.annotation.Timed;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@ApplicationScoped
public class MetricAppBean {
@Inject
@Metric(description = "red-description", displayName = "red-display-name")
private Counter redCount;
@Inject
@Metric(name = "blue")
private Counter blueCount;
public static final String TAGGED_COUNTER = "taggedCounter";
public static final String TAGGED_HISTOGRAM = "taggedHistogram";
public static final String TAGGED_TIMER = "taggedTimer";
public static final String TAGGED_SIMPLETIMER = "taggedSimpleTimer";
public static final String TAGGED_METER = "taggedMeter";
public static final String TAGGED_GAUGE = "taggedGauge";
public static final String TAGGED_CONCURRENTGAUGE = "taggedConcurrentGauge";
@Inject
@Metric(name = "semiColonTaggedCounter", tags = {"scTag=semi;colons;are;bad"})
private Counter semiColonTaggedCounter;
@Inject
@Metric(name = TAGGED_COUNTER)
private Counter counterNoTag;
@Inject
@Metric(name = TAGGED_COUNTER, tags = {"number=one"})
private Counter counterNumberOneTag;
@Inject
@Metric(name = TAGGED_COUNTER, tags = {"number=two"})
private Counter counterNumberTwoTag;
@Inject
@Metric(name = TAGGED_HISTOGRAM, absolute = true, unit = "marshmellow")
private Histogram histogramNoTag;
@Inject
@Metric(name = TAGGED_HISTOGRAM, absolute = true, unit = "marshmellow", tags = {"number=one"})
private Histogram histogramOneTag;
@Inject
@Metric(name = TAGGED_HISTOGRAM, absolute = true, unit = "marshmellow", tags = {"number=two"})
private Histogram histogramTwoTag;
@Inject
@Metric(name = TAGGED_TIMER, absolute = true)
private Timer timerNoTag;
@Inject
@Metric(name = TAGGED_TIMER, absolute = true, tags = {"number=one"})
private Timer timerOneTag;
@Inject
@Metric(name = TAGGED_TIMER, absolute = true, tags = {"number=two"})
private Timer timerTwoTag;
@org.eclipse.microprofile.metrics.annotation.Gauge(name = TAGGED_GAUGE, absolute = true, unit = MetricUnits.NONE)
public long gaugeMeTagged() {
return 1000L;
}
@org.eclipse.microprofile.metrics.annotation.Gauge(name = TAGGED_GAUGE, absolute = true, unit = MetricUnits.NONE, tags = {
"number=one"})
public long gaugeMeTaggedOne() {
return 1000L;
}
@org.eclipse.microprofile.metrics.annotation.Gauge(name = TAGGED_GAUGE, absolute = true, unit = MetricUnits.NONE, tags = {
"number=two"})
public long gaugeMeTaggedTwo() {
return 1000L;
}
@Inject
@Metric(absolute = true)
private Counter greenCount;
@Inject
@Metric(name = "purple", absolute = true, tags = "app=myShop")
private Counter purpleCount;
@Inject
@Metric(absolute = true, unit = "jellybeans", description = "jellybeans-description", displayName = "jellybeans-displayName")
private Histogram jellybeanHistogram;
@Inject
private MetricRegistry metrics;
public void countMe() {
Counter counter = metrics.counter("metricTest.test1.count");
counter.inc();
}
@Counted(name = "metricTest.test1.countMeA", absolute = true, description = "count-me-a-description", displayName = "count-me-a-display-name")
public void countMeA() {
}
@Counted(name = "metricTest.test1.countMeB", absolute = true, unit = "jellybean")
public long countMeB() {
return 666666;
}
public void gaugeMe() {
@SuppressWarnings("unchecked")
Gauge<Long> gauge = (Gauge<Long>) metrics.getGauge(new MetricID("metricTest.test1.gauge"));
if (gauge == null) {
Supplier<Long> gaugeSupp = () -> {
return 19L;
};
Metadata metadata = Metadata.builder().withName("metricTest.test1.gauge")
.withType(MetricType.GAUGE).withUnit(MetricUnits.GIGABYTES).build();
metrics.gauge(metadata, gaugeSupp);
}
}
@org.eclipse.microprofile.metrics.annotation.Gauge(unit = MetricUnits.KIBIBITS, description = "gauge-me-a-description", displayName = "gauge-me-a-displayname")
public long gaugeMeA() {
return 1000L;
}
@org.eclipse.microprofile.metrics.annotation.Gauge(unit = "hands")
public long gaugeMeB() {
return 7777777;
}
public void histogramMe() {
Metadata metadata = Metadata.builder().withName("metricTest.test1.histogram")
.withType(MetricType.HISTOGRAM).withUnit(MetricUnits.BYTES).build();
Histogram histogram = metrics.histogram(metadata);
// Go both ways to minimize error due to decay
for (int i = 0; i < 500; i++) {
histogram.update(i);
histogram.update(999 - i);
}
Metadata metadata2 = Metadata.builder().withName("metricTest.test1.histogram2")
.withType(MetricType.HISTOGRAM).withUnit(MetricUnits.NONE).build();
Histogram histogram2 = metrics.histogram(metadata2);
histogram2.update(1);
}
public void timeMe() {
Timer timer = metrics.timer("metricTest.test1.timer");
Timer.Context context = timer.time();
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
} finally {
context.stop();
}
}
@Timed
public void timeMeA() {
}
/**
* We create a few metrics with names that are outside the characters that OpenMetrics allows which is [a-zA-Z0-9_]
*/
public void createPromMetrics() {
metrics.counter("pm_counter-with-dashes");
metrics.counter("pm_counter#hash_x'y_");
metrics.counter("pm_counter-umlaut-äöü");
metrics.counter("pm_counter+accent_ê_");
}
}