stathat.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "log"
  4. "runtime"
  5. "strings"
  6. "time"
  7. "github.com/abh/geodns/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
  8. "github.com/abh/geodns/Godeps/_workspace/src/github.com/stathat/go"
  9. )
  10. func (zs *Zones) statHatPoster() {
  11. if len(Config.StatHat.ApiKey) == 0 {
  12. return
  13. }
  14. stathatGroups := append(serverGroups, "total", serverID)
  15. suffix := strings.Join(stathatGroups, ",")
  16. lastCounts := map[string]int64{}
  17. lastEdnsCounts := map[string]int64{}
  18. for name, zone := range *zs {
  19. lastCounts[name] = zone.Metrics.Queries.Count()
  20. lastEdnsCounts[name] = zone.Metrics.EdnsQueries.Count()
  21. }
  22. for {
  23. time.Sleep(60 * time.Second)
  24. for name, zone := range *zs {
  25. count := zone.Metrics.Queries.Count()
  26. newCount := count - lastCounts[name]
  27. lastCounts[name] = count
  28. if zone.Logging != nil && zone.Logging.StatHat == true {
  29. apiKey := zone.Logging.StatHatAPI
  30. if len(apiKey) == 0 {
  31. apiKey = Config.StatHat.ApiKey
  32. }
  33. if len(apiKey) == 0 {
  34. continue
  35. }
  36. stathat.PostEZCount("zone "+name+" queries~"+suffix, Config.StatHat.ApiKey, int(newCount))
  37. ednsCount := zone.Metrics.EdnsQueries.Count()
  38. newEdnsCount := ednsCount - lastEdnsCounts[name]
  39. lastEdnsCounts[name] = ednsCount
  40. stathat.PostEZCount("zone "+name+" edns queries~"+suffix, Config.StatHat.ApiKey, int(newEdnsCount))
  41. }
  42. }
  43. }
  44. }
  45. func statHatPoster() {
  46. qCounter := metrics.Get("queries").(metrics.Meter)
  47. lastQueryCount := qCounter.Count()
  48. stathatGroups := append(serverGroups, "total", serverID)
  49. suffix := strings.Join(stathatGroups, ",")
  50. // stathat.Verbose = true
  51. for {
  52. time.Sleep(60 * time.Second)
  53. if !Config.Flags.HasStatHat {
  54. log.Println("No stathat configuration")
  55. continue
  56. }
  57. log.Println("Posting to stathat")
  58. current := qCounter.Count()
  59. newQueries := current - lastQueryCount
  60. lastQueryCount = current
  61. stathat.PostEZCount("queries~"+suffix, Config.StatHat.ApiKey, int(newQueries))
  62. stathat.PostEZValue("goroutines "+serverID, Config.StatHat.ApiKey, float64(runtime.NumGoroutine()))
  63. }
  64. }