Skip to content

Commit 9e2dd78

Browse files
authored
feat: pcap using pcapgo (#31)
* add: output/otel-trace * remove otel-trace * fix: update loki pkg * fix: loki * fix: metrics test * fix: pcap * feat: remove libpcap dependency * fix: enable Direction using BPF Filter * fix: PCAP plugin for DNS packet processing - Fixed incorrect assignment of dm.QueryMessage and dm.ResponseMessage: - Correctly assign QueryMessage for client queries and ResponseMessage for client responses. - Added error handling for ic.Writer.Write to log write failures. - Optimized time.Now calls by caching the result to improve performance. - Enhanced safety of `send` condition by adding nil checks for MAC addresses. - Improved logging for unknown TransportLayer by including layer type information. - Refactored redundant code for setting dnstap.Message fields into a helper function to reduce duplication. - Added detailed error messages for extractLinkLayer to include packet information. - Ensured proper cleanup in Start function by adding defer handle.Close(). - Improved Setup function error messages for better clarity on invalid parameters. * doc: update input schema * fix: enhance PCAP setup with environment variable and error handling
1 parent 75b1398 commit 9e2dd78

34 files changed

Lines changed: 1236 additions & 964 deletions

.github/workflows/go.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
uses: actions/setup-go@v4
2121
with:
2222
go-version: '1.23'
23+
- name: Set environment variable for PCAP interface
24+
run: echo "DTAP_TEST_PCAP_IFNAME=eth0" >> $GITHUB_ENV
2325
- name: Test
2426
run: |
2527
go install github.com/onsi/ginkgo/v2/ginkgo

go.mod

Lines changed: 147 additions & 92 deletions
Large diffs are not rendered by default.

go.sum

Lines changed: 424 additions & 658 deletions
Large diffs are not rendered by default.

misc/merge-schema/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
76
"path/filepath"
7+
8+
"github.com/goccy/go-json"
89
)
910

1011
type Doc struct {

pkg/config/config_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ package config_test
1818

1919
import (
2020
_ "embed"
21-
"encoding/json"
21+
22+
"github.com/goccy/go-json"
2223

2324
"github.com/mimuret/dtap/v2/pkg/config"
2425
"github.com/mimuret/dtap/v2/pkg/plugin"

pkg/core/plugins/plugin_linux.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// go:build linux
2+
3+
/*
4+
* Copyright (c) 2022 Manabu Sonoda
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package plugin
19+
20+
import (
21+
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/pcap"
22+
)

pkg/core/plugins/plugins.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// input
2929
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/file"
3030
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/nats"
31-
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/pcap"
31+
3232
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/tcp"
3333
_ "github.com/mimuret/dtap/v2/pkg/plugin/input/unix"
3434

@@ -40,6 +40,7 @@ import (
4040
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/loki"
4141
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/nats"
4242
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/nop"
43+
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/otel-log"
4344
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/stdout"
4445
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/tcp"
4546
_ "github.com/mimuret/dtap/v2/pkg/plugin/output/unix"

pkg/plugin/filter/metrics/metrics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/mimuret/dnsutils/getter"
2424
"github.com/mimuret/dtap/v2/pkg/plugin"
2525
"github.com/mimuret/dtap/v2/pkg/plugin/registry"
26+
"github.com/mimuret/dtap/v2/pkg/promauto"
2627
"github.com/mimuret/dtap/v2/pkg/types"
2728
"github.com/pkg/errors"
2829
"github.com/prometheus/client_golang/prometheus"
@@ -139,7 +140,7 @@ func (c *MetricsRule) Setup() error {
139140
c.labels = append(c.labels, string(label.Name))
140141
}
141142
c.counter = prometheus.NewCounterVec(c.CounterOps, c.labels)
142-
if err := prometheus.DefaultRegisterer.Register(c.counter); err != nil {
143+
if err := promauto.DefaultRegisterer().Register(c.counter); err != nil {
143144
return errors.Wrap(err, "failed to register metrics")
144145
}
145146
return nil

pkg/plugin/filter/metrics/metrics_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/mimuret/dnsutils/getter"
2929
"github.com/mimuret/dnsutils/testtool"
3030
_ "github.com/mimuret/dtap/v2/pkg/plugin/filter/static"
31+
"github.com/mimuret/dtap/v2/pkg/promauto"
3132
"github.com/prometheus/client_golang/prometheus"
3233
dto "github.com/prometheus/client_model/go"
3334
"google.golang.org/protobuf/proto"
@@ -58,7 +59,7 @@ func getCounterValue(rule *metrics.MetricsRule, labels []string) (float64, error
5859

5960
var _ = Describe("output/metrics", func() {
6061
BeforeEach(func() {
61-
prometheus.DefaultRegisterer = prometheus.NewRegistry()
62+
promauto.Set(prometheus.NewRegistry())
6263
})
6364
Context("Setup", func() {
6465
var (

pkg/plugin/input/nats/config-schema.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
"type": "string"
4545
},
4646
"Format": {
47-
"description": "The `Format` is the nats message format.",
48-
"type": "string",
49-
"default": "DtapFrame"
47+
"$ref": "https://github.com/mimuret/dtap/raw/refs/heads/v2/schemas/types.json#/$defs/OutputBinFormat"
5048
}
5149
}
5250
}

0 commit comments

Comments
 (0)