2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-02-12 05:36:06 +04:00
|
|
|
// 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
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
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"
|
|
|
|
"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"
|
2013-04-19 16:44:15 +04:00
|
|
|
"time"
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
|
|
|
"code.google.com/p/goprotobuf/proto"
|
|
|
|
"github.com/matttproud/golang_protobuf_extensions/ext"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/vendor/goautoneg"
|
2012-05-20 01:59:25 +04:00
|
|
|
)
|
|
|
|
|
2012-05-24 22:02:44 +04:00
|
|
|
const (
|
2013-01-19 17:48:30 +04:00
|
|
|
authorization = "Authorization"
|
|
|
|
authorizationHeader = "WWW-Authenticate"
|
|
|
|
authorizationHeaderValue = "Basic"
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
|
|
acceptEncodingHeader = "Accept-Encoding"
|
2013-01-19 17:48:30 +04:00
|
|
|
contentEncodingHeader = "Content-Encoding"
|
|
|
|
contentTypeHeader = "Content-Type"
|
|
|
|
gzipAcceptEncodingValue = "gzip"
|
|
|
|
gzipContentEncodingValue = "gzip"
|
|
|
|
jsonContentType = "application/json"
|
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 {
|
2013-04-19 16:11:01 +04:00
|
|
|
BaseLabels map[string]string `json:"baseLabels"`
|
|
|
|
Docstring string `json:"docstring"`
|
|
|
|
Metric Metric `json:"metric"`
|
2013-01-19 17:48:30 +04:00
|
|
|
name string
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
type containers []*container
|
|
|
|
|
|
|
|
func (c containers) Len() int {
|
|
|
|
return len(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c containers) Swap(i, j int) {
|
|
|
|
c[i], c[j] = c[j], c[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c containers) Less(i, j int) bool {
|
|
|
|
return c[i].name < c[j].name
|
|
|
|
}
|
|
|
|
|
2013-02-18 08:09:21 +04:00
|
|
|
type registry struct {
|
|
|
|
mutex sync.RWMutex
|
2013-06-27 20:46:16 +04:00
|
|
|
signatureContainers map[uint64]*container
|
2013-02-18 08:09:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2013-04-03 20:33:32 +04:00
|
|
|
Register(name, docstring string, baseLabels map[string]string, metric 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 {
|
2013-06-27 20:46:16 +04:00
|
|
|
return ®istry{
|
|
|
|
signatureContainers: make(map[uint64]*container),
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 05:36:06 +04:00
|
|
|
// Associate a Metric with the DefaultRegistry.
|
2013-04-03 20:33:32 +04:00
|
|
|
func Register(name, docstring string, baseLabels map[string]string, metric Metric) error {
|
2013-01-19 17:48:30 +04:00
|
|
|
return DefaultRegistry.Register(name, docstring, baseLabels, metric)
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:11:01 +04:00
|
|
|
// Implements json.Marshaler
|
2013-06-27 20:46:16 +04:00
|
|
|
func (r *registry) MarshalJSON() ([]byte, error) {
|
|
|
|
containers := make(containers, 0, len(r.signatureContainers))
|
2013-04-19 16:11:01 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
for _, container := range r.signatureContainers {
|
|
|
|
containers = append(containers, container)
|
2013-04-19 16:11:01 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
sort.Sort(containers)
|
2013-04-19 16:11:01 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return json.Marshal(containers)
|
2013-04-19 16:11:01 +04:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
// 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-06-27 20:46:16 +04:00
|
|
|
func (r *registry) isValidCandidate(name string, baseLabels map[string]string) (signature uint64, 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)
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
if *abortOnMisuse {
|
2013-01-19 17:48:30 +04:00
|
|
|
panic(err)
|
2013-06-27 20:46:16 +04:00
|
|
|
} else if *debugRegistration {
|
2013-01-19 17:48:30 +04:00
|
|
|
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)
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
if *abortOnMisuse {
|
2013-01-19 17:48:30 +04:00
|
|
|
panic(err)
|
2013-06-27 20:46:16 +04:00
|
|
|
} else if *debugRegistration {
|
2013-01-19 17:48:30 +04:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return signature, err
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
baseLabels[nameLabel] = name
|
2013-04-03 20:33:32 +04:00
|
|
|
signature = labelsToSignature(baseLabels)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
if _, contains := r.signatureContainers[signature]; contains {
|
|
|
|
err = fmt.Errorf("metric named %s with baseLabels %s is already registered", name, baseLabels)
|
2013-06-27 20:46:16 +04:00
|
|
|
if *abortOnMisuse {
|
2013-01-19 17:48:30 +04:00
|
|
|
panic(err)
|
2013-06-27 20:46:16 +04:00
|
|
|
} else if *debugRegistration {
|
2013-01-19 17:48:30 +04:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return signature, err
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
if *useAggressiveSanityChecks {
|
2013-01-19 17:48:30 +04:00
|
|
|
for _, container := range r.signatureContainers {
|
|
|
|
if container.name == name {
|
2013-04-19 16:11:01 +04:00
|
|
|
err = fmt.Errorf("metric named %s with baseLabels %s is already registered as %s and risks causing confusion", name, baseLabels, container.BaseLabels)
|
2013-06-27 20:46:16 +04:00
|
|
|
if *abortOnMisuse {
|
2013-01-19 17:48:30 +04:00
|
|
|
panic(err)
|
2013-06-27 20:46:16 +04:00
|
|
|
} else if *debugRegistration {
|
2013-01-19 17:48:30 +04:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return signature, err
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return signature, err
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
func (r *registry) Register(name, docstring string, baseLabels map[string]string, metric Metric) error {
|
2012-05-20 01:59:25 +04:00
|
|
|
r.mutex.Lock()
|
|
|
|
defer r.mutex.Unlock()
|
|
|
|
|
2013-09-11 19:22:00 +04:00
|
|
|
labels := map[string]string{}
|
|
|
|
|
|
|
|
if baseLabels != nil {
|
|
|
|
for k, v := range baseLabels {
|
|
|
|
labels[k] = v
|
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:22:00 +04:00
|
|
|
signature, err := r.isValidCandidate(name, labels)
|
2013-01-19 17:48:30 +04:00
|
|
|
if err != nil {
|
2013-06-27 20:46:16 +04:00
|
|
|
return err
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
r.signatureContainers[signature] = &container{
|
2013-09-11 19:22:00 +04:00
|
|
|
BaseLabels: labels,
|
2013-04-19 16:11:01 +04:00
|
|
|
Docstring: docstring,
|
|
|
|
Metric: metric,
|
2013-01-19 17:48:30 +04:00
|
|
|
name: name,
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return nil
|
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-06-27 20:46:16 +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-06-27 20:46:16 +04:00
|
|
|
log.Println("Registry.YieldBasicAuthExporter is deprecated.")
|
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-01-19 17:48:30 +04:00
|
|
|
// 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-06-27 20:46:16 +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-06-27 20:46:16 +04:00
|
|
|
func (r *registry) dumpDelimitedPB(w io.Writer) {
|
|
|
|
r.mutex.RLock()
|
|
|
|
defer r.mutex.RUnlock()
|
|
|
|
|
|
|
|
f := new(dto.MetricFamily)
|
|
|
|
for _, container := range r.signatureContainers {
|
|
|
|
f.Reset()
|
|
|
|
|
|
|
|
f.Name = proto.String(container.name)
|
|
|
|
f.Help = proto.String(container.Docstring)
|
|
|
|
|
|
|
|
container.Metric.dumpChildren(f)
|
|
|
|
|
|
|
|
for name, value := range container.BaseLabels {
|
|
|
|
p := &dto.LabelPair{
|
|
|
|
Name: proto.String(name),
|
|
|
|
Value: proto.String(value),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range f.Metric {
|
|
|
|
child.Label = append(child.Label, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ext.WriteDelimited(w, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (registry *registry) Handler() http.HandlerFunc {
|
2012-05-21 12:23:22 +04:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2013-04-19 16:44:15 +04:00
|
|
|
defer requestLatencyAccumulator(time.Now())
|
2012-05-21 12:23:22 +04:00
|
|
|
|
2013-04-19 16:44:15 +04:00
|
|
|
requestCount.Increment(nil)
|
2013-06-27 20:46:16 +04:00
|
|
|
header := w.Header()
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
writer := decorateWriter(r, w)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
if closer, ok := writer.(io.Closer); ok {
|
|
|
|
defer closer.Close()
|
|
|
|
}
|
2012-05-21 12:23:22 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
accepts := goautoneg.ParseAccept(r.Header.Get("Accept"))
|
|
|
|
for _, accept := range accepts {
|
|
|
|
if accept.Type != "application" {
|
|
|
|
continue
|
2012-05-21 12:23:22 +04:00
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
if accept.SubType == "vnd.google.protobuf" {
|
|
|
|
if accept.Params["proto"] != "io.prometheus.client.MetricFamily" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if accept.Params["encoding"] != "delimited" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Set(contentTypeHeader, DelimitedTelemetryContentType)
|
|
|
|
registry.dumpDelimitedPB(writer)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
2013-06-27 20:46:16 +04:00
|
|
|
|
|
|
|
header.Set(contentTypeHeader, TelemetryContentType)
|
|
|
|
json.NewEncoder(writer).Encode(registry)
|
2012-05-20 01:59:25 +04:00
|
|
|
}
|
|
|
|
}
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
var (
|
|
|
|
abortOnMisuse = flag.Bool(FlagNamespace+"abortonmisuse", false, "abort if a semantic misuse is encountered (bool).")
|
|
|
|
debugRegistration = flag.Bool(FlagNamespace+"debugregistration", false, "display information about the metric registration process (bool).")
|
|
|
|
useAggressiveSanityChecks = flag.Bool(FlagNamespace+"useaggressivesanitychecks", false, "perform expensive validation of metrics (bool).")
|
|
|
|
)
|