Skip to content

Commit

Permalink
Experiment with generating prometheus metrics from log processor
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed May 9, 2018
1 parent 58ca629 commit ad2425f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion geodns-influxdb/influx.go
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/kr/pretty" "github.com/kr/pretty"
) )


const UserAgent = "geodns-logs/1.1" const UserAgent = "geodns-logs/1.2"


type influxClient struct { type influxClient struct {
ServerID string ServerID string
Expand Down
17 changes: 16 additions & 1 deletion geodns-influxdb/process-stats.go
Expand Up @@ -6,13 +6,16 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net/http"
"os" "os"
"strings" "strings"
"sync" "sync"
"time" "time"


"github.com/hpcloud/tail" "github.com/hpcloud/tail"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"


"github.com/abh/geodns/countries" "github.com/abh/geodns/countries"
"github.com/abh/geodns/querylog" "github.com/abh/geodns/querylog"
Expand Down Expand Up @@ -49,6 +52,18 @@ func main() {
} }
} }


queries = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "dns_logs_total",
Help: "Number of served queries",
},
[]string{"zone", "vendor", "usercc", "poolcc", "qtype"},
)
prometheus.MustRegister(queries)

http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":8054", nil)

influx := NewInfluxClient() influx := NewInfluxClient()
influx.URL = os.Getenv("INFLUXDB_URL") influx.URL = os.Getenv("INFLUXDB_URL")
influx.Username = os.Getenv("INFLUXDB_USERNAME") influx.Username = os.Getenv("INFLUXDB_USERNAME")
Expand Down Expand Up @@ -174,6 +189,7 @@ func processChan(in chan string, out chan<- *Stats, wg *sync.WaitGroup) error {
log.Printf("Can't unmarshal '%s': %s", line, err) log.Printf("Can't unmarshal '%s': %s", line, err)
return err return err
} }
e.Name = strings.ToLower(e.Name)


eMinute := ((e.Time - e.Time%int64(submitInterval)) / int64(time.Second)) eMinute := ((e.Time - e.Time%int64(submitInterval)) / int64(time.Second))
e.Time = eMinute e.Time = eMinute
Expand All @@ -192,7 +208,6 @@ func processChan(in chan string, out chan<- *Stats, wg *sync.WaitGroup) error {
} }
} }


e.Name = strings.ToLower(e.Name)
// fmt.Printf("%s %s\n", e.Origin, e.Name) // fmt.Printf("%s %s\n", e.Origin, e.Name)


err = stats.Add(&e) err = stats.Add(&e)
Expand Down
18 changes: 17 additions & 1 deletion geodns-influxdb/stats.go
Expand Up @@ -8,6 +8,7 @@ import (


"github.com/abh/geodns/querylog" "github.com/abh/geodns/querylog"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
) )


type statsEntry struct { type statsEntry struct {
Expand All @@ -26,6 +27,8 @@ type Stats struct {
Map map[string]*statsEntry Map map[string]*statsEntry
} }


var queries *prometheus.CounterVec

func NewStats() *Stats { func NewStats() *Stats {
return &Stats{ return &Stats{
Map: map[string]*statsEntry{}, Map: map[string]*statsEntry{},
Expand Down Expand Up @@ -88,6 +91,19 @@ func (stats *Stats) Add(e *querylog.Entry) error {


stats.Count++ stats.Count++


qtypeString := dns.TypeToString[e.Qtype]

userCC := ""
for _, cc := range e.Targets {
if len(cc) == 2 {
userCC = cc
break
}
}

// []string{"zone", "vendor", "usercc", "poolcc", "qtype"},
queries.WithLabelValues(e.Origin, vendor, userCC, poolCC, qtypeString).Inc()

key := stats.Key(e) key := stats.Key(e)


if s, ok := stats.Map[key]; ok { if s, ok := stats.Map[key]; ok {
Expand All @@ -101,7 +117,7 @@ func (stats *Stats) Add(e *querylog.Entry) error {
Vendor: vendor, Vendor: vendor,
Label: e.LabelName, Label: e.LabelName,
PoolCC: poolCC, PoolCC: poolCC,
Qtype: dns.TypeToString[e.Qtype], Qtype: qtypeString,
Count: 1, Count: 1,
} }
} }
Expand Down
2 changes: 1 addition & 1 deletion server/serve.go
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/abh/geodns/applog" "github.com/abh/geodns/applog"
"github.com/abh/geodns/querylog" "github.com/abh/geodns/querylog"
"github.com/abh/geodns/zones" "github.com/abh/geodns/zones"
"github.com/prometheus/client_golang/prometheus"


"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
) )


func getQuestionName(z *zones.Zone, fqdn string) string { func getQuestionName(z *zones.Zone, fqdn string) string {
Expand Down

0 comments on commit ad2425f

Please sign in to comment.