Skip to content

Commit

Permalink
histogram: part 1
Browse files Browse the repository at this point in the history
need to convert to a histogram graph
  • Loading branch information
metonymic-smokey committed Sep 16, 2021
1 parent 8308972 commit fe332ad
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"math/rand"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -26,6 +27,7 @@ func (c *CityStats) TemperatureAndHumidity() (
return
}
if len(dat.Data.Timestep) == 0 {
fmt.Println("empty result!")
return
}

Expand Down Expand Up @@ -57,14 +59,23 @@ var (
"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,
Expand Down Expand Up @@ -102,6 +113,7 @@ func main() {
reg.MustRegister(
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
prometheus.NewGoCollector(),
httpDuration,
)

http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
Expand Down

0 comments on commit fe332ad

Please sign in to comment.