Skip to content

Commit 7963feb

Browse files
authored
MDAS-448 Metrics (#23)
* Enabled metrics endpoint * added dependency * added dependencies * Standard metrics can be opened * Monitoring the inflight metric * Monitoring the count of Ereignistyp * Fixed metrics name * Spotless * Processing time metric not working * Time metric works * Spotless * Checkstyle * Added license * Configuration of metric names * Configuration of metric names * reformated the file * Tests fixed * Maximum file size metric * Tests fixed * Spotless * RELEASENOTES * Tests fixed * Removed unused constructor
1 parent bafcac9 commit 7963feb

11 files changed

Lines changed: 278 additions & 7 deletions

File tree

RELEASENOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release-Notes
22

3+
## Sprint 18 (01.10.2024 - 22.10.2024)
4+
## Hinzugefügt
5+
- Micrometer Metriken
6+
37
## Sprint 16 (20.08.2024 - 09.09.2024)
48
### Hinzugefügt
59
- Schadcode-Erkennung und Mimetype-Prüfung

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<itm-java-codeformat.version>1.0.9</itm-java-codeformat.version>
6666

6767
<!-- Other -->
68-
<org.projectlombok.lombok.version>1.18.26</org.projectlombok.lombok.version>
68+
<org.projectlombok.lombok.version>1.18.30</org.projectlombok.lombok.version>
6969
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7070
</properties>
7171

@@ -201,9 +201,13 @@
201201
<artifactId>woodstox-core</artifactId>
202202
<version>7.0.0</version>
203203
</dependency>
204+
<dependency>
205+
<groupId>org.apache.camel.springboot</groupId>
206+
<artifactId>camel-micrometer-starter</artifactId>
207+
<version>${camel.version}</version>
208+
</dependency>
204209
</dependencies>
205210

206-
207211
<build>
208212
<resources>
209213
<resource>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2024 Landeshauptstadt München | it@M
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package de.muenchen.mobidam.config;
24+
25+
import de.muenchen.mobidam.security.FileSizeProcessor;
26+
import io.micrometer.core.instrument.Counter;
27+
import io.micrometer.core.instrument.Gauge;
28+
import io.micrometer.core.instrument.MeterRegistry;
29+
import io.micrometer.core.instrument.Timer;
30+
import lombok.Getter;
31+
import lombok.Setter;
32+
import org.apache.camel.CamelContext;
33+
import org.springframework.stereotype.Component;
34+
35+
@Component
36+
@Getter
37+
@Setter
38+
public class MetricsConfiguration {
39+
40+
private final MeterRegistry meterRegistry;
41+
private final MetricsNameConfig metricsNameConfig;
42+
private final CamelContext camelContext;
43+
private final FileSizeProcessor fileSizeProcessor;
44+
45+
private final Counter beginnCounter;
46+
private final Counter endeCounter;
47+
private final Counter fehlerCounter;
48+
private final Counter erfolgCounter;
49+
private final Counter warnungenCounter;
50+
private final Gauge inflightExchanges;
51+
private final Gauge maxFileSize;
52+
private Timer processingTime;
53+
54+
public MetricsConfiguration(final MeterRegistry meterRegistry, MetricsNameConfig metricsNameConfig, CamelContext camelContext,
55+
FileSizeProcessor fileSizeProcessor) {
56+
this.meterRegistry = meterRegistry;
57+
this.metricsNameConfig = metricsNameConfig;
58+
this.camelContext = camelContext;
59+
this.fileSizeProcessor = fileSizeProcessor;
60+
this.beginnCounter = Counter.builder(metricsNameConfig.getBeginnCounterMetric()).register(meterRegistry);
61+
this.endeCounter = Counter.builder(metricsNameConfig.getEndCounterMetric()).register(meterRegistry);
62+
this.fehlerCounter = Counter.builder(metricsNameConfig.getFehlerCounterMetric()).register(meterRegistry);
63+
this.erfolgCounter = Counter.builder(metricsNameConfig.getErfolgCounterMetric()).register(meterRegistry);
64+
this.warnungenCounter = Counter.builder(metricsNameConfig.getWarnungenCounterMetric()).register(meterRegistry);
65+
this.inflightExchanges = Gauge.builder(metricsNameConfig.getInflightExchangesMetric(), camelContext, context -> context.getInflightRepository().size())
66+
.register(meterRegistry);
67+
this.processingTime = Timer.builder(metricsNameConfig.getProcessingTimeMetric()).register(meterRegistry);
68+
this.maxFileSize = Gauge.builder(metricsNameConfig.getMaxFileSizeMetric(), fileSizeProcessor::getMaxStreamSize).register(meterRegistry);
69+
70+
}
71+
72+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2024 Landeshauptstadt München | it@M
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package de.muenchen.mobidam.config;
24+
25+
import lombok.Getter;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
@Configuration
30+
@Getter
31+
public class MetricsNameConfig {
32+
33+
@Value("${mobidam.metrics.beginn-counter-metric}")
34+
private String beginnCounterMetric;
35+
@Value("${mobidam.metrics.ende-counter-metric}")
36+
private String endCounterMetric;
37+
@Value("${mobidam.metrics.erfolg-counter-metric}")
38+
private String erfolgCounterMetric;
39+
@Value("${mobidam.metrics.fehler-counter-metric}")
40+
private String fehlerCounterMetric;
41+
@Value("${mobidam.metrics.warnungen-counter-metric}")
42+
private String warnungenCounterMetric;
43+
@Value("${mobidam.metrics.processing-time-metric}")
44+
private String processingTimeMetric;
45+
@Value("${mobidam.metrics.inflight-exchanges-metric}")
46+
private String inflightExchangesMetric;
47+
@Value("${mobidam.metrics.max-file-size-metric}")
48+
private String maxFileSizeMetric;
49+
50+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2024 Landeshauptstadt München | it@M
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package de.muenchen.mobidam.config;
24+
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.Profile;
28+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
29+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
30+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
31+
import org.springframework.security.web.SecurityFilterChain;
32+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
33+
34+
/**
35+
* The central class for configuration of all security aspects.
36+
*/
37+
@Configuration
38+
@Profile("!no-security")
39+
@EnableWebSecurity
40+
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
41+
public class SecurityConfiguration {
42+
43+
@Bean
44+
public SecurityFilterChain securityFilterChain(final HttpSecurity http) throws Exception {
45+
46+
return http
47+
.authorizeHttpRequests((requests) -> requests.requestMatchers(
48+
// allow access to /actuator/info
49+
AntPathRequestMatcher.antMatcher("/actuator/info"),
50+
// allow access to /actuator/health for OpenShift Health Check
51+
AntPathRequestMatcher.antMatcher("/actuator/health"),
52+
// allow access to single metrics values
53+
AntPathRequestMatcher.antMatcher("/actuator/metrics/*"),
54+
// allow access to /actuator/metrics for Prometheus monitoring in OpenShift
55+
AntPathRequestMatcher.antMatcher("/actuator/metrics"))
56+
.permitAll())
57+
.build();
58+
}
59+
}

src/main/java/de/muenchen/mobidam/mobilithek/InterfaceMessageFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package de.muenchen.mobidam.mobilithek;
2424

2525
import de.muenchen.mobidam.Constants;
26+
import de.muenchen.mobidam.config.MetricsConfiguration;
2627
import de.muenchen.mobidam.integration.client.domain.DatentransferCreateDTO;
2728
import de.muenchen.mobidam.sstmanagment.EreignisTyp;
2829
import java.time.LocalDateTime;
@@ -35,8 +36,12 @@
3536
@AllArgsConstructor
3637
public class InterfaceMessageFactory {
3738

39+
private MetricsConfiguration metricsConfiguration;
40+
3841
public void mobilithekMessageStart(Exchange exchange) {
3942

43+
metricsConfiguration.getBeginnCounter().increment();
44+
4045
var dto = new DatentransferCreateDTO();
4146
dto.setEreignis(EreignisTyp.BEGINN.name());
4247
dto.setZeitstempel(LocalDateTime.now());
@@ -47,6 +52,8 @@ public void mobilithekMessageStart(Exchange exchange) {
4752

4853
public void mobilithekMessageSuccess(Exchange exchange) {
4954

55+
metricsConfiguration.getErfolgCounter().increment();
56+
5057
var dto = new DatentransferCreateDTO();
5158
dto.setEreignis(EreignisTyp.ERFOLG.name());
5259
dto.setZeitstempel(LocalDateTime.now());
@@ -59,6 +66,8 @@ public void mobilithekMessageSuccess(Exchange exchange) {
5966

6067
public void mobilithekMessageError(Exchange exchange) {
6168

69+
metricsConfiguration.getFehlerCounter().increment();
70+
6271
var dto = new DatentransferCreateDTO();
6372
dto.setEreignis(EreignisTyp.FEHLER.name());
6473
dto.setZeitstempel(LocalDateTime.now());
@@ -71,6 +80,8 @@ public void mobilithekMessageError(Exchange exchange) {
7180

7281
public void mobilithekMessageEnd(Exchange exchange) {
7382

83+
metricsConfiguration.getEndeCounter().increment();
84+
7485
var dto = new DatentransferCreateDTO();
7586
dto.setEreignis(EreignisTyp.ENDE.name());
7687
dto.setZeitstempel(LocalDateTime.now());

src/main/java/de/muenchen/mobidam/mobilithek/MobilithekEaiRouteBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void configure() {
8686
.process("mimeTypeProcessor")
8787
.process("codeDetectionProcessor")
8888
.process("s3ObjectKeyProvider")
89+
.process("fileSizeProcessor")
8990
.toD("aws2-s3://${header.bucketName}?accessKey=RAW(${header.accessKey})&secretKey=RAW(${header.secretKey})&region=${properties:camel.component.aws2-s3.region}&overrideEndpoint=true&uriEndpointOverride=${properties:camel.component.aws2-s3.override-endpoint}").id(MOBIDAM_ENDPOINT_S3_ID)
9091
.bean("interfaceMessageFactory", "mobilithekMessageSuccess")
9192
.bean("sstManagementIntegrationService", "logDatentransfer")

src/main/java/de/muenchen/mobidam/scheduler/MobilithekJobExecute.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import de.muenchen.mobidam.Constants;
2626
import de.muenchen.mobidam.config.Interfaces;
27+
import de.muenchen.mobidam.config.MetricsConfiguration;
2728
import de.muenchen.mobidam.mobilithek.MobilithekEaiRouteBuilder;
2829
import lombok.AllArgsConstructor;
2930
import lombok.Getter;
@@ -47,6 +48,8 @@ public class MobilithekJobExecute implements Job {
4748

4849
private Interfaces mobidamInterfaces;
4950

51+
private MetricsConfiguration metricsConfiguration;
52+
5053
@Produce(MobilithekEaiRouteBuilder.MOBIDAM_S3_ROUTE)
5154
private ProducerTemplate producer;
5255

@@ -55,12 +58,11 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
5558
var identifier = context.getJobDetail().getJobDataMap().get(Constants.INTERFACE_TYPE);
5659
log.info("Scheduler starts mobilithek '{}' request at '{}'.", identifier, context.getFireTime().toString());
5760

58-
var exchange = ExchangeBuilder.anExchange(getCamelContext())
61+
var exchange = metricsConfiguration.getProcessingTime().record(() -> ExchangeBuilder.anExchange(getCamelContext())
5962
.withHeader(Constants.INTERFACE_TYPE, getMobidamInterfaces().getInterfaces().get(identifier))
60-
.build();
63+
.build());
6164

6265
producer.send(exchange);
63-
6466
}
6567

6668
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2024 Landeshauptstadt München | it@M
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package de.muenchen.mobidam.security;
24+
25+
import lombok.Getter;
26+
import lombok.NoArgsConstructor;
27+
import lombok.extern.slf4j.Slf4j;
28+
import org.apache.camel.Exchange;
29+
import org.apache.camel.Processor;
30+
import org.apache.camel.converter.stream.InputStreamCache;
31+
import org.springframework.stereotype.Service;
32+
33+
@Service
34+
@NoArgsConstructor
35+
@Slf4j
36+
@Getter
37+
public class FileSizeProcessor implements Processor {
38+
39+
private long maxStreamSize = 0L;
40+
41+
@Override
42+
public void process(Exchange exchange) throws Exception {
43+
InputStreamCache stream = exchange.getMessage().getBody(InputStreamCache.class);
44+
stream.reset();
45+
maxStreamSize = Math.max(maxStreamSize, stream.length());
46+
}
47+
48+
}

src/main/resources/application.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ management:
3434
enabled-by-default: false
3535
web:
3636
exposure:
37-
include: health,info
37+
include: health,info,metrics
3838
endpoint:
3939
info:
4040
enabled: true
4141
health:
4242
enabled: true
43+
metrics:
44+
enabled: true
4345
info:
4446
env:
4547
enabled: true
@@ -82,4 +84,13 @@ mobidam:
8284
bucket-credential-config:
8385
s3-bucket-1:
8486
access-key-env-var: ...
85-
secret-key-env-var: ...
87+
secret-key-env-var: ...
88+
metrics:
89+
beginn-counter-metric: mobidam.exchanges.ereignis.beginn.counter
90+
ende-counter-metric: mobidam.exchanges.ereignis.ende.counter
91+
fehler-counter-metric: mobidam.exchanges.ereignis.fehler.counter
92+
erfolg-counter-metric: mobidam.exchanges.ereignis.erfolg.counter
93+
warnungen-counter-metric: mobidam.exchanges.ereignis.warnungen.counter
94+
inflight-exchanges-metric: mobidam.exchanges.inflight
95+
processing-time-metric: mobidam.exchanges.processingtime
96+
max-file-size-metric: mobidam.exchanges.filesize.max

0 commit comments

Comments
 (0)