-
Notifications
You must be signed in to change notification settings - Fork 4
/
probe.go
55 lines (43 loc) · 1.27 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package fxpubsub
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/pubsub"
"github.com/ekkinox/fx-template/modules/fxconfig"
"github.com/ekkinox/fx-template/modules/fxhealthchecker"
"github.com/ekkinox/fx-template/modules/fxlogger"
)
type PubSubProbe struct {
config *fxconfig.Config
client *pubsub.Client
logger *fxlogger.Logger
}
func NewPubSubProbe(config *fxconfig.Config, client *pubsub.Client, logger *fxlogger.Logger) *PubSubProbe {
return &PubSubProbe{
config: config,
client: client,
logger: logger,
}
}
func (p *PubSubProbe) Name() string {
return "pubsub"
}
func (p *PubSubProbe) Check(ctx context.Context) *fxhealthchecker.HealthCheckerProbeResult {
success := true
var messages []string
for _, topicName := range p.config.GetStringMapString("pubsub.topics") {
topic := p.client.Topic(topicName)
exist, err := topic.Exists(ctx)
if err != nil {
p.logger.Error().Err(err).Msgf("failed to check if topic %s exists", topicName)
}
if exist {
messages = append(messages, fmt.Sprintf("topic %s exists", topicName))
} else {
messages = append(messages, fmt.Sprintf("topic does not %s exist", topicName))
}
success = success && exist
}
return fxhealthchecker.NewHealthCheckerProbeResult(success, strings.Join(messages, ", "))
}