• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stillya / wg-relay / 17277209982

27 Aug 2025 07:52PM UTC coverage: 13.246% (+8.4%) from 4.874%
17277209982

Pull #3

github

stillya
added prom integration
Pull Request #3: Added metrics support

78 of 293 new or added lines in 11 files covered. (26.62%)

5 existing lines in 4 files now uncovered.

111 of 838 relevant lines covered (13.25%)

0.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/metrics/collector.go
1
package metrics
2

3
import (
4
        "context"
5
        log "log/slog"
6

7
        "github.com/prometheus/client_golang/prometheus"
8
        "github.com/stillya/wg-relay/pkg/maps/metricsmap"
9
)
10

11
type MetricCollectorSource interface {
12
        Collect(ctx context.Context) ([]metricsmap.MetricData, error)
13
        Name() string
14
}
15

16
type BpfCollector struct {
17
        source MetricCollectorSource
18
        mode   string
19

20
        packetsDesc *prometheus.Desc
21
        bytesDesc   *prometheus.Desc
22
}
23

NEW
24
func NewBpfCollector(source MetricCollectorSource, mode string) *BpfCollector {
×
NEW
25
        return &BpfCollector{
×
NEW
26
                source: source,
×
NEW
27
                mode:   mode,
×
NEW
28
                packetsDesc: prometheus.NewDesc(
×
NEW
29
                        "wg_relay_packets",
×
NEW
30
                        "Current total number of WireGuard packets processed",
×
NEW
31
                        []string{"mode", "direction", "reason"},
×
NEW
32
                        nil,
×
NEW
33
                ),
×
NEW
34
                bytesDesc: prometheus.NewDesc(
×
NEW
35
                        "wg_relay_bytes",
×
NEW
36
                        "Current total bytes of WireGuard packets processed",
×
NEW
37
                        []string{"mode", "direction", "reason"},
×
NEW
38
                        nil,
×
NEW
39
                ),
×
NEW
40
        }
×
NEW
41
}
×
42

NEW
43
func (c *BpfCollector) Describe(ch chan<- *prometheus.Desc) {
×
NEW
44
        ch <- c.packetsDesc
×
NEW
45
        ch <- c.bytesDesc
×
NEW
46
}
×
47

NEW
48
func (c *BpfCollector) Collect(ch chan<- prometheus.Metric) {
×
NEW
49
        ctx := context.Background()
×
NEW
50
        metricsData, err := c.source.Collect(ctx)
×
NEW
51
        if err != nil {
×
NEW
52
                log.Error("Failed to collect metrics", "error", err)
×
NEW
53
                return
×
NEW
54
        }
×
55

NEW
56
        for _, metric := range metricsData {
×
NEW
57
                dirLabel := metricsmap.DirectionToString(metric.Key.Dir)
×
NEW
58
                reasonLabel := metricsmap.ReasonToString(metric.Key.Reason)
×
NEW
59

×
NEW
60
                ch <- prometheus.MustNewConstMetric(
×
NEW
61
                        c.packetsDesc,
×
NEW
62
                        prometheus.GaugeValue,
×
NEW
63
                        float64(metric.Value.Packets),
×
NEW
64
                        c.mode, dirLabel, reasonLabel,
×
NEW
65
                )
×
NEW
66

×
NEW
67
                ch <- prometheus.MustNewConstMetric(
×
NEW
68
                        c.bytesDesc,
×
NEW
69
                        prometheus.GaugeValue,
×
NEW
70
                        float64(metric.Value.Bytes),
×
NEW
71
                        c.mode, dirLabel, reasonLabel,
×
NEW
72
                )
×
NEW
73
        }
×
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc