-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
60 lines (50 loc) · 1.67 KB
/
topic.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
56
57
58
59
60
package healthcheck
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/pubsub"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/healthcheck"
)
// TopicsProbeName is the name of the GCP pub/sub topics probe.
const TopicsProbeName = "gcppubsub-topics"
// GcpPubSubTopicsProbe is a probe compatible with the [healthcheck] module.
//
// [healthcheck]: https://github.com/ankorstore/yokai/tree/main/healthcheck
type GcpPubSubTopicsProbe struct {
config *config.Config
client *pubsub.Client
}
// NewGcpPubSubTopicsProbe returns a new [GcpPubSubTopicsProbe].
func NewGcpPubSubTopicsProbe(config *config.Config, client *pubsub.Client) *GcpPubSubTopicsProbe {
return &GcpPubSubTopicsProbe{
config: config,
client: client,
}
}
// Name returns the name of the [GcpPubSubTopicsProbe].
func (p *GcpPubSubTopicsProbe) Name() string {
return TopicsProbeName
}
// Check returns a successful [healthcheck.CheckerProbeResult] if all configured topics exist.
func (p *GcpPubSubTopicsProbe) Check(ctx context.Context) *healthcheck.CheckerProbeResult {
success := true
var messages []string
for _, topicName := range p.config.GetStringSlice("modules.gcppubsub.healthcheck.topics") {
topic := p.client.Topic(topicName)
exists, err := topic.Exists(ctx)
if err != nil {
success = false
messages = append(messages, fmt.Sprintf("topic %s error: %v", topicName, err))
} else {
if !exists {
success = false
messages = append(messages, fmt.Sprintf("topic %s does not exist", topicName))
} else {
messages = append(messages, fmt.Sprintf("topic %s exists", topicName))
}
}
}
return healthcheck.NewCheckerProbeResult(success, strings.Join(messages, ", "))
}