补充prometheus
This commit is contained in:
parent
e3a6e821d9
commit
d37d099619
@ -28,8 +28,10 @@ func (c *consumer) Run(ctx context.Context) error {
|
||||
// 发送的数据时Json格式, 接收用的JSON, 发送也需要使用JSON
|
||||
err = e.Load(m.Value)
|
||||
if err == nil {
|
||||
// 保存日志, 保持失败的次数统计起来,披露给外部, 编写一个采集器,来采统计失败次数
|
||||
if err := event.GetService().SaveEvent(ctx, types.NewSet[*event.Event]().Add(e)); err != nil {
|
||||
c.log.Error().Msgf("save event error, %s", err)
|
||||
c.collector.Inc()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"122.51.31.227/go-course/go18/devcloud/audit/apps/event"
|
||||
"github.com/infraboard/mcube/v2/ioc"
|
||||
"github.com/infraboard/mcube/v2/ioc/config/log"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
ioc_kafka "github.com/infraboard/mcube/v2/ioc/config/kafka"
|
||||
@ -37,6 +38,9 @@ type consumer struct {
|
||||
GroupId string `toml:"group_id" json:"group_id" yaml:"group_id" env:"GROUP_ID"`
|
||||
// 当前这个消费者 配置的topic
|
||||
Topics []string `toml:"topic" json:"topic" yaml:"topic" env:"TOPIC"`
|
||||
|
||||
// 采集器
|
||||
collector *EventCollector
|
||||
}
|
||||
|
||||
// 对象名称
|
||||
@ -48,6 +52,11 @@ func (i *consumer) Name() string {
|
||||
func (i *consumer) Init() error {
|
||||
// 对象
|
||||
i.log = log.Sub(i.Name())
|
||||
|
||||
// 准备好采集器, 注册给Prometheus
|
||||
i.collector = NewEventCollector()
|
||||
prometheus.MustRegister(i.collector)
|
||||
|
||||
i.reader = ioc_kafka.ConsumerGroup(i.GroupId, i.Topics)
|
||||
|
||||
go i.Run(i.ctx)
|
||||
|
38
devcloud/audit/apps/event/consumer/metric.go
Normal file
38
devcloud/audit/apps/event/consumer/metric.go
Normal file
@ -0,0 +1,38 @@
|
||||
package consumer
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
// # HELP save_event_error_count 事件入库失败个数统计
|
||||
// # TYPE save_event_error_count gauge
|
||||
// save_event_error_count{service="devcloud"} 0
|
||||
func NewEventCollector() *EventCollector {
|
||||
return &EventCollector{
|
||||
errCountDesc: prometheus.NewDesc(
|
||||
"save_event_error_count",
|
||||
"事件入库失败个数统计",
|
||||
[]string{},
|
||||
prometheus.Labels{"service": "devcloud"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// 收集事件指标的采集器
|
||||
type EventCollector struct {
|
||||
errCountDesc *prometheus.Desc
|
||||
// 需要自己根据实践情况来维护这个变量
|
||||
errCount int
|
||||
}
|
||||
|
||||
func (c *EventCollector) Inc() {
|
||||
c.errCount++
|
||||
}
|
||||
|
||||
// 指标元数据注册
|
||||
func (c *EventCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- c.errCountDesc
|
||||
}
|
||||
|
||||
// 指标的值的采集
|
||||
func (c *EventCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(c.errCountDesc, prometheus.GaugeValue, float64(c.errCount))
|
||||
}
|
3
skills/metric/README.md
Normal file
3
skills/metric/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Metric
|
||||
|
||||
https://www.mcube.top/guide/config/metric.html
|
Loading…
x
Reference in New Issue
Block a user