From 12fbd5af3453279d654db6fe5994cc0e096285b1 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:35:09 +0200 Subject: [PATCH 1/3] fix: invalid ingress counter for dnstap collector --- workers/dnstapserver.go | 3 --- workers/dnstapserver_test.go | 43 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/workers/dnstapserver.go b/workers/dnstapserver.go index 5337dbbd..e11fac93 100644 --- a/workers/dnstapserver.go +++ b/workers/dnstapserver.go @@ -344,9 +344,6 @@ func (w *DNSTapProcessor) StartCollect() { continue } - // count global messages - w.CountIngressTraffic() - // init dns message dm := dnsutils.DNSMessage{} dm.Init() diff --git a/workers/dnstapserver_test.go b/workers/dnstapserver_test.go index 74c6ad70..4c391c56 100644 --- a/workers/dnstapserver_test.go +++ b/workers/dnstapserver_test.go @@ -240,7 +240,7 @@ func Test_DnstapProcessor_toDNSMessage(t *testing.T) { } } -func Test_DnstapProcessor_DecodeCounters(t *testing.T) { +func Test_DnstapProcessor_DecodeDNSCounters(t *testing.T) { logger := logger.New(true) var o bytes.Buffer logger.SetOutput(&o) @@ -622,3 +622,44 @@ func Test_DnstapProcessor_BufferLoggerIsFull(t *testing.T) { t.Errorf("invalid qname in second dns message: %s", dm2.DNS.Qname) } } + +// test for telemetry counter +func Test_DnstapProcessor_TelemetryCounters(t *testing.T) { + logger := logger.New(true) + var o bytes.Buffer + logger.SetOutput(&o) + + // run the consumer with a fake logger + fl := GetWorkerForTest(pkgconfig.DefaultBufferSize) + + // init the dnstap consumer + consumer := NewDNSTapProcessor(0, "peertest", pkgconfig.GetDefaultConfig(), logger, "test", 512) + consumer.AddDefaultRoute(fl) + consumer.AddDroppedRoute(fl) + + // get dns packet + queryPkt, _ := dnsutils.GetFakeDNS() + + // prepare dnstap + dt := &dnstap.Dnstap{} + dt.Type = dnstap.Dnstap_Type.Enum(1) + + dt.Message = &dnstap.Message{} + dt.Message.Type = dnstap.Message_Type.Enum(5) // CLIENT_QUERY + dt.Message.ResponseMessage = queryPkt + data, _ := proto.Marshal(dt) + + // start the consumer + go consumer.StartCollect() + + // add packet to consumer and read output + consumer.GetDataChannel() <- data + <-fl.GetInputChannel() + + if consumer.totalIngress != 1 { + t.Errorf("invalid total ingress oucnter: got %d expect 1", consumer.totalIngress) + } + if consumer.totalEgress != 1 { + t.Errorf("invalid total egress counter: got %d expect 1", consumer.totalEgress) + } +} From e1f8132f5c8d6e40efea9fb918b6d988a88f73b8 Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:35:41 +0200 Subject: [PATCH 2/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5dea20a..9110543c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@

Go Report Go version -Go tests +Go tests Go bench Go lines

From 55157588859eea3d5c53f9a12914e893b054deef Mon Sep 17 00:00:00 2001 From: dmachard <5562930+dmachard@users.noreply.github.com> Date: Wed, 9 Oct 2024 18:18:50 +0200 Subject: [PATCH 3/3] fix race condition in test --- workers/dnstapserver_test.go | 21 ++++++++++++++++----- workers/worker.go | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/workers/dnstapserver_test.go b/workers/dnstapserver_test.go index 4c391c56..756a9b62 100644 --- a/workers/dnstapserver_test.go +++ b/workers/dnstapserver_test.go @@ -11,6 +11,7 @@ import ( "github.com/dmachard/go-dnscollector/dnsutils" "github.com/dmachard/go-dnscollector/pkgconfig" + "github.com/dmachard/go-dnscollector/telemetry" "github.com/dmachard/go-dnstap-protobuf" "github.com/dmachard/go-framestream" "github.com/dmachard/go-logger" @@ -632,8 +633,16 @@ func Test_DnstapProcessor_TelemetryCounters(t *testing.T) { // run the consumer with a fake logger fl := GetWorkerForTest(pkgconfig.DefaultBufferSize) + cfg := pkgconfig.GetDefaultConfig() + cfg.Global.Telemetry.Enabled = true + cfg.Global.Worker.InternalMonitor = 1 + + config := pkgconfig.Config{} + metrics := telemetry.NewPrometheusCollector(&config) + // init the dnstap consumer - consumer := NewDNSTapProcessor(0, "peertest", pkgconfig.GetDefaultConfig(), logger, "test", 512) + consumer := NewDNSTapProcessor(0, "peertest", cfg, logger, "test", 512) + consumer.SetMetrics(metrics) consumer.AddDefaultRoute(fl) consumer.AddDroppedRoute(fl) @@ -654,12 +663,14 @@ func Test_DnstapProcessor_TelemetryCounters(t *testing.T) { // add packet to consumer and read output consumer.GetDataChannel() <- data + <-fl.GetInputChannel() + r := <-metrics.Record - if consumer.totalIngress != 1 { - t.Errorf("invalid total ingress oucnter: got %d expect 1", consumer.totalIngress) + if r.TotalIngress != 1 { + t.Errorf("invalid total ingress oucnter: got %d expect 1", r.TotalIngress) } - if consumer.totalEgress != 1 { - t.Errorf("invalid total egress counter: got %d expect 1", consumer.totalEgress) + if r.TotalEgress != 1 { + t.Errorf("invalid total egress counter: got %d expect 1", r.TotalEgress) } } diff --git a/workers/worker.go b/workers/worker.go index f793065d..b56a5754 100644 --- a/workers/worker.go +++ b/workers/worker.go @@ -232,7 +232,7 @@ func (w *GenericWorker) Monitor() { // // send to telemetry? if w.config.Global.Telemetry.Enabled && w.metrics != nil { - if w.totalIngress > 0 || w.totalForwarded > 0 || w.totalDropped > 0 { + if w.totalIngress > 0 || w.totalEgress > 0 || w.totalForwarded > 0 || w.totalDropped > 0 { w.metrics.Record <- telemetry.WorkerStats{ Name: w.GetName(), TotalIngress: w.totalIngress,