This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Add support for proxying user connection to Alertmanager #202
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6dd993
Add support for proxying user connection to Alertmanager
prymitive 3eb28b3
Use functional options pattern when creating Alertmanager instances
prymitive db0d86f
Don't modify path in the proxy, strip prefix in the handler
prymitive 6aec8bf
Cleanup path setup a bit
prymitive 2176c44
Drop names in slices
prymitive c8b7b3b
Implement functional options, this time for real
prymitive efda91f
Rename Alertmanager.Timeout -> Alertmanager.RequestTimeout
prymitive 983c7f5
Refactor NewAlertmanager() to not register new instance by default
prymitive File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,15 @@ import ( | |
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Option allows to pass functional options to NewAlertmanager() | ||
type Option func(am *Alertmanager) | ||
|
||
var ( | ||
upstreams = map[string]*Alertmanager{} | ||
) | ||
|
||
func newAlertmanager(name, uri string, timeout time.Duration, proxyRequests bool) error { | ||
// NewAlertmanager creates a new Alertmanager instance | ||
func NewAlertmanager(name, uri string, opts ...Option) error { | ||
if _, found := upstreams[name]; found { | ||
return fmt.Errorf("Alertmanager upstream '%s' already exist", name) | ||
} | ||
|
@@ -25,16 +29,15 @@ func newAlertmanager(name, uri string, timeout time.Duration, proxyRequests bool | |
} | ||
} | ||
|
||
upstreams[name] = &Alertmanager{ | ||
URI: uri, | ||
Timeout: timeout, | ||
Name: name, | ||
ProxyRequests: proxyRequests, | ||
lock: sync.RWMutex{}, | ||
alertGroups: []models.AlertGroup{}, | ||
silences: map[string]models.Silence{}, | ||
colors: models.LabelsColorMap{}, | ||
autocomplete: []models.Autocomplete{}, | ||
am := &Alertmanager{ | ||
URI: uri, | ||
Timeout: time.Second * 10, | ||
Name: name, | ||
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, | ||
|
@@ -43,21 +46,15 @@ func newAlertmanager(name, uri string, timeout time.Duration, proxyRequests bool | |
}, | ||
} | ||
|
||
log.Infof("[%s] Configured Alertmanager source at %s", name, uri) | ||
for _, opt := range opts { | ||
opt(am) | ||
} | ||
|
||
return nil | ||
} | ||
upstreams[name] = am | ||
|
||
// 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) error { | ||
return newAlertmanager(name, uri, timeout, false) | ||
} | ||
log.Infof("[%s] Configured Alertmanager source at %s", name, uri) | ||
|
||
// NewProxiedAlertmanager creates a new proxied Alertmanager instance, unsee | ||
// clients will talk to it via unsee | ||
func NewProxiedAlertmanager(name, uri string, timeout time.Duration) error { | ||
return newAlertmanager(name, uri, timeout, false) | ||
return nil | ||
} | ||
|
||
// GetAlertmanagers returns a list of all defined Alertmanager instances | ||
|
@@ -78,3 +75,19 @@ func GetAlertmanagerByName(name string) *Alertmanager { | |
} | ||
return nil | ||
} | ||
|
||
// WithProxy option can be passed to NewAlertmanager in order to enable request | ||
// proxying for unsee clients | ||
func WithProxy(proxied bool) Option { | ||
return func(am *Alertmanager) { | ||
am.ProxyRequests = proxied | ||
} | ||
} | ||
|
||
// WithRequestTimeout option can be passed to NewAlertmanager in order to set | ||
// a custom timeout for Alertmanager upstream requests | ||
func WithRequestTimeout(timeout time.Duration) Option { | ||
return func(am *Alertmanager) { | ||
am.Timeout = timeout | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to change the struct member name as well? Any reason to set a request timeout rather than using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd argue this should be
AddAlertmanager
because while you do allocate you don't actually get the Alertmanager instance back.For testing reasons (you're creating the Alertmanger manually in test) perhaps it should, and leave it up to the consumer to add it to the upstreams slice/type. I'll accept this as a separate PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm refactoring this