Skip to content

Commit

Permalink
add metric for species count
Browse files Browse the repository at this point in the history
  • Loading branch information
waisbrot committed Jan 2, 2024
1 parent 5bfc978 commit 72ee378
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion birdweather/forStation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/hourlyMetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
26 changes: 16 additions & 10 deletions metrics/influx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -66,16 +67,21 @@ 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)
}

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)
}
6 changes: 6 additions & 0 deletions metrics/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 72ee378

Please sign in to comment.