-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
122 lines (105 loc) · 2.72 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type CityStats struct {
Zone string
}
//temperature by country
func (c *CityStats) TemperatureAndHumidity() (
tempByCity map[string]float64, humidityByCity map[string]float64,
) {
// get real time API temp data here
tempByCity = make(map[string]float64)
dat, err := getTempData()
if err != nil {
fmt.Println(err)
return
}
if len(dat.Data.Timestep) == 0 {
fmt.Println("empty result!")
return
}
cities := []string{"bangalore", "london"}
for ind, interval := range dat.Data.Timestep[0].TempVal {
tempByCity[cities[ind%2]] = interval.Values.Temp
}
humidityByCity = map[string]float64{
"bangalore": rand.Float64(),
"london": rand.Float64(),
}
return
}
type CityStatsCollector struct {
CityStats *CityStats
}
var (
tempDesc = prometheus.NewDesc(
"temperature_city_fahrenheit",
"temperature of a city in fahrenheit",
[]string{"city"}, nil,
)
humidityDesc = prometheus.NewDesc(
"humidity_city_fraction",
"humidity of a city as a fraction",
[]string{"city"}, nil,
)
httpDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "http_response_time_seconds",
Help: "Duration of HTTP requests.",
Buckets: prometheus.LinearBuckets(20, 5, 5),
})
)
func (cc CityStatsCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(cc, ch)
}
func (cc CityStatsCollector) Collect(ch chan<- prometheus.Metric) {
begin := time.Now()
tempByCity, humidityByCity := cc.CityStats.TemperatureAndHumidity()
duration := time.Since(begin)
httpDuration.Observe(float64(duration))
for city, temp := range tempByCity {
ch <- prometheus.MustNewConstMetric(
tempDesc,
prometheus.CounterValue,
float64(temp),
city,
)
}
for city, humidity := range humidityByCity {
ch <- prometheus.MustNewConstMetric(
humidityDesc,
prometheus.GaugeValue,
humidity,
city,
)
}
}
func NewCityStats(zone string, reg prometheus.Registerer) *CityStats {
c := &CityStats{
Zone: zone,
}
cc := CityStatsCollector{CityStats: c}
prometheus.WrapRegistererWith(prometheus.Labels{"zone": zone}, reg).MustRegister(cc)
return c
}
func main() {
reg := prometheus.NewRegistry()
NewCityStats("db", reg)
NewCityStats("ca", reg)
// Add the standard process and Go metrics to the custom registry.
reg.MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
collectors.NewGoCollector(),
httpDuration,
)
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(":2112", nil))
}