Skip to content

Commit bb3602b

Browse files
committed
WIP
1 parent 5887ee8 commit bb3602b

File tree

6 files changed

+113
-19
lines changed

6 files changed

+113
-19
lines changed

agent/agentssh/agentssh_internal_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ func Test_sessionStart_orphan(t *testing.T) {
5959
go func() {
6060
defer close(done)
6161

62-
m := metricsForSession(s.metrics.sessions, "ssh")
6362
// we don't really care what the error is here. In the larger scenario,
6463
// the client has disconnected, so we can't return any error information
6564
// to them.
66-
_ = s.startPTYSession(sess, m, cmd, ptyInfo, windowSize)
65+
_ = s.startPTYSession(sess, "ssh", cmd, ptyInfo, windowSize)
6766
}()
6867

6968
readDone := make(chan struct{})

agent/metrics.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"strings"
77

88
"github.com/prometheus/client_golang/prometheus"
9+
prompb "github.com/prometheus/client_model/go"
910
"tailscale.com/util/clientmetric"
1011

12+
"cdr.dev/slog"
13+
1114
"github.com/coder/coder/codersdk/agentsdk"
1215
)
1316

@@ -55,35 +58,53 @@ func (a *agent) collectMetrics(ctx context.Context) []agentsdk.AgentMetric {
5558
})
5659
}
5760

58-
// FIXME Agent metrics
59-
/*metricFamilies, err := a.prometheusRegistry.Gather()
61+
metricFamilies, err := a.prometheusRegistry.Gather()
6062
if err != nil {
6163
a.logger.Error(ctx, "can't gather agent metrics", slog.Error(err))
6264
return collected
6365
}
6466

6567
for _, metricFamily := range metricFamilies {
6668
for _, metric := range metricFamily.GetMetric() {
69+
labels := toAgentMetricLabels(metric.Label)
70+
6771
if metric.Counter != nil {
6872
collected = append(collected, agentsdk.AgentMetric{
69-
Name: metricFamily.GetName(),
70-
Type: agentsdk.AgentMetricTypeCounter,
71-
Value: metric.Counter.GetValue(),
73+
Name: metricFamily.GetName(),
74+
Type: agentsdk.AgentMetricTypeCounter,
75+
Value: metric.Counter.GetValue(),
76+
Labels: labels,
7277
})
7378
} else if metric.Gauge != nil {
7479
collected = append(collected, agentsdk.AgentMetric{
75-
Name: metricFamily.GetName(),
76-
Type: agentsdk.AgentMetricTypeGauge,
77-
Value: metric.Gauge.GetValue(),
80+
Name: metricFamily.GetName(),
81+
Type: agentsdk.AgentMetricTypeGauge,
82+
Value: metric.Gauge.GetValue(),
83+
Labels: labels,
7884
})
7985
} else {
8086
a.logger.Error(ctx, "unsupported metric type", slog.F("type", metricFamily.Type.String()))
8187
}
8288
}
83-
}*/
89+
}
8490
return collected
8591
}
8692

93+
func toAgentMetricLabels(metricLabels []*prompb.LabelPair) []agentsdk.AgentMetricLabel {
94+
if len(metricLabels) == 0 {
95+
return nil
96+
}
97+
98+
labels := make([]agentsdk.AgentMetricLabel, 0, len(metricLabels))
99+
for _, metricLabel := range metricLabels {
100+
labels = append(labels, agentsdk.AgentMetricLabel{
101+
Name: metricLabel.GetName(),
102+
Value: metricLabel.GetValue(),
103+
})
104+
}
105+
return labels
106+
}
107+
87108
// isIgnoredMetric checks if the metric should be ignored, as Coder agent doesn't use related features.
88109
// Expected metric families: magicsock_*, derp_*, tstun_*, netcheck_*, portmap_*, etc.
89110
func isIgnoredMetric(metricName string) bool {

coderd/apidoc/docs.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/agentsdk/agentsdk.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,15 @@ const (
496496
)
497497

498498
type AgentMetric struct {
499-
Name string `json:"name" validate:"required"`
500-
Type AgentMetricType `json:"type" validate:"required" enums:"counter,gauge"`
501-
Value float64 `json:"value" validate:"required"`
499+
Name string `json:"name" validate:"required"`
500+
Type AgentMetricType `json:"type" validate:"required" enums:"counter,gauge"`
501+
Value float64 `json:"value" validate:"required"`
502+
Labels []AgentMetricLabel `json:"labels,omitempty"`
503+
}
504+
505+
type AgentMetricLabel struct {
506+
Name string `json:"name" validate:"required"`
507+
Value string `json:"value" validate:"required"`
502508
}
503509

504510
type StatsResponse struct {

docs/api/schemas.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
```json
2222
{
23+
"labels": [
24+
{
25+
"name": "string",
26+
"value": "string"
27+
}
28+
],
2329
"name": "string",
2430
"type": "counter",
2531
"value": 0
@@ -28,11 +34,12 @@
2834

2935
### Properties
3036

31-
| Name | Type | Required | Restrictions | Description |
32-
| ------- | ---------------------------------------------------- | -------- | ------------ | ----------- |
33-
| `name` | string | true | | |
34-
| `type` | [agentsdk.AgentMetricType](#agentsdkagentmetrictype) | true | | |
35-
| `value` | number | true | | |
37+
| Name | Type | Required | Restrictions | Description |
38+
| -------- | --------------------------------------------------------------- | -------- | ------------ | ----------- |
39+
| `labels` | array of [agentsdk.AgentMetricLabel](#agentsdkagentmetriclabel) | false | | |
40+
| `name` | string | true | | |
41+
| `type` | [agentsdk.AgentMetricType](#agentsdkagentmetrictype) | true | | |
42+
| `value` | number | true | | |
3643

3744
#### Enumerated Values
3845

@@ -41,6 +48,22 @@
4148
| `type` | `counter` |
4249
| `type` | `gauge` |
4350

51+
## agentsdk.AgentMetricLabel
52+
53+
```json
54+
{
55+
"name": "string",
56+
"value": "string"
57+
}
58+
```
59+
60+
### Properties
61+
62+
| Name | Type | Required | Restrictions | Description |
63+
| ------- | ------ | -------- | ------------ | ----------- |
64+
| `name` | string | true | | |
65+
| `value` | string | true | | |
66+
4467
## agentsdk.AgentMetricType
4568

4669
```json
@@ -370,6 +393,12 @@
370393
},
371394
"metrics": [
372395
{
396+
"labels": [
397+
{
398+
"name": "string",
399+
"value": "string"
400+
}
401+
],
373402
"name": "string",
374403
"type": "counter",
375404
"value": 0

0 commit comments

Comments
 (0)