diff --git a/birdweather/forStation.go b/birdweather/forStation.go index 731ab43..1463279 100644 --- a/birdweather/forStation.go +++ b/birdweather/forStation.go @@ -32,7 +32,7 @@ func BirdsForStation(stationid string) (string, []structs.BirdCount) { return counts.Station.Name, result } -func RecordCountsForStationPastMinutes(stationId string, hours int) { +func RecordCountsForStationPastHours(stationId string, hours int) { client := graphql.NewClient("https://app.birdweather.com/graphql", http.DefaultClient) duration := InputDuration{ Count: hours, @@ -43,6 +43,7 @@ func RecordCountsForStationPastMinutes(stationId string, hours int) { panic(err) } fmt.Printf("Got %d species for station %s\n", len(counts.Station.TopSpecies), stationId) + metrics.RecordSpecies(counts.Station.Name, len(counts.Station.TopSpecies)) for _, count := range counts.Station.TopSpecies { metrics.RecordBird(counts.Station.Name, count.Species.CommonName, count.Breakdown.AlmostCertain) fmt.Printf("Recorded %d of %s\n", count.Breakdown.AlmostCertain, count.Species.CommonName) diff --git a/cmd/hourlyMetrics.go b/cmd/hourlyMetrics.go index e5509bf..7018b96 100644 --- a/cmd/hourlyMetrics.go +++ b/cmd/hourlyMetrics.go @@ -20,7 +20,7 @@ var hourlyMetricsCmd = &cobra.Command{ stationIds := viper.GetIntSlice("stations") for _, stationId := range stationIds { fmt.Printf("Recording counts for station %d\n", stationId) - birdweather.RecordCountsForStationPastMinutes(fmt.Sprint(stationId), 1) + birdweather.RecordCountsForStationPastHours(fmt.Sprint(stationId), 1) } }, } diff --git a/metrics/influx.go b/metrics/influx.go index f09e003..1fa1ef8 100644 --- a/metrics/influx.go +++ b/metrics/influx.go @@ -23,8 +23,10 @@ func initInflux(args []string) { command = "" } options := &influxdb.Options{} - options.SetApplicationName("birdweather") - options.AddDefaultTag("command", command) + options. + SetApplicationName("birdweather"). + AddDefaultTag("command", command). + AddDefaultTag("application", "birdweather") influxClient = influxdb.NewClientWithOptions( viper.GetString("influx.url"), viper.GetString("influx.token"), @@ -37,13 +39,13 @@ func initInflux(args []string) { func finishInflux() { duration := time.Since(invokeTime) p := influxdb.NewPointWithMeasurement("execution"). - AddField("latency", duration.Milliseconds()). - SetTime(time.Now()) + AddField("latency", duration.Milliseconds()) writePoint(p) influxClient.Close() } func writePoint(p *write.Point) { + p.SetTime(time.Now()) err := writeAPI.WritePoint(context.Background(), p) if err != nil { panic(err) @@ -54,8 +56,7 @@ func writePoint(p *write.Point) { func recordInfluxFetch(station string, speciesCount int) { p := influxdb.NewPointWithMeasurement("fetch"). AddTag("station", station). - AddField("species", speciesCount). - SetTime(time.Now()) + AddField("species", speciesCount) writePoint(p) } @@ -66,8 +67,7 @@ func recordInfluxInvoked() { func recordInfluxEmail(recipientCount int, bodyLength int) { p := influxdb.NewPointWithMeasurement("email"). AddField("recipients", recipientCount). - AddField("body_length", bodyLength). - SetTime(time.Now()) + AddField("body_length", bodyLength) writePoint(p) } @@ -75,7 +75,13 @@ func recordInfluxBird(stationName string, birdName string, count int) { p := influxdb.NewPointWithMeasurement("bird"). AddTag("station", stationName). AddTag("name", birdName). - AddField("count", count). - SetTime(time.Now()) + AddField("count", count) + writePoint(p) +} + +func recordInfluxSpecies(stationName string, count int) { + p := influxdb.NewPointWithMeasurement("species"). + AddTag("station", stationName). + AddField("count", count) writePoint(p) } diff --git a/metrics/root.go b/metrics/root.go index 7a8ccf3..9cbfcf7 100644 --- a/metrics/root.go +++ b/metrics/root.go @@ -46,3 +46,9 @@ func RecordBird(stationName string, birdName string, count int) { recordInfluxBird(stationName, birdName, count) } } + +func RecordSpecies(stationName string, count int) { + if produceMetrics { + recordInfluxSpecies(stationName, count) + } +}