2019-11-26 23:46:31 +00:00
|
|
|
package prometheus
|
2015-10-22 16:17:57 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-21 23:37:53 +00:00
|
|
|
"context"
|
2019-03-01 19:19:31 +00:00
|
|
|
"crypto/tls"
|
2015-10-22 16:17:57 +00:00
|
|
|
"fmt"
|
2018-08-01 22:43:34 +00:00
|
|
|
"net"
|
2015-10-28 22:19:13 +00:00
|
|
|
"net/http"
|
2019-03-08 22:54:16 +00:00
|
|
|
"net/url"
|
2016-07-10 13:47:47 +00:00
|
|
|
"sync"
|
2016-11-15 11:33:39 +00:00
|
|
|
"time"
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-15 11:33:39 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2019-03-01 19:19:31 +00:00
|
|
|
tlsint "github.com/influxdata/telegraf/internal/tls"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2019-11-26 23:46:31 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs/prometheus_client/v1"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs/prometheus_client/v2"
|
2015-10-22 16:17:57 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-10-18 21:51:08 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2015-10-22 16:17:57 +00:00
|
|
|
)
|
|
|
|
|
2019-04-02 19:42:48 +00:00
|
|
|
var (
|
2019-11-26 23:46:31 +00:00
|
|
|
defaultListen = ":9273"
|
|
|
|
defaultPath = "/metrics"
|
|
|
|
defaultExpirationInterval = internal.Duration{Duration: 60 * time.Second}
|
2019-04-02 19:42:48 +00:00
|
|
|
)
|
2016-03-22 16:34:33 +00:00
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Address to listen on
|
2018-08-24 23:37:11 +00:00
|
|
|
listen = ":9273"
|
2016-11-15 11:33:39 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
## Metric version controls the mapping from Telegraf metrics into
|
|
|
|
## Prometheus format. When using the prometheus input, use the same value in
|
|
|
|
## both plugins to ensure metrics are round-tripped without modification.
|
|
|
|
##
|
|
|
|
## example: metric_version = 1; deprecated in 1.13
|
|
|
|
## metric_version = 2; recommended version
|
|
|
|
# metric_version = 1
|
|
|
|
|
2018-08-24 23:37:11 +00:00
|
|
|
## Use HTTP Basic Authentication.
|
|
|
|
# basic_username = "Foo"
|
|
|
|
# basic_password = "Bar"
|
2018-02-01 19:12:16 +00:00
|
|
|
|
2018-08-24 23:37:11 +00:00
|
|
|
## If set, the IP Ranges which are allowed to access metrics.
|
|
|
|
## ex: ip_range = ["192.168.0.0/24", "192.168.1.0/30"]
|
|
|
|
# ip_range = []
|
2018-02-01 19:12:16 +00:00
|
|
|
|
2018-08-24 23:37:11 +00:00
|
|
|
## Path to publish the metrics on.
|
|
|
|
# path = "/metrics"
|
2018-08-01 22:43:34 +00:00
|
|
|
|
2018-08-24 23:37:11 +00:00
|
|
|
## Expiration interval for each metric. 0 == no expiration
|
2016-11-15 11:33:39 +00:00
|
|
|
# expiration_interval = "60s"
|
2017-10-18 21:51:08 +00:00
|
|
|
|
|
|
|
## Collectors to enable, valid entries are "gocollector" and "process".
|
|
|
|
## If unset, both are enabled.
|
2018-08-24 23:37:11 +00:00
|
|
|
# collectors_exclude = ["gocollector", "process"]
|
2018-02-16 22:07:26 +00:00
|
|
|
|
2018-08-24 23:37:11 +00:00
|
|
|
## Send string metrics as Prometheus labels.
|
|
|
|
## Unless set to false all string metrics will be sent as labels.
|
|
|
|
# string_as_label = true
|
|
|
|
|
|
|
|
## If set, enable TLS with the given certificate.
|
|
|
|
# tls_cert = "/etc/ssl/telegraf.crt"
|
|
|
|
# tls_key = "/etc/ssl/telegraf.key"
|
2019-03-08 22:54:16 +00:00
|
|
|
|
2019-02-26 18:46:49 +00:00
|
|
|
## Set one or more allowed client CA certificate file names to
|
|
|
|
## enable mutually authenticated TLS connections
|
|
|
|
# tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
|
2019-01-17 18:51:18 +00:00
|
|
|
|
|
|
|
## Export metric collection time.
|
|
|
|
# export_timestamp = false
|
2015-10-22 16:17:57 +00:00
|
|
|
`
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
type Collector interface {
|
|
|
|
Describe(ch chan<- *prometheus.Desc)
|
|
|
|
Collect(ch chan<- prometheus.Metric)
|
|
|
|
Add(metrics []telegraf.Metric) error
|
|
|
|
}
|
2018-02-01 19:12:16 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
type PrometheusClient struct {
|
|
|
|
Listen string `toml:"listen"`
|
|
|
|
MetricVersion int `toml:"metric_version"`
|
|
|
|
BasicUsername string `toml:"basic_username"`
|
|
|
|
BasicPassword string `toml:"basic_password"`
|
|
|
|
IPRange []string `toml:"ip_range"`
|
|
|
|
ExpirationInterval internal.Duration `toml:"expiration_interval"`
|
|
|
|
Path string `toml:"path"`
|
|
|
|
CollectorsExclude []string `toml:"collectors_exclude"`
|
|
|
|
StringAsLabel bool `toml:"string_as_label"`
|
|
|
|
ExportTimestamp bool `toml:"export_timestamp"`
|
|
|
|
tlsint.ServerConfig
|
2018-08-01 22:43:34 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
|
|
|
|
|
|
|
server *http.Server
|
|
|
|
url *url.URL
|
|
|
|
collector Collector
|
|
|
|
wg sync.WaitGroup
|
2018-02-01 19:12:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
func (p *PrometheusClient) Description() string {
|
|
|
|
return "Configuration for the Prometheus client to spawn"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrometheusClient) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrometheusClient) Init() error {
|
2017-12-12 02:00:19 +00:00
|
|
|
defaultCollectors := map[string]bool{
|
|
|
|
"gocollector": true,
|
|
|
|
"process": true,
|
|
|
|
}
|
2017-10-18 21:51:08 +00:00
|
|
|
for _, collector := range p.CollectorsExclude {
|
2017-12-12 02:00:19 +00:00
|
|
|
delete(defaultCollectors, collector)
|
|
|
|
}
|
|
|
|
|
|
|
|
registry := prometheus.NewRegistry()
|
2018-10-19 20:32:54 +00:00
|
|
|
for collector := range defaultCollectors {
|
2017-10-18 21:51:08 +00:00
|
|
|
switch collector {
|
|
|
|
case "gocollector":
|
2017-12-12 02:00:19 +00:00
|
|
|
registry.Register(prometheus.NewGoCollector())
|
2017-10-18 21:51:08 +00:00
|
|
|
case "process":
|
2019-01-17 18:51:18 +00:00
|
|
|
registry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
|
2017-10-18 21:51:08 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognized collector %s", collector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
switch p.MetricVersion {
|
|
|
|
default:
|
|
|
|
fallthrough
|
|
|
|
case 1:
|
|
|
|
p.Log.Warnf("Use of deprecated configuration: metric_version = 1; please update to metric_version = 2")
|
|
|
|
p.collector = v1.NewCollector(p.ExpirationInterval.Duration, p.StringAsLabel, p.Log)
|
|
|
|
err := registry.Register(p.collector)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case 2:
|
2020-04-06 20:21:01 +00:00
|
|
|
p.collector = v2.NewCollector(p.ExpirationInterval.Duration, p.StringAsLabel, p.ExportTimestamp)
|
2019-11-26 23:46:31 +00:00
|
|
|
err := registry.Register(p.collector)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-01 21:26:11 +00:00
|
|
|
}
|
2017-12-12 02:00:19 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
ipRange := make([]*net.IPNet, 0, len(p.IPRange))
|
|
|
|
for _, cidr := range p.IPRange {
|
|
|
|
_, ipNet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing ip_range: %v", err)
|
|
|
|
}
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
ipRange = append(ipRange, ipNet)
|
2017-09-19 18:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
authHandler := internal.AuthHandler(p.BasicUsername, p.BasicPassword, "prometheus", onAuthError)
|
2019-11-26 23:46:31 +00:00
|
|
|
rangeHandler := internal.IPRangeHandler(ipRange, onError)
|
|
|
|
promHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})
|
|
|
|
|
2017-01-21 23:37:53 +00:00
|
|
|
mux := http.NewServeMux()
|
2019-11-26 23:46:31 +00:00
|
|
|
if p.Path == "" {
|
|
|
|
p.Path = "/"
|
|
|
|
}
|
|
|
|
mux.Handle(p.Path, authHandler(rangeHandler(promHandler)))
|
2017-01-21 23:37:53 +00:00
|
|
|
|
2019-02-22 22:18:36 +00:00
|
|
|
tlsConfig, err := p.TLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-26 23:46:31 +00:00
|
|
|
|
2019-02-22 22:18:36 +00:00
|
|
|
p.server = &http.Server{
|
|
|
|
Addr: p.Listen,
|
|
|
|
Handler: mux,
|
|
|
|
TLSConfig: tlsConfig,
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrometheusClient) listen() (net.Listener, error) {
|
|
|
|
if p.server.TLSConfig != nil {
|
|
|
|
return tls.Listen("tcp", p.Listen, p.server.TLSConfig)
|
2019-03-01 19:19:31 +00:00
|
|
|
} else {
|
2019-11-26 23:46:31 +00:00
|
|
|
return net.Listen("tcp", p.Listen)
|
2019-03-01 19:19:31 +00:00
|
|
|
}
|
2019-11-26 23:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrometheusClient) Connect() error {
|
|
|
|
listener, err := p.listen()
|
2019-03-01 19:19:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
scheme := "http"
|
|
|
|
if p.server.TLSConfig != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
p.url = &url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: listener.Addr().String(),
|
|
|
|
Path: p.Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Log.Infof("Listening on %s", p.URL())
|
2019-03-08 22:54:16 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
p.wg.Add(1)
|
2017-07-05 21:28:44 +00:00
|
|
|
go func() {
|
2019-11-26 23:46:31 +00:00
|
|
|
defer p.wg.Done()
|
2019-03-01 19:19:31 +00:00
|
|
|
err := p.server.Serve(listener)
|
2018-02-01 19:12:16 +00:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2019-11-26 23:46:31 +00:00
|
|
|
p.Log.Errorf("Server error: %v", err)
|
2017-07-05 21:28:44 +00:00
|
|
|
}
|
|
|
|
}()
|
2018-02-01 19:12:16 +00:00
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:13:44 +00:00
|
|
|
func onAuthError(_ http.ResponseWriter) {
|
2019-03-08 22:54:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
func onError(rw http.ResponseWriter, code int) {
|
|
|
|
http.Error(rw, http.StatusText(code), code)
|
|
|
|
}
|
2019-03-08 22:54:16 +00:00
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
// Address returns the address the plugin is listening on. If not listening
|
|
|
|
// an empty string is returned.
|
|
|
|
func (p *PrometheusClient) URL() string {
|
|
|
|
if p.url != nil {
|
|
|
|
return p.url.String()
|
2019-03-08 22:54:16 +00:00
|
|
|
}
|
2019-11-26 23:46:31 +00:00
|
|
|
return ""
|
2019-03-08 22:54:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
func (p *PrometheusClient) Close() error {
|
2019-11-26 23:46:31 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
2017-01-21 23:37:53 +00:00
|
|
|
defer cancel()
|
2019-11-26 23:46:31 +00:00
|
|
|
|
2017-07-25 22:41:18 +00:00
|
|
|
err := p.server.Shutdown(ctx)
|
2019-11-26 23:46:31 +00:00
|
|
|
p.wg.Wait()
|
|
|
|
p.url = nil
|
|
|
|
prometheus.Unregister(p.collector)
|
2017-07-25 22:41:18 +00:00
|
|
|
return err
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
|
2019-11-26 23:46:31 +00:00
|
|
|
return p.collector.Add(metrics)
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("prometheus_client", func() telegraf.Output {
|
2016-11-15 11:33:39 +00:00
|
|
|
return &PrometheusClient{
|
2019-11-26 23:46:31 +00:00
|
|
|
Listen: defaultListen,
|
|
|
|
Path: defaultPath,
|
|
|
|
ExpirationInterval: defaultExpirationInterval,
|
2018-02-16 22:07:26 +00:00
|
|
|
StringAsLabel: true,
|
2016-11-15 11:33:39 +00:00
|
|
|
}
|
2015-10-22 16:17:57 +00:00
|
|
|
})
|
|
|
|
}
|