Skip to content

Commit

Permalink
MDAS-448 Metrics (#23)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mirrodi authored Oct 17, 2024
1 parent bafcac9 commit 7963feb
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 7 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release-Notes

## Sprint 18 (01.10.2024 - 22.10.2024)
## Hinzugefügt
- Micrometer Metriken

## Sprint 16 (20.08.2024 - 09.09.2024)
### Hinzugefügt
- Schadcode-Erkennung und Mimetype-Prüfung
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<itm-java-codeformat.version>1.0.9</itm-java-codeformat.version>

<!-- Other -->
<org.projectlombok.lombok.version>1.18.26</org.projectlombok.lombok.version>
<org.projectlombok.lombok.version>1.18.30</org.projectlombok.lombok.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -201,9 +201,13 @@
<artifactId>woodstox-core</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-micrometer-starter</artifactId>
<version>${camel.version}</version>
</dependency>
</dependencies>


<build>
<resources>
<resource>
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/de/muenchen/mobidam/config/MetricsConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* The MIT License
* Copyright © 2024 Landeshauptstadt München | it@M
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.muenchen.mobidam.config;

import de.muenchen.mobidam.security.FileSizeProcessor;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import lombok.Setter;
import org.apache.camel.CamelContext;
import org.springframework.stereotype.Component;

@Component
@Getter
@Setter
public class MetricsConfiguration {

private final MeterRegistry meterRegistry;
private final MetricsNameConfig metricsNameConfig;
private final CamelContext camelContext;
private final FileSizeProcessor fileSizeProcessor;

private final Counter beginnCounter;
private final Counter endeCounter;
private final Counter fehlerCounter;
private final Counter erfolgCounter;
private final Counter warnungenCounter;
private final Gauge inflightExchanges;
private final Gauge maxFileSize;
private Timer processingTime;

public MetricsConfiguration(final MeterRegistry meterRegistry, MetricsNameConfig metricsNameConfig, CamelContext camelContext,
FileSizeProcessor fileSizeProcessor) {
this.meterRegistry = meterRegistry;
this.metricsNameConfig = metricsNameConfig;
this.camelContext = camelContext;
this.fileSizeProcessor = fileSizeProcessor;
this.beginnCounter = Counter.builder(metricsNameConfig.getBeginnCounterMetric()).register(meterRegistry);
this.endeCounter = Counter.builder(metricsNameConfig.getEndCounterMetric()).register(meterRegistry);
this.fehlerCounter = Counter.builder(metricsNameConfig.getFehlerCounterMetric()).register(meterRegistry);
this.erfolgCounter = Counter.builder(metricsNameConfig.getErfolgCounterMetric()).register(meterRegistry);
this.warnungenCounter = Counter.builder(metricsNameConfig.getWarnungenCounterMetric()).register(meterRegistry);
this.inflightExchanges = Gauge.builder(metricsNameConfig.getInflightExchangesMetric(), camelContext, context -> context.getInflightRepository().size())
.register(meterRegistry);
this.processingTime = Timer.builder(metricsNameConfig.getProcessingTimeMetric()).register(meterRegistry);
this.maxFileSize = Gauge.builder(metricsNameConfig.getMaxFileSizeMetric(), fileSizeProcessor::getMaxStreamSize).register(meterRegistry);

}

}
50 changes: 50 additions & 0 deletions src/main/java/de/muenchen/mobidam/config/MetricsNameConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License
* Copyright © 2024 Landeshauptstadt München | it@M
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.muenchen.mobidam.config;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
public class MetricsNameConfig {

@Value("${mobidam.metrics.beginn-counter-metric}")
private String beginnCounterMetric;
@Value("${mobidam.metrics.ende-counter-metric}")
private String endCounterMetric;
@Value("${mobidam.metrics.erfolg-counter-metric}")
private String erfolgCounterMetric;
@Value("${mobidam.metrics.fehler-counter-metric}")
private String fehlerCounterMetric;
@Value("${mobidam.metrics.warnungen-counter-metric}")
private String warnungenCounterMetric;
@Value("${mobidam.metrics.processing-time-metric}")
private String processingTimeMetric;
@Value("${mobidam.metrics.inflight-exchanges-metric}")
private String inflightExchangesMetric;
@Value("${mobidam.metrics.max-file-size-metric}")
private String maxFileSizeMetric;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* The MIT License
* Copyright © 2024 Landeshauptstadt München | it@M
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.muenchen.mobidam.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

/**
* The central class for configuration of all security aspects.
*/
@Configuration
@Profile("!no-security")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration {

@Bean
public SecurityFilterChain securityFilterChain(final HttpSecurity http) throws Exception {

return http
.authorizeHttpRequests((requests) -> requests.requestMatchers(
// allow access to /actuator/info
AntPathRequestMatcher.antMatcher("/actuator/info"),
// allow access to /actuator/health for OpenShift Health Check
AntPathRequestMatcher.antMatcher("/actuator/health"),
// allow access to single metrics values
AntPathRequestMatcher.antMatcher("/actuator/metrics/*"),
// allow access to /actuator/metrics for Prometheus monitoring in OpenShift
AntPathRequestMatcher.antMatcher("/actuator/metrics"))
.permitAll())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package de.muenchen.mobidam.mobilithek;

import de.muenchen.mobidam.Constants;
import de.muenchen.mobidam.config.MetricsConfiguration;
import de.muenchen.mobidam.integration.client.domain.DatentransferCreateDTO;
import de.muenchen.mobidam.sstmanagment.EreignisTyp;
import java.time.LocalDateTime;
Expand All @@ -35,8 +36,12 @@
@AllArgsConstructor
public class InterfaceMessageFactory {

private MetricsConfiguration metricsConfiguration;

public void mobilithekMessageStart(Exchange exchange) {

metricsConfiguration.getBeginnCounter().increment();

var dto = new DatentransferCreateDTO();
dto.setEreignis(EreignisTyp.BEGINN.name());
dto.setZeitstempel(LocalDateTime.now());
Expand All @@ -47,6 +52,8 @@ public void mobilithekMessageStart(Exchange exchange) {

public void mobilithekMessageSuccess(Exchange exchange) {

metricsConfiguration.getErfolgCounter().increment();

var dto = new DatentransferCreateDTO();
dto.setEreignis(EreignisTyp.ERFOLG.name());
dto.setZeitstempel(LocalDateTime.now());
Expand All @@ -59,6 +66,8 @@ public void mobilithekMessageSuccess(Exchange exchange) {

public void mobilithekMessageError(Exchange exchange) {

metricsConfiguration.getFehlerCounter().increment();

var dto = new DatentransferCreateDTO();
dto.setEreignis(EreignisTyp.FEHLER.name());
dto.setZeitstempel(LocalDateTime.now());
Expand All @@ -71,6 +80,8 @@ public void mobilithekMessageError(Exchange exchange) {

public void mobilithekMessageEnd(Exchange exchange) {

metricsConfiguration.getEndeCounter().increment();

var dto = new DatentransferCreateDTO();
dto.setEreignis(EreignisTyp.ENDE.name());
dto.setZeitstempel(LocalDateTime.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void configure() {
.process("mimeTypeProcessor")
.process("codeDetectionProcessor")
.process("s3ObjectKeyProvider")
.process("fileSizeProcessor")
.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)
.bean("interfaceMessageFactory", "mobilithekMessageSuccess")
.bean("sstManagementIntegrationService", "logDatentransfer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import de.muenchen.mobidam.Constants;
import de.muenchen.mobidam.config.Interfaces;
import de.muenchen.mobidam.config.MetricsConfiguration;
import de.muenchen.mobidam.mobilithek.MobilithekEaiRouteBuilder;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -47,6 +48,8 @@ public class MobilithekJobExecute implements Job {

private Interfaces mobidamInterfaces;

private MetricsConfiguration metricsConfiguration;

@Produce(MobilithekEaiRouteBuilder.MOBIDAM_S3_ROUTE)
private ProducerTemplate producer;

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

var exchange = ExchangeBuilder.anExchange(getCamelContext())
var exchange = metricsConfiguration.getProcessingTime().record(() -> ExchangeBuilder.anExchange(getCamelContext())
.withHeader(Constants.INTERFACE_TYPE, getMobidamInterfaces().getInterfaces().get(identifier))
.build();
.build());

producer.send(exchange);

}

}
48 changes: 48 additions & 0 deletions src/main/java/de/muenchen/mobidam/security/FileSizeProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License
* Copyright © 2024 Landeshauptstadt München | it@M
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.muenchen.mobidam.security;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.converter.stream.InputStreamCache;
import org.springframework.stereotype.Service;

@Service
@NoArgsConstructor
@Slf4j
@Getter
public class FileSizeProcessor implements Processor {

private long maxStreamSize = 0L;

@Override
public void process(Exchange exchange) throws Exception {
InputStreamCache stream = exchange.getMessage().getBody(InputStreamCache.class);
stream.reset();
maxStreamSize = Math.max(maxStreamSize, stream.length());
}

}
15 changes: 13 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ management:
enabled-by-default: false
web:
exposure:
include: health,info
include: health,info,metrics
endpoint:
info:
enabled: true
health:
enabled: true
metrics:
enabled: true
info:
env:
enabled: true
Expand Down Expand Up @@ -82,4 +84,13 @@ mobidam:
bucket-credential-config:
s3-bucket-1:
access-key-env-var: ...
secret-key-env-var: ...
secret-key-env-var: ...
metrics:
beginn-counter-metric: mobidam.exchanges.ereignis.beginn.counter
ende-counter-metric: mobidam.exchanges.ereignis.ende.counter
fehler-counter-metric: mobidam.exchanges.ereignis.fehler.counter
erfolg-counter-metric: mobidam.exchanges.ereignis.erfolg.counter
warnungen-counter-metric: mobidam.exchanges.ereignis.warnungen.counter
inflight-exchanges-metric: mobidam.exchanges.inflight
processing-time-metric: mobidam.exchanges.processingtime
max-file-size-metric: mobidam.exchanges.filesize.max
9 changes: 9 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ mobidam:
int-mdasc-mdasdev:
access-key-env-var: MOBIDAM_INT-MDASC-MDASDEV_ACCESS_KEY
secret-key-env-var: MOBIDAM_INT-MDASC-MDASDEV_SECRET_KEY
metrics:
beginn-counter-metric: mobidam.exchanges.ereignis.beginn.counter
ende-counter-metric: mobidam.exchanges.ereignis.ende.counter
fehler-counter-metric: mobidam.exchanges.ereignis.fehler.counter
erfolg-counter-metric: mobidam.exchanges.ereignis.erfolg.counter
warnungen-counter-metric: mobidam.exchanges.ereignis.warnungen.counter
inflight-exchanges-metric: mobidam.exchanges.inflight
processing-time-metric: mobidam.exchanges.processingtime
max-file-size-metric: mobidam.exchanges.filesize.max

0 comments on commit 7963feb

Please sign in to comment.