Skip to content

Commit

Permalink
Add datacenter coördinates (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon authored Nov 18, 2021
1 parent 69802ce commit 6ed1c03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 21 additions & 5 deletions pkg/api/datacenter_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import (
"fmt"
"net/http"
"sort"
"strconv"
"sync"

"github.com/prometheus/client_golang/prometheus"
)

// Datacenter models a single datacenter as returned by the Fastly API.
type Datacenter struct {
Code string `json:"code"` // Prometheus label is "datacenter" to make grouping at query time less tedious
Name string `json:"name"`
Group string `json:"group"`
Code string `json:"code"` // Prometheus label is "datacenter" to make grouping at query time less tedious
Name string `json:"name"`
Group string `json:"group"`
Coördinates Coördinates `json:"coordinates"`
}

// Coördinates of a specific datacenter.
type Coördinates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}

// DatacenterCache polls api.fastly.com/datacenters and maintains a local cache
Expand Down Expand Up @@ -87,7 +95,7 @@ func (c *DatacenterCache) Gatherer(namespace, subsystem string) (prometheus.Gath
var (
fqName = prometheus.BuildFQName(namespace, subsystem, "datacenter_info")
help = "Metadata about Fastly datacenters."
labels = []string{"datacenter", "name", "group"}
labels = []string{"datacenter", "name", "group", "latitude", "longitude"}
constLabels = prometheus.Labels{}
desc = prometheus.NewDesc(fqName, help, labels, constLabels)
collector = &datacenterCollector{desc: desc, cache: c}
Expand All @@ -112,6 +120,14 @@ func (c *datacenterCollector) Describe(ch chan<- *prometheus.Desc) {

func (c *datacenterCollector) Collect(ch chan<- prometheus.Metric) {
for _, dc := range c.cache.Datacenters() {
ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, 1, dc.Code, dc.Name, dc.Group)
var (
desc = c.desc
valueType = prometheus.GaugeValue
value = 1.0
latitude = strconv.FormatFloat(dc.Coördinates.Latitude, 'f', -1, 64)
longitude = strconv.FormatFloat(dc.Coördinates.Longitude, 'f', -1, 64)
labelValues = []string{dc.Code, dc.Name, dc.Group, latitude, longitude}
)
ch <- prometheus.MustNewConstMetric(desc, valueType, value, labelValues...)
}
}
4 changes: 2 additions & 2 deletions pkg/api/datacenter_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestDatacenterCache(t *testing.T) {
client: fixedResponseClient{code: http.StatusOK, response: datacentersResponseSmall},
wantErr: nil,
wantDCs: []api.Datacenter{
{Code: "AMS", Name: "Amsterdam", Group: "Europe"},
{Code: "WLG", Name: "Wellington", Group: "Asia/Pacific"},
{Code: "AMS", Name: "Amsterdam", Group: "Europe", Coördinates: api.Coördinates{Latitude: 52.308613, Longitude: 4.763889}},
{Code: "WLG", Name: "Wellington", Group: "Asia/Pacific", Coördinates: api.Coördinates{Latitude: -41.327221, Longitude: 174.805278}},
},
},
{
Expand Down

0 comments on commit 6ed1c03

Please sign in to comment.