-
-
Save terinjokes/18c666ec4a52cd5bd10df29b4e6f95a6 to your computer and use it in GitHub Desktop.
Alertmanager with functional options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "alertmanager" | |
func main() { | |
dur, _ := time.ParseDuration("1m") | |
// without a proxy: | |
alertmanager.NewAlertmanager("prod", "https://alertmanager", dur) | |
// or | |
alertmanager.NewAlertmanager("prod", "https://alertmanager", dur, alertmanager.WithProxy(false)) | |
// with a proxy: | |
alertmanager.NewAlertmanager("local", "http://localhost:3411", dur, alertmanager.WithProxy(true)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package alertmanager | |
type Alertmanager struct {} | |
type Option function(am *Alertmanager) {} | |
var ( | |
upstreams = map[string]*Alertmanager{} | |
) | |
// NewAlertmanager creates a new Alertmanager instance, unsee clients will talk | |
// to directly to it without unsee proxying any request | |
func NewAlertmanager(name, uri string, timeout time.Duration, opts ...Option) error { | |
if _, found := upstreams[name]; found { | |
return fmt.Errorf("Alertmanager upstream '%s' already exist", name) | |
} | |
for _, am := range upstreams { | |
if am.URI == uri { | |
return fmt.Errorf("Alertmanager upstream '%s' already collects from '%s'", am.Name, am.URI) | |
} | |
} | |
am := &Alertmanager{ | |
URI: uri, | |
Timeout: timeout, | |
Name: name, | |
ProxyRequests: false, | |
lock: sync.RWMutex{}, | |
alertGroups: []models.AlertGroup{}, | |
silences: map[string]models.Silence{}, | |
colors: models.LabelsColorMap{}, | |
autocomplete: []models.Autocomplete{}, | |
metrics: alertmanagerMetrics{ | |
errors: map[string]float64{ | |
labelValueErrorsAlerts: 0, | |
labelValueErrorsSilences: 0, | |
}, | |
}, | |
} | |
for _, opt := range opts { | |
opt(am) | |
} | |
upstreams[name] = am | |
return nil | |
} | |
func WithProxy(proxied bool) Option { | |
return func(am *Alertmanager) { | |
am.ProxyRequests := proxied | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment