mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-30 05:25:35 +00:00
5336362e64
- Wrap each prober function into a probe class that allows associating metric labels and custom metrics with a given probe; - Make sure all existing probe classes set a `class` metric label; - Move bandwidth probe size from being a metric label to a separate gauge metric; this will make it possible to use it to calculate average used bandwidth using a PromQL query; - Also export transfer time for the bandwidth prober (more accurate than the total probe time, since it excludes connection establishment time). Updates tailscale/corp#17912 Signed-off-by: Anton Tolchanov <anton@tailscale.com>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package prober
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const maxHTTPBody = 4 << 20 // MiB
|
|
|
|
// HTTP returns a ProbeClass that healthchecks an HTTP URL.
|
|
//
|
|
// The probe function sends a GET request for url, expects an HTTP 200
|
|
// response, and verifies that want is present in the response
|
|
// body.
|
|
func HTTP(url, wantText string) ProbeClass {
|
|
return ProbeClass{
|
|
Probe: func(ctx context.Context) error {
|
|
return probeHTTP(ctx, url, []byte(wantText))
|
|
},
|
|
Class: "http",
|
|
}
|
|
}
|
|
|
|
func probeHTTP(ctx context.Context, url string, want []byte) error {
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("constructing request: %w", err)
|
|
}
|
|
|
|
// Get a completely new transport each time, so we don't reuse a
|
|
// past connection.
|
|
tr := http.DefaultTransport.(*http.Transport).Clone()
|
|
defer tr.CloseIdleConnections()
|
|
c := &http.Client{
|
|
Transport: tr,
|
|
}
|
|
|
|
resp, err := c.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("fetching %q: %w", url, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return fmt.Errorf("fetching %q: status code %d, want 200", url, resp.StatusCode)
|
|
}
|
|
|
|
bs, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPBody))
|
|
if err != nil {
|
|
return fmt.Errorf("reading body of %q: %w", url, err)
|
|
}
|
|
|
|
if !bytes.Contains(bs, want) {
|
|
// Log response body, but truncate it if it's too large; the limit
|
|
// has been chosen arbitrarily.
|
|
if maxlen := 300; len(bs) > maxlen {
|
|
bs = bs[:maxlen]
|
|
}
|
|
return fmt.Errorf("body of %q does not contain %q (got: %q)", url, want, string(bs))
|
|
}
|
|
|
|
return nil
|
|
}
|