🥐 의존성 추가
implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
🐣 Swagger 3.0과 actuator 충돌 문제 해결
- 초기화 후에 호출되는 메서드 postProcessAfterInitialization을 재정의
- 빈 타입이 WebMvcRequestHandlerProvider 또는 WebFluxRequestHandlerProvider일때 로직 수행
- WebMvcRequestHandlerProvider 또는 WebFluxRequestHandlerProvider 클래스에서 handlerMappings 이름을 가진 빌드를 찾는다.
- patternParser가 null인 매핑만 필터링
@Configuration
public class Config {
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider
|| bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping>
void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy =
mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}
}
/etc/prometheus/prometheus.yml
# Sample config for Prometheus.
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'example'
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
scrape_timeout: 5s
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: node
# If prometheus-node-exporter is installed, grab stats about the local
# machine by default.
static_configs:
- targets: ['localhost:9100']
- job_name: music
metrics_path: "/music/actuator/prometheus"
static_configs:
- targets: ['localhost:8084']
- job_name: user
metrics_path: "/users/actuator/prometheus"
static_configs:
- targets: ['localhost:8086']
- job_name: config
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ['localhost:8088']
- job_name: discovery
metrics_path: "/actuator/discovery"
static_configs:
- targets: ['localhost:8761']
- job_name: "batch"
metrics_path: "/actuator/prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:8083"]
- job_name: "gateway"
metrics_path: "/actuator/prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:8081"]
참고 👇👇👇
https://velog.io/@723poil/Spring-Boot-Actuator-%EC%99%80-Swagger-3.0.0-%EC%B6%A9%EB%8F%8C
https://lordofkangs.tistory.com/327
반응형