Added an option to specify metrics log file

Previously the metrics log file was hardcoded and the broker wasn't
behaving properly if it was unable to open the file for logging.

Added a commandline option to specify the logfile that defaults to
Stdout.

Fixed up some documentation and log output formatting
This commit is contained in:
Cecylia Bocovich 2019-05-10 12:03:07 -04:00
parent 72e54bdc2e
commit ba4fe1a73e
3 changed files with 26 additions and 19 deletions

View file

@ -10,6 +10,7 @@ import (
"crypto/tls" "crypto/tls"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@ -37,10 +38,10 @@ type BrokerContext struct {
metrics *Metrics metrics *Metrics
} }
func NewBrokerContext() *BrokerContext { func NewBrokerContext(metricsLogger *log.Logger) *BrokerContext {
snowflakes := new(SnowflakeHeap) snowflakes := new(SnowflakeHeap)
heap.Init(snowflakes) heap.Init(snowflakes)
metrics, err := NewMetrics() metrics, err := NewMetrics(metricsLogger)
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
@ -253,6 +254,7 @@ func main() {
var geoip6Database string var geoip6Database string
var disableTLS bool var disableTLS bool
var disableGeoip bool var disableGeoip bool
var metricsFilename string
flag.StringVar(&acmeEmail, "acme-email", "", "optional contact email for Let's Encrypt notifications") flag.StringVar(&acmeEmail, "acme-email", "", "optional contact email for Let's Encrypt notifications")
flag.StringVar(&acmeHostnamesCommas, "acme-hostnames", "", "comma-separated hostnames for TLS certificate") flag.StringVar(&acmeHostnamesCommas, "acme-hostnames", "", "comma-separated hostnames for TLS certificate")
@ -261,11 +263,27 @@ func main() {
flag.StringVar(&geoip6Database, "geoip6db", "/usr/share/tor/geoip6", "path to correctly formatted geoip database mapping IPv6 address ranges to country codes") flag.StringVar(&geoip6Database, "geoip6db", "/usr/share/tor/geoip6", "path to correctly formatted geoip database mapping IPv6 address ranges to country codes")
flag.BoolVar(&disableTLS, "disable-tls", false, "don't use HTTPS") flag.BoolVar(&disableTLS, "disable-tls", false, "don't use HTTPS")
flag.BoolVar(&disableGeoip, "disable-geoip", false, "don't use geoip for stats collection") flag.BoolVar(&disableGeoip, "disable-geoip", false, "don't use geoip for stats collection")
flag.StringVar(&metricsFilename, "metrics-log", "", "path to metrics logging output")
flag.Parse() flag.Parse()
var metricsFile io.Writer = os.Stdout
var err error
log.SetFlags(log.LstdFlags | log.LUTC) log.SetFlags(log.LstdFlags | log.LUTC)
ctx := NewBrokerContext() if metricsFilename != "" {
metricsFile, err = os.OpenFile(metricsFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err.Error())
}
} else {
metricsFile = os.Stdout
}
metricsLogger := log.New(metricsFile, "", log.LstdFlags|log.LUTC)
ctx := NewBrokerContext(metricsLogger)
if !disableGeoip { if !disableGeoip {
err := ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database) err := ctx.metrics.LoadGeoipDatabases(geoipDatabase, geoip6Database)
@ -283,7 +301,6 @@ func main() {
http.Handle("/answer", SnowflakeHandler{ctx, proxyAnswers}) http.Handle("/answer", SnowflakeHandler{ctx, proxyAnswers})
http.Handle("/debug", SnowflakeHandler{ctx, debugHandler}) http.Handle("/debug", SnowflakeHandler{ctx, debugHandler})
var err error
server := http.Server{ server := http.Server{
Addr: addr, Addr: addr,
} }

View file

@ -209,7 +209,8 @@ func GeoIPLoadFile(table GeoIPTable, pathname string) error {
return nil return nil
} }
//Returns the country location of an IPv4 or IPv6 address. //Returns the country location of an IPv4 or IPv6 address, and a boolean value
//that indicates whether the IP address was present in the geoip database
func GetCountryByAddr(table GeoIPTable, ip net.IP) (string, bool) { func GetCountryByAddr(table GeoIPTable, ip net.IP) (string, bool) {
table.Lock() table.Lock()

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"os"
"sync" "sync"
"time" "time"
) )
@ -28,7 +27,7 @@ type Metrics struct {
} }
func (s CountryStats) Display() string { func (s CountryStats) Display() string {
return fmt.Sprintln(s.counts) return fmt.Sprint(s.counts)
} }
func (m *Metrics) UpdateCountryStats(addr string) { func (m *Metrics) UpdateCountryStats(addr string) {
@ -56,9 +55,7 @@ func (m *Metrics) UpdateCountryStats(addr string) {
} }
//update map of countries and counts //update map of countries and counts
if country != "" {
m.countryStats.counts[country]++ m.countryStats.counts[country]++
}
return return
} }
@ -88,17 +85,9 @@ func (m *Metrics) LoadGeoipDatabases(geoipDB string, geoip6DB string) error {
return nil return nil
} }
func NewMetrics() (*Metrics, error) { func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
m := new(Metrics) m := new(Metrics)
f, err := os.OpenFile("metrics.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
metricsLogger := log.New(f, "", log.LstdFlags|log.LUTC)
m.countryStats = CountryStats{ m.countryStats = CountryStats{
counts: make(map[string]int), counts: make(map[string]int),
} }