Skip to content

Commit 777fefe

Browse files
committed
Adding association between a Kafka cluster and a notifier
Signed-off-by: muicoder <muicoder@gmail.com> linkedin#611 Adding WeCom/DingTalk template
1 parent e593345 commit 777fefe

14 files changed

+212
-27
lines changed

.github/workflows/action.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
jobs:
2+
redis-operator:
3+
runs-on: ubuntu-latest
4+
steps:
5+
- name: Checkout
6+
uses: actions/checkout@v3
7+
with:
8+
fetch-depth: 0
9+
- name: Set up QEMU
10+
uses: docker/setup-qemu-action@v2
11+
- name: Set up Docker Buildx
12+
uses: docker/setup-buildx-action@v2
13+
- name: Login to DockerHub
14+
uses: docker/login-action@v2
15+
with:
16+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
17+
username: ${{ secrets.DOCKERHUB_USERNAME }}
18+
- name: Build and push
19+
uses: docker/build-push-action@v3
20+
with:
21+
context: .
22+
platforms: linux/amd64,linux/arm64
23+
provenance: false
24+
pull: true
25+
push: true
26+
sbom: false
27+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/burrow:action
28+
name: build
29+
on:
30+
workflow_dispatch:

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# stage 1: builder
2-
FROM golang:1.20.4-alpine as builder
2+
FROM golang:1.20-alpine as builder
33

44
ENV BURROW_SRC /usr/src/Burrow/
55

6-
RUN apk add --no-cache git curl
6+
RUN apk add --no-cache git curl make
77
COPY . $BURROW_SRC
88
WORKDIR $BURROW_SRC
99

10-
RUN go mod tidy && go build -o /tmp/burrow .
10+
RUN make && go build -trimpath -ldflags "-extldflags -static -w -s" -o /tmp/burrow .
1111

1212
# stage 2: runner
1313
FROM alpine:3.18

config/burrow.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ min-distance=1
5858

5959
[notifier.default]
6060
class-name="http"
61+
cluster="local"
6162
url-open="http://someservice.example.com:1467/v1/event"
6263
interval=60
6364
timeout=5

config/default-dingtalk-post.tmpl

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{"msgtype": "markdown","markdown": {"title":"Kafka LagChecker", "text": "
2+
{{- $StatusURL := "https://pkg.go.dev/github.com/linkedin/Burrow/core/protocol#StatusConstant"}}
3+
{{- $FormatString := "2006-01-02 15:04:05"}}
4+
# Kafka: {{.Cluster}}
5+
ConsumerGroup: {{.Group}}{{- with .Result.Status}}
6+
{{- if eq . 0}}NotFound{{end}}
7+
{{- if eq . 1}}normal{{end}}
8+
{{- if eq . 2}}lagging{{end}}
9+
{{- if eq . 3}}abnormal{{end}}
10+
{{- end}}
11+
**Status:** Total(Partitions={{.Result.TotalPartitions}},Lag={{.Result.TotalLag}})[{{- with .Result.Status}}
12+
{{- if eq . 0}}NotFound{{end}}
13+
{{- if eq . 1}}{{.}}{{end}}
14+
{{- if eq . 2}}{{.}}{{end}}
15+
{{- if eq . 3}}{{.}}{{end}}
16+
{{- end}}]({{$StatusURL}}){{printf "%.2f" .Result.Complete}}
17+
{{- if .Result.Maxlag|maxlag}}
18+
**MaxLagDetails:**
19+
{{- with .Result.Maxlag}}
20+
{{.Topic}}[{{.Status.String}}](){{printf "%.2f" .Complete}}
21+
{{- if .Owner}}\n\tConsumerHost={{.Owner}}{{end}}
22+
{{- if .ClientID}}\n\tConsumerClientID={{.ClientID}}{{end}}
23+
\tCurrentLag={{.CurrentLag}}
24+
\tPartition={{.Partition}}
25+
{{- end}}
26+
{{- end}}
27+
{{- $TotalErrors := len .Result.Partitions}}
28+
{{- if $TotalErrors}}
29+
### {{$TotalErrors}} partitions have problems
30+
>**CountPartitions:**
31+
{{- range $k,$v := .Result.Partitions|partitioncounts}}
32+
{{- if ne $v 0}}\n\t{{$k}}={{$v}}{{end}}
33+
{{- end}}
34+
**TopicsByStatus:**
35+
{{- range $k,$v := .Result.Partitions|topicsbystatus}}
36+
\t{{$k}}={{$v}}
37+
{{- end}}
38+
**PartitionDetails:**
39+
{{- range .Result.Partitions}}
40+
{{.Topic}}[{{.Status.String}}](){{printf "%.2f" .Complete}}
41+
{{- if .Owner}}\n\tConsumerHost={{.Owner}}{{end}}
42+
{{- if .ClientID}}\n\tConsumerClientID={{.ClientID}}{{end}}
43+
\tCurrentLag={{.CurrentLag}}
44+
\tPartition={{.Partition}}
45+
\tStart={{formattimestamp .Start.Timestamp $FormatString}}
46+
\t\tOffset={{.Start.Offset}}\tLag={{.Start.Lag.Value}}
47+
\tEnd={{formattimestamp .End.Timestamp $FormatString}}
48+
\t\tOffset={{.End.Offset}}\tLag={{.End.Lag.Value}}
49+
{{- end}}
50+
{{- end}}
51+
"
52+
}}

config/default-wecom-post.tmpl

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{"msgtype": "markdown","markdown": {"content": "
2+
{{- $StatusURL := "https://pkg.go.dev/github.com/linkedin/Burrow/core/protocol#StatusConstant"}}
3+
{{- $FormatString := "2006-01-02 15:04:05"}}
4+
# Kafka: {{.Cluster}}
5+
ConsumerGroup: {{.Group}}{{- with .Result.Status}}
6+
{{- if eq . 0}}NotFound{{end}}
7+
{{- if eq . 1}}<font color=\"info\">normal</font>{{end}}
8+
{{- if eq . 2}}<font color=\"warning\">lagging</font>{{end}}
9+
{{- if eq . 3}}<font color=\"comment\">abnormal</font>{{end}}
10+
{{- end}}
11+
**Status:** Total(Partitions={{.Result.TotalPartitions}},Lag={{.Result.TotalLag}})[{{- with .Result.Status}}
12+
{{- if eq . 0}}NotFound{{end}}
13+
{{- if eq . 1}}<font color=\"info\">{{.}}</font>{{end}}
14+
{{- if eq . 2}}<font color=\"warning\">{{.}}</font>{{end}}
15+
{{- if eq . 3}}<font color=\"comment\">{{.}}</font>{{end}}
16+
{{- end}}]({{$StatusURL}}){{printf "%.2f" .Result.Complete}}
17+
{{- if .Result.Maxlag|maxlag}}
18+
**MaxLagDetails:**
19+
{{- with .Result.Maxlag}}
20+
{{.Topic}}[{{.Status.String}}](){{printf "%.2f" .Complete}}
21+
{{- if .Owner}}\n\tConsumerHost={{.Owner}}{{end}}
22+
{{- if .ClientID}}\n\tConsumerClientID={{.ClientID}}{{end}}
23+
\tCurrentLag={{.CurrentLag}}
24+
\tPartition={{.Partition}}
25+
{{- end}}
26+
{{- end}}
27+
{{- $TotalErrors := len .Result.Partitions}}
28+
{{- if $TotalErrors}}
29+
### <font color=\"comment\">{{$TotalErrors}} partitions have problems</font>
30+
>**CountPartitions:**
31+
{{- range $k,$v := .Result.Partitions|partitioncounts}}
32+
{{- if ne $v 0}}\n\t{{$k}}={{$v}}{{end}}
33+
{{- end}}
34+
**TopicsByStatus:**
35+
{{- range $k,$v := .Result.Partitions|topicsbystatus}}
36+
\t{{$k}}={{$v}}
37+
{{- end}}
38+
**PartitionDetails:**
39+
{{- range .Result.Partitions}}
40+
{{.Topic}}[{{.Status.String}}](){{printf "%.2f" .Complete}}
41+
{{- if .Owner}}\n\tConsumerHost={{.Owner}}{{end}}
42+
{{- if .ClientID}}\n\tConsumerClientID={{.ClientID}}{{end}}
43+
\tCurrentLag={{.CurrentLag}}
44+
\tPartition={{.Partition}}
45+
\tStart={{formattimestamp .Start.Timestamp $FormatString}}
46+
\t\tOffset={{.Start.Offset}}\tLag={{.Start.Lag.Value}}
47+
\tEnd={{formattimestamp .End.Timestamp $FormatString}}
48+
\t\tOffset={{.End.Offset}}\tLag={{.End.Lag.Value}}
49+
{{- end}}
50+
{{- end}}
51+
"
52+
}}

core/internal/helpers/coordinators.go

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ func (m *MockModule) GetName() string {
7474
return args.String(0)
7575
}
7676

77+
// GetCluster mocks the notifier.Module GetCluster func
78+
func (m *MockModule) GetCluster() string {
79+
args := m.Called()
80+
return args.String(0)
81+
}
82+
7783
// GetGroupAllowlist mocks the notifier.Module GetGroupAllowlist func
7884
func (m *MockModule) GetGroupAllowlist() *regexp.Regexp {
7985
args := m.Called()

core/internal/httpserver/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ func (hc *Coordinator) configNotifierHTTP(w http.ResponseWriter, r *http.Request
213213
SendClose: viper.GetBool(configRoot + ".send-close"),
214214
ExtraCa: viper.GetString(configRoot + ".extra-ca"),
215215
NoVerify: viper.GetString(configRoot + ".noverify"),
216+
Cluster: viper.GetString(configRoot + ".cluster"),
216217
},
217218
Request: requestInfo,
218219
})
@@ -265,6 +266,7 @@ func (hc *Coordinator) configNotifierEmail(w http.ResponseWriter, r *http.Reques
265266
To: viper.GetString(configRoot + ".to"),
266267
ExtraCa: viper.GetString(configRoot + ".extra-ca"),
267268
NoVerify: viper.GetString(configRoot + ".noverify"),
269+
Cluster: viper.GetString(configRoot + ".cluster"),
268270
},
269271
Request: requestInfo,
270272
})

core/internal/httpserver/structs.go

+2
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ type httpResponseConfigModuleNotifierHTTP struct {
202202
SendClose bool `json:"send-close"`
203203
ExtraCa string `json:"extra-ca"`
204204
NoVerify string `json:"noverify"`
205+
Cluster string `json:"cluster"`
205206
}
206207

207208
type httpResponseConfigModuleNotifierSlack struct {
@@ -238,6 +239,7 @@ type httpResponseConfigModuleNotifierEmail struct {
238239
To string `json:"to"`
239240
ExtraCa string `json:"extra-ca"`
240241
NoVerify string `json:"noverify"`
242+
Cluster string `json:"cluster"`
241243
}
242244

243245
type httpResponseConfigModuleNotifierNull struct {

core/internal/notifier/coordinator.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
type Module interface {
4949
protocol.Module
5050
GetName() string
51+
GetCluster() string
5152
GetGroupAllowlist() *regexp.Regexp
5253
GetGroupDenylist() *regexp.Regexp
5354
GetLogger() *zap.Logger
@@ -95,7 +96,7 @@ type Coordinator struct {
9596

9697
// getModuleForClass returns the correct module based on the passed className. As part of the Configure steps, if there
9798
// is any error, it will panic with an appropriate message describing the problem.
98-
func getModuleForClass(app *protocol.ApplicationContext, moduleName, className string, groupAllowlist, groupDenylist *regexp.Regexp, extras map[string]string, templateOpen, templateClose *template.Template) protocol.Module {
99+
func getModuleForClass(app *protocol.ApplicationContext, moduleName, className string, groupAllowlist, groupDenylist *regexp.Regexp, extras map[string]string, templateOpen, templateClose *template.Template, cluster string) protocol.Module {
99100
logger := app.Logger.With(
100101
zap.String("type", "module"),
101102
zap.String("coordinator", "notifier"),
@@ -113,6 +114,7 @@ func getModuleForClass(app *protocol.ApplicationContext, moduleName, className s
113114
extras: extras,
114115
templateOpen: templateOpen,
115116
templateClose: templateClose,
117+
cluster: cluster,
116118
}
117119
case "email":
118120
return &EmailNotifier{
@@ -123,6 +125,7 @@ func getModuleForClass(app *protocol.ApplicationContext, moduleName, className s
123125
extras: extras,
124126
templateOpen: templateOpen,
125127
templateClose: templateClose,
128+
cluster: cluster,
126129
}
127130
case "null":
128131
return &NullNotifier{
@@ -133,6 +136,7 @@ func getModuleForClass(app *protocol.ApplicationContext, moduleName, className s
133136
extras: extras,
134137
templateOpen: templateOpen,
135138
templateClose: templateClose,
139+
cluster: cluster,
136140
}
137141
default:
138142
panic("Unknown notifier className provided: " + className)
@@ -194,6 +198,8 @@ func (nc *Coordinator) Configure() {
194198
groupAllowlist = re
195199
}
196200

201+
cluster := viper.GetString(configRoot + ".cluster")
202+
197203
// Compile the denylist for the consumer groups to not notify for
198204
var groupDenylist *regexp.Regexp
199205
denylist := viper.GetString(configRoot + ".group-denylist")
@@ -227,7 +233,7 @@ func (nc *Coordinator) Configure() {
227233
templateClose = tmpl.Templates()[0]
228234
}
229235

230-
module := getModuleForClass(nc.App, name, viper.GetString(configRoot+".class-name"), groupAllowlist, groupDenylist, extras, templateOpen, templateClose)
236+
module := getModuleForClass(nc.App, name, viper.GetString(configRoot+".class-name"), groupAllowlist, groupDenylist, extras, templateOpen, templateClose, cluster)
231237
module.Configure(name, configRoot)
232238
nc.modules[name] = module
233239
interval := viper.GetInt64(configRoot + ".interval")
@@ -436,6 +442,9 @@ func (nc *Coordinator) checkAndSendResponseToModules(response *protocol.Consumer
436442
for _, genericModule := range nc.modules {
437443
module := genericModule.(Module)
438444

445+
if module.GetCluster() != "" && response.Cluster != module.GetCluster() {
446+
continue
447+
}
439448
// No allowlist means everything passes
440449
groupAllowlist := module.GetGroupAllowlist()
441450
groupDenylist := module.GetGroupDenylist()

core/internal/notifier/coordinator_test.go

+31-21
Original file line numberDiff line numberDiff line change
@@ -472,34 +472,39 @@ var notifyModuleTests = []struct {
472472
ExpectClose bool
473473
ExpectID bool
474474
SendOnce bool
475+
Cluster string
475476
}{
476477
// {1, 0, false, false, false, false, false},
477478
// {2, 0, false, false, false, false, false},
478479
// {1, 0, true, false, false, false, false},
479480
// {1, 0, false, true, false, false, false},
480481
// {1, 0, true, true, false, false, false},
481482

482-
{1, 1, false, false, true, false, false, false},
483-
{1, 1, false, true, true, false, false, false},
484-
{1, 1, true, false, true, false, false, false},
485-
{1, 1, true, true, true, true, false, true},
486-
487-
{1, 2, false, false, true, false, true, false},
488-
{1, 2, false, true, true, false, true, false},
489-
{1, 2, true, false, true, false, true, false},
490-
{1, 2, true, true, true, false, true, false},
491-
{1, 2, true, true, false, false, true, true},
492-
{1, 2, false, true, true, false, true, true},
493-
494-
{3, 2, false, false, false, false, true, false},
495-
{3, 2, false, true, false, false, true, false},
496-
{3, 2, true, false, false, false, true, false},
497-
{3, 2, true, true, false, false, true, false},
498-
499-
{2, 1, false, false, false, false, false, false},
500-
{2, 1, false, true, false, false, false, false},
501-
{2, 1, true, false, false, false, false, false},
502-
{2, 1, true, true, true, true, false, false},
483+
{1, 1, false, false, true, false, false, false, ""},
484+
{1, 1, false, true, true, false, false, false, "testcluster"},
485+
{1, 1, true, false, true, false, false, false, "unmatchedCluster"},
486+
{1, 1, true, true, true, true, false, true, ""},
487+
488+
{1, 2, false, false, true, false, true, false, ""},
489+
{1, 2, false, true, true, false, true, false, ""},
490+
{1, 2, true, false, true, false, true, false, ""},
491+
{1, 2, true, true, true, false, true, false, ""},
492+
{1, 2, true, true, false, false, true, true, ""},
493+
{1, 2, false, true, true, false, true, true, ""},
494+
495+
{3, 2, false, false, false, false, true, false, ""},
496+
{3, 2, false, true, false, false, true, false, ""},
497+
{3, 2, true, false, false, false, true, false, ""},
498+
{3, 2, true, true, false, false, true, false, ""},
499+
500+
{2, 1, false, false, false, false, false, false, ""},
501+
{2, 1, false, true, false, false, false, false, ""},
502+
{2, 1, true, false, false, false, false, false, ""},
503+
{2, 1, true, true, true, true, false, false, ""},
504+
}
505+
506+
func checkNotifierClusterMatch(cluster string) bool {
507+
return cluster == "" || cluster == "testcluster"
503508
}
504509

505510
func TestCoordinator_checkAndSendResponseToModules(t *testing.T) {
@@ -558,10 +563,15 @@ func TestCoordinator_checkAndSendResponseToModules(t *testing.T) {
558563
// Set up the mock module and expected calls
559564
mockModule := &helpers.MockModule{}
560565
coordinator.modules["test"] = mockModule
566+
mockModule.On("GetCluster").Return(testSet.Cluster)
567+
568+
if checkNotifierClusterMatch(testSet.Cluster) {
561569
mockModule.On("GetName").Return("test")
562570
mockModule.On("GetGroupAllowlist").Return((*regexp.Regexp)(nil))
563571
mockModule.On("GetGroupDenylist").Return((*regexp.Regexp)(nil))
564572
mockModule.On("AcceptConsumerGroup", response).Return(true)
573+
}
574+
565575
if testSet.ExpectSend {
566576
mockModule.On("Notify", response, mock.MatchedBy(func(s string) bool { return true }), mock.MatchedBy(func(t time.Time) bool { return true }), testSet.ExpectClose).Return()
567577
}

core/internal/notifier/email.go

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type EmailNotifier struct {
3838
Log *zap.Logger
3939

4040
name string
41+
cluster string
4142
groupAllowlist *regexp.Regexp
4243
groupDenylist *regexp.Regexp
4344
extras map[string]string
@@ -139,6 +140,11 @@ func (module *EmailNotifier) GetName() string {
139140
return module.name
140141
}
141142

143+
// GetCluster returns the configured name of this module
144+
func (module *EmailNotifier) GetCluster() string {
145+
return module.cluster
146+
}
147+
142148
// GetGroupAllowlist returns the compiled group allowlist (or nil, if there is not one)
143149
func (module *EmailNotifier) GetGroupAllowlist() *regexp.Regexp {
144150
return module.groupAllowlist

core/internal/notifier/helpers.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,9 @@ func maxLagHelper(a *protocol.PartitionStatus) uint64 {
163163
}
164164

165165
func formatTimestamp(timestamp int64, formatString string) string {
166-
return time.Unix(0, timestamp*int64(time.Millisecond)).Format(formatString)
166+
if timestamp > 0 {
167+
return time.Unix(0, timestamp*int64(time.Millisecond)).Format(formatString)
168+
} else {
169+
return time.Now().Format(formatString)
170+
}
167171
}

0 commit comments

Comments
 (0)