Skip to content

Commit 8e63d75

Browse files
committed
client/tailscale: add LocalClient.IncrementMetric func
A #cleanup to add a func to utilize the already-present "/localapi/v0/upload-client-metrics" localapi endpoint. Signed-off-by: Sonia Appasamy <sonia@tailscale.com>
1 parent c17a817 commit 8e63d75

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

client/tailscale/localclient.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ func (lc *LocalClient) DaemonMetrics(ctx context.Context) ([]byte, error) {
259259
return lc.get200(ctx, "/localapi/v0/metrics")
260260
}
261261

262+
// IncrementMetric increments the value of a Tailscale daemon's metric by
263+
// the given delta. If the metric has yet to exist, a new counter metric is
264+
// created and initialized to delta.
265+
//
266+
// IncrementMetric only supports counter metrics and non-negative delta values.
267+
// Gauge metrics are unsupported.
268+
func (lc *LocalClient) IncrementMetric(ctx context.Context, name string, delta int) error {
269+
type metricUpdate struct {
270+
Name string `json:"name"`
271+
Type string `json:"type"`
272+
Value int `json:"value"` // amount to increment by
273+
}
274+
if delta < 0 {
275+
return errors.New("negative delta not allowed")
276+
}
277+
_, err := lc.send(ctx, "POST", "/localapi/v0/upload-client-metrics", 200, jsonBody([]metricUpdate{{
278+
Name: name,
279+
Type: "counter",
280+
Value: delta,
281+
}}))
282+
return err
283+
}
284+
262285
// TailDaemonLogs returns a stream the Tailscale daemon's logs as they arrive.
263286
// Close the context to stop the stream.
264287
func (lc *LocalClient) TailDaemonLogs(ctx context.Context) (io.Reader, error) {

ipn/localapi/localapi.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,10 +1458,9 @@ func (h *Handler) serveUploadClientMetrics(w http.ResponseWriter, r *http.Reques
14581458
return
14591459
}
14601460
type clientMetricJSON struct {
1461-
Name string `json:"name"`
1462-
// One of "counter" or "gauge"
1463-
Type string `json:"type"`
1464-
Value int `json:"value"`
1461+
Name string `json:"name"`
1462+
Type string `json:"type"` // one of "counter" or "gauge"
1463+
Value int `json:"value"` // amount to increment metric by
14651464
}
14661465

14671466
var clientMetrics []clientMetricJSON

0 commit comments

Comments
 (0)