summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/haproxy/collect.go
blob: 203ff1ec4a6a537466cd2f34b07cdc43362e4607 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package haproxy

import (
	"errors"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/prometheus"
)

const (
	metricBackendSessionsTotal              = "haproxy_backend_sessions_total"
	metricBackendCurrentSessions            = "haproxy_backend_current_sessions"
	metricBackendHTTPResponsesTotal         = "haproxy_backend_http_responses_total"
	metricBackendResponseTimeAverageSeconds = "haproxy_backend_response_time_average_seconds"
	metricBackendCurrentQueue               = "haproxy_backend_current_queue"
	metricBackendQueueTimeAverageSeconds    = "haproxy_backend_queue_time_average_seconds"
	metricBackendBytesInTotal               = "haproxy_backend_bytes_in_total"
	metricBackendBytesOutTotal              = "haproxy_backend_bytes_out_total"
)

func isHaproxyMetrics(pms prometheus.Series) bool {
	for _, pm := range pms {
		if strings.HasPrefix(pm.Name(), "haproxy_") {
			return true
		}
	}
	return false
}

func (h *Haproxy) collect() (map[string]int64, error) {
	pms, err := h.prom.ScrapeSeries()
	if err != nil {
		return nil, err
	}

	if h.validateMetrics && !isHaproxyMetrics(pms) {
		return nil, errors.New("unexpected metrics (not HAProxy)")
	}
	h.validateMetrics = false

	mx := make(map[string]int64)
	for _, pm := range pms {
		proxy := pm.Labels.Get("proxy")
		if proxy == "" {
			continue
		}

		if !h.proxies[proxy] {
			h.proxies[proxy] = true
			h.addProxyToCharts(proxy)
		}

		mx[dimID(pm)] = int64(pm.Value * multiplier(pm))
	}

	return mx, nil
}

func (h *Haproxy) addProxyToCharts(proxy string) {
	h.addDimToChart(chartBackendCurrentSessions.ID, &module.Dim{
		ID:   proxyDimID(metricBackendCurrentSessions, proxy),
		Name: proxy,
	})
	h.addDimToChart(chartBackendSessions.ID, &module.Dim{
		ID:   proxyDimID(metricBackendSessionsTotal, proxy),
		Name: proxy,
		Algo: module.Incremental,
	})

	h.addDimToChart(chartBackendResponseTimeAverage.ID, &module.Dim{
		ID:   proxyDimID(metricBackendResponseTimeAverageSeconds, proxy),
		Name: proxy,
	})
	if err := h.Charts().Add(newChartBackendHTTPResponses(proxy)); err != nil {
		h.Warning(err)
	}

	h.addDimToChart(chartBackendCurrentQueue.ID, &module.Dim{
		ID:   proxyDimID(metricBackendCurrentQueue, proxy),
		Name: proxy,
	})
	h.addDimToChart(chartBackendQueueTimeAverage.ID, &module.Dim{
		ID:   proxyDimID(metricBackendQueueTimeAverageSeconds, proxy),
		Name: proxy,
	})

	if err := h.Charts().Add(newChartBackendNetworkIO(proxy)); err != nil {
		h.Warning(err)
	}
}

func (h *Haproxy) addDimToChart(chartID string, dim *module.Dim) {
	chart := h.Charts().Get(chartID)
	if chart == nil {
		h.Warningf("error on adding '%s' dimension: can not find '%s' chart", dim.ID, chartID)
		return
	}
	if err := chart.AddDim(dim); err != nil {
		h.Warning(err)
		return
	}
	chart.MarkNotCreated()
}

func multiplier(pm prometheus.SeriesSample) float64 {
	switch pm.Name() {
	case metricBackendResponseTimeAverageSeconds,
		metricBackendQueueTimeAverageSeconds:
		// to milliseconds
		return 1000
	}
	return 1
}

func dimID(pm prometheus.SeriesSample) string {
	proxy := pm.Labels.Get("proxy")
	if proxy == "" {
		return ""
	}

	name := cleanMetricName(pm.Name())
	if pm.Name() == metricBackendHTTPResponsesTotal {
		name += "_" + pm.Labels.Get("code")
	}
	return proxyDimID(name, proxy)
}

func proxyDimID(metric, proxy string) string {
	return cleanMetricName(metric) + "_proxy_" + proxy
}

func cleanMetricName(name string) string {
	if strings.HasSuffix(name, "_total") {
		return name[:len(name)-6]
	}
	if strings.HasSuffix(name, "_seconds") {
		return name[:len(name)-8]
	}
	return name
}