Now exposing the key record types as metrics, updated the readme as well

main
Ray Slakinski 2021-04-09 10:18:28 -04:00
parent fb043cb4b1
commit db222e778b
2 changed files with 33 additions and 20 deletions

View File

@ -11,8 +11,15 @@ $ go run main.go
## Reading metrics ## Reading metrics
```bash ```bash
$ curl -s localhost:8080/metrics |grep ont |grep -v "#" $ curl -s localhost:8080/metrics |grep records
ont_covid_data_days_left 84
ont_covid_data_previous_day_doses_administered 108563 # HELP records_previous_day_doses_administered Previous Day Doses Administered
ont_covid_data_total_doses_administered 2.834784e+06 # TYPE records_previous_day_doses_administered gauge
records_previous_day_doses_administered 105382
# HELP records_total_doses_administered Total Doses Administered
# TYPE records_total_doses_administered gauge
records_total_doses_administered 2.940166e+06
# HELP records_total_doses_in_fully_vaccinated_individuals Total Doses In Fully Vaccinated Individuals
# TYPE records_total_doses_in_fully_vaccinated_individuals gauge
records_total_doses_in_fully_vaccinated_individuals 328598
``` ```

38
main.go
View File

@ -52,19 +52,20 @@ type OntCovidData struct {
} `json:"result"` } `json:"result"`
} }
var metrics struct { var records struct {
TotalDosesAdministered func() prometheus.Gauge `name:"total_doses_administered" help:"Total Doses Administered"` TotalDosesAdministered func() prometheus.Gauge `name:"total_doses_administered" help:"Total Doses Administered"`
PreviousDayDosesAdministered func() prometheus.Gauge `name:"previous_day_doses_administered" help:"Previous Day Doses Administered"` PreviousDayDosesAdministered func() prometheus.Gauge `name:"previous_day_doses_administered" help:"Previous Day Doses Administered"`
DaysLeft func() prometheus.Gauge `name:"days_left" help:"Days Left"` TotalDosesInFullyVaccinatedIndividuals func() prometheus.Gauge `name:"total_doses_in_fully_vaccinated_individuals" help:"Total Doses In Fully Vaccinated Individuals"`
TotalIndividualsFullyVaccinated func() prometheus.Gauge `name:"total_individuals_fully_vaccinated" help:"Total Individuals Fully Vaccinated"`
} }
func init() { func init() {
gotoprom.MustInit(&metrics, "ont_covid_data") gotoprom.MustInit(&records, "records")
} }
func updateMetrics() { func updateMetrics() {
data_url := "https://data.ontario.ca/api/3/action/datastore_search?sort=report_date+desc&limit=1&resource_id=8a89caa9-511c-4568-af89-7f2174b4378c" data_url := "https://data.ontario.ca/api/3/action/datastore_search?sort=report_date+desc&limit=1&resource_id=8a89caa9-511c-4568-af89-7f2174b4378c"
population := int64(11971129) // population := int64(11971129)
resp, getErr := http.Get(data_url) resp, getErr := http.Get(data_url)
if getErr != nil { if getErr != nil {
log.Fatalln(getErr) log.Fatalln(getErr)
@ -78,27 +79,32 @@ func updateMetrics() {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
totalString := strings.Replace(data.Result.Records[0].TotalDosesAdministered, ",", "", -1) totalDosesAdministeredString := strings.Replace(data.Result.Records[0].TotalDosesAdministered, ",", "", -1)
total, _ := strconv.ParseInt(totalString, 0, 64) totalDosesAdministered, _ := strconv.ParseInt(totalDosesAdministeredString, 0, 64)
prevDayString := strings.Replace(data.Result.Records[0].PreviousDayDosesAdministered, ",", "", -1) records.TotalDosesAdministered().Set(float64(totalDosesAdministered))
prevDay, _ := strconv.ParseInt(prevDayString, 0, 64)
// reportDate := data.Result.Records[0].ReportDate
days := (population - total) / prevDay previousDayDosesAdministeredString := strings.Replace(data.Result.Records[0].PreviousDayDosesAdministered, ",", "", -1)
previousDayDosesAdministered, _ := strconv.ParseInt(previousDayDosesAdministeredString, 0, 64)
records.PreviousDayDosesAdministered().Set(float64(previousDayDosesAdministered))
metrics.TotalDosesAdministered().Set(float64(total)) totalDosesInFullyVaccinatedIndividualsString := strings.Replace(data.Result.Records[0].TotalDosesInFullyVaccinatedIndividuals, ",", "", -1)
metrics.PreviousDayDosesAdministered().Set(float64(prevDay)) totalDosesInFullyVaccinatedIndividuals, _ := strconv.ParseInt(totalDosesInFullyVaccinatedIndividualsString, 0, 64)
metrics.DaysLeft().Set(float64(days)) records.TotalDosesInFullyVaccinatedIndividuals().Set(float64(totalDosesInFullyVaccinatedIndividuals))
totalIndividualsFullyVaccinatedString := strings.Replace(data.Result.Records[0].TotalIndividualsFullyVaccinated, ",", "", -1)
totalIndividualsFullyVaccinated, _ := strconv.ParseInt(totalIndividualsFullyVaccinatedString, 0, 64)
records.TotalDosesInFullyVaccinatedIndividuals().Set(float64(totalIndividualsFullyVaccinated))
} }
func main() { func main() {
c := cron.New() c := cron.New()
c.AddFunc("@hourly", func() { updateMetrics() }) c.AddFunc("@daily", func() { updateMetrics() })
c.Start() c.Start()
updateMetrics() updateMetrics()
flag.Parse() flag.Parse()
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
fmt.Println("Listening on port 8080")
log.Fatal(http.ListenAndServe(*addr, nil)) log.Fatal(http.ListenAndServe(*addr, nil))
} }