Created
December 5, 2018 02:55
-
-
Save caitong93/695e63cf37e9061520b559e7c2c07919 to your computer and use it in GitHub Desktop.
Custom transport for prometheus client golang
This file contains hidden or 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
| var defaultTransport = &http.Transport{ | |
| Proxy: http.ProxyFromEnvironment, | |
| Dial: (&net.Dialer{ | |
| Timeout: 30 * time.Second, | |
| KeepAlive: 30 * time.Second, | |
| }).Dial, | |
| TLSHandshakeTimeout: 10 * time.Second, | |
| TLSClientConfig: &tls.Config{ | |
| InsecureSkipVerify: true, | |
| }, | |
| } | |
| type AuthTransport struct { | |
| *http.Transport | |
| Token string | |
| Username string | |
| Password string | |
| } | |
| func NewAuthTransport(username, password, token string) *AuthTransport { | |
| return &AuthTransport{ | |
| Transport: defaultTransport, | |
| Token: token, | |
| Username: username, | |
| Password: password, | |
| } | |
| } | |
| func (t *AuthTransport) RoundTrip(req *http.Request) (*http.Response, error) { | |
| if len(t.Token) != 0 { | |
| token := oauth2.Token{ | |
| TokenType: "Bearer", | |
| AccessToken: t.Token, | |
| } | |
| token.SetAuthHeader(req) | |
| } else { | |
| req.SetBasicAuth(t.Username, t.Password) | |
| } | |
| return t.Transport.RoundTrip(req) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment