forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s2geo.go
78 lines (67 loc) · 1.66 KB
/
s2geo.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
package geo
import (
"fmt"
"github.com/golang/geo/s2"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/processors"
)
type Geo struct {
LatField string `toml:"lat_field"`
LonField string `toml:"lon_field"`
TagKey string `toml:"tag_key"`
CellLevel int `toml:"cell_level"`
}
var SampleConfig = `
## The name of the lat and lon fields containing WGS-84 latitude and
## longitude in decimal degrees.
# lat_field = "lat"
# lon_field = "lon"
## New tag to create
# tag_key = "s2_cell_id"
## Cell level (see https://s2geometry.io/resources/s2cell_statistics.html)
# cell_level = 9
`
func (g *Geo) SampleConfig() string {
return SampleConfig
}
func (g *Geo) Description() string {
return "Add the S2 Cell ID as a tag based on latitude and longitude fields"
}
func (g *Geo) Init() error {
if g.CellLevel < 0 || g.CellLevel > 30 {
return fmt.Errorf("invalid cell level %d", g.CellLevel)
}
return nil
}
func (g *Geo) Apply(in ...telegraf.Metric) []telegraf.Metric {
for _, point := range in {
var latOk, lonOk bool
var lat, lon float64
for _, field := range point.FieldList() {
switch field.Key {
case g.LatField:
lat, latOk = field.Value.(float64)
case g.LonField:
lon, lonOk = field.Value.(float64)
}
}
if latOk && lonOk {
cellID := s2.CellIDFromLatLng(s2.LatLngFromDegrees(lat, lon))
if cellID.IsValid() {
value := cellID.Parent(g.CellLevel).ToToken()
point.AddTag(g.TagKey, value)
}
}
}
return in
}
func init() {
processors.Add("s2geo", func() telegraf.Processor {
return &Geo{
LatField: "lat",
LonField: "lon",
TagKey: "s2_cell_id",
CellLevel: 9,
}
})
}