Skip to content

Commit

Permalink
feat: make threaded replies optional
Browse files Browse the repository at this point in the history
fixes #74
  • Loading branch information
mr-karan committed Mar 13, 2024
1 parent 7174e47 commit a873437
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
21 changes: 11 additions & 10 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ func initProviders(ko *koanf.Koanf, lo *slog.Logger, metrics *metrics.Manager) (
case "google_chat":
gchat, err := google_chat.NewGoogleChat(
google_chat.GoogleChatOpts{
Log: lo,
Timeout: ko.MustDuration(fmt.Sprintf("%s.timeout", cfgKey)),
MaxIdleConn: ko.MustInt(fmt.Sprintf("%s.max_idle_conns", cfgKey)),
ProxyURL: ko.String(fmt.Sprintf("%s.proxy_url", cfgKey)),
Endpoint: ko.MustString(fmt.Sprintf("%s.endpoint", cfgKey)),
Room: name,
Template: ko.MustString(fmt.Sprintf("%s.template", cfgKey)),
ThreadTTL: ko.MustDuration(fmt.Sprintf("%s.thread_ttl", cfgKey)),
Metrics: metrics,
DryRun: ko.Bool(fmt.Sprintf("%s.dry_run", cfgKey)),
Log: lo,
Timeout: ko.MustDuration(fmt.Sprintf("%s.timeout", cfgKey)),
MaxIdleConn: ko.MustInt(fmt.Sprintf("%s.max_idle_conns", cfgKey)),
ProxyURL: ko.String(fmt.Sprintf("%s.proxy_url", cfgKey)),
Endpoint: ko.MustString(fmt.Sprintf("%s.endpoint", cfgKey)),
Room: name,
Template: ko.MustString(fmt.Sprintf("%s.template", cfgKey)),
ThreadTTL: ko.MustDuration(fmt.Sprintf("%s.thread_ttl", cfgKey)),
ThreadedReplies: ko.Bool(fmt.Sprintf("%s.threaded_replies", cfgKey)),
Metrics: metrics,
DryRun: ko.Bool(fmt.Sprintf("%s.dry_run", cfgKey)),
},
)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ timeout = "30s" # Timeout for making requests to Provider.
# proxy_url = "http://internal-squid-proxy.com:3128" # Specify `proxy_url` as your proxy endpoint to route all HTTP requests to the provider via a proxy.
template = "static/message.tmpl" # Path to specify the message template path.
thread_ttl = "12h" # Timeout to keep active alerts in memory. Once this TTL expires, a new thread will be created.
threaded_replies = true # Whether to send threaded replies or not.
dry_run = false

[providers.dev_alerts]
Expand All @@ -22,4 +23,5 @@ timeout = "30s"
# proxy_url = "http://internal-squid-proxy.com:3128"
template = "static/message.tmpl"
thread_ttl = "12h"
threaded_replies = false # Whether to send threaded replies or not.
dry_run = false
43 changes: 23 additions & 20 deletions internal/providers/google_chat/google_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ import (
)

type GoogleChatManager struct {
lo *slog.Logger
metrics *metrics.Manager
activeAlerts *ActiveAlerts
endpoint string
room string
client *http.Client
msgTmpl *template.Template
dryRun bool
lo *slog.Logger
metrics *metrics.Manager
activeAlerts *ActiveAlerts
endpoint string
room string
client *http.Client
msgTmpl *template.Template
dryRun bool
threadedReplies bool
}

type GoogleChatOpts struct {
Log *slog.Logger
Metrics *metrics.Manager
DryRun bool
MaxIdleConn int
Timeout time.Duration
ProxyURL string
Endpoint string
Room string
Template string
ThreadTTL time.Duration
Log *slog.Logger
Metrics *metrics.Manager
DryRun bool
MaxIdleConn int
Timeout time.Duration
ProxyURL string
Endpoint string
Room string
Template string
ThreadTTL time.Duration
ThreadedReplies bool
}

// NewGoogleChat initializes a Google Chat provider object.
Expand Down Expand Up @@ -92,8 +94,9 @@ func NewGoogleChat(opts GoogleChatOpts) (*GoogleChatManager, error) {
lo: opts.Log,
metrics: opts.Metrics,
},
msgTmpl: tmpl,
dryRun: opts.DryRun,
msgTmpl: tmpl,
dryRun: opts.DryRun,
threadedReplies: opts.ThreadedReplies,
}
// Start a background worker to cleanup alerts based on TTL mechanism.
go mgr.activeAlerts.startPruneWorker(1*time.Hour, opts.ThreadTTL)
Expand Down
9 changes: 7 additions & 2 deletions internal/providers/google_chat/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func (m *GoogleChatManager) sendMessage(msg ChatMessage, threadKey string) error
return err
}
q := u.Query()
q.Set("threadKey", threadKey)
q.Set("messageReplyOption", "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD")
// Default behaviour is to start a new thread for every alert.
q.Set("messageReplyOption", "MESSAGE_REPLY_OPTION_UNSPECIFIED")
if m.threadedReplies {
// If threaded replies are enabled, use the threadKey to reply to the same thread.
q.Set("messageReplyOption", "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD")
q.Set("threadKey", threadKey)
}
u.RawQuery = q.Encode()
endpoint := u.String()

Expand Down

0 comments on commit a873437

Please sign in to comment.