stathat.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "github.com/stathat/go"
  4. "log"
  5. "runtime"
  6. "strings"
  7. "time"
  8. )
  9. func (zs *Zones) statHatPoster() {
  10. if len(Config.StatHat.ApiKey) == 0 {
  11. return
  12. }
  13. stathatGroups := append(serverGroups, "total", serverID)
  14. suffix := strings.Join(stathatGroups, ",")
  15. lastCounts := map[string]int64{}
  16. lastEdnsCounts := map[string]int64{}
  17. for name, zone := range *zs {
  18. if zone.Logging.StatHat == true {
  19. lastCounts[name] = zone.Metrics.Queries.Count()
  20. lastEdnsCounts[name] = zone.Metrics.EdnsQueries.Count()
  21. }
  22. }
  23. for {
  24. time.Sleep(60 * time.Second)
  25. for name, zone := range *zs {
  26. count := zone.Metrics.Queries.Count()
  27. newCount := count - lastCounts[name]
  28. lastCounts[name] = count
  29. if zone.Logging != nil && zone.Logging.StatHat == true {
  30. apiKey := zone.Logging.StatHatAPI
  31. if len(apiKey) == 0 {
  32. apiKey = Config.StatHat.ApiKey
  33. }
  34. if len(apiKey) == 0 {
  35. continue
  36. }
  37. stathat.PostEZCount("zone "+name+" queries~"+suffix, Config.StatHat.ApiKey, int(newCount))
  38. ednsCount := zone.Metrics.EdnsQueries.Count()
  39. newEdnsCount := ednsCount - lastEdnsCounts[name]
  40. lastEdnsCounts[name] = ednsCount
  41. stathat.PostEZCount("zone "+name+" edns queries~"+suffix, Config.StatHat.ApiKey, int(newEdnsCount))
  42. }
  43. }
  44. }
  45. }
  46. func statHatPoster() {
  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. }