Skip to content

Commit

Permalink
query logging wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Jul 19, 2016
1 parent dc6fbb8 commit 0aab8e6
Show file tree
Hide file tree
Showing 15 changed files with 1,071 additions and 65 deletions.
7 changes: 6 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -4,6 +4,9 @@ all: templates.go
templates.go: templates/*.html monitor.go
go generate

test:
go test -race $(go list ./... | grep -v /vendor/)

devel:
go build -tags devel

Expand Down
11 changes: 11 additions & 0 deletions countries/countries.go
Expand Up @@ -251,3 +251,14 @@ var CountryContinent = map[string]string{
"zm": "africa",
"zw": "africa",
}

var ContinentCountries = map[string][]string{}

func init() {
for cc, co := range CountryContinent {
if _, ok := ContinentCountries[co]; !ok {
ContinentCountries[co] = []string{}
}
ContinentCountries[co] = append(ContinentCountries[co], cc)
}
}
122 changes: 65 additions & 57 deletions countries/regiongroups.go
Expand Up @@ -4,72 +4,80 @@ import (
"log"
)

func CountryRegionGroup(country, region string) string {
var RegionGroups = map[string]string{
"us-ak": "us-west",
"us-az": "us-west",
"us-ca": "us-west",
"us-co": "us-west",
"us-hi": "us-west",
"us-id": "us-west",
"us-mt": "us-west",
"us-nm": "us-west",
"us-nv": "us-west",
"us-or": "us-west",
"us-ut": "us-west",
"us-wa": "us-west",
"us-wy": "us-west",

if country != "us" {
return ""
}
"us-ar": "us-central",
"us-ia": "us-central",
"us-il": "us-central",
"us-in": "us-central",
"us-ks": "us-central",
"us-la": "us-central",
"us-mn": "us-central",
"us-mo": "us-central",
"us-nd": "us-central",
"us-ne": "us-central",
"us-ok": "us-central",
"us-sd": "us-central",
"us-tx": "us-central",
"us-wi": "us-central",

"us-al": "us-east",
"us-ct": "us-east",
"us-dc": "us-east",
"us-de": "us-east",
"us-fl": "us-east",
"us-ga": "us-east",
"us-ky": "us-east",
"us-ma": "us-east",
"us-md": "us-east",
"us-me": "us-east",
"us-mi": "us-east",
"us-ms": "us-east",
"us-nc": "us-east",
"us-nh": "us-east",
"us-nj": "us-east",
"us-ny": "us-east",
"us-oh": "us-east",
"us-pa": "us-east",
"us-ri": "us-east",
"us-sc": "us-east",
"us-tn": "us-east",
"us-va": "us-east",
"us-vt": "us-east",
"us-wv": "us-east",
}

regions := map[string]string{
"us-ak": "us-west",
"us-az": "us-west",
"us-ca": "us-west",
"us-co": "us-west",
"us-hi": "us-west",
"us-id": "us-west",
"us-mt": "us-west",
"us-nm": "us-west",
"us-nv": "us-west",
"us-or": "us-west",
"us-ut": "us-west",
"us-wa": "us-west",
"us-wy": "us-west",
var RegionGroupRegions = map[string][]string{}

"us-ar": "us-central",
"us-ia": "us-central",
"us-il": "us-central",
"us-in": "us-central",
"us-ks": "us-central",
"us-la": "us-central",
"us-mn": "us-central",
"us-mo": "us-central",
"us-nd": "us-central",
"us-ne": "us-central",
"us-ok": "us-central",
"us-sd": "us-central",
"us-tx": "us-central",
"us-wi": "us-central",
func CountryRegionGroup(country, region string) string {

"us-al": "us-east",
"us-ct": "us-east",
"us-dc": "us-east",
"us-de": "us-east",
"us-fl": "us-east",
"us-ga": "us-east",
"us-ky": "us-east",
"us-ma": "us-east",
"us-md": "us-east",
"us-me": "us-east",
"us-mi": "us-east",
"us-ms": "us-east",
"us-nc": "us-east",
"us-nh": "us-east",
"us-nj": "us-east",
"us-ny": "us-east",
"us-oh": "us-east",
"us-pa": "us-east",
"us-ri": "us-east",
"us-sc": "us-east",
"us-tn": "us-east",
"us-va": "us-east",
"us-vt": "us-east",
"us-wv": "us-east",
if country != "us" {
return ""
}

if group, ok := regions[region]; ok {
if group, ok := RegionGroups[region]; ok {
return group
}

log.Printf("Did not find a region group for '%s'/'%s'", country, region)
return ""
}

func init() {
for ccrc, rg := range RegionGroups {
RegionGroupRegions[rg] = append(RegionGroupRegions[rg], ccrc)
}
}
3 changes: 2 additions & 1 deletion geodns.go
Expand Up @@ -33,7 +33,7 @@ import (
)

// VERSION is the current version of GeoDNS
var VERSION string = "2.6.0"
var VERSION string = "2.7.0"
var buildTime string
var gitVersion string

Expand Down Expand Up @@ -95,6 +95,7 @@ func main() {
}

srv := Server{}
srv.SetQueryLogger("log/queries.log")

if len(*flagLogFile) > 0 {
logToFileOpen(*flagLogFile)
Expand Down
51 changes: 51 additions & 0 deletions querylog/querylog.go
@@ -0,0 +1,51 @@
package querylog

import (
"encoding/json"

"gopkg.in/natefinch/lumberjack.v2"
)

type QueryLogger interface {
Write(*Entry) error
}

// easyjson:json
type Entry struct {
Time int64
Origin string
Name string
Qtype uint16
Rcode int
Answers int
Targets []string
LabelName string
RemoteAddr string
ClientAddr string
HasECS bool
}

type FileLogger struct {
logger lumberjack.Logger
}

func NewFileLogger(filename string) (*FileLogger, error) {
fl := &FileLogger{}
fl.logger = lumberjack.Logger{
Filename: filename,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
}
return fl, nil
}

func (l *FileLogger) Write(e *Entry) error {
js, err := json.Marshal(e)
if err != nil {
return err
}
js = append(js, []byte("\n")...)
_, err = l.logger.Write(js)
return err
}

0 comments on commit 0aab8e6

Please sign in to comment.