Add vendor directory

This commit is contained in:
sm
2018-03-08 13:58:15 -08:00
parent 2dc8df887a
commit 1938841b90
115 changed files with 26574 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
MIT License
Copyright (c) 2018 Stephen McQuay
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+3
View File
@@ -0,0 +1,3 @@
# metrics
A library that serves as an example of how to wrap http.Handler*
+101
View File
@@ -0,0 +1,101 @@
package metrics
import (
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
// Metrics provides a simple way to track latency and http status
type Metrics struct {
latency *prometheus.SummaryVec
status *prometheus.CounterVec
}
func New(prefix string) (*Metrics, error) {
m := &Metrics{
latency: prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: prefix,
Subsystem: "http",
Name: "request_latency_ms",
Help: "Latency in ms of http requests grouped by req path",
}, []string{"path"}),
status: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: prefix,
Subsystem: "http",
Name: "status_count",
Help: "The count of http responses issued classified by status and api endpoint",
}, []string{"path", "code"}),
}
if err := m.registerPromMetrics(); err != nil {
return nil, errors.Wrap(err, "registration")
}
return m, nil
}
// registerPromMetrics registers all the metrics that eventmanger uses.
func (m *Metrics) registerPromMetrics() error {
if err := prometheus.Register(m.latency); err != nil {
return errors.Wrap(err, "http request latency")
}
if err := prometheus.Register(m.status); err != nil {
return errors.Wrap(err, "http response counter")
}
return nil
}
// Wrap calls a http.Handler and tracks status code and latency.
func (m *Metrics) Wrap(prefix string, h http.Handler) http.HandlerFunc {
return m.WrapFunc(prefix, h.ServeHTTP)
}
// WrapFunc calls a http.Handler and tracks status code and latency.
func (m *Metrics) WrapFunc(prefix string, h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
defer func() {
m.HTTPLatency(prefix, start)
}()
lw := NewStatusRecorder(w)
h(lw, req)
m.HTTPStatus(prefix, lw.Status())
}
}
// HTTPLatency records a request latency for a given url path.
func (m *Metrics) HTTPLatency(path string, start time.Time) {
m.latency.WithLabelValues(path).Observe(msSince(start))
}
// HTTPStatus records counts of http statuses, bucketed according to status types.
func (m *Metrics) HTTPStatus(path string, status int) {
m.status.WithLabelValues(path, fmt.Sprintf("%d", bucketHTTPStatus(status))).Inc()
}
// bucketHTTPStatus rounds down to the nearest hundred to facilitate categorizing http statuses.
func bucketHTTPStatus(i int) int {
return i - i%100
}
// msSince returns milliseconds since start.
func msSince(start time.Time) float64 {
return float64(time.Since(start)) / float64(time.Millisecond)
}
// buckets returns the default prometheus buckets scaled to milliseconds.
func buckets() []float64 {
r := []float64{}
for _, v := range prometheus.DefBuckets {
r = append(r, v*float64(time.Second/time.Millisecond))
}
return r
}
+29
View File
@@ -0,0 +1,29 @@
package metrics
import (
"net/http"
)
// StatusRecorder is a simple http status recorder
type StatusRecorder struct {
http.ResponseWriter
status int
}
// NewStatusRecorder returns an initialized StatusRecorder, with 200 as the
// default status.
func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder {
return &StatusRecorder{ResponseWriter: w, status: http.StatusOK}
}
// Status returns the cached http status value.
func (sr *StatusRecorder) Status() int {
return sr.status
}
// WriteHeader caches the status, then calls the underlying ResponseWriter.
func (sr *StatusRecorder) WriteHeader(status int) {
sr.status = status
sr.ResponseWriter.WriteHeader(status)
}