From ce2df385a6f994889b70d7ea169df7b04dc83320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Thu, 17 Oct 2024 02:29:53 +0200 Subject: [PATCH] tcp: fix panic on collector (#1699) --- internal/collector/tcp/tcp_test.go | 4 ++++ internal/perfdata/v1/collector.go | 2 +- internal/perfdata/v1/perflib.go | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/collector/tcp/tcp_test.go b/internal/collector/tcp/tcp_test.go index 848df74d..d33cc95f 100644 --- a/internal/collector/tcp/tcp_test.go +++ b/internal/collector/tcp/tcp_test.go @@ -10,3 +10,7 @@ import ( func BenchmarkCollector(b *testing.B) { testutils.FuncBenchmarkCollector(b, tcp.Name, tcp.NewWithFlags) } + +func TestCollector(t *testing.T) { + testutils.TestCollector(t, tcp.New, nil) +} diff --git a/internal/perfdata/v1/collector.go b/internal/perfdata/v1/collector.go index 5e0ee686..274fde86 100644 --- a/internal/perfdata/v1/collector.go +++ b/internal/perfdata/v1/collector.go @@ -44,7 +44,7 @@ func (c *Collector) Collect() (map[string]map[string]perftypes.CounterValues, er return nil, fmt.Errorf("QueryPerformanceData: %w", err) } - if len(perfObjects) == 0 { + if len(perfObjects) == 0 || perfObjects[0] == nil || len(perfObjects[0].Instances) == 0 { return map[string]map[string]perftypes.CounterValues{}, nil } diff --git a/internal/perfdata/v1/perflib.go b/internal/perfdata/v1/perflib.go index 010f0d82..c392ad16 100644 --- a/internal/perfdata/v1/perflib.go +++ b/internal/perfdata/v1/perflib.go @@ -418,6 +418,10 @@ func QueryPerformanceData(query string, counterName string) ([]*PerfObject, erro } } + if counterName != "" { + return objects[i : i+1], nil + } + // Next perfObjectType objOffset += int64(obj.TotalByteLength) numFilteredObjects++