[AB-184] adding various metrics to the system, organizing imports, changing some transactional behaviour

adding okhttp metrics, split bot service into multiple services (guild, message), unified some places that services are used in order to interact with the api, and not directly the objects (this is to make it easier for metric to be accurate)
This commit is contained in:
Sheldan
2021-01-29 17:46:41 +01:00
parent 2a2a3aea70
commit b838678c15
362 changed files with 3045 additions and 1573 deletions

View File

@@ -0,0 +1,9 @@
package dev.sheldan.abstracto.core.metrics.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:metrics.properties")
public class MetricsConfig {
}

View File

@@ -0,0 +1,20 @@
package dev.sheldan.abstracto.core.metrics.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
public void configure(HttpSecurity http) throws Exception {
http.cors().and()
.csrf().disable().authorizeRequests()
.antMatchers("/actuator/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.httpBasic();
}
}

View File

@@ -0,0 +1,111 @@
package dev.sheldan.abstracto.core.metrics.service;
import io.micrometer.core.instrument.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
@Component
@Slf4j
public class PrometheusMetricServiceBean implements MetricService {
private final List<Counter> counters = new ArrayList<>();
private final List<Gauge> gauges = new ArrayList<>();
@Autowired
private MeterRegistry registry;
@Override
public void registerCounter(CounterMetric metric, String description) {
if(doesCounterExist(metric)) {
throw new IllegalArgumentException("Counter metric already exists.");
}
List<Tag> micrometerTags = new ArrayList<>();
metric.getTagList().forEach(metricTag ->
micrometerTags.add(Tag.of(metricTag.getKey(), metricTag.getValue()))
);
Counter counter = Counter.builder(metric.getName())
.tags(micrometerTags)
.description(description)
.register(registry);
counters.add(counter);
}
@Override
public void registerGauge(CounterMetric counterMetric, Supplier<Number> f, String help) {
registerGauge(counterMetric, f, help, null);
}
@Override
public void registerGauge(CounterMetric counterMetric, Supplier<Number> f, String help, String baseUnit) {
List<Tag> micrometerTags = new ArrayList<>();
counterMetric.getTagList().forEach(metricTag ->
micrometerTags.add(Tag.of(metricTag.getKey(), metricTag.getValue()))
);
Gauge gauge = Gauge
.builder(counterMetric.getName(), f)
.tags(micrometerTags)
.baseUnit(baseUnit)
.description(help)
.register(registry);
gauges.add(gauge);
}
@Override
public <T> void registerGauge(CounterMetric counterMetric, T obj, ToDoubleFunction<T> f, String help) {
registerGauge(counterMetric, obj, f, help, null);
}
@Override
public <T> void registerGauge(CounterMetric counterMetric, T obj, ToDoubleFunction<T> f, String help, String baseUnit) {
List<Tag> micrometerTags = new ArrayList<>();
counterMetric.getTagList().forEach(metricTag ->
micrometerTags.add(Tag.of(metricTag.getKey(), metricTag.getValue()))
);
Gauge gauge = Gauge
.builder(counterMetric.getName(), obj, f)
.tags(micrometerTags)
.baseUnit(baseUnit)
.description(help)
.register(registry);
gauges.add(gauge);
}
@Override
public void incrementCounter(CounterMetric counterMetric) {
incrementCounter(counterMetric, 1L);
}
@Override
public void incrementCounter(CounterMetric counterMetric, Long amount) {
Optional<Counter> counterOptional = counters.stream().filter(counter -> compareCounterIdAndCounterMetric(counter.getId(), counterMetric)).findFirst();
counterOptional.ifPresent(counter -> counter.increment(amount));
if(!counterOptional.isPresent()) {
log.warn("Trying to increment counter {} with tags {}, which was not available (yet).", counterMetric.getName(), String.join(counterMetric.getTagList().toString()));
}
}
private boolean doesCounterExist(CounterMetric counterMetric) {
return counters.stream().anyMatch(counter -> compareCounterIdAndCounterMetric(counter.getId(), counterMetric));
}
private boolean compareCounterIdAndCounterMetric(Meter.Id id, CounterMetric counterMetric) {
boolean nameEquals = id.getName().equals(counterMetric.getName());
if(!nameEquals) {
return false;
}
// in case there are global tags we don't know about them, so we only need to check if the one we are searching matches with the one we know
return counterMetric.getTagList().stream().allMatch(metricTag ->
id.getTags().stream().anyMatch(tag -> tag.getKey().equals(metricTag.getKey()) && tag.getValue().equals(metricTag.getValue()))
);
}
}

View File

@@ -0,0 +1,8 @@
management.security.enabled=false
management.endpoint.health.roles=ACTUATOR
management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.endpoint.health.show-details=always
management.endpoint.beans.enabled=true