summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/internal/telemetry/reporter.go
blob: 8296b3c5d4e8acdf72ae77ab9f7795ef22279c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package telemetry

import (
	"bytes"
	"context"
	"crypto/tls"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"net/url"
	"time"

	prompb "buf.build/gen/go/prometheus/prometheus/protocolbuffers/go"
	"github.com/cenkalti/backoff/v4"
	"github.com/gogo/protobuf/proto"
	"github.com/golang/snappy"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/common/expfmt"
	"github.com/prometheus/common/model"

	log "github.com/authzed/spicedb/internal/logging"
	"github.com/authzed/spicedb/pkg/x509util"
)

const (
	// DefaultEndpoint is the endpoint to which telemetry will report if none
	// other is specified.
	DefaultEndpoint = "https://telemetry.authzed.com"

	// DefaultInterval is the default amount of time to wait between telemetry
	// reports.
	DefaultInterval = 1 * time.Hour

	// MaxElapsedTimeBetweenReports is the maximum amount of time that the
	// telemetry reporter will attempt to write to the telemetry endpoint
	// before terminating the reporter.
	MaxElapsedTimeBetweenReports = 168 * time.Hour

	// MinimumAllowedInterval is the minimum amount of time one can request
	// between telemetry reports.
	MinimumAllowedInterval = 1 * time.Minute
)

func writeTimeSeries(ctx context.Context, client *http.Client, endpoint string, ts []*prompb.TimeSeries) error {
	// Reference upstream client:
	// https://github.com/prometheus/prometheus/blob/6555cc68caf8d8f323056e497ae7bb1e32a81667/storage/remote/client.go#L191
	pbBytes, err := proto.Marshal(&prompb.WriteRequest{
		Timeseries: ts,
	})
	if err != nil {
		return fmt.Errorf("failed to marshal Prometheus remote write protobuf: %w", err)
	}
	compressedPB := snappy.Encode(nil, pbBytes)

	r, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(compressedPB))
	if err != nil {
		return fmt.Errorf("failed to create Prometheus remote write http request: %w", err)
	}

	r.Header.Add("X-Prometheus-Remote-Write-Version", "0.1.0")
	r.Header.Add("Content-Encoding", "snappy")
	r.Header.Set("Content-Type", "application/x-protobuf")

	resp, err := client.Do(r)
	if err != nil {
		return fmt.Errorf("failed to send Prometheus remote write: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode/100 != 2 {
		body, _ := io.ReadAll(resp.Body)
		return fmt.Errorf(
			"unexpected Prometheus remote write response: %d: %s",
			resp.StatusCode,
			string(body),
		)
	}

	return nil
}

func discoverTimeseries(registry *prometheus.Registry) (allTS []*prompb.TimeSeries, err error) {
	metricFams, err := registry.Gather()
	if err != nil {
		return nil, fmt.Errorf("failed to gather telemetry metrics: %w", err)
	}

	defaultTimestamp := model.Time(time.Now().UnixNano() / int64(time.Millisecond))
	sampleVector, err := expfmt.ExtractSamples(&expfmt.DecodeOptions{
		Timestamp: defaultTimestamp,
	}, metricFams...)
	if err != nil {
		return nil, fmt.Errorf("unable to extract sample from metrics families: %w", err)
	}

	for _, sample := range sampleVector {
		allTS = append(allTS, &prompb.TimeSeries{
			Labels: convertLabels(sample.Metric),
			Samples: []*prompb.Sample{{
				Value:     float64(sample.Value),
				Timestamp: int64(sample.Timestamp),
			}},
		})
	}

	return
}

func discoverAndWriteMetrics(
	ctx context.Context,
	registry *prometheus.Registry,
	client *http.Client,
	endpoint string,
) error {
	ts, err := discoverTimeseries(registry)
	if err != nil {
		return err
	}

	return writeTimeSeries(ctx, client, endpoint, ts)
}

type Reporter func(ctx context.Context) error

// RemoteReporter creates a telemetry reporter with the specified parameters, or errors
// if the configuration was invalid.
func RemoteReporter(
	registry *prometheus.Registry,
	endpoint string,
	caOverridePath string,
	interval time.Duration,
) (Reporter, error) {
	if _, err := url.Parse(endpoint); err != nil {
		return nil, fmt.Errorf("invalid telemetry endpoint: %w", err)
	}
	if interval < MinimumAllowedInterval {
		return nil, fmt.Errorf("invalid telemetry reporting interval: %s < %s", interval, MinimumAllowedInterval)
	}
	if endpoint == DefaultEndpoint && interval != DefaultInterval {
		return nil, fmt.Errorf("cannot change the telemetry reporting interval for the default endpoint")
	}

	client := &http.Client{}
	if caOverridePath != "" {
		pool, err := x509util.CustomCertPool(caOverridePath)
		if err != nil {
			return nil, fmt.Errorf("invalid custom cert pool path `%s`: %w", caOverridePath, err)
		}

		t := &http.Transport{
			TLSClientConfig: &tls.Config{
				RootCAs:    pool,
				MinVersion: tls.VersionTLS12,
			},
		}

		client.Transport = t
	}

	return func(ctx context.Context) error {
		// nolint:gosec
		// G404 use of non cryptographically secure random number generator is not a security concern here,
		// as this is only used to smear the startup delay out over 10% of the reporting interval
		startupDelay := time.Duration(rand.Int63n(int64(interval.Seconds()/10))) * time.Second

		log.Ctx(ctx).Info().
			Stringer("interval", interval).
			Str("endpoint", endpoint).
			Stringer("next", startupDelay).
			Msg("telemetry reporter scheduled")

		backoffInterval := backoff.NewExponentialBackOff()
		backoffInterval.InitialInterval = interval
		backoffInterval.MaxInterval = MaxElapsedTimeBetweenReports
		backoffInterval.MaxElapsedTime = 0

		// Must reset the backoff object after changing parameters
		backoffInterval.Reset()

		ticker := time.After(startupDelay)

		for {
			select {
			case <-ticker:
				nextPush := backoffInterval.InitialInterval
				if err := discoverAndWriteMetrics(ctx, registry, client, endpoint); err != nil {
					nextPush = backoffInterval.NextBackOff()
					log.Ctx(ctx).Warn().
						Err(err).
						Str("endpoint", endpoint).
						Stringer("next", nextPush).
						Msg("failed to push telemetry metric")
				} else {
					log.Ctx(ctx).Debug().
						Str("endpoint", endpoint).
						Stringer("next", nextPush).
						Msg("reported telemetry")
					backoffInterval.Reset()
				}
				if nextPush == backoff.Stop {
					return fmt.Errorf(
						"exceeded maximum time between successful reports of %s",
						MaxElapsedTimeBetweenReports,
					)
				}
				ticker = time.After(nextPush)

			case <-ctx.Done():
				return nil
			}
		}
	}, nil
}

func DisabledReporter(ctx context.Context) error {
	log.Ctx(ctx).Info().Msg("telemetry disabled")
	return nil
}

func SilentlyDisabledReporter(_ context.Context) error {
	return nil
}

func convertLabels(labels model.Metric) []*prompb.Label {
	out := make([]*prompb.Label, 0, len(labels))
	for name, value := range labels {
		out = append(out, &prompb.Label{
			Name:  string(name),
			Value: string(value),
		})
	}
	return out
}