Skip to content

Commit 4561220

Browse files
committed
use self-monitoring prefix for views
1 parent b544861 commit 4561220

4 files changed

Lines changed: 94 additions & 45 deletions

File tree

src/main/java/rocks/inspectit/ocelot/eum/server/configuration/model/selfmonitoring/SelfMonitoringSettings.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import jakarta.validation.Valid;
99
import jakarta.validation.constraints.NotBlank;
1010
import jakarta.validation.constraints.NotNull;
11+
import rocks.inspectit.ocelot.eum.server.configuration.model.metrics.definition.view.ViewDefinitionSettings;
12+
1113
import java.util.Collections;
14+
import java.util.LinkedHashMap;
1215
import java.util.Map;
1316

1417
/**
@@ -33,4 +36,31 @@ public class SelfMonitoringSettings {
3336
* The prefix used for the self-monitoring metrics.
3437
*/
3538
private String metricPrefix;
39+
40+
/**
41+
* Marker to tell if the {@link #metricPrefix} has been applied to views
42+
*/
43+
private boolean prefixNotApplied = true;
44+
45+
/**
46+
* Adds the {@link #metricPrefix} for all view names. The prefix to the metrics has to be added manually.
47+
*
48+
* @return the metrics with prefixed views
49+
*/
50+
public Map<String, MetricDefinitionSettings> getMetricsWithPrefixedViews() {
51+
if (prefixNotApplied) {
52+
metrics.forEach((metricName, metricDefinition) -> {
53+
54+
Map<String, ViewDefinitionSettings> prefixedViews = new LinkedHashMap<>();
55+
metricDefinition.getViews()
56+
.forEach((viewName, viewDefinition) ->
57+
prefixedViews.put(metricPrefix + viewName, viewDefinition)
58+
);
59+
metricDefinition.setViews(prefixedViews);
60+
61+
});
62+
prefixNotApplied = false;
63+
}
64+
return metrics;
65+
}
3666
}

src/main/java/rocks/inspectit/ocelot/eum/server/metrics/SelfMonitoringMetricManager.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,34 @@ public class SelfMonitoringMetricManager {
2929
*/
3030
public void initMetrics() {
3131
SelfMonitoringSettings selfMonitoringSettings = configuration.getSelfMonitoring();
32-
for (Map.Entry<String, MetricDefinitionSettings> metricEntry : selfMonitoringSettings.getMetrics().entrySet()) {
33-
String measureName = metricEntry.getKey();
32+
for (Map.Entry<String, MetricDefinitionSettings> metricEntry : selfMonitoringSettings.getMetricsWithPrefixedViews().entrySet()) {
33+
String metricName = metricEntry.getKey();
3434
MetricDefinitionSettings metricDefinitionSettings = metricEntry.getValue();
3535

36-
String metricName = selfMonitoringSettings.getMetricPrefix() + measureName;
36+
String fullMetricName = selfMonitoringSettings.getMetricPrefix() + metricName;
3737
log.info("Registering self-monitoring metric: {}", metricName);
3838

39-
instrumentManager.createInstrument(metricName, metricDefinitionSettings);
39+
instrumentManager.createInstrument(fullMetricName, metricDefinitionSettings);
4040
}
4141
}
4242

4343
/**
44-
* Records a self-monitoring measurement with the common attributes.
45-
* Only records a measurement if self monitoring is enabled.
44+
* Records a self-monitoring metric with the common attributes.
45+
* Only records a metric if self monitoring is enabled.
4646
*
47-
* @param measureName the name of the measure, excluding the metrics prefix
48-
* @param value the actual value
49-
* @param customAttributes custom attributes
47+
* @param metricName the name of the metric, excluding the metrics prefix
48+
* @param value the value to record
49+
* @param customAttributes the custom attributes for the metric
5050
*/
51-
public void record(String measureName, Number value, Map<String, String> customAttributes) {
51+
public void record(String metricName, Number value, Map<String, String> customAttributes) {
5252
SelfMonitoringSettings selfMonitoringSettings = configuration.getSelfMonitoring();
53-
if (selfMonitoringSettings.isEnabled() && selfMonitoringSettings.getMetrics().containsKey(measureName)) {
54-
MetricDefinitionSettings metricDefinitionSettings = selfMonitoringSettings.getMetrics().get(measureName);
53+
if (selfMonitoringSettings.isEnabled() && selfMonitoringSettings.getMetrics().containsKey(metricName)) {
54+
MetricDefinitionSettings metricDefinition = selfMonitoringSettings.getMetricsWithPrefixedViews().get(metricName);
5555

56-
String metricName = selfMonitoringSettings.getMetricPrefix() + measureName;
56+
String fullMetricName = selfMonitoringSettings.getMetricPrefix() + metricName;
5757

5858
try (Scope scope = instrumentManager.getBaggage(customAttributes).makeCurrent()) {
59-
instrumentManager.recordMetric(metricName, metricDefinitionSettings, value);
59+
instrumentManager.recordMetric(fullMetricName, metricDefinition, value);
6060
}
6161
}
6262
}

src/main/resources/application.yml

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# EUM-SERVER PROPERTIES
12
inspectit-eum-server:
23

34
definitions:
@@ -150,7 +151,7 @@ inspectit-eum-server:
150151

151152
metrics:
152153
prometheus:
153-
# Determines whether the prometheus exporter is enabled.
154+
# determines whether the prometheus exporter is enabled.
154155
enabled: DISABLED
155156

156157
# The host of the prometheus HTTP endpoint.
@@ -159,7 +160,7 @@ inspectit-eum-server:
159160
# The port of the prometheus HTTP endpoint.
160161
port: 8888
161162

162-
# settings for the OtlpGrpcMetricExporter used in OtlpGrpcMetricExporterService
163+
# settings for the OTLP metrics exporter
163164
otlp:
164165
# Determines whether the otlp metrics exporter is enabled.
165166
enabled: IF_CONFIGURED
@@ -179,9 +180,10 @@ inspectit-eum-server:
179180
timeout: 10s
180181

181182
tracing:
182-
# Specifies whether client IP addresses which are added to spans should be masked.
183+
# specifies whether client IP addresses which are added to spans should be masked.
183184
mask-span-ip-addresses: true
184185

186+
# settings for the OTLP trace exporter
185187
otlp:
186188
# If OTLP exporter for the OT received spans is enabled.
187189
enabled: IF_CONFIGURED
@@ -197,26 +199,27 @@ inspectit-eum-server:
197199
timeout: 10s
198200

199201
beacons:
202+
200203
http:
201-
# Whether beacons should be exported via HTTP.
204+
# whether beacons should be exported via HTTP
202205
enabled: DISABLED
203206

204-
# The endpoint to which the beacons are to be sent.
207+
# the endpoint to which the beacons are to be sent
205208
endpoint-url: http://localhost:8080
206209

207-
# The max. amount of threads exporting beacons (min. 1).
210+
# the max. amount of threads exporting beacons (min. 1)
208211
worker-threads: 2
209212

210-
# The maximum number of beacons to be exported using a single HTTP request (min. 1).
213+
# the maximum number of beacons to be exported using a single HTTP request (min. 1)
211214
max-batch-size: 100
212215

213-
# The flush interval to export beacons in case the 'max-batch-size' has not been reached (min. 1 second).
216+
# the flush interval to export beacons in case the 'max-batch-size' has not been reached (min. 1 second)
214217
flush-interval: 5s
215218

216-
# When specified, the request will be using this username for Basic authentication.
219+
# when specified, the request will be using this username for Basic authentication
217220
# username:
218221

219-
# The password used for Basic authentication.
222+
# the password used for Basic authentication
220223
# password:
221224

222225
# settings for the EUM server's self-monitoring
@@ -225,7 +228,7 @@ inspectit-eum-server:
225228
# whether self-monitoring is enabled
226229
enabled: true
227230

228-
# the prefix used for the self-monitoring metrics
231+
# the prefix used for the self-monitoring metrics and views
229232
metricPrefix: "inspectit_eum_self_"
230233

231234
# definitions of the self-monitoring metrics
@@ -236,7 +239,7 @@ inspectit-eum-server:
236239
value-type: LONG
237240
unit: amount
238241
views:
239-
inspectit_eum_self_beacons_received:
242+
beacons_received:
240243
aggregation: SUM
241244
attributes:
242245
is_error: true
@@ -246,7 +249,7 @@ inspectit-eum-server:
246249
value-type: LONG
247250
unit: amount
248251
views:
249-
inspectit_eum_self_beacons_export:
252+
beacons_export:
250253
aggregation: HISTOGRAM
251254
attributes:
252255
exporter: true
@@ -258,7 +261,7 @@ inspectit-eum-server:
258261
unit: amount
259262
views:
260263
# the amount of elements in a single export execution
261-
inspectit_eum_self_beacons_export_batch:
264+
beacons_export_batch:
262265
aggregation: SUM
263266
attributes:
264267
exporter: true
@@ -269,7 +272,7 @@ inspectit-eum-server:
269272
value-type: LONG
270273
unit: amount
271274
views:
272-
inspectit_eum_self_beacons_processor:
275+
beacons_processor:
273276
aggregation: HISTOGRAM
274277
attributes:
275278
beacon_processor: true
@@ -280,7 +283,7 @@ inspectit-eum-server:
280283
value-type: LONG
281284
unit: amount
282285
views:
283-
inspectit_eum_self_traces_received:
286+
traces_received:
284287
aggregation: HISTOGRAM
285288
attributes:
286289
is_error: true
@@ -290,7 +293,7 @@ inspectit-eum-server:
290293
value-type: LONG
291294
unit: amount
292295
views:
293-
inspectit_eum_self_traces_span_size:
296+
traces_span_size:
294297
aggregation: SUM
295298
attributes:
296299
is_error: true
@@ -306,21 +309,22 @@ inspectit-eum-server:
306309
- "/boomerang/**"
307310
auth-provider:
308311
simple:
309-
# Enable/Disable Provider
312+
# enable/disable provider
310313
enabled: false
311-
# Flag indicates if the directory should be watched for changes and tokens reloaded
314+
# flag indicates if the directory should be watched for changes and tokens reloaded
312315
watch: true
313-
# How often directory should be watched for changes
316+
# how often directory should be watched for changes
314317
frequency: 60s
315-
# The directory where token files are stored. Empty by default to force users to provide one
318+
# the directory where token files are stored. Empty by default to force users to provide one
316319
token-directory: ""
317-
# The name of the initial token file. If a name is provided file will be created with an initial token
320+
# the name of the initial token file. If a name is provided file will be created with an initial token
318321
default-file-name: "default-token-file.yaml"
319322

320323

321-
# SERVER properties
322-
# Avoid exposing internals to outside via error page
324+
# SERVER PROPERTIES
323325
server:
326+
port: 8080
327+
# avoid exposing internals to outside via error page
324328
error:
325329
include-stacktrace: never
326330
include-binding-errors: never
@@ -330,15 +334,15 @@ server:
330334

331335
# ACTUATOR PROPERTIES
332336
management:
333-
# Whether to enable or disable all endpoints by default.
337+
# whether to enable or disable all endpoints by default
334338
endpoints.enabled-by-default: false
335339
endpoint:
336-
# Whether to enable the health endpoint.
340+
# whether to enable the health endpoint.
337341
health.enabled: true
338342

339343
spring:
340344
autoconfigure:
341-
# Disable ErrorMvcAutoConfiguration to avoid nasty exceptions if whitelabel error page is disabled
345+
# disable ErrorMvcAutoConfiguration to avoid nasty exceptions if whitelabel error page is disabled
342346
# https://github.com/spring-projects/spring-boot/issues/2001
343347
exclude: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
344348

src/test/java/rocks/inspectit/ocelot/eum/server/metrics/SelfMonitoringMetricManagerTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package rocks.inspectit.ocelot.eum.server.metrics;
22

3+
import io.opentelemetry.api.baggage.Baggage;
34
import io.opentelemetry.sdk.metrics.InstrumentType;
45
import io.opentelemetry.sdk.metrics.InstrumentValueType;
56
import org.junit.jupiter.api.BeforeEach;
@@ -49,7 +50,7 @@ void setupConfiguration() {
4950
.attribute("TAG_1", true)
5051
.build();
5152
Map<String, ViewDefinitionSettings> views = new HashMap<>();
52-
views.put("inspectit-eum/self/beacons_received", view);
53+
views.put("beacons_received", view);
5354

5455
MetricDefinitionSettings dummyMetricDefinition = MetricDefinitionSettings.builder()
5556
.description("Dummy description")
@@ -64,7 +65,7 @@ void setupConfiguration() {
6465
definitionMap.put("beacons_received", dummyMetricDefinition);
6566
selfMonitoringSettings.setEnabled(true);
6667
selfMonitoringSettings.setMetrics(definitionMap);
67-
selfMonitoringSettings.setMetricPrefix("inspectit-eum/self/");
68+
selfMonitoringSettings.setMetricPrefix("inspectit_eum_self_");
6869
}
6970

7071
@Test
@@ -93,10 +94,24 @@ void verifySelfMonitoringMetricManagerIsCalled() {
9394
selfMonitoringMetricManager.initMetrics();
9495

9596
ArgumentCaptor<MetricDefinitionSettings> mdsCaptor = ArgumentCaptor.forClass(MetricDefinitionSettings.class);
96-
verify(instrumentManager).createInstrument(eq("inspectit-eum/self/beacons_received"), mdsCaptor.capture());
97+
verify(instrumentManager).createInstrument(eq("inspectit_eum_self_beacons_received"), mdsCaptor.capture());
9798
verifyNoMoreInteractions(instrumentManager);
9899

99-
assertThat(mdsCaptor.getValue().getViews().keySet()).containsExactly("inspectit-eum/self/beacons_received");
100+
assertThat(mdsCaptor.getValue().getViews().keySet()).containsExactly("inspectit_eum_self_beacons_received");
101+
}
102+
103+
@Test
104+
void verifyViewIsRecorded() {
105+
when(configuration.getSelfMonitoring()).thenReturn(selfMonitoringSettings);
106+
when(instrumentManager.getBaggage(any())).thenReturn(Baggage.empty());
107+
108+
selfMonitoringMetricManager.record("beacons_received", 1);
109+
110+
ArgumentCaptor<MetricDefinitionSettings> mdsCaptor = ArgumentCaptor.forClass(MetricDefinitionSettings.class);
111+
verify(instrumentManager).recordMetric(eq("inspectit_eum_self_beacons_received"), mdsCaptor.capture(), any(Number.class));
112+
verifyNoMoreInteractions(instrumentManager);
113+
114+
assertThat(mdsCaptor.getValue().getViews().keySet()).containsExactly("inspectit_eum_self_beacons_received");
100115
}
101116
}
102117
}

0 commit comments

Comments
 (0)