From 6ed1c03fe8de8be7ecf37749ab519355003d4639 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Thu, 18 Nov 2021 18:08:38 +0100 Subject: [PATCH] =?UTF-8?q?Add=20datacenter=20co=C3=B6rdinates=20(#81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/datacenter_cache.go | 26 +++++++++++++++++++++----- pkg/api/datacenter_cache_test.go | 4 ++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/api/datacenter_cache.go b/pkg/api/datacenter_cache.go index 745d52d..c3abdd0 100644 --- a/pkg/api/datacenter_cache.go +++ b/pkg/api/datacenter_cache.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "sort" + "strconv" "sync" "github.com/prometheus/client_golang/prometheus" @@ -13,9 +14,16 @@ import ( // 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 @@ -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} @@ -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...) } } diff --git a/pkg/api/datacenter_cache_test.go b/pkg/api/datacenter_cache_test.go index 34e65ba..dd343a9 100644 --- a/pkg/api/datacenter_cache_test.go +++ b/pkg/api/datacenter_cache_test.go @@ -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}}, }, }, {