1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright (c) 2021 Uber Technologies, Inc.
//
// 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.
package prometheus
import (
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
prom "github.com/prometheus/client_golang/prometheus"
)
// Configuration is a configuration for a Prometheus reporter.
type Configuration struct {
// HandlerPath if specified will be used instead of using the default
// HTTP handler path "/metrics".
HandlerPath string `yaml:"handlerPath"`
// ListenNetwork if specified will be used instead of using tcp network.
// Supported networks: tcp, tcp4, tcp6 and unix.
ListenNetwork string `yaml:"listenNetwork"`
// ListenAddress if specified will be used instead of just registering the
// handler on the default HTTP serve mux without listening.
ListenAddress string `yaml:"listenAddress"`
// TimerType is the default Prometheus type to use for Tally timers.
TimerType string `yaml:"timerType"`
// DefaultHistogramBuckets if specified will set the default histogram
// buckets to be used by the reporter.
DefaultHistogramBuckets []HistogramObjective `yaml:"defaultHistogramBuckets"`
// DefaultSummaryObjectives if specified will set the default summary
// objectives to be used by the reporter.
DefaultSummaryObjectives []SummaryObjective `yaml:"defaultSummaryObjectives"`
// OnError specifies what to do when an error either with listening
// on the specified listen address or registering a metric with the
// Prometheus. By default the registerer will panic.
OnError string `yaml:"onError"`
}
// HistogramObjective is a Prometheus histogram bucket.
// See: https://godoc.org/github.com/prometheus/client_golang/prometheus#HistogramOpts
type HistogramObjective struct {
Upper float64 `yaml:"upper"`
}
// SummaryObjective is a Prometheus summary objective.
// See: https://godoc.org/github.com/prometheus/client_golang/prometheus#SummaryOpts
type SummaryObjective struct {
Percentile float64 `yaml:"percentile"`
AllowedError float64 `yaml:"allowedError"`
}
// ConfigurationOptions allows some programatic options, such as using a
// specific registry and what error callback to register.
type ConfigurationOptions struct {
// Registry if not nil will specify the specific registry to use
// for registering metrics.
Registry *prom.Registry
// OnError allows for customization of what to do when a metric
// registration error fails, the default is to panic.
OnError func(e error)
}
// NewReporter creates a new M3 reporter from this configuration.
func (c Configuration) NewReporter(
configOpts ConfigurationOptions,
) (Reporter, error) {
var opts Options
if configOpts.Registry != nil {
opts.Registerer = configOpts.Registry
}
if configOpts.OnError != nil {
opts.OnRegisterError = configOpts.OnError
} else {
switch c.OnError {
case "stderr":
opts.OnRegisterError = func(err error) {
fmt.Fprintf(os.Stderr, "tally prometheus reporter error: %v\n", err)
}
case "log":
opts.OnRegisterError = func(err error) {
log.Printf("tally prometheus reporter error: %v\n", err)
}
case "none":
opts.OnRegisterError = func(err error) {}
default:
opts.OnRegisterError = func(err error) {
panic(err)
}
}
}
switch c.TimerType {
case "summary":
opts.DefaultTimerType = SummaryTimerType
case "histogram":
opts.DefaultTimerType = HistogramTimerType
}
if len(c.DefaultHistogramBuckets) > 0 {
var values []float64
for _, value := range c.DefaultHistogramBuckets {
values = append(values, value.Upper)
}
opts.DefaultHistogramBuckets = values
}
if len(c.DefaultSummaryObjectives) > 0 {
values := make(map[float64]float64)
for _, value := range c.DefaultSummaryObjectives {
values[value.Percentile] = value.AllowedError
}
opts.DefaultSummaryObjectives = values
}
reporter := NewReporter(opts)
path := "/metrics"
if handlerPath := strings.TrimSpace(c.HandlerPath); handlerPath != "" {
path = handlerPath
}
if addr := strings.TrimSpace(c.ListenAddress); addr == "" {
http.Handle(path, reporter.HTTPHandler())
} else {
mux := http.NewServeMux()
mux.Handle(path, reporter.HTTPHandler())
go func() {
network := c.ListenNetwork
if network == "" {
network = "tcp"
}
listener, err := net.Listen(network, addr)
if err != nil {
opts.OnRegisterError(err)
return
}
defer listener.Close()
if err = http.Serve(listener, mux); err != nil {
opts.OnRegisterError(err)
}
}()
}
return reporter, nil
}
|