This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(consul): collect server network lan rtt (#1035)
- Loading branch information
Showing
6 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package consul | ||
|
||
import ( | ||
"math" | ||
"time" | ||
|
||
"github.com/netdata/go.d.plugin/pkg/metrics" | ||
) | ||
|
||
const ( | ||
// https://developer.hashicorp.com/consul/api-docs/coordinate#read-lan-coordinates-for-all-nodes | ||
urlPathCoordinateNodes = "/v1/coordinate/nodes" | ||
) | ||
|
||
type nodeCoordinates struct { | ||
Node string | ||
Coord struct { | ||
Vec []float64 | ||
Error float64 | ||
Adjustment float64 | ||
Height float64 | ||
} | ||
} | ||
|
||
func (c *Consul) collectNetworkRTT(mx map[string]int64) error { | ||
var coords []nodeCoordinates | ||
|
||
if err := c.doOKDecode(urlPathCoordinateNodes, &coords); err != nil { | ||
return err | ||
} | ||
|
||
var thisNode nodeCoordinates | ||
var ok bool | ||
|
||
coords, thisNode, ok = removeNodeCoordinates(coords, c.cfg.Config.NodeName) | ||
if !ok || len(coords) == 0 { | ||
return nil | ||
} | ||
|
||
sum := metrics.NewSummary() | ||
for _, v := range coords { | ||
d := calcDistance(thisNode, v) | ||
sum.Observe(d.Seconds()) | ||
} | ||
sum.WriteTo(mx, "network_lan_rtt", 1e9, 1) | ||
|
||
return nil | ||
} | ||
|
||
func calcDistance(a, b nodeCoordinates) time.Duration { | ||
// https://developer.hashicorp.com/consul/docs/architecture/coordinates#working-with-coordinates | ||
sum := 0.0 | ||
for i := 0; i < len(a.Coord.Vec); i++ { | ||
diff := a.Coord.Vec[i] - b.Coord.Vec[i] | ||
sum += diff * diff | ||
} | ||
|
||
rtt := math.Sqrt(sum) + a.Coord.Height + b.Coord.Height | ||
|
||
adjusted := rtt + a.Coord.Adjustment + b.Coord.Adjustment | ||
if adjusted > 0.0 { | ||
rtt = adjusted | ||
} | ||
|
||
return time.Duration(rtt * 1e9) // nanoseconds | ||
} | ||
|
||
func removeNodeCoordinates(coords []nodeCoordinates, node string) ([]nodeCoordinates, nodeCoordinates, bool) { | ||
for i, v := range coords { | ||
if v.Node == node { | ||
return append(coords[:i], coords[i+1:]...), v, true | ||
} | ||
} | ||
return coords, nodeCoordinates{}, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
modules/consul/testdata/v1.13.2/server_v1-coordinate-nodes.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
[ | ||
{ | ||
"Node": "satya-vm", | ||
"Segment": "", | ||
"Coord": { | ||
"Vec": [ | ||
0.014829503547751722, | ||
0.0072173849395880596, | ||
0.004329474334739038, | ||
-0.0032798752739064438, | ||
-0.010134170963372591, | ||
-0.008257638503292454, | ||
0.00752142875530981, | ||
0.0017901665053347217 | ||
], | ||
"Error": 0.493977389081921, | ||
"Adjustment": 0.00017401717315766792, | ||
"Height": 2.8272088782225915e-05 | ||
} | ||
}, | ||
{ | ||
"Node": "satya-vm2", | ||
"Segment": "", | ||
"Coord": { | ||
"Vec": [ | ||
0.01485399579339927, | ||
0.007233318963330601, | ||
0.004314864811042585, | ||
-0.0032764668107421653, | ||
-0.010133938771787391, | ||
-0.008238915750721635, | ||
0.0075168683512753035, | ||
0.001776534386752108 | ||
], | ||
"Error": 0.3003366063730667, | ||
"Adjustment": 0.00019935098724887628, | ||
"Height": 4.192904954404545e-05 | ||
} | ||
}, | ||
{ | ||
"Node": "satya-vm3", | ||
"Segment": "", | ||
"Coord": { | ||
"Vec": [ | ||
0.014782092899311995, | ||
0.007186516660508205, | ||
0.004357885422476095, | ||
-0.003286526239099157, | ||
-0.010134722455521066, | ||
-0.008294075475167818, | ||
0.007530358624901773, | ||
0.0018166544975743123 | ||
], | ||
"Error": 0.12048664650994341, | ||
"Adjustment": 0.00014477073973997567, | ||
"Height": 0.0005656138448826895 | ||
} | ||
} | ||
] |