Skip to content

Commit

Permalink
update: 增加告警组到自定义模板接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang21 committed Aug 4, 2023
1 parent 478b24b commit 9176aee
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 144 deletions.
18 changes: 8 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
SHELL:=/bin/sh
.PHONY: all vet test build clean docker docker-push docker-test
.PHONY: all format vet test build clean docker docker-push docker-test

export GO111MODULE=on
export GOPROXY=https://goproxy.io

pkgs = $(shell go list ./... | grep -v vendor/)

# path related
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MKFILE_DIR := $(dir $(MKFILE_PATH))
RELEASE_DIR := ${MKFILE_DIR}/build/bin

pkgs = $(shell go list ./... | grep -v vendor/)
MKFILE_DIR := $(dir $(MKFILE_PATH))

DOCKER_IMAGE_NAME ?= feiyu563/prometheus-alert

BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILDDATE ?= $(shell date -I'seconds')
BUILDUSER ?= $(shell whoami)@$(shell hostname)
REVISION ?= $(shell git rev-parse HEAD)
Expand All @@ -26,16 +25,15 @@ VERSION_LDFLAGS := \
-X main.BuildDate=$(BUILDDATE)

# go source files, ignore vendor directory
SOURCE = $(shell find ${MKFILE_DIR} -type f -name "*.go")
TARGET = ${RELEASE_DIR}/PrometheusAlert
SOURCE = $(shell find ${MKFILE_DIR} -path "${MKFILE_DIR}vendor" -prune -o -type f -name "*.go" -print)
TARGET = ${MKFILE_DIR}/PrometheusAlert

all: ${TARGET}

${TARGET}: ${SOURCE}
@echo ">> building code"
go mod tidy
go mod vendor
mkdir -p ${RELEASE_DIR}
go build -ldflags "$(VERSION_LDFLAGS)" -o ${TARGET}

format:
Expand All @@ -54,7 +52,7 @@ build: all

clean:
@echo ">> cleaning build"
rm -rf ${MKFILE_DIR}build
rm ${TARGET}

docker:
@echo ">> building docker image"
Expand Down
11 changes: 1 addition & 10 deletions conf/app-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,6 @@ AT_USER_ID="xxxxxxxx"
# 是否启用告警组功能
open-alertgroup=0

# demo 告警组,用于测试案例测试
[ag-demo]
wxurl=wxurl1,wxurl2
ddurl=ddurl1,ddurl1,
fsurl=fsurl1
email=email1,
phone=phone1,phone2
groupid=groupid1

# 自定义的告警组既可以写在这里,也可以写在单独的文件里。
# 写在单独的告警组配置里更便于修改。
include "alertgroup.conf"
# include "alertgroup.conf"
26 changes: 0 additions & 26 deletions conf/prometheus-demo.json

This file was deleted.

30 changes: 23 additions & 7 deletions controllers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ func Alertgroup(alertgroup string) map[string]string {
}

ags := strings.Split(alertgroup, ",")
var wxurl, ddurl, fsurl, email, phone, groupid string
// url likes: url1,url2...
var wxurl, ddurl, fsurl, email, phone, groupid, webhookurl string

// Assembling multiple alertgroups of url together
for _, v := range ags {
wxurl = wxurl + "," + beego.AppConfig.String(v+"::wxurl")
Expand All @@ -414,15 +416,17 @@ func Alertgroup(alertgroup string) map[string]string {
phone = phone + "," + beego.AppConfig.String(v+"::phone")
email = email + "," + beego.AppConfig.String(v+"::email")
groupid = groupid + "," + beego.AppConfig.String(v+"::groupid")
webhookurl = webhookurl + "," + beego.AppConfig.String(v+"::webhookurl")
}

agMap = map[string]string{
"wxurl": URLDeduplication(wxurl),
"ddurl": URLDeduplication(ddurl),
"fsurl": URLDeduplication(fsurl),
"phone": URLDeduplication(phone),
"email": URLDeduplication(email),
"groupid": URLDeduplication(groupid),
"wxurl": URLDeduplication(wxurl),
"ddurl": URLDeduplication(ddurl),
"fsurl": URLDeduplication(fsurl),
"phone": URLDeduplication(phone),
"email": URLDeduplication(email),
"groupid": URLDeduplication(groupid),
"webhookurl": URLDeduplication(webhookurl),
}

return agMap
Expand All @@ -438,6 +442,8 @@ func URLDeduplication(url string) string {

uniqueMap := make(map[string]bool)
for _, s := range urlSlice {
// Remove space at the begin or end.
s = strings.TrimSpace(s)
uniqueMap[s] = true
}

Expand All @@ -455,3 +461,13 @@ func URLDeduplication(url string) string {

return newURL
}

// checkURL checks urls and return non-nil url
func checkURL(urls ...string) string {
for _, url := range urls {
if len(strings.TrimSpace(url)) != 0 {
return url
}
}
return ""
}
161 changes: 130 additions & 31 deletions controllers/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ package controllers

import (
"encoding/json"
"path/filepath"
"runtime"
"os"
"testing"

"github.com/astaxie/beego"
"github.com/stretchr/testify/assert"
)

// Load app conf
func init() {
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}

func TestURLDeduplication(t *testing.T) {
assert := assert.New(t)
var want, result string
Expand All @@ -25,42 +17,126 @@ func TestURLDeduplication(t *testing.T) {
result = URLDeduplication("")
assert.Equal(want, result)

want = "ag0-wxurl0,ag0-wxurl1"
result = URLDeduplication("ag0-wxurl0,ag0-wxurl1")
want = "sa-wxurl0"
result = URLDeduplication("sa-wxurl0")
assert.Equal(want, result)

want = "sa-wxurl0"
result = URLDeduplication("sa-wxurl0,")
assert.Equal(want, result)

want = "sa-wxurl0"
result = URLDeduplication(",sa-wxurl0")
assert.Equal(want, result)

want = "ag0-wxurl0,ag0-wxurl1"
result = URLDeduplication("ag0-wxurl0,ag0-wxurl1,")
want = "sa-wxurl0"
result = URLDeduplication(",sa-wxurl0,")
assert.Equal(want, result)

want = "ag0-wxurl0,ag0-wxurl1"
result = URLDeduplication("ag0-wxurl0,ag0-wxurl1,ag0-wxurl0,ag0-wxurl1")
want = "sa-wxurl0"
result = URLDeduplication("sa-wxurl0, ")
assert.Equal(want, result)

want = "sa-wxurl0,sa-wxurl1"
result = URLDeduplication("sa-wxurl0,sa-wxurl1")
assert.Equal(want, result)

want = "sa-wxurl0,sa-wxurl1"
result = URLDeduplication("sa-wxurl0, sa-wxurl1")
assert.Equal(want, result)

want = "sa-wxurl0,sa-wxurl1"
result = URLDeduplication("sa-wxurl0,sa-wxurl1, ")
assert.Equal(want, result)

want = "sa-wxurl0,sa-wxurl1"
result = URLDeduplication("sa-wxurl0,sa-wxurl1,sa-wxurl0,sa-wxurl1")
assert.Equal(want, result)
}

func TestAlertgroup(t *testing.T) {
assert := assert.New(t)
var want, result map[string]string

// tempFile is /tmp/app*.conf
tempFile, err := os.CreateTemp("", "app*.conf")
defer tempFile.Close()
defer os.Remove(tempFile.Name())
assert.Nil(err)

// mock app.conf
configData := `
[sa]
wxurl=wxurl1
ddurl=ddurl1,
fsurl=,fsurl1
email=,email1,
phone=phone1,phone1
groupid=groupid1,groupid1,
[ops]
wxurl=wxurl1,wxurl2
ddurl=ddurl1,ddurl2,
fsurl=,fsurl1,fsurl2
email=,email1,email2,
phone=phone2
groupid=groupid2
[customtpl]
wxurl=wxurl1,wxurl2
ddurl=ddurl1,ddurl2
fsurl=fsurl1,fsurl2
email=email1,email2
phone=phone1,phone2
groupid=groupid1,groupid2
webhookurl=webhookurl1,webhookurl2
`

// Write mock conf into tempFile
_, err = tempFile.Write([]byte(configData))
assert.Nil(err)

beego.InitBeegoBeforeTest(tempFile.Name())

result = Alertgroup("")
assert.Equal(want, result)

open := beego.AppConfig.String("open-alertgroup")
if open == "1" {
result = Alertgroup("ag-demo")
want = map[string]string{
"wxurl": "wxurl1,wxurl2",
"ddurl": "ddurl1",
"fsurl": "fsurl1",
"phone": "phone1,phone2",
"email": "email1",
"groupid": "groupid1",
}
assert.Equal(want, result)
want = map[string]string{
"wxurl": "wxurl1",
"ddurl": "ddurl1",
"fsurl": "fsurl1",
"phone": "phone1",
"email": "email1",
"groupid": "groupid1",
"webhookurl": "",
}
result = Alertgroup("sa")
assert.Equal(want, result)

want = map[string]string{
"wxurl": "wxurl1,wxurl2",
"ddurl": "ddurl1,ddurl2",
"fsurl": "fsurl1,fsurl2",
"email": "email1,email2",
"phone": "phone1,phone2",
"groupid": "groupid1,groupid2",
"webhookurl": "",
}
result = Alertgroup("sa,ops")
assert.Equal(want, result)

want = map[string]string{
"wxurl": "wxurl1,wxurl2",
"ddurl": "ddurl1,ddurl2",
"fsurl": "fsurl1,fsurl2",
"email": "email1,email2",
"phone": "phone1,phone2",
"groupid": "groupid1,groupid2",
"webhookurl": "webhookurl1,webhookurl2",
}
result = Alertgroup("customtpl")
assert.Equal(want, result)
}

func TestPrometheus(t *testing.T) {
func TestPrometheusJSON(t *testing.T) {
// A sample prometheus alertmanager JSON payload
payload := `
{
Expand Down Expand Up @@ -92,7 +168,30 @@ func TestPrometheus(t *testing.T) {

alert := Prometheus{}
err := json.Unmarshal([]byte(payload), &alert)
if err != nil {
t.Errorf("Prometheus alertmanager json payload parse error: %s", err)
}
assert.Nil(t, err)
}

func TestCheckURL(t *testing.T) {
assert := assert.New(t)
var want, result string

want = ""
result = checkURL("", "")
assert.Equal(want, result)

want = "url1"
result = checkURL("url1", "", "")
assert.Equal(want, result)

want = "url2"
result = checkURL("", "url2", "")
assert.Equal(want, result)

want = "url3"
result = checkURL("", "", "url3")
assert.Equal(want, result)

want = "url1"
result = checkURL("url1", "url2", "url3")
assert.Equal(want, result)
}
Loading

0 comments on commit 9176aee

Please sign in to comment.