-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from Luzilla/refactor-main
Refactor: move app bootstrap out
- Loading branch information
Showing
7 changed files
with
233 additions
and
265 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.