Skip to content

Commit

Permalink
some small adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
raylas committed Nov 28, 2022
1 parent c00b972 commit 2f4b11a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:

- name: Run tests
run: go test -v ./...
env:
# False values to satisfy environment checks.
NEXTDNS_PROFILE: bhu7e3
NEXTDNS_API_KEY: 1234567890abcdef

release:
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
Expand Down
30 changes: 17 additions & 13 deletions grafana/dashboards/nextdns.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "nextdns_queries_total",
"expr": "nextdns_queries_total{profile=\"$profile\"}",
"legendFormat": "All",
"range": true,
"refId": "all"
Expand Down Expand Up @@ -176,8 +176,8 @@
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "nextdns_blocked_queries",
"editorMode": "code",
"expr": "nextdns_blocked_queries{profile=\"$profile\"}",
"legendFormat": "{{domain}}",
"range": true,
"refId": "domains"
Expand Down Expand Up @@ -240,8 +240,8 @@
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "nextdns_device_queries",
"editorMode": "code",
"expr": "nextdns_device_queries{profile=\"$profile\"}",
"legendFormat": "{{name}}",
"range": true,
"refId": "devices"
Expand Down Expand Up @@ -306,7 +306,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "sum(increase(nextdns_blocked_queries_total[$__range]))",
"expr": "sum(increase(nextdns_blocked_queries_total{profile=\"$profile\"}[$__range]))",
"hide": true,
"legendFormat": "Blocked",
"range": true,
Expand All @@ -318,7 +318,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "sum(increase(nextdns_queries_total[$__range]))",
"expr": "sum(increase(nextdns_queries_total{profile=\"$profile\"}[$__range]))",
"hide": true,
"legendFormat": "Total",
"range": true,
Expand Down Expand Up @@ -419,7 +419,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "sum(increase(nextdns_queries_total[$__range]))",
"expr": "sum(increase(nextdns_queries_total{profile=\"$profile\"}[$__range]))",
"hide": false,
"legendFormat": "Total",
"range": true,
Expand Down Expand Up @@ -511,8 +511,8 @@
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"editorMode": "builder",
"expr": "nextdns_type_queries",
"editorMode": "code",
"expr": "nextdns_type_queries{profile=\"$profile\"}",
"legendFormat": "{{name}}",
"range": true,
"refId": "types"
Expand Down Expand Up @@ -605,7 +605,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "nextdns_blocked_queries_total",
"expr": "nextdns_blocked_queries_total{profile=\"$profile\"}",
"legendFormat": "Blocked",
"range": true,
"refId": "blocked"
Expand Down Expand Up @@ -697,7 +697,7 @@
"uid": "PBFA97CFB590B2093"
},
"editorMode": "code",
"expr": "nextdns_ip_version_queries",
"expr": "nextdns_ip_version_queries{profile=\"$profile\"}",
"legendFormat": "IPv{{version}}",
"range": true,
"refId": "ip_versions"
Expand All @@ -720,6 +720,10 @@
"text": "e6e9b2",
"value": "e6e9b2"
},
"datasource": {
"type": "prometheus",
"uid": "PBFA97CFB590B2093"
},
"definition": "label_values(profile)",
"hide": 0,
"includeAll": false,
Expand All @@ -739,7 +743,7 @@
]
},
"time": {
"from": "now-30m",
"from": "now-15m",
"to": "now"
},
"timepicker": {},
Expand Down
39 changes: 24 additions & 15 deletions internal/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package util

import (
"fmt"
"runtime/debug"
"os"

"github.com/hashicorp/go-hclog"
)
Expand All @@ -12,6 +12,8 @@ const (
BaseURL = "https://api.nextdns.io"
)

var version = "dev" // Set by goreleaser.

var (
Log hclog.Logger
Version string
Expand All @@ -26,23 +28,30 @@ var (
// Initialize the configuration.
func init() {
// Set up logging.
level := GetEnv("LOG_LEVEL", "INFO")
level := DefaultEnv("LOG_LEVEL", "INFO")
Log = hclog.New(&hclog.LoggerOptions{
Level: hclog.LevelFromString(level),
})

// Retrieve version.
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
Version = info.Main.Version
} else {
Version = "dev"
}
// Set version.
Version = version

// Retrieve configuration, or use defaults.
Port = fmt.Sprintf(":%s", DefaultEnv("METRICS_PORT", "9948"))
MetricsPath = DefaultEnv("METRICS_PATH", "/metrics")
ResultWindow = DefaultEnv("NEXTDNS_RESULT_WINDOW", "-5m")
ResultLimit = DefaultEnv("NEXTDNS_RESULT_LIMIT", "50")

// Set up exporter.
Port = fmt.Sprintf(":%s", GetEnv("METRICS_PORT", "9948"))
MetricsPath = GetEnv("METRICS_PATH", "/metrics")
Profile = GetEnv("NEXTDNS_PROFILE", "")
APIKey = GetEnv("NEXTDNS_API_KEY", "")
ResultWindow = GetEnv("NEXTDNS_RESULT_WINDOW", "-5m")
ResultLimit = GetEnv("NEXTDNS_RESULT_LIMIT", "50")
// Required configuration.
var set bool
Profile, set = os.LookupEnv("NEXTDNS_PROFILE")
if !set {
Log.Error("NEXTDNS_PROFILE must be set")
os.Exit(1)
}
APIKey, set = os.LookupEnv("NEXTDNS_API_KEY")
if !set {
Log.Error("NEXTDNS_API_KEY must be set")
os.Exit(1)
}
}
5 changes: 3 additions & 2 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"os"
)

// Return the value of an environment variable, or a default value if it is not set.
func GetEnv(key, usual string) string {
// Return the value of an environment variable,
// or a default value if it is not set.
func DefaultEnv(key, usual string) string {
value := os.Getenv(key)
if len(value) == 0 {
return usual
Expand Down
6 changes: 3 additions & 3 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"testing"
)

func TestGetEnv(t *testing.T) {
func TestDefaultEnv(t *testing.T) {
os.Setenv("FOO", "bar")

if GetEnv("FOO", "baz") != "bar" {
if DefaultEnv("FOO", "baz") != "bar" {
t.Error("expected to get `bar`")
}
if GetEnv("BAR", "baz") != "baz" {
if DefaultEnv("BAR", "baz") != "baz" {
t.Error("expected to get `baz`")
}
}

0 comments on commit 2f4b11a

Please sign in to comment.