-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jikun.zhang <jikun.zhang@megatronix.co>
- Loading branch information
jikun.zhang
committed
Jun 19, 2024
1 parent
a1d5d6a
commit e28b3bb
Showing
18 changed files
with
395 additions
and
677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/IBM/sarama" | ||
"github.com/astaxie/beego" | ||
"time" | ||
) | ||
|
||
func GetKafkaProducer() sarama.SyncProducer { | ||
kafka_server := beego.AppConfig.Strings("kafka_server") | ||
config := sarama.NewConfig() | ||
config.Producer.RequiredAcks = sarama.WaitForAll // 等待所有follower都回复ack,确保Kafka不会丢消息 | ||
config.Producer.Return.Successes = true | ||
config.Producer.Partitioner = sarama.NewHashPartitioner | ||
|
||
// 对Key进行Hash,同样的Key每次都落到一个分区,这样消息是有序的 | ||
producer, err := sarama.NewSyncProducer(kafka_server, config) | ||
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return producer | ||
} | ||
|
||
func SendKafka(message, logsign string) string { | ||
//发送kafka | ||
open := beego.AppConfig.String("open-kafka") | ||
if open != "1" { | ||
beego.Info(logsign, "[kafka]", "kafka未配置未开启状态,请先配置open-kafka为1") | ||
return "kafka未配置未开启状态,请先配置open-kafka为1" | ||
} | ||
t1 := time.Now().UnixMilli() | ||
producer := GetKafkaProducer() | ||
kafka_topic := beego.AppConfig.String("kafka_topic") | ||
Key_string := beego.AppConfig.String("kafka_key") + "-" + logsign | ||
msg := &sarama.ProducerMessage{ | ||
Topic: kafka_topic, | ||
Value: sarama.StringEncoder(message), | ||
Key: sarama.StringEncoder(Key_string), | ||
} | ||
|
||
partition, offset, err := producer.SendMessage(msg) | ||
defer producer.Close() | ||
t2 := time.Now().UnixMilli() | ||
if err == nil { | ||
beego.Debug("发送kafka消息:", Key_string, "成功, partition:", partition, ",offset:", offset, ",cost:", t2-t1, " ms") | ||
return "发送kafka消息:" + Key_string + "成功" | ||
} else { | ||
beego.Error(err) | ||
return err.Error() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# PrometheusAlert全家桶Kafka配置说明 | ||
|
||
----------------- | ||
|
||
PrometheusAlert支持将收到的json消息通过模板渲染后转发给Kafka集群,使用前需要在配置文件app.conf中配置好Kafka服务的连接信息 | ||
|
||
``` | ||
#---------------------↓kafka地址----------------------- | ||
# kafka服务器的地址 | ||
open-kafka=1 | ||
kafka_server = 127.0.0.1:9092 | ||
# 写入消息的kafka topic | ||
kafka_topic = devops | ||
# 用户标记该消息是来自PrometheusAlert,一般无需修改 | ||
kafka_key = PrometheusAlert | ||
``` | ||
|
||
**如何使用** | ||
|
||
以Prometheus配合自定义模板为例: | ||
|
||
Prometheus配置参考: | ||
|
||
``` | ||
global: | ||
resolve_timeout: 5m | ||
route: | ||
group_by: ['instance'] | ||
group_wait: 10m | ||
group_interval: 10s | ||
repeat_interval: 10m | ||
receiver: 'web.hook.prometheusalert' | ||
receivers: | ||
- name: 'web.hook.prometheusalert' | ||
webhook_configs: | ||
- url: 'http://[prometheusalert_url]:8080/prometheusalert?type=kafka&tpl=prometheus-kafka' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.