提交 44b75bdb 编写于 作者: 如梦技术's avatar 如梦技术 🐛

mica-prometheus 添加 alert webhook。

上级 51f37b74
......@@ -18,7 +18,8 @@
compile("net.dreamlu:mica-prometheus:${version}")
```
## 使用
## http-sd 使用
### 添加配置
```yaml
- job_name: micax-cloud
honor_timestamps: true
......@@ -30,6 +31,26 @@ compile("net.dreamlu:mica-prometheus:${version}")
- url: 'http://{ip}:{port}/actuator/prometheus/sd'
```
## 效果图
### 效果图
![mica-prometheus 效果图](../docs/images/mica-prometheus-show.png)
## alert web hook
### 添加配置
```yaml
receivers:
- name: "alerts"
webhook_configs:
- url: 'http://{ip}:{port}/actuator/prometheus/alerts'
send_resolved: true
```
### 自定义监听事件并处理
```java
@Async
@EventListener
public void onAlertEvent(AlertMessage message) {
// 处理 alert webhook message
}
```
\ No newline at end of file
......@@ -14,14 +14,17 @@
* limitations under the License.
*/
package net.dreamlu.mica.prometheus.sd;
package net.dreamlu.mica.prometheus.api.config;
import net.dreamlu.mica.prometheus.api.core.PrometheusApi;
import net.dreamlu.mica.prometheus.api.core.ReactivePrometheusApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -41,8 +44,9 @@ public class PrometheusConfiguration {
public static class PrometheusApiConfiguration {
@Bean
public PrometheusApi prometheusApi(DiscoveryClient discoveryClient) {
return new PrometheusApi(discoveryClient);
public PrometheusApi prometheusApi(DiscoveryClient discoveryClient,
ApplicationEventPublisher eventPublisher) {
return new PrometheusApi(discoveryClient, eventPublisher);
}
}
......@@ -54,8 +58,9 @@ public class PrometheusConfiguration {
public static class ReactivePrometheusApiConfiguration {
@Bean
public ReactivePrometheusApi reactivePrometheusApi(ReactiveDiscoveryClient discoveryClient) {
return new ReactivePrometheusApi(discoveryClient);
public ReactivePrometheusApi reactivePrometheusApi(ReactiveDiscoveryClient discoveryClient,
ApplicationEventPublisher eventPublisher) {
return new ReactivePrometheusApi(discoveryClient, eventPublisher);
}
}
......
......@@ -14,15 +14,17 @@
* limitations under the License.
*/
package net.dreamlu.mica.prometheus.sd;
package net.dreamlu.mica.prometheus.api.core;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.auto.annotation.AutoIgnore;
import net.dreamlu.mica.prometheus.api.pojo.AlertMessage;
import net.dreamlu.mica.prometheus.api.pojo.TargetGroup;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import java.util.HashMap;
......@@ -40,6 +42,7 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class ReactivePrometheusApi {
private final ReactiveDiscoveryClient discoveryClient;
private final ApplicationEventPublisher eventPublisher;
@GetMapping("sd")
public Flux<TargetGroup> getList() {
......@@ -55,4 +58,10 @@ public class ReactivePrometheusApi {
});
}
@PostMapping("alerts")
public ResponseEntity<Object> postAlerts(@RequestBody AlertMessage message) {
eventPublisher.publishEvent(message);
return ResponseEntity.ok().build();
}
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.prometheus.api.pojo;
import lombok.Data;
import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.Map;
/**
* 告警模型
*
* @author L.cm
*/
@Data
public class AlertInfo implements Serializable {
/**
* 状态 resolved|firing
*/
private String status;
/**
* 标签集合
*/
private Map<String, String> labels;
/**
* 注释集合
*/
private Map<String, String> annotations;
/**
* 开始时间
*/
private OffsetDateTime startsAt;
/**
* 结束时间
*/
private OffsetDateTime endsAt;
/**
* identifies the entity that caused the alert
*/
private String generatorURL;
/**
* fingerprint to identify the alert
*/
private String fingerprint;
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.prometheus.api.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* alert hook
*
* @author L.cm
*/
@Data
public class AlertMessage implements Serializable {
/**
* 版本号
*/
private String version;
/**
* 由于 “max_alerts” 而截断了多少警报
*/
private Integer truncatedAlerts;
/**
* 分组 key
*/
private String groupKey;
/**
* 状态 resolved|firing
*/
private String status;
/**
* 接收者
*/
private String receiver;
/**
* 分组 labels
*/
private Map<String, String> groupLabels;
/**
* 通用 label
*/
private Map<String, String> commonLabels;
/**
* 通用注解
*/
private Map<String, String> commonAnnotations;
/**
* 扩展 url 地址
*/
private String externalURL;
/**
* alerts
*/
private List<AlertInfo> alerts;
}
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package net.dreamlu.mica.prometheus.sd;
package net.dreamlu.mica.prometheus.api.pojo;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册