Skip to content

Commit 2f97fa5

Browse files
committed
Add test uuid, to help with outer joins
1 parent 313fe15 commit 2f97fa5

File tree

3 files changed

+96
-57
lines changed

3 files changed

+96
-57
lines changed

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ go 1.16
44

55
require (
66
github.com/golang/protobuf v1.5.1 // indirect
7+
github.com/google/uuid v1.2.0
78
github.com/prometheus/client_golang v1.10.0
89
github.com/prometheus/common v0.18.0
910
github.com/showwin/speedtest-go v1.1.2
10-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 // indirect
11+
github.com/stretchr/testify v1.7.0 // indirect
12+
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
1113
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
1214
)

go.sum

+8-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
101101
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
102102
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
103103
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
104+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
105+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
104106
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
105107
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
106108
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
@@ -264,8 +266,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
264266
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
265267
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
266268
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
267-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
268269
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
270+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
271+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
269272
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
270273
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
271274
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
@@ -345,8 +348,8 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
345348
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
346349
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
347350
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
348-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 h1:64ChN/hjER/taL4YJuA+gpLfIMT+/NFherRZixbxOhg=
349-
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
351+
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs=
352+
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
350353
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
351354
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
352355
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -413,8 +416,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
413416
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
414417
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
415418
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
416-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
417419
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
420+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
421+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
418422
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
419423
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
420424
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/exporter/exporter.go

+85-52
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/google/uuid"
78
"github.com/prometheus/client_golang/prometheus"
89
"github.com/prometheus/common/log"
910
"github.com/showwin/speedtest-go/speedtest"
@@ -17,29 +18,29 @@ var (
1718
up = prometheus.NewDesc(
1819
prometheus.BuildFQName(namespace, "", "up"),
1920
"Was the last speedtest successful.",
20-
nil, nil,
21+
[]string{"test_uuid"}, nil,
2122
)
2223
scrapeDurationSeconds = prometheus.NewDesc(
2324
prometheus.BuildFQName(namespace, "", "scrape_duration_seconds"),
2425
"Time to preform last speed test",
25-
nil, nil,
26+
[]string{"test_uuid"}, nil,
2627
)
2728
latency = prometheus.NewDesc(
2829
prometheus.BuildFQName(namespace, "", "latency_seconds"),
2930
"Measured latency on last speed test",
30-
[]string{"user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
31+
[]string{"test_uuid", "user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
3132
nil,
3233
)
3334
upload = prometheus.NewDesc(
3435
prometheus.BuildFQName(namespace, "", "upload_speed_Bps"),
3536
"Last upload speedtest result",
36-
[]string{"user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
37+
[]string{"test_uuid", "user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
3738
nil,
3839
)
3940
download = prometheus.NewDesc(
4041
prometheus.BuildFQName(namespace, "", "download_speed_Bps"),
4142
"Last download speedtest result",
42-
[]string{"user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
43+
[]string{"test_uuid", "user_lat", "user_lon", "user_ip", "user_isp", "server_lat", "server_lon", "server_id", "server_name", "server_country", "distance"},
4344
nil,
4445
)
4546
)
@@ -66,24 +67,28 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
6667
// as Prometheus metrics. It implements prometheus.Collector.
6768
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
6869
start := time.Now()
69-
ok := e.speedtest(ch)
70+
testUUID := uuid.New().String()
71+
ok := e.speedtest(testUUID, ch)
7072
d := time.Since(start).Seconds()
7173

7274
if ok {
7375
ch <- prometheus.MustNewConstMetric(
7476
up, prometheus.GaugeValue, 1.0,
77+
testUUID,
7578
)
7679
ch <- prometheus.MustNewConstMetric(
7780
scrapeDurationSeconds, prometheus.GaugeValue, d,
81+
testUUID,
7882
)
7983
} else {
8084
ch <- prometheus.MustNewConstMetric(
8185
up, prometheus.GaugeValue, 0.0,
86+
testUUID,
8287
)
8388
}
8489
}
8590

86-
func (e *Exporter) speedtest(ch chan<- prometheus.Metric) bool {
91+
func (e *Exporter) speedtest(testUUID string, ch chan<- prometheus.Metric) bool {
8792
user, err := speedtest.FetchUserInfo()
8893
if err != nil {
8994
log.Errorf("could not fetch user information: %s", err.Error())
@@ -100,59 +105,87 @@ func (e *Exporter) speedtest(ch chan<- prometheus.Metric) bool {
100105
servers := serverList.Servers
101106
server := servers[0]
102107

103-
if err := server.PingTest(); err == nil {
104-
ch <- prometheus.MustNewConstMetric(
105-
latency, prometheus.GaugeValue, server.Latency.Seconds(),
106-
user.Lat,
107-
user.Lon,
108-
user.IP,
109-
user.Isp,
110-
server.Lat,
111-
server.Lon,
112-
server.ID,
113-
server.Name,
114-
server.Country,
115-
fmt.Sprintf("%f", server.Distance),
116-
)
117-
} else {
108+
ok := pingTest(testUUID, user, server, ch)
109+
ok = downloadTest(testUUID, user, server, ch) && ok
110+
ok = uploadTest(testUUID, user, server, ch) && ok
111+
112+
if ok {
113+
return true
114+
}
115+
return false
116+
}
117+
118+
func pingTest(testUUID string, user *speedtest.User, server *speedtest.Server, ch chan<- prometheus.Metric) bool {
119+
err := server.PingTest()
120+
if err != nil {
118121
log.Errorf("failed to carry out ping test: %s", err.Error())
122+
return false
119123
}
120124

121-
if err := server.DownloadTest(false); err == nil {
122-
ch <- prometheus.MustNewConstMetric(
123-
download, prometheus.GaugeValue, server.DLSpeed*1024*1024,
124-
user.Lat,
125-
user.Lon,
126-
user.IP,
127-
user.Isp,
128-
server.Lat,
129-
server.Lon,
130-
server.ID,
131-
server.Name,
132-
server.Country,
133-
fmt.Sprintf("%f", server.Distance),
134-
)
135-
} else {
125+
ch <- prometheus.MustNewConstMetric(
126+
latency, prometheus.GaugeValue, server.Latency.Seconds(),
127+
testUUID,
128+
user.Lat,
129+
user.Lon,
130+
user.IP,
131+
user.Isp,
132+
server.Lat,
133+
server.Lon,
134+
server.ID,
135+
server.Name,
136+
server.Country,
137+
fmt.Sprintf("%f", server.Distance),
138+
)
139+
140+
return true
141+
}
142+
143+
func downloadTest(testUUID string, user *speedtest.User, server *speedtest.Server, ch chan<- prometheus.Metric) bool {
144+
err := server.DownloadTest(false)
145+
if err != nil {
136146
log.Errorf("failed to carry out download test: %s", err.Error())
147+
return false
137148
}
138149

139-
if err := server.UploadTest(false); err == nil {
140-
ch <- prometheus.MustNewConstMetric(
141-
upload, prometheus.GaugeValue, server.ULSpeed*1024*1024,
142-
user.Lat,
143-
user.Lon,
144-
user.IP,
145-
user.Isp,
146-
server.Lat,
147-
server.Lon,
148-
server.ID,
149-
server.Name,
150-
server.Country,
151-
fmt.Sprintf("%f", server.Distance),
152-
)
153-
} else {
150+
ch <- prometheus.MustNewConstMetric(
151+
download, prometheus.GaugeValue, server.DLSpeed*1024*1024,
152+
testUUID,
153+
user.Lat,
154+
user.Lon,
155+
user.IP,
156+
user.Isp,
157+
server.Lat,
158+
server.Lon,
159+
server.ID,
160+
server.Name,
161+
server.Country,
162+
fmt.Sprintf("%f", server.Distance),
163+
)
164+
165+
return true
166+
}
167+
168+
func uploadTest(testUUID string, user *speedtest.User, server *speedtest.Server, ch chan<- prometheus.Metric) bool {
169+
err := server.UploadTest(false)
170+
if err != nil {
154171
log.Errorf("failed to carry out upload test: %s", err.Error())
172+
return false
155173
}
156174

175+
ch <- prometheus.MustNewConstMetric(
176+
upload, prometheus.GaugeValue, server.ULSpeed*1024*1024,
177+
testUUID,
178+
user.Lat,
179+
user.Lon,
180+
user.IP,
181+
user.Isp,
182+
server.Lat,
183+
server.Lon,
184+
server.ID,
185+
server.Name,
186+
server.Country,
187+
fmt.Sprintf("%f", server.Distance),
188+
)
189+
157190
return true
158191
}

0 commit comments

Comments
 (0)