Skip to content

Commit dc202c4

Browse files
committed
Send agent metrics
1 parent 6516216 commit dc202c4

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

agent/agent.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"os"
1717
"os/user"
1818
"path/filepath"
19-
"reflect"
2019
"sort"
2120
"strconv"
2221
"strings"
@@ -1221,11 +1220,11 @@ func (a *agent) startReportingConnectionStats(ctx context.Context) {
12211220
// Convert from microseconds to milliseconds.
12221221
stats.ConnectionMedianLatencyMS /= 1000
12231222

1224-
lastStat := a.latestStat.Load()
1225-
if lastStat != nil && reflect.DeepEqual(lastStat, stats) {
1226-
a.logger.Info(ctx, "skipping stat because nothing changed")
1227-
return
1228-
}
1223+
// Collect agent metrics.
1224+
// Agent metrics are changing all the time, so there is no need to perform
1225+
// reflect.DeepEqual to see if stats should be transferred.
1226+
stats.Metrics = collectMetrics()
1227+
12291228
a.latestStat.Store(stats)
12301229

12311230
select {

agent/metrics.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package agent
2+
3+
import (
4+
"fmt"
5+
6+
"tailscale.com/util/clientmetric"
7+
8+
"github.com/coder/coder/codersdk/agentsdk"
9+
)
10+
11+
func collectMetrics() []agentsdk.AgentMetric {
12+
// Tailscale metrics
13+
metrics := clientmetric.Metrics()
14+
collected := make([]agentsdk.AgentMetric, 0, len(metrics))
15+
for _, m := range metrics {
16+
collected = append(collected, agentsdk.AgentMetric{
17+
Name: m.Name(),
18+
Type: asMetricType(m.Type()),
19+
Value: float64(m.Value()),
20+
})
21+
}
22+
return collected
23+
}
24+
25+
func asMetricType(typ clientmetric.Type) agentsdk.AgentMetricType {
26+
switch typ {
27+
case clientmetric.TypeGauge:
28+
return agentsdk.AgentMetricTypeGauge
29+
case clientmetric.TypeCounter:
30+
return agentsdk.AgentMetricTypeCounter
31+
default:
32+
panic(fmt.Sprintf("unknown metric type: %d", typ))
33+
}
34+
}

codersdk/agentsdk/agentsdk.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,17 @@ type Stats struct {
488488
Metrics []AgentMetric `json:"metrics"`
489489
}
490490

491+
type AgentMetricType string
492+
493+
const (
494+
AgentMetricTypeCounter AgentMetricType = "counter"
495+
AgentMetricTypeGauge AgentMetricType = "gauge"
496+
)
497+
491498
type AgentMetric struct {
492-
Name string `json:"name"`
493-
Type string `json:"type"`
494-
Value float64 `json:"value"`
499+
Name string `json:"name" validate:"required"`
500+
Type AgentMetricType `json:"type" validate:"required" enums:"counter,gauge"`
501+
Value float64 `json:"value" validate:"required"`
495502
}
496503

497504
type StatsResponse struct {

0 commit comments

Comments
 (0)