stathat.go 2.0 KB

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