feat(inputs.internet_speed): Add the best server selection via latency and jitter field (#12707)
This commit is contained in:
parent
312fb04b68
commit
4cd0a647c0
2
go.mod
2
go.mod
|
|
@ -149,7 +149,7 @@ require (
|
|||
github.com/safchain/ethtool v0.0.0-20210803160452-9aa261dae9b1
|
||||
github.com/sensu/sensu-go/api/core/v2 v2.15.0
|
||||
github.com/shirou/gopsutil/v3 v3.22.12
|
||||
github.com/showwin/speedtest-go v1.2.1
|
||||
github.com/showwin/speedtest-go v1.4.2
|
||||
github.com/signalfx/golib/v3 v3.3.46
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/sleepinggenius2/gosmi v0.4.4
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -2074,8 +2074,8 @@ github.com/shirou/gopsutil/v3 v3.22.12/go.mod h1:Xd7P1kwZcp5VW52+9XsirIKd/BROzbb
|
|||
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
|
||||
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
|
||||
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/showwin/speedtest-go v1.2.1 h1:5GrQFGn5N4YRBCaiph6ay6Py9yL2k7Ja10bbUZl9HPE=
|
||||
github.com/showwin/speedtest-go v1.2.1/go.mod h1:dJugxvC/AQDt4HQQKZ9lKNa2+b1c8nzj9IL0a/F8l1U=
|
||||
github.com/showwin/speedtest-go v1.4.2 h1:3YjBajURQTJCv/rVwJsd5UtCYlaiqCihg5NhPxJapk8=
|
||||
github.com/showwin/speedtest-go v1.4.2/go.mod h1:Y7c+pxzaNAlo4mYP+x83pnYY8IM3bkHGDhTdrgUnkNE=
|
||||
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
|
||||
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
|
||||
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
|
||||
|
|
|
|||
|
|
@ -52,10 +52,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
|||
It collects the following fields:
|
||||
|
||||
| Name | field name | type | Unit |
|
||||
| -------------- | ---------- | ------- | ---- |
|
||||
|----------------|------------| ------- | ---- |
|
||||
| Download Speed | download | float64 | Mbps |
|
||||
| Upload Speed | upload | float64 | Mbps |
|
||||
| Latency | latency | float64 | ms |
|
||||
| Jitter | jitter | float64 | ms |
|
||||
|
||||
And the following tags:
|
||||
|
||||
|
|
@ -67,5 +68,5 @@ And the following tags:
|
|||
## Example Output
|
||||
|
||||
```sh
|
||||
internet_speed,host=speedtest02.z4internet.com:8080,server_id=54619 download=318.37580265897725,upload=30.444407341274385,latency=37.73174 1675458921000000000
|
||||
internet_speed,host=speedtest02.z4internet.com:8080,server_id=54619 download=318.37580265897725,upload=30.444407341274385,latency=37.73174,jitter=1.99810 1675458921000000000
|
||||
```
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ package internet_speed
|
|||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/showwin/speedtest-go/speedtest"
|
||||
|
|
@ -40,7 +41,7 @@ func (is *InternetSpeed) Init() error {
|
|||
is.MemorySavingMode = is.MemorySavingMode || is.EnableFileDownload
|
||||
|
||||
var err error
|
||||
is.serverFilter, err = filter.NewIncludeExcludeFilter(is.ServerIDInclude, is.ServerIDExclude)
|
||||
is.serverFilter, err = filter.NewIncludeExcludeFilterDefaults(is.ServerIDInclude, is.ServerIDExclude, false, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error compiling server ID filters: %w", err)
|
||||
}
|
||||
|
|
@ -49,7 +50,7 @@ func (is *InternetSpeed) Init() error {
|
|||
}
|
||||
|
||||
func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
||||
// if not caching, go find closest server each time
|
||||
// if not caching, go find the closest server each time
|
||||
if !is.Cache || is.server == nil {
|
||||
if err := is.findClosestServer(); err != nil {
|
||||
return fmt.Errorf("unable to find closest server: %w", err)
|
||||
|
|
@ -73,12 +74,14 @@ func (is *InternetSpeed) Gather(acc telegraf.Accumulator) error {
|
|||
"download": is.server.DLSpeed,
|
||||
"upload": is.server.ULSpeed,
|
||||
"latency": timeDurationMillisecondToFloat64(is.server.Latency),
|
||||
"jitter": timeDurationMillisecondToFloat64(is.server.Jitter),
|
||||
}
|
||||
tags := map[string]string{
|
||||
"server_id": is.server.ID,
|
||||
"host": is.server.Host,
|
||||
}
|
||||
|
||||
// recycle the detailed data of each test to prevent data backlog
|
||||
is.server.Context.Reset()
|
||||
acc.AddFields(measurement, fields, tags)
|
||||
return nil
|
||||
}
|
||||
|
|
@ -97,16 +100,30 @@ func (is *InternetSpeed) findClosestServer() error {
|
|||
return fmt.Errorf("no servers found")
|
||||
}
|
||||
|
||||
// return the first match
|
||||
for _, server := range serverList {
|
||||
// return the first match or the server with the lowest latency
|
||||
// when filter mismatch all servers.
|
||||
var min int64 = math.MaxInt64
|
||||
selectIndex := -1
|
||||
for index, server := range serverList {
|
||||
if is.serverFilter.Match(server.ID) {
|
||||
is.server = server
|
||||
is.Log.Debugf("using server %s in %s (%s)\n", is.server.ID, is.server.Name, is.server.Host)
|
||||
return nil
|
||||
selectIndex = index
|
||||
break
|
||||
}
|
||||
if server.Latency > 0 {
|
||||
if min > server.Latency.Milliseconds() {
|
||||
min = server.Latency.Milliseconds()
|
||||
selectIndex = index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("no server set: filter excluded all servers")
|
||||
if selectIndex != -1 {
|
||||
is.server = serverList[selectIndex]
|
||||
is.Log.Debugf("using server %s in %s (%s)\n", is.server.ID, is.server.Name, is.server.Host)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("no server set: filter excluded all servers or no available server found")
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue