1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,92 @@
# Health Output Plugin
This plugin provides a HTTP health check endpoint that can be configured to
return failure status codes based on the value of a metric.
When the plugin is healthy it will return a 200 response; when unhealthy it
will return a 503 response. The default state is healthy, one or more checks
must fail in order for the resource to enter the failed state.
⭐ Telegraf v1.11.0
🏷️ applications
💻 all
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Configurable HTTP health check resource based on metrics
[[outputs.health]]
## Address and port to listen on.
## ex: service_address = "http://localhost:8080"
## service_address = "unix:///var/run/telegraf-health.sock"
# service_address = "http://:8080"
## The maximum duration for reading the entire request.
# read_timeout = "5s"
## The maximum duration for writing the entire response.
# write_timeout = "5s"
## Username and password to accept for HTTP basic authentication.
# basic_username = "user1"
# basic_password = "secret"
## Allowed CA certificates for client certificates.
# tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
## TLS server certificate and private key.
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## NOTE: Due to the way TOML is parsed, tables must be at the END of the
## plugin definition, otherwise additional config options are read as part of
## the table
## One or more check sub-tables should be defined, it is also recommended to
## use metric filtering to limit the metrics that flow into this output.
##
## When using the default buffer sizes, this example will fail when the
## metric buffer is half full.
##
## namepass = ["internal_write"]
## tagpass = { output = ["influxdb"] }
##
## [[outputs.health.compares]]
## field = "buffer_size"
## lt = 5000.0
##
## [[outputs.health.contains]]
## field = "buffer_size"
```
### compares
The `compares` check is used to assert basic mathematical relationships. Use
it by choosing a field key and one or more comparisons that must hold true. If
the field is not found on a metric no comparison will be made.
Comparisons must be hold true on all metrics for the check to pass.
The available comparison operators are:
- `gt` greater than
- `ge` greater than or equal to
- `lt` less than
- `le` less than or equal to
- `eq` equal to
- `ne` not equal to
### contains
The `contains` check can be used to require a field key to exist on at least
one metric.
If the field is found on any metric the check passes.

View file

@ -0,0 +1,76 @@
package health
import (
"github.com/influxdata/telegraf"
)
type Compares struct {
Field string `toml:"field"`
GT *float64 `toml:"gt"`
GE *float64 `toml:"ge"`
LT *float64 `toml:"lt"`
LE *float64 `toml:"le"`
EQ *float64 `toml:"eq"`
NE *float64 `toml:"ne"`
}
func (c *Compares) runChecks(fv float64) bool {
if c.GT != nil && !(fv > *c.GT) {
return false
}
if c.GE != nil && !(fv >= *c.GE) {
return false
}
if c.LT != nil && !(fv < *c.LT) {
return false
}
if c.LE != nil && !(fv <= *c.LE) {
return false
}
if c.EQ != nil && !(fv == *c.EQ) {
return false
}
if c.NE != nil && !(fv != *c.NE) {
return false
}
return true
}
func (c *Compares) Check(metrics []telegraf.Metric) bool {
success := true
for _, m := range metrics {
fv, ok := m.GetField(c.Field)
if !ok {
continue
}
f, ok := asFloat(fv)
if !ok {
return false
}
result := c.runChecks(f)
if !result {
success = false
}
}
return success
}
func asFloat(fv interface{}) (float64, bool) {
switch v := fv.(type) {
case int64:
return float64(v), true
case float64:
return v, true
case uint64:
return float64(v), true
case bool:
if v {
return 1.0, true
}
return 0.0, true
default:
return 0.0, false
}
}

View file

@ -0,0 +1,269 @@
package health_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs/health"
"github.com/influxdata/telegraf/testutil"
)
func addr(v float64) *float64 {
return &v
}
func TestFieldNotFoundIsSuccess(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Now()),
}
compares := &health.Compares{
Field: "time_idle",
GT: addr(42.0),
}
result := compares.Check(metrics)
require.True(t, result)
}
func TestStringFieldIsFailure(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": "foo",
},
time.Now()),
}
compares := &health.Compares{
Field: "time_idle",
GT: addr(42.0),
}
result := compares.Check(metrics)
require.False(t, result)
}
func TestFloatConvert(t *testing.T) {
tests := []struct {
name string
metrics []telegraf.Metric
expected bool
}{
{
name: "int64 field",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": int64(42.0),
},
time.Now()),
},
expected: true,
},
{
name: "uint64 field",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": uint64(42.0),
},
time.Now()),
},
expected: true,
},
{
name: "float64 field",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": float64(42.0),
},
time.Now()),
},
expected: true,
},
{
name: "bool field true",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": true,
},
time.Now()),
},
expected: true,
},
{
name: "bool field false",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": false,
},
time.Now()),
},
expected: false,
},
{
name: "string field",
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": "42.0",
},
time.Now()),
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compares := &health.Compares{
Field: "time_idle",
GT: addr(0.0),
}
actual := compares.Check(tt.metrics)
require.Equal(t, tt.expected, actual)
})
}
}
func TestOperators(t *testing.T) {
tests := []struct {
name string
compares *health.Compares
expected bool
}{
{
name: "gt",
compares: &health.Compares{
Field: "time_idle",
GT: addr(41.0),
},
expected: true,
},
{
name: "not gt",
compares: &health.Compares{
Field: "time_idle",
GT: addr(42.0),
},
expected: false,
},
{
name: "ge",
compares: &health.Compares{
Field: "time_idle",
GE: addr(42.0),
},
expected: true,
},
{
name: "not ge",
compares: &health.Compares{
Field: "time_idle",
GE: addr(43.0),
},
expected: false,
},
{
name: "lt",
compares: &health.Compares{
Field: "time_idle",
LT: addr(43.0),
},
expected: true,
},
{
name: "not lt",
compares: &health.Compares{
Field: "time_idle",
LT: addr(42.0),
},
expected: false,
},
{
name: "le",
compares: &health.Compares{
Field: "time_idle",
LE: addr(42.0),
},
expected: true,
},
{
name: "not le",
compares: &health.Compares{
Field: "time_idle",
LE: addr(41.0),
},
expected: false,
},
{
name: "eq",
compares: &health.Compares{
Field: "time_idle",
EQ: addr(42.0),
},
expected: true,
},
{
name: "not eq",
compares: &health.Compares{
Field: "time_idle",
EQ: addr(41.0),
},
expected: false,
},
{
name: "ne",
compares: &health.Compares{
Field: "time_idle",
NE: addr(41.0),
},
expected: true,
},
{
name: "not ne",
compares: &health.Compares{
Field: "time_idle",
NE: addr(42.0),
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42.0,
},
time.Now()),
}
actual := tt.compares.Check(metrics)
require.Equal(t, tt.expected, actual)
})
}
}

View file

@ -0,0 +1,19 @@
package health
import "github.com/influxdata/telegraf"
type Contains struct {
Field string `toml:"field"`
}
func (c *Contains) Check(metrics []telegraf.Metric) bool {
success := false
for _, m := range metrics {
ok := m.HasField(c.Field)
if ok {
success = true
}
}
return success
}

View file

@ -0,0 +1,69 @@
package health_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs/health"
"github.com/influxdata/telegraf/testutil"
)
func TestFieldFound(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42.0,
},
time.Now()),
}
contains := &health.Contains{
Field: "time_idle",
}
result := contains.Check(metrics)
require.True(t, result)
}
func TestFieldNotFound(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Now()),
}
contains := &health.Contains{
Field: "time_idle",
}
result := contains.Check(metrics)
require.False(t, result)
}
func TestOneMetricWithFieldIsSuccess(t *testing.T) {
metrics := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{},
time.Now()),
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42.0,
},
time.Now()),
}
contains := &health.Contains{
Field: "time_idle",
}
result := contains.Check(metrics)
require.True(t, result)
}

View file

@ -0,0 +1,233 @@
//go:generate ../../../tools/readme_config_includer/generator
package health
import (
"context"
"crypto/tls"
_ "embed"
"errors"
"net"
"net/http"
"net/url"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
common_tls "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)
//go:embed sample.conf
var sampleConfig string
const (
defaultServiceAddress = "tcp://:8080"
defaultReadTimeout = 5 * time.Second
defaultWriteTimeout = 5 * time.Second
)
type Checker interface {
// Check returns true if the metrics meet its criteria.
Check(metrics []telegraf.Metric) bool
}
type Health struct {
ServiceAddress string `toml:"service_address"`
ReadTimeout config.Duration `toml:"read_timeout"`
WriteTimeout config.Duration `toml:"write_timeout"`
BasicUsername string `toml:"basic_username"`
BasicPassword string `toml:"basic_password"`
common_tls.ServerConfig
Compares []*Compares `toml:"compares"`
Contains []*Contains `toml:"contains"`
Log telegraf.Logger `toml:"-"`
checkers []Checker
wg sync.WaitGroup
server *http.Server
origin string
network string
address string
tlsConf *tls.Config
mu sync.Mutex
healthy bool
}
func (*Health) SampleConfig() string {
return sampleConfig
}
func (h *Health) Init() error {
u, err := url.Parse(h.ServiceAddress)
if err != nil {
return err
}
switch u.Scheme {
case "http", "https":
h.network = "tcp"
h.address = u.Host
case "unix":
h.network = u.Scheme
h.address = u.Path
case "tcp4", "tcp6", "tcp":
h.network = u.Scheme
h.address = u.Host
default:
return errors.New("service_address contains invalid scheme")
}
h.tlsConf, err = h.ServerConfig.TLSConfig()
if err != nil {
return err
}
h.checkers = make([]Checker, 0)
for i := range h.Compares {
h.checkers = append(h.checkers, h.Compares[i])
}
for i := range h.Contains {
h.checkers = append(h.checkers, h.Contains[i])
}
return nil
}
// Connect starts the HTTP server.
func (h *Health) Connect() error {
authHandler := internal.BasicAuthHandler(h.BasicUsername, h.BasicPassword, "health", onAuthError)
h.server = &http.Server{
Addr: h.ServiceAddress,
Handler: authHandler(h),
ReadTimeout: time.Duration(h.ReadTimeout),
WriteTimeout: time.Duration(h.WriteTimeout),
TLSConfig: h.tlsConf,
}
listener, err := h.listen()
if err != nil {
return err
}
h.origin = h.getOrigin(listener)
h.Log.Infof("Listening on %s", h.origin)
h.wg.Add(1)
go func() {
defer h.wg.Done()
err := h.server.Serve(listener)
if err != http.ErrServerClosed {
h.Log.Errorf("Serve error on %s: %v", h.origin, err)
}
h.origin = ""
}()
return nil
}
func onAuthError(_ http.ResponseWriter) {
}
func (h *Health) listen() (net.Listener, error) {
if h.tlsConf != nil {
return tls.Listen(h.network, h.address, h.tlsConf)
}
return net.Listen(h.network, h.address)
}
func (h *Health) ServeHTTP(rw http.ResponseWriter, _ *http.Request) {
var code = http.StatusOK
if !h.isHealthy() {
code = http.StatusServiceUnavailable
}
rw.Header().Set("Server", internal.ProductToken())
http.Error(rw, http.StatusText(code), code)
}
// Write runs all checks over the metric batch and adjust health state.
func (h *Health) Write(metrics []telegraf.Metric) error {
healthy := true
for _, checker := range h.checkers {
success := checker.Check(metrics)
if !success {
healthy = false
}
}
h.setHealthy(healthy)
return nil
}
// Close shuts down the HTTP server.
func (h *Health) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := h.server.Shutdown(ctx)
h.wg.Wait()
return err
}
// Origin returns the URL of the HTTP server.
func (h *Health) Origin() string {
return h.origin
}
func (h *Health) getOrigin(listener net.Listener) string {
scheme := "http"
if h.tlsConf != nil {
scheme = "https"
}
if h.network == "unix" {
scheme = "unix"
}
switch h.network {
case "unix":
origin := &url.URL{
Scheme: scheme,
Path: listener.Addr().String(),
}
return origin.String()
default:
origin := &url.URL{
Scheme: scheme,
Host: listener.Addr().String(),
}
return origin.String()
}
}
func (h *Health) setHealthy(healthy bool) {
h.mu.Lock()
defer h.mu.Unlock()
h.healthy = healthy
}
func (h *Health) isHealthy() bool {
h.mu.Lock()
defer h.mu.Unlock()
return h.healthy
}
func NewHealth() *Health {
return &Health{
ServiceAddress: defaultServiceAddress,
ReadTimeout: config.Duration(defaultReadTimeout),
WriteTimeout: config.Duration(defaultWriteTimeout),
healthy: true,
}
}
func init() {
outputs.Add("health", func() telegraf.Output {
return NewHealth()
})
}

View file

@ -0,0 +1,216 @@
package health_test
import (
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs/health"
"github.com/influxdata/telegraf/testutil"
)
var pki = testutil.NewPKI("../../../testutil/pki")
func TestHealth(t *testing.T) {
type Options struct {
Compares []*health.Compares `toml:"compares"`
Contains []*health.Contains `toml:"contains"`
}
now := time.Now()
tests := []struct {
name string
options Options
metrics []telegraf.Metric
expectedCode int
}{
{
name: "healthy on startup",
expectedCode: 200,
},
{
name: "check passes",
options: Options{
Compares: []*health.Compares{
{
Field: "time_idle",
GT: func() *float64 { v := 0.0; return &v }(),
},
},
},
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42,
},
now),
},
expectedCode: 200,
},
{
name: "check fails",
options: Options{
Compares: []*health.Compares{
{
Field: "time_idle",
LT: func() *float64 { v := 0.0; return &v }(),
},
},
},
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42,
},
now),
},
expectedCode: 503,
},
{
name: "mixed check fails",
options: Options{
Compares: []*health.Compares{
{
Field: "time_idle",
LT: func() *float64 { v := 0.0; return &v }(),
},
},
Contains: []*health.Contains{
{
Field: "foo",
},
},
},
metrics: []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42,
},
now),
},
expectedCode: 503,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := health.NewHealth()
output.ServiceAddress = "tcp://127.0.0.1:0"
output.Compares = tt.options.Compares
output.Contains = tt.options.Contains
output.Log = testutil.Logger{}
err := output.Init()
require.NoError(t, err)
err = output.Connect()
require.NoError(t, err)
err = output.Write(tt.metrics)
require.NoError(t, err)
resp, err := http.Get(output.Origin())
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, tt.expectedCode, resp.StatusCode)
_, err = io.ReadAll(resp.Body)
require.NoError(t, err)
err = output.Close()
require.NoError(t, err)
})
}
}
func TestInitServiceAddress(t *testing.T) {
tests := []struct {
name string
plugin *health.Health
err bool
origin string
}{
{
name: "port without scheme is not allowed",
plugin: &health.Health{
ServiceAddress: ":8080",
Log: testutil.Logger{},
},
err: true,
},
{
name: "path without scheme is not allowed",
plugin: &health.Health{
ServiceAddress: "/tmp/telegraf",
Log: testutil.Logger{},
},
err: true,
},
{
name: "tcp with port maps to http",
plugin: &health.Health{
ServiceAddress: "tcp://:8080",
Log: testutil.Logger{},
},
},
{
name: "tcp with tlsconf maps to https",
plugin: &health.Health{
ServiceAddress: "tcp://:8080",
ServerConfig: *pki.TLSServerConfig(),
Log: testutil.Logger{},
},
},
{
name: "tcp4 is allowed",
plugin: &health.Health{
ServiceAddress: "tcp4://:8080",
Log: testutil.Logger{},
},
},
{
name: "tcp6 is allowed",
plugin: &health.Health{
ServiceAddress: "tcp6://:8080",
Log: testutil.Logger{},
},
},
{
name: "http scheme",
plugin: &health.Health{
ServiceAddress: "http://:8080",
Log: testutil.Logger{},
},
},
{
name: "https scheme",
plugin: &health.Health{
ServiceAddress: "https://:8080",
Log: testutil.Logger{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := health.NewHealth()
output.ServiceAddress = tt.plugin.ServiceAddress
output.Log = testutil.Logger{}
err := output.Init()
if tt.err {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}

View file

@ -0,0 +1,42 @@
# Configurable HTTP health check resource based on metrics
[[outputs.health]]
## Address and port to listen on.
## ex: service_address = "http://localhost:8080"
## service_address = "unix:///var/run/telegraf-health.sock"
# service_address = "http://:8080"
## The maximum duration for reading the entire request.
# read_timeout = "5s"
## The maximum duration for writing the entire response.
# write_timeout = "5s"
## Username and password to accept for HTTP basic authentication.
# basic_username = "user1"
# basic_password = "secret"
## Allowed CA certificates for client certificates.
# tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
## TLS server certificate and private key.
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## NOTE: Due to the way TOML is parsed, tables must be at the END of the
## plugin definition, otherwise additional config options are read as part of
## the table
## One or more check sub-tables should be defined, it is also recommended to
## use metric filtering to limit the metrics that flow into this output.
##
## When using the default buffer sizes, this example will fail when the
## metric buffer is half full.
##
## namepass = ["internal_write"]
## tagpass = { output = ["influxdb"] }
##
## [[outputs.health.compares]]
## field = "buffer_size"
## lt = 5000.0
##
## [[outputs.health.contains]]
## field = "buffer_size"