From 18c7524c5f9de616c0d277c472eebf5a66bded6d Mon Sep 17 00:00:00 2001 From: ilyam8 Date: Wed, 23 Nov 2022 17:20:57 +0200 Subject: [PATCH] feat(httpcheck): add 'url' label to charts --- modules/httpcheck/charts.go | 2 +- modules/httpcheck/httpcheck.go | 6 +++++- modules/httpcheck/httpcheck_test.go | 5 ++++- modules/httpcheck/init.go | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/modules/httpcheck/charts.go b/modules/httpcheck/charts.go index a5ea410f6..b8acbf0a3 100644 --- a/modules/httpcheck/charts.go +++ b/modules/httpcheck/charts.go @@ -13,7 +13,7 @@ const ( prioResponseInStatusDuration ) -var charts = module.Charts{ +var httpCheckCharts = module.Charts{ responseTimeChart.Copy(), responseLengthChart.Copy(), responseStatusChart.Copy(), diff --git a/modules/httpcheck/httpcheck.go b/modules/httpcheck/httpcheck.go index a59a6a5d8..30c7b4272 100644 --- a/modules/httpcheck/httpcheck.go +++ b/modules/httpcheck/httpcheck.go @@ -47,6 +47,8 @@ type ( Config `yaml:",inline"` UpdateEvery int `yaml:"update_every"` + charts *module.Charts + acceptedStatuses map[int]bool reResponse *regexp.Regexp client client @@ -63,6 +65,8 @@ func (hc *HTTPCheck) Init() bool { return false } + hc.charts = hc.initCharts() + httpClient, err := hc.initHTTPClient() if err != nil { hc.Errorf("init HTTP client: %v", err) @@ -96,7 +100,7 @@ func (hc *HTTPCheck) Check() bool { } func (hc *HTTPCheck) Charts() *module.Charts { - return charts.Copy() + return hc.charts } func (hc *HTTPCheck) Collect() map[string]int64 { diff --git a/modules/httpcheck/httpcheck_test.go b/modules/httpcheck/httpcheck_test.go index 33d0e57fd..1f4f705db 100644 --- a/modules/httpcheck/httpcheck_test.go +++ b/modules/httpcheck/httpcheck_test.go @@ -52,7 +52,10 @@ func TestHTTPCheck_Check(t *testing.T) { } func TestHTTPCheck_Charts(t *testing.T) { - assert.NotNil(t, New().Charts()) + job := New() + job.URL = testURL + require.True(t, job.Init()) + assert.NotNil(t, job.Charts()) } func TestHTTPCheck_Collect(t *testing.T) { diff --git a/modules/httpcheck/init.go b/modules/httpcheck/init.go index 5edada918..29f05f339 100644 --- a/modules/httpcheck/init.go +++ b/modules/httpcheck/init.go @@ -4,9 +4,11 @@ package httpcheck import ( "errors" - "github.com/netdata/go.d.plugin/pkg/web" "net/http" "regexp" + + "github.com/netdata/go.d.plugin/agent/module" + "github.com/netdata/go.d.plugin/pkg/web" ) func (hc *HTTPCheck) validateConfig() error { @@ -26,3 +28,15 @@ func (hc *HTTPCheck) initResponseMatchRegexp() (*regexp.Regexp, error) { } return regexp.Compile(hc.ResponseMatch) } + +func (hc *HTTPCheck) initCharts() *module.Charts { + charts := httpCheckCharts.Copy() + + for _, chart := range *charts { + chart.Labels = []module.Label{ + {Key: "url", Value: hc.URL}, + } + } + + return charts +}