2013-02-12 05:36:06 +04:00
|
|
|
// Copyright (c) 2012, Matt T. Proud
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
|
|
|
// in the LICENSE file.
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2012-05-21 12:43:21 +04:00
|
|
|
package registry
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
import (
|
2013-01-19 17:48:30 +04:00
|
|
|
"compress/gzip"
|
2013-01-08 19:56:19 +04:00
|
|
|
"encoding/base64"
|
2012-05-20 01:59:25 +04:00
|
|
|
"encoding/json"
|
2013-01-19 17:48:30 +04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-01-25 20:50:41 +04:00
|
|
|
"github.com/prometheus/client_golang/metrics"
|
|
|
|
"github.com/prometheus/client_golang/utility"
|
2013-01-19 17:48:30 +04:00
|
|
|
"io"
|
2012-05-20 01:59:25 +04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-01-19 17:48:30 +04:00
|
|
|
"sort"
|
2012-05-20 01:59:25 +04:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
const (
|
2013-01-19 17:48:30 +04:00
|
|
|
acceptEncodingHeader = "Accept-Encoding"
|
|
|
|
authorization = "Authorization"
|
|
|
|
authorizationHeader = "WWW-Authenticate"
|
|
|
|
authorizationHeaderValue = "Basic"
|
|
|
|
contentEncodingHeader = "Content-Encoding"
|
|
|
|
contentTypeHeader = "Content-Type"
|
|
|
|
gzipAcceptEncodingValue = "gzip"
|
|
|
|
gzipContentEncodingValue = "gzip"
|
|
|
|
jsonContentType = "application/json"
|
|
|
|
jsonSuffix = ".json"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
abortOnMisuse bool
|
|
|
|
debugRegistration bool
|
|
|
|
useAggressiveSanityChecks bool
|
2013-02-12 05:36:06 +04:00
|
|
|
)
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
// container represents a top-level registered metric that encompasses its
|
|
|
|
// static metadata.
|
|
|
|
type container struct {
|
|
|
|
baseLabels map[string]string
|
|
|
|
docstring string
|
|
|
|
metric metrics.Metric
|
|
|
|
name string
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
type registry struct {
|
|
|
|
mutex sync.RWMutex
|
|
|
|
signatureContainers map[string]container
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registry is a registrar where metrics are listed.
|
2013-02-12 05:36:06 +04:00
|
|
|
//
|
|
|
|
// In most situations, using DefaultRegistry is sufficient versus creating one's
|
|
|
|
// own.
|
2013-02-18 08:09:21 +04:00
|
|
|
type Registry interface {
|
|
|
|
// Register a metric with a given name. Name should be globally unique.
|
|
|
|
Register(name, docstring string, baseLabels map[string]string, metric metrics.Metric) error
|
2013-02-18 19:30:47 +04:00
|
|
|
// Create a http.HandlerFunc that is tied to a Registry such that requests
|
|
|
|
// against it generate a representation of the housed metrics.
|
2013-02-18 08:09:21 +04:00
|
|
|
Handler() http.HandlerFunc
|
2013-02-18 19:30:47 +04:00
|
|
|
// This is a legacy version of Handler and is deprecated. Please stop
|
|
|
|
// using.
|
|
|
|
YieldExporter() http.HandlerFunc
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 05:36:06 +04:00
|
|
|
// This builds a new metric registry. It is not needed in the majority of
|
|
|
|
// cases.
|
2013-02-18 08:09:21 +04:00
|
|
|
func NewRegistry() Registry {
|
|
|
|
return registry{
|
2013-01-19 17:48:30 +04:00
|
|
|
signatureContainers: make(map[string]container),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 05:36:06 +04:00
|
|
|
// Associate a Metric with the DefaultRegistry.
|
2013-01-19 17:48:30 +04:00
|
|
|
func Register(name, docstring string, baseLabels map[string]string, metric metrics.Metric) error {
|
|
|
|
return DefaultRegistry.Register(name, docstring, baseLabels, metric)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidCandidate returns true if the candidate is acceptable for use. In the
|
|
|
|
// event of any apparent incorrect use it will report the problem, invalidate
|
|
|
|
// the candidate, or outright abort.
|
2013-02-18 08:09:21 +04:00
|
|
|
func (r registry) isValidCandidate(name string, baseLabels map[string]string) (signature string, err error) {
|
2013-01-19 17:48:30 +04:00
|
|
|
if len(name) == 0 {
|
|
|
|
err = fmt.Errorf("unnamed metric named with baseLabels %s is invalid", baseLabels)
|
|
|
|
|
|
|
|
if abortOnMisuse {
|
|
|
|
panic(err)
|
|
|
|
} else if debugRegistration {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, contains := baseLabels[nameLabel]; contains {
|
|
|
|
err = fmt.Errorf("metric named %s with baseLabels %s contains reserved label name %s in baseLabels", name, baseLabels, nameLabel)
|
|
|
|
|
|
|
|
if abortOnMisuse {
|
|
|
|
panic(err)
|
|
|
|
} else if debugRegistration {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
baseLabels[nameLabel] = name
|
|
|
|
signature = utility.LabelsToSignature(baseLabels)
|
|
|
|
|
|
|
|
if _, contains := r.signatureContainers[signature]; contains {
|
|
|
|
err = fmt.Errorf("metric named %s with baseLabels %s is already registered", name, baseLabels)
|
|
|
|
if abortOnMisuse {
|
|
|
|
panic(err)
|
|
|
|
} else if debugRegistration {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if useAggressiveSanityChecks {
|
|
|
|
for _, container := range r.signatureContainers {
|
|
|
|
if container.name == name {
|
|
|
|
err = fmt.Errorf("metric named %s with baseLabels %s is already registered as %s and risks causing confusion", name, baseLabels, container.baseLabels)
|
|
|
|
if abortOnMisuse {
|
|
|
|
panic(err)
|
|
|
|
} else if debugRegistration {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
func (r registry) Register(name, docstring string, baseLabels map[string]string, metric metrics.Metric) (err error) {
|
2012-05-20 01:59:25 +04:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
if baseLabels == nil {
|
|
|
|
baseLabels = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := r.isValidCandidate(name, baseLabels)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.signatureContainers[signature] = container{
|
|
|
|
baseLabels: baseLabels,
|
|
|
|
docstring: docstring,
|
|
|
|
metric: metric,
|
|
|
|
name: name,
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
return
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
// YieldBasicAuthExporter creates a http.HandlerFunc that is protected by HTTP's
|
|
|
|
// basic authentication.
|
2013-02-18 08:09:21 +04:00
|
|
|
func (register registry) YieldBasicAuthExporter(username, password string) http.HandlerFunc {
|
2013-02-12 05:36:06 +04:00
|
|
|
// XXX: Work with Daniel to get this removed from the library, as it is really
|
|
|
|
// superfluous and can be much more elegantly accomplished via
|
|
|
|
// delegation.
|
2013-01-08 19:56:19 +04:00
|
|
|
exporter := register.YieldExporter()
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
authenticated := false
|
|
|
|
|
2013-01-09 15:47:33 +04:00
|
|
|
if auth := r.Header.Get(authorization); auth != "" {
|
2013-01-08 19:56:19 +04:00
|
|
|
base64Encoded := strings.SplitAfter(auth, " ")[1]
|
|
|
|
decoded, err := base64.URLEncoding.DecodeString(base64Encoded)
|
|
|
|
if err == nil {
|
|
|
|
usernamePassword := strings.Split(string(decoded), ":")
|
|
|
|
if usernamePassword[0] == username && usernamePassword[1] == password {
|
|
|
|
authenticated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if authenticated {
|
|
|
|
exporter.ServeHTTP(w, r)
|
|
|
|
} else {
|
2013-01-19 17:48:30 +04:00
|
|
|
w.Header().Add(authorizationHeader, authorizationHeaderValue)
|
2013-01-09 16:59:50 +04:00
|
|
|
http.Error(w, "access forbidden", 401)
|
2013-01-08 19:56:19 +04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
func (registry registry) dumpToWriter(writer io.Writer) (err error) {
|
2013-01-19 17:48:30 +04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
dumpErrorCount.Increment(nil)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
numberOfMetrics := len(registry.signatureContainers)
|
|
|
|
keys := make([]string, 0, numberOfMetrics)
|
|
|
|
for key := range registry.signatureContainers {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
_, err = writer.Write([]byte("["))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
container := registry.signatureContainers[key]
|
|
|
|
intermediate := map[string]interface{}{
|
|
|
|
baseLabelsKey: container.baseLabels,
|
|
|
|
docstringKey: container.docstring,
|
|
|
|
metricKey: container.metric.AsMarshallable(),
|
|
|
|
}
|
|
|
|
marshaled, err := json.Marshal(intermediate)
|
|
|
|
if err != nil {
|
|
|
|
marshalErrorCount.Increment(nil)
|
|
|
|
index++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if index > 0 && index < numberOfMetrics {
|
|
|
|
_, err = writer.Write([]byte(","))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = writer.Write(marshaled)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = writer.Write([]byte("]"))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decorateWriter annotates the response writer to handle any other behaviors
|
|
|
|
// that might be beneficial to the client---e.g., GZIP encoding.
|
|
|
|
func decorateWriter(request *http.Request, writer http.ResponseWriter) io.Writer {
|
|
|
|
if !strings.Contains(request.Header.Get(acceptEncodingHeader), gzipAcceptEncodingValue) {
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set(contentEncodingHeader, gzipContentEncodingValue)
|
|
|
|
gziper := gzip.NewWriter(writer)
|
|
|
|
|
|
|
|
return gziper
|
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
func (registry registry) YieldExporter() http.HandlerFunc {
|
2013-02-12 05:36:06 +04:00
|
|
|
log.Println("Registry.YieldExporter is deprecated in favor of Registry.Handler.")
|
|
|
|
|
|
|
|
return registry.Handler()
|
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
func (registry registry) Handler() http.HandlerFunc {
|
2012-05-21 12:23:22 +04:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var instrumentable metrics.InstrumentableCall = func() {
|
2013-01-19 17:48:30 +04:00
|
|
|
requestCount.Increment(nil)
|
2012-05-21 12:23:22 +04:00
|
|
|
url := r.URL
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
if strings.HasSuffix(url.Path, jsonSuffix) {
|
2013-01-19 17:48:30 +04:00
|
|
|
header := w.Header()
|
|
|
|
header.Set(ProtocolVersionHeader, APIVersion)
|
|
|
|
header.Set(contentTypeHeader, jsonContentType)
|
|
|
|
|
|
|
|
writer := decorateWriter(r, w)
|
|
|
|
|
|
|
|
if closer, ok := writer.(io.Closer); ok {
|
|
|
|
defer closer.Close()
|
2012-05-21 12:23:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
registry.dumpToWriter(writer)
|
2012-05-21 12:23:22 +04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
|
|
|
}
|
2012-05-21 12:23:22 +04:00
|
|
|
metrics.InstrumentCall(instrumentable, requestLatencyAccumulator)
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&abortOnMisuse, FlagNamespace+"abortonmisuse", false, "abort if a semantic misuse is encountered (bool).")
|
|
|
|
flag.BoolVar(&debugRegistration, FlagNamespace+"debugregistration", false, "display information about the metric registration process (bool).")
|
|
|
|
flag.BoolVar(&useAggressiveSanityChecks, FlagNamespace+"useaggressivesanitychecks", false, "perform expensive validation of metrics (bool).")
|
|
|
|
}
|