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>
pull/8882/head
Sonia Appasamy 1 year ago committed by Sonia Appasamy
parent c17a817769
commit 8e63d75018

@ -259,6 +259,29 @@ func (lc *LocalClient) DaemonMetrics(ctx context.Context) ([]byte, error) {
return lc.get200(ctx, "/localapi/v0/metrics") return lc.get200(ctx, "/localapi/v0/metrics")
} }
// IncrementMetric increments the value of a Tailscale daemon's metric by
// the given delta. If the metric has yet to exist, a new counter metric is
// created and initialized to delta.
//
// IncrementMetric only supports counter metrics and non-negative delta values.
// Gauge metrics are unsupported.
func (lc *LocalClient) IncrementMetric(ctx context.Context, name string, delta int) error {
type metricUpdate struct {
Name string `json:"name"`
Type string `json:"type"`
Value int `json:"value"` // amount to increment by
}
if delta < 0 {
return errors.New("negative delta not allowed")
}
_, err := lc.send(ctx, "POST", "/localapi/v0/upload-client-metrics", 200, jsonBody([]metricUpdate{{
Name: name,
Type: "counter",
Value: delta,
}}))
return err
}
// TailDaemonLogs returns a stream the Tailscale daemon's logs as they arrive. // TailDaemonLogs returns a stream the Tailscale daemon's logs as they arrive.
// Close the context to stop the stream. // Close the context to stop the stream.
func (lc *LocalClient) TailDaemonLogs(ctx context.Context) (io.Reader, error) { func (lc *LocalClient) TailDaemonLogs(ctx context.Context) (io.Reader, error) {

@ -1459,9 +1459,8 @@ func (h *Handler) serveUploadClientMetrics(w http.ResponseWriter, r *http.Reques
} }
type clientMetricJSON struct { type clientMetricJSON struct {
Name string `json:"name"` Name string `json:"name"`
// One of "counter" or "gauge" Type string `json:"type"` // one of "counter" or "gauge"
Type string `json:"type"` Value int `json:"value"` // amount to increment metric by
Value int `json:"value"`
} }
var clientMetrics []clientMetricJSON var clientMetrics []clientMetricJSON

Loading…
Cancel
Save