Skip to content

Commit

Permalink
Add serverID options
Browse files Browse the repository at this point in the history
  • Loading branch information
dwillcocks committed Jul 26, 2021
1 parent c6444ff commit 3a5f264
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ A [Speedtest](https://www.speedtest.net) exporter for Prometheus.
$ ./speedtest_exporter --help
Usage of speedtest_exporter
-port string
Listening port for prometheus endpoint (default 9090)
listening port to expose metrics on (default "9090")
-server_fallback
If the serverID given is not available, should we fallback to closest available server
-server_id int
Speedtest.net server ID to run test against, -1 will pick the closest server to your location (default -1)

```

Expand Down
4 changes: 3 additions & 1 deletion cmd/speedtest_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const (

func main() {
port := flag.String("port", "9090", "listening port to expose metrics on")
serverID := flag.Int("server_id", -1, "Speedtest.net server ID to run test against, -1 will pick the closest server to your location")
serverFallback := flag.Bool("server_fallback", false, "If the server_id given is not available, should we fallback to closest available server")
flag.Parse()

exporter, err := exporter.New()
exporter, err := exporter.New(*serverID, *serverFallback)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.16

require (
github.com/golang/protobuf v1.5.1 // indirect
github.com/google/uuid v1.0.0
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0 // indirect
github.com/showwin/speedtest-go v1.1.2
github.com/showwin/speedtest-go v1.1.3
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
13 changes: 7 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -120,8 +120,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -176,8 +176,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/showwin/speedtest-go v1.1.2 h1:QPWG8UWQaert3/F2Vm692GhHgM3+sHi5v7o5llG5JP4=
github.com/showwin/speedtest-go v1.1.2/go.mod h1:Evr4so/j097J4zgdEyYvaBhzyKMgrTNUOwFQcXqUUzc=
github.com/showwin/speedtest-go v1.1.3 h1:nM1XYWyUr6E6pYR4ZAborvSlexvMRuzfrYjXeJlWmlg=
github.com/showwin/speedtest-go v1.1.3/go.mod h1:dJugxvC/AQDt4HQQKZ9lKNa2+b1c8nzj9IL0a/F8l1U=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
Expand Down Expand Up @@ -277,8 +277,9 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
39 changes: 30 additions & 9 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ var (

// Exporter runs speedtest and exports them using
// the prometheus metrics package.
type Exporter struct{}
type Exporter struct {
serverID int
serverFallback bool
}

// New returns an initialized Exporter.
func New() (*Exporter, error) {
return &Exporter{}, nil
func New(serverID int, serverFallback bool) (*Exporter, error) {
return &Exporter{
serverID: serverID,
serverFallback: serverFallback,
}, nil
}

// Describe describes all the metrics. It implements prometheus.Collector.
Expand All @@ -66,18 +72,17 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// Collect fetches the stats from Starlink dish and delivers them
// as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
testUUID := uuid.New().String()
start := time.Now()
ok := e.speedtest(testUUID, ch)
d := time.Since(start).Seconds()

if ok {
ch <- prometheus.MustNewConstMetric(
up, prometheus.GaugeValue, 1.0,
testUUID,
)
ch <- prometheus.MustNewConstMetric(
scrapeDurationSeconds, prometheus.GaugeValue, d,
scrapeDurationSeconds, prometheus.GaugeValue, time.Since(start).Seconds(),
testUUID,
)
} else {
Expand All @@ -101,9 +106,25 @@ func (e *Exporter) speedtest(testUUID string, ch chan<- prometheus.Metric) bool
log.Errorf("could not fetch server list: %s", err.Error())
return false
}
// taking the closes server
servers := serverList.Servers
server := servers[0]

var server *speedtest.Server

if e.serverID == -1 {
server = serverList.Servers[0]
} else {
servers, err := serverList.FindServer([]int{e.serverID})
if err != nil {
log.Error(err)
return false
}

if servers[0].ID != fmt.Sprintf("%d", e.serverID) && !e.serverFallback {
log.Errorf("could not find your choosen server ID %d in the list of avaiable servers, server_fallback is not set so failing this test", e.serverID)
return false
}

server = servers[0]
}

ok := pingTest(testUUID, user, server, ch)
ok = downloadTest(testUUID, user, server, ch) && ok
Expand Down

0 comments on commit 3a5f264

Please sign in to comment.