Skip to content

Commit

Permalink
Merge pull request #79 from Luzilla/refactor-main
Browse files Browse the repository at this point in the history
Refactor: move app bootstrap out
  • Loading branch information
till authored Apr 4, 2021
2 parents 1180cd1 + 1c3e08d commit 7d7f68d
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 265 deletions.
65 changes: 0 additions & 65 deletions .circleci/config.yml

This file was deleted.

56 changes: 56 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
name: integration

on:
pull_request:

jobs:
snapshot:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --config ./.goreleaser.ci.yml --rm-dist --snapshot
- name: Copy .ini files
run: cp targets.ini rbls.ini ./dist/dnsbl_exporter_linux_amd64
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: dnsbl_exporter
path: dist/dnsbl_exporter_linux_amd64

integration:
runs-on: ubuntu-latest
needs:
- snapshot
services:
unbound:
image: klutchell/unbound:latest
ports:
- 5053:5053
- 5053:5053/udp
steps:
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: dnsbl_exporter
- name: Allow running exporter
run: chmod +x ./dnsbl-exporter
- name: Start dnsbl_exporter
run: ./dnsbl-exporter --config.dns-resolver=unbound:5053 &
- name: Test "/" exists
run: curl -I http://127.0.0.1:9211/
- name: Test "/metrics" exists
run: curl -I http://127.0.0.1:9211/metrics
- name: Test "/metrics" with targets
run: curl -i http://127.0.0.1:9211/metrics
52 changes: 0 additions & 52 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,3 @@ jobs:
uses: actions/checkout@v2
- name: Test
run: go test ./...

snapshot:
runs-on: ubuntu-latest
needs:
- test
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --config ./.goreleaser.ci.yml --rm-dist --snapshot
- name: Copy .ini files
run: cp targets.ini rbls.ini ./dist/dnsbl_exporter_linux_amd64
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: dnsbl_exporter
path: dist/dnsbl_exporter_linux_amd64

integration:
runs-on: ubuntu-latest
needs:
- snapshot
services:
unbound:
image: klutchell/unbound:latest
ports:
- 5053:5053
- 5053:5053/udp
steps:
- name: Download artifact
uses: actions/download-artifact@v2
with:
name: dnsbl_exporter
- name: Allow running exporter
run: chmod +x ./dnsbl-exporter
- name: Start dnsbl_exporter
run: ./dnsbl-exporter --config.dns-resolver=unbound:5053 &
- name: Test "/" exists
run: curl -I http://127.0.0.1:9211/
- name: Test "/metrics" exists
run: curl -I http://127.0.0.1:9211/metrics
- name: Test "/metrics" with targets
run: curl -i http://127.0.0.1:9211/metrics
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
GO_VERSION:=0.16

.PHONY: build
build:
goreleaser --snapshot --rm-dist
goreleaser r --snapshot --rm-dist

.PHONY: test
test:
Expand All @@ -9,5 +11,5 @@ test:
--rm \
-v $(CURDIR):/src/github.com/Luzilla/dnsbl_exporter \
-w /src/github.com/Luzilla/dnsbl_exporter \
golang:1.16 \
golang:$(GO_VERSION) \
sh -c "go mod download && go test ./..."
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dnsbl-exporter

[![CircleCI](https://circleci.com/gh/Luzilla/dnsbl_exporter.svg?style=svg)](https://circleci.com/gh/Luzilla/dnsbl_exporter) [![pr](https://github.com/Luzilla/dnsbl_exporter/actions/workflows/pr.yml/badge.svg)](https://github.com/Luzilla/dnsbl_exporter/actions/workflows/pr.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/31b95e6c679f60e30bea/maintainability)](https://codeclimate.com/github/Luzilla/dnsbl_exporter/maintainability)
[![pr](https://github.com/Luzilla/dnsbl_exporter/actions/workflows/pr.yml/badge.svg)](https://github.com/Luzilla/dnsbl_exporter/actions/workflows/pr.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/31b95e6c679f60e30bea/maintainability)](https://codeclimate.com/github/Luzilla/dnsbl_exporter/maintainability) [![Go Report Card](https://goreportcard.com/badge/github.com/Luzilla/dnsbl_exporter)](https://goreportcard.com/report/github.com/Luzilla/dnsbl_exporter)

This is a server which checks the configured hosts against various DNSBL (sometimes refered to as RBLs).

Expand All @@ -19,7 +19,7 @@ Otherwise:
```
$ dnsbl-exporter -h
...
--config.dns-resolver value IP address of the resolver to use. (default: "127.0.0.1")
--config.dns-resolver value IP address of the resolver to use. (default: "127.0.0.1:53")
--config.rbls value Configuration file which contains RBLs (default: "./rbls.ini")
--config.targets value Configuration file which contains the targets to check. (default: "./targets.ini")
--web.listen-address value Address to listen on for web interface and telemetry. (default: ":9211")
Expand Down
167 changes: 167 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package app

import (
"net/http"
"os"

"github.com/Luzilla/dnsbl_exporter/collector"
"github.com/Luzilla/dnsbl_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli"

log "github.com/sirupsen/logrus"
)

type DNSBLApp struct {
App *cli.App
}

// NewApp ...
func NewApp(name string, version string) DNSBLApp {

cli.VersionFlag = cli.BoolFlag{
Name: "version, V",
Usage: "Print the version information.",
}

app := cli.NewApp()
app.Name = name
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config.dns-resolver",
Value: "127.0.0.1:53",
Usage: "IP address[:port] of the resolver to use.",
},
cli.StringFlag{
Name: "config.rbls",
Value: "./rbls.ini",
Usage: "Configuration file which contains RBLs",
},
cli.StringFlag{
Name: "config.targets",
Value: "./targets.ini",
Usage: "Configuration file which contains the targets to check.",
},
cli.StringFlag{
Name: "web.listen-address",
Value: ":9211",
Usage: "Address to listen on for web interface and telemetry.",
},
cli.StringFlag{
Name: "web.telemetry-path",
Value: "/metrics",
Usage: "Path under which to expose metrics.",
},
cli.BoolFlag{
Name: "web.include-exporter-metrics",
Usage: "Include metrics about the exporter itself (promhttp_*, process_*, go_*).",
},
cli.BoolFlag{
Name: "log.debug",
Usage: "Enable more output in the logs, otherwise INFO.",
},
cli.StringFlag{
Name: "log.output",
Value: "stdout",
Usage: "Destination of our logs: stdout, stderr",
},
}

return DNSBLApp{
App: app,
}
}

func (app *DNSBLApp) Bootstrap() {
app.App.Action = func(ctx *cli.Context) error {
// setup logging
switch ctx.String("log.output") {
case "stdout":
log.SetOutput(os.Stdout)
case "stderr":
log.SetOutput(os.Stderr)
default:
cli.ShowAppHelp(ctx)
return cli.NewExitError("We currently support only stdout and stderr: --log.output", 2)
}
if ctx.Bool("log.debug") {
log.SetLevel(log.DebugLevel)
}

cfgRbls, err := config.LoadFile(ctx.String("config.rbls"), "rbl")
if err != nil {
return err
}

cfgTargets, err := config.LoadFile(ctx.String("config.targets"), "targets")
if err != nil {
return err
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>` + app.App.Name + `</title></head>
<body>
<h1>` + app.App.Name + ` @ ` + app.App.Version + `</h1>
<p><a href="` + ctx.String("web.telemetry-path") + `">Metrics</a></p>
<p><a href="https://github.com/Luzilla/dnsbl_exporter">Code on Github</a></p>
</body>
</html>`))
})

rbls := config.GetRbls(cfgRbls)
targets := config.GetTargets(cfgTargets)

registry := createRegistry()

collector := createCollector(rbls, targets, ctx.String("config.dns-resolver"))
registry.MustRegister(collector)

registryExporter := createRegistry()

if ctx.Bool("web.include-exporter-metrics") {
log.Infoln("Exposing exporter metrics")

registryExporter.MustRegister(
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
prometheus.NewGoCollector(),
)
}

handler := promhttp.HandlerFor(
prometheus.Gatherers{
registry,
registryExporter,
},
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
Registry: registry,
},
)

http.Handle(ctx.String("web.telemetry-path"), handler)

err = http.ListenAndServe(ctx.String("web.listen-address"), nil)
if err != nil {
return err
}

log.Infoln("Listening on", ctx.String("web.listen-address"))

return nil
}
}

func (app *DNSBLApp) Run(args []string) error {
return app.App.Run(args)
}

func createCollector(rbls []string, targets []string, resolver string) *collector.RblCollector {
return collector.NewRblCollector(rbls, targets, resolver)
}

func createRegistry() *prometheus.Registry {
return prometheus.NewRegistry()
}
Loading

0 comments on commit 7d7f68d

Please sign in to comment.