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 all commits
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
|
||
"github.com/cloudflare/unsee/internal/alertmanager" | ||
"github.com/cloudflare/unsee/internal/config" | ||
"github.com/gin-gonic/gin" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func proxyPathPrefix(name string) string { | ||
return fmt.Sprintf("%sproxy/alertmanager/%s", config.Config.Listen.Prefix, name) | ||
} | ||
|
||
func proxyPath(name, path string) string { | ||
return fmt.Sprintf("%s%s", proxyPathPrefix(name), path) | ||
} | ||
|
||
// NewAlertmanagerProxy creates a proxy instance for given alertmanager instance | ||
func NewAlertmanagerProxy(alertmanager *alertmanager.Alertmanager) (*httputil.ReverseProxy, error) { | ||
upstreamURL, err := url.Parse(alertmanager.URI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
proxy := httputil.ReverseProxy{ | ||
Director: func(req *http.Request) { | ||
req.URL.Scheme = upstreamURL.Scheme | ||
req.URL.Host = upstreamURL.Host | ||
// drop Accept-Encoding header so we always get uncompressed reponses from | ||
// upstream, there's a gzip middleware that's global so we don't want it | ||
// to gzip twice | ||
req.Header.Del("Accept-Encoding") | ||
log.Debugf("[%s] Proxy request for %s", alertmanager.Name, req.URL.Path) | ||
}, | ||
ModifyResponse: func(resp *http.Response) error { | ||
// drop Content-Length header from upstream responses, gzip middleware | ||
// will compress those and that could cause a mismatch | ||
resp.Header.Del("Content-Length") | ||
return nil | ||
}, | ||
} | ||
return &proxy, nil | ||
} | ||
|
||
func setupRouterProxyHandlers(router *gin.Engine, alertmanager *alertmanager.Alertmanager) error { | ||
proxy, err := NewAlertmanagerProxy(alertmanager) | ||
if err != nil { | ||
return err | ||
} | ||
router.POST( | ||
proxyPath(alertmanager.Name, "/api/v1/silences"), | ||
gin.WrapH(http.StripPrefix(proxyPathPrefix(alertmanager.Name), proxy))) | ||
router.DELETE( | ||
proxyPath(alertmanager.Name, "/api/v1/silence/*id"), | ||
gin.WrapH(http.StripPrefix(proxyPathPrefix(alertmanager.Name), proxy))) | ||
return nil | ||
} |
Oops, something went wrong.
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.
It is possible to accept a logger?
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.
I don't see how that would be useful right now
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.
Mostly useful for configuring the logging for proxy specifically. But mostly a nit.