|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | + |
| 3 | +package querymiddleware |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/grafana/dskit/cancellation" |
| 10 | +) |
| 11 | + |
| 12 | +const requestValidationFailedFmt = "request validation failed for " |
| 13 | + |
| 14 | +var errMetricsQueryRequestValidationFailed = cancellation.NewErrorf( |
| 15 | + requestValidationFailedFmt + "metrics query", |
| 16 | +) |
| 17 | +var errLabelsQueryRequestValidationFailed = cancellation.NewErrorf( |
| 18 | + requestValidationFailedFmt + "labels query", |
| 19 | +) |
| 20 | +var errCardinalityQueryRequestValidationFailed = cancellation.NewErrorf( |
| 21 | + requestValidationFailedFmt + "cardinality query", |
| 22 | +) |
| 23 | + |
| 24 | +type MetricsQueryRequestValidationRoundTripper struct { |
| 25 | + codec Codec |
| 26 | + next http.RoundTripper |
| 27 | +} |
| 28 | + |
| 29 | +func NewMetricsQueryRequestValidationRoundTripper(codec Codec, next http.RoundTripper) http.RoundTripper { |
| 30 | + return MetricsQueryRequestValidationRoundTripper{ |
| 31 | + codec: codec, |
| 32 | + next: next, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (rt MetricsQueryRequestValidationRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 37 | + ctx, cancel := context.WithCancelCause(r.Context()) |
| 38 | + defer cancel(errMetricsQueryRequestValidationFailed) |
| 39 | + r = r.WithContext(ctx) |
| 40 | + |
| 41 | + _, err := rt.codec.DecodeMetricsQueryRequest(ctx, r) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return rt.next.RoundTrip(r) |
| 46 | +} |
| 47 | + |
| 48 | +type LabelsQueryRequestValidationRoundTripper struct { |
| 49 | + codec Codec |
| 50 | + next http.RoundTripper |
| 51 | +} |
| 52 | + |
| 53 | +func NewLabelsQueryRequestValidationRoundTripper(codec Codec, next http.RoundTripper) http.RoundTripper { |
| 54 | + return LabelsQueryRequestValidationRoundTripper{ |
| 55 | + codec: codec, |
| 56 | + next: next, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (rt LabelsQueryRequestValidationRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 61 | + ctx, cancel := context.WithCancelCause(r.Context()) |
| 62 | + defer cancel(errLabelsQueryRequestValidationFailed) |
| 63 | + r = r.WithContext(ctx) |
| 64 | + |
| 65 | + _, err := rt.codec.DecodeLabelsQueryRequest(ctx, r) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return rt.next.RoundTrip(r) |
| 70 | +} |
| 71 | + |
| 72 | +type CardinalityQueryRequestValidationRoundTripper struct { |
| 73 | + next http.RoundTripper |
| 74 | +} |
| 75 | + |
| 76 | +func NewCardinalityQueryRequestValidationRoundTripper(next http.RoundTripper) http.RoundTripper { |
| 77 | + return CardinalityQueryRequestValidationRoundTripper{ |
| 78 | + next: next, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (rt CardinalityQueryRequestValidationRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 83 | + ctx, cancel := context.WithCancelCause(r.Context()) |
| 84 | + defer cancel(errCardinalityQueryRequestValidationFailed) |
| 85 | + r = r.WithContext(ctx) |
| 86 | + |
| 87 | + _, err := DecodeCardinalityQueryParams(r) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + return rt.next.RoundTrip(r) |
| 92 | +} |
0 commit comments