created docker-prometheus compose file and edited the Prometheus yml file
This commit is contained in:
110
prom/util/httputil/client.go
Normal file
110
prom/util/httputil/client.go
Normal file
@ -0,0 +1,110 @@
|
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NewClient returns a http.Client using the specified http.RoundTripper.
|
||||
func NewClient(rt http.RoundTripper) *http.Client {
|
||||
return &http.Client{Transport: rt}
|
||||
}
|
||||
|
||||
// NewDeadlineClient returns a new http.Client which will time out long running
|
||||
// requests.
|
||||
func NewDeadlineClient(timeout time.Duration, proxyURL *url.URL) *http.Client {
|
||||
return NewClient(NewDeadlineRoundTripper(timeout, proxyURL))
|
||||
}
|
||||
|
||||
// NewDeadlineRoundTripper returns a new http.RoundTripper which will time out
|
||||
// long running requests.
|
||||
func NewDeadlineRoundTripper(timeout time.Duration, proxyURL *url.URL) http.RoundTripper {
|
||||
return &http.Transport{
|
||||
// Set proxy (if null, then becomes a direct connection)
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
// We need to disable keepalive, because we set a deadline on the
|
||||
// underlying connection.
|
||||
DisableKeepAlives: true,
|
||||
Dial: func(netw, addr string) (c net.Conn, err error) {
|
||||
start := time.Now()
|
||||
|
||||
c, err = net.DialTimeout(netw, addr, timeout)
|
||||
|
||||
if err == nil {
|
||||
c.SetDeadline(start.Add(timeout))
|
||||
}
|
||||
|
||||
return
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type bearerAuthRoundTripper struct {
|
||||
bearerToken string
|
||||
rt http.RoundTripper
|
||||
}
|
||||
|
||||
// NewBearerAuthRoundTripper adds the provided bearer token to a request unless the authorization
|
||||
// header has already been set.
|
||||
func NewBearerAuthRoundTripper(bearer string, rt http.RoundTripper) http.RoundTripper {
|
||||
return &bearerAuthRoundTripper{bearer, rt}
|
||||
}
|
||||
|
||||
func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if len(req.Header.Get("Authorization")) == 0 {
|
||||
req = cloneRequest(req)
|
||||
req.Header.Set("Authorization", "Bearer "+rt.bearerToken)
|
||||
}
|
||||
|
||||
return rt.rt.RoundTrip(req)
|
||||
}
|
||||
|
||||
type basicAuthRoundTripper struct {
|
||||
username string
|
||||
password string
|
||||
rt http.RoundTripper
|
||||
}
|
||||
|
||||
// NewBasicAuthRoundTripper will apply a BASIC auth authorization header to a request unless it has
|
||||
// already been set.
|
||||
func NewBasicAuthRoundTripper(username, password string, rt http.RoundTripper) http.RoundTripper {
|
||||
return &basicAuthRoundTripper{username, password, rt}
|
||||
}
|
||||
|
||||
func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if len(req.Header.Get("Authorization")) != 0 {
|
||||
return rt.rt.RoundTrip(req)
|
||||
}
|
||||
req = cloneRequest(req)
|
||||
req.SetBasicAuth(rt.username, rt.password)
|
||||
return rt.rt.RoundTrip(req)
|
||||
}
|
||||
|
||||
// cloneRequest returns a clone of the provided *http.Request.
|
||||
// The clone is a shallow copy of the struct and its Header map.
|
||||
func cloneRequest(r *http.Request) *http.Request {
|
||||
// Shallow copy of the struct.
|
||||
r2 := new(http.Request)
|
||||
*r2 = *r
|
||||
// Deep copy of the Header.
|
||||
r2.Header = make(http.Header)
|
||||
for k, s := range r.Header {
|
||||
r2.Header[k] = s
|
||||
}
|
||||
return r2
|
||||
}
|
92
prom/util/httputil/compression.go
Normal file
92
prom/util/httputil/compression.go
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright 2013 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"compress/zlib"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
acceptEncodingHeader = "Accept-Encoding"
|
||||
contentEncodingHeader = "Content-Encoding"
|
||||
gzipEncoding = "gzip"
|
||||
deflateEncoding = "deflate"
|
||||
)
|
||||
|
||||
// Wrapper around http.Handler which adds suitable response compression based
|
||||
// on the client's Accept-Encoding headers.
|
||||
type compressedResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
// Writes HTTP response content data.
|
||||
func (c *compressedResponseWriter) Write(p []byte) (int, error) {
|
||||
return c.writer.Write(p)
|
||||
}
|
||||
|
||||
// Closes the compressedResponseWriter and ensures to flush all data before.
|
||||
func (c *compressedResponseWriter) Close() {
|
||||
if zlibWriter, ok := c.writer.(*zlib.Writer); ok {
|
||||
zlibWriter.Flush()
|
||||
}
|
||||
if gzipWriter, ok := c.writer.(*gzip.Writer); ok {
|
||||
gzipWriter.Flush()
|
||||
}
|
||||
if closer, ok := c.writer.(io.Closer); ok {
|
||||
defer closer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Constructs a new compressedResponseWriter based on client request headers.
|
||||
func newCompressedResponseWriter(writer http.ResponseWriter, req *http.Request) *compressedResponseWriter {
|
||||
encodings := strings.Split(req.Header.Get(acceptEncodingHeader), ",")
|
||||
for _, encoding := range encodings {
|
||||
switch strings.TrimSpace(encoding) {
|
||||
case gzipEncoding:
|
||||
writer.Header().Set(contentEncodingHeader, gzipEncoding)
|
||||
return &compressedResponseWriter{
|
||||
ResponseWriter: writer,
|
||||
writer: gzip.NewWriter(writer),
|
||||
}
|
||||
case deflateEncoding:
|
||||
writer.Header().Set(contentEncodingHeader, deflateEncoding)
|
||||
return &compressedResponseWriter{
|
||||
ResponseWriter: writer,
|
||||
writer: zlib.NewWriter(writer),
|
||||
}
|
||||
}
|
||||
}
|
||||
return &compressedResponseWriter{
|
||||
ResponseWriter: writer,
|
||||
writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
// CompressionHandler is a wrapper around http.Handler which adds suitable
|
||||
// response compression based on the client's Accept-Encoding headers.
|
||||
type CompressionHandler struct {
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
// ServeHTTP adds compression to the original http.Handler's ServeHTTP() method.
|
||||
func (c CompressionHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
|
||||
compWriter := newCompressedResponseWriter(writer, req)
|
||||
c.Handler.ServeHTTP(compWriter, req)
|
||||
compWriter.Close()
|
||||
}
|
Reference in New Issue
Block a user