Skip to content

Commit 762efd7

Browse files
authored
Merge pull request #160 from Luzilla/error-msg
Fix: error handling
2 parents 8a79d3c + 47a8f44 commit 762efd7

File tree

20 files changed

+549
-181
lines changed

20 files changed

+549
-181
lines changed

.github/workflows/golint.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: golangci-lint
2+
on:
3+
pull_request:
4+
5+
permissions:
6+
contents: read
7+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
8+
# pull-requests: read
9+
10+
jobs:
11+
golangci:
12+
name: lint
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: actions/setup-go@v4
17+
with:
18+
go-version-file: go.mod
19+
cache: false
20+
- uses: golangci/golangci-lint-action@v3
21+
with:
22+
version: latest
23+
skip-cache: true

.github/workflows/integration.yml

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
pull_request:
66

77
jobs:
8-
snapshot:
8+
e2e:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
@@ -38,20 +38,31 @@ jobs:
3838
unbound:
3939
image: klutchell/unbound:latest
4040
ports:
41-
- 5053:5053
41+
- 5053:5053/tcp
4242
- 5053:5053/udp
4343
steps:
44-
- name: Download artifact
45-
uses: actions/download-artifact@v3
44+
- uses: actions/checkout@v3
4645
with:
47-
name: dnsbl_exporter
48-
- name: Allow running exporter
49-
run: chmod +x ./dnsbl-exporter
50-
- name: Start dnsbl_exporter
51-
run: ./dnsbl-exporter --config.dns-resolver=unbound:5053 &
52-
- name: Test "/" exists
53-
run: curl -I http://127.0.0.1:9211/
54-
- name: Test "/metrics" exists
55-
run: curl -I http://127.0.0.1:9211/metrics
56-
- name: Test "/metrics" with targets
57-
run: curl -i http://127.0.0.1:9211/metrics
46+
fetch-depth: 0
47+
- uses: actions/setup-go@v4
48+
with:
49+
go-version-file: go.mod
50+
cache: false
51+
- uses: goreleaser/goreleaser-action@v4
52+
with:
53+
version: latest
54+
args: release --config ./.goreleaser.ci.yml --clean --snapshot
55+
- run: cp targets.ini rbls.ini ./dist/dnsbl_exporter_linux_amd64_v1
56+
- uses: JarvusInnovations/background-action@v1
57+
with:
58+
run: |
59+
ls -lah && ./dnsbl-exporter --log.debug --config.dns-resolver=localhost:5053 &
60+
wait-on: |
61+
http-get://localhost:9211/
62+
http-get://localhost:9211/metrics
63+
http-get://localhost:9211/prober?target=github.com
64+
tail: true # true = stderr,stdout
65+
log-output-resume: stderr
66+
wait-for: 1m
67+
log-output: stderr,stdout
68+
working-directory: ./dist/dnsbl_exporter_linux_amd64_v1

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run:
2+
timeout: 2m

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ build:
44

55
.PHONY: run-dev
66
run-dev:
7-
go run dnsbl_exporter.go --log.debug
7+
go run dnsbl_exporter.go \
8+
--log.debug \
9+
--config.dns-resolver 0.0.0.0:15353
810

911
.PHONY: snapshot
1012
snapshot:

app/app.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import (
1313
"github.com/Luzilla/dnsbl_exporter/internal/metrics"
1414
"github.com/Luzilla/dnsbl_exporter/internal/prober"
1515
"github.com/Luzilla/dnsbl_exporter/internal/setup"
16-
"github.com/prometheus/client_golang/prometheus"
16+
"github.com/Luzilla/dnsbl_exporter/pkg/dns"
17+
"github.com/prometheus/client_golang/prometheus/collectors"
1718
"github.com/urfave/cli/v2"
1819
"golang.org/x/exp/slog"
20+
21+
x "github.com/miekg/dns"
1922
)
2023

2124
type DNSBLApp struct {
@@ -104,7 +107,6 @@ func NewApp(name string, version string) DNSBLApp {
104107
func (a *DNSBLApp) Bootstrap() {
105108
a.App.Action = func(ctx *cli.Context) error {
106109
// setup logging
107-
fmt.Println("VERSION: " + appVersion)
108110
handler := &slog.HandlerOptions{}
109111
var writer io.Writer
110112

@@ -161,7 +163,13 @@ func (a *DNSBLApp) Bootstrap() {
161163

162164
registry := setup.CreateRegistry()
163165

164-
rblCollector := setup.CreateCollector(rbls, targets, resolver, log.With("area", "metrics"))
166+
dnsUtil, err := dns.New(new(x.Client), resolver, log)
167+
if err != nil {
168+
log.Error("failed to initialize dns client")
169+
return err
170+
}
171+
172+
rblCollector := setup.CreateCollector(rbls, targets, dnsUtil, log.With("area", "metrics"))
165173
registry.MustRegister(rblCollector)
166174

167175
registryExporter := setup.CreateRegistry()
@@ -170,8 +178,8 @@ func (a *DNSBLApp) Bootstrap() {
170178
log.Info("Exposing exporter metrics")
171179

172180
registryExporter.MustRegister(
173-
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
174-
prometheus.NewGoCollector(),
181+
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
182+
collectors.NewGoCollector(),
175183
)
176184
}
177185

@@ -183,13 +191,16 @@ func (a *DNSBLApp) Bootstrap() {
183191
http.Handle(ctx.String("web.telemetry-path"), mHandler.Handler())
184192

185193
pHandler := prober.ProberHandler{
186-
Resolver: resolver,
187-
Rbls: rbls,
188-
Logger: log.With("area", "prober"),
194+
DNS: dnsUtil,
195+
Rbls: rbls,
196+
Logger: log.With("area", "prober"),
189197
}
190198
http.Handle("/prober", pHandler)
191199

192-
log.Info("Starting on: " + ctx.String("web.listen-address"))
200+
log.Info("starting exporter",
201+
slog.String("web.listen-address", ctx.String("web.listen-address")),
202+
slog.String("resolver", resolver),
203+
)
193204
err = http.ListenAndServe(ctx.String("web.listen-address"), nil)
194205
if err != nil {
195206
return err

collector/collector.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/Luzilla/dnsbl_exporter/pkg/dns"
88
"github.com/Luzilla/dnsbl_exporter/pkg/rbl"
9-
x "github.com/miekg/dns"
109
"github.com/prometheus/client_golang/prometheus"
1110
"golang.org/x/exp/slog"
1211
)
@@ -23,7 +22,7 @@ type RblCollector struct {
2322
targetsMetric *prometheus.Desc
2423
durationMetric *prometheus.Desc
2524
rbls []string
26-
resolver string
25+
util *dns.DNSUtil
2726
targets []string
2827
logger *slog.Logger
2928
}
@@ -33,7 +32,7 @@ func BuildFQName(metric string) string {
3332
}
3433

3534
// NewRblCollector ... creates the collector
36-
func NewRblCollector(rbls []string, targets []string, resolver string, logger *slog.Logger) *RblCollector {
35+
func NewRblCollector(rbls []string, targets []string, util *dns.DNSUtil, logger *slog.Logger) *RblCollector {
3736
return &RblCollector{
3837
configuredMetric: prometheus.NewDesc(
3938
BuildFQName("used"),
@@ -71,10 +70,10 @@ func NewRblCollector(rbls []string, targets []string, resolver string, logger *s
7170
nil,
7271
nil,
7372
),
74-
rbls: rbls,
75-
resolver: resolver,
76-
targets: targets,
77-
logger: logger,
73+
rbls: rbls,
74+
util: util,
75+
targets: targets,
76+
logger: logger,
7877
}
7978
}
8079

@@ -106,36 +105,51 @@ func (c *RblCollector) Collect(ch chan<- prometheus.Metric) {
106105
// this should be a map of blacklist and a counter (for listings)
107106
var listed sync.Map
108107

109-
// iterate over hosts -> resolve to ip, check
108+
resolver := rbl.NewRBLResolver(c.logger, c.util)
109+
110+
// iterate over hosts -> resolve to ip
111+
targets := make(chan []rbl.Target)
110112
for _, host := range hosts {
111-
logger := c.logger.With("target", host)
113+
go resolver.Do(host, targets)
114+
}
115+
116+
// run the check
117+
for _, target := range <-targets {
112118

113-
logger.Debug("Starting check")
119+
results := make([]rbl.Result, 0)
114120

115-
r := rbl.New(dns.New(new(x.Client), c.resolver, logger), logger)
116-
r.Update(host, c.rbls)
121+
result := make(chan rbl.Result)
122+
for _, blocklist := range c.rbls {
123+
logger := c.logger.With("host", target.Host)
124+
125+
logger.Debug("starting check")
126+
127+
r := rbl.New(c.util, logger)
128+
go r.Update(target, blocklist, result)
129+
results = append(results, <-result)
130+
}
117131

118-
for _, result := range r.Results {
132+
for _, check := range results {
119133
metricValue := 0
120134

121-
val, _ := listed.LoadOrStore(result.Rbl, 0)
122-
if result.Listed {
135+
val, _ := listed.LoadOrStore(check.Rbl, 0)
136+
if check.Listed {
123137
metricValue = 1
124-
listed.Store(result.Rbl, val.(int)+1)
138+
listed.Store(check.Rbl, val.(int)+1)
125139
}
126140

127-
logger.Debug(result.Rbl+" listed?", slog.Int("v", metricValue))
141+
c.logger.Debug("listed?", slog.Int("v", metricValue), slog.String("rbl", check.Rbl))
128142

129-
labelValues := []string{result.Rbl, result.Address, host}
143+
labelValues := []string{check.Rbl, check.Target.IP.String(), check.Target.Host}
130144

131-
// this is an "error" from the RBL
132-
if result.Error {
133-
logger.Error(result.Text)
145+
// this is an "error" from the RBL/transport
146+
if check.Error {
147+
c.logger.Error(check.ErrorType.Error(), slog.String("text", check.Text))
134148
ch <- prometheus.MustNewConstMetric(
135149
c.errorsMetrics,
136150
prometheus.GaugeValue,
137151
1,
138-
[]string{result.Rbl}...,
152+
[]string{check.Rbl}...,
139153
)
140154
}
141155

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *Config) GetTargets(cfg *ini.File) []string {
4949

5050
// LoadFile ...
5151
func (c *Config) LoadFile(path string) (*ini.File, error) {
52-
c.Logger.Debug("Loading configuration file: " + path)
52+
c.Logger.Debug("loading configuration file: " + path)
5353

5454
cfg, err := loadConfig(path)
5555
if err != nil {

dnsbl_exporter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// The following are customized during build
1111
var exporterName string = "dnsbl-exporter"
12-
var exporterVersion string
12+
var exporterVersion string = "dev"
1313

1414
func main() {
1515
dnsbl := app.NewApp(exporterName, exporterVersion)
@@ -20,5 +20,4 @@ func main() {
2020
fmt.Println("error: " + err.Error())
2121
os.Exit(1)
2222
}
23-
2423
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require github.com/prometheus/client_golang v1.17.0
66

77
require (
88
github.com/Luzilla/godnsbl v1.0.0
9+
github.com/foxcpp/go-mockdns v1.0.0
910
github.com/miekg/dns v1.1.56
1011
github.com/stretchr/testify v1.8.4
1112
github.com/urfave/cli/v2 v2.25.7

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
99
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1010
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1111
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
13+
github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
1214
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1315
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
1416
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
@@ -20,6 +22,7 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2022
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2123
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
2224
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
25+
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
2326
github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE=
2427
github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY=
2528
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -41,18 +44,32 @@ github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
4144
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
4245
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
4346
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
47+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
48+
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
4449
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
4550
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
4651
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
4752
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
53+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
54+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
55+
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
4856
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
4957
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
5058
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
59+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5160
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
61+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
62+
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
63+
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5264
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
5365
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
66+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
67+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
68+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
69+
golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5470
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
5571
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
72+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5673
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5774
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
5875
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

0 commit comments

Comments
 (0)