|
| 1 | +package tripperware |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + "unsafe" |
| 9 | + |
| 10 | + "github.com/gogo/protobuf/proto" |
| 11 | + jsoniter "github.com/json-iterator/go" |
| 12 | + "github.com/opentracing/opentracing-go" |
| 13 | + "github.com/prometheus/common/model" |
| 14 | + |
| 15 | + "github.com/cortexproject/cortex/pkg/cortexpb" |
| 16 | +) |
| 17 | + |
| 18 | +// Codec is used to encode/decode query range requests and responses so they can be passed down to middlewares. |
| 19 | +type Codec interface { |
| 20 | + Merger |
| 21 | + // DecodeRequest decodes a Request from an http request. |
| 22 | + DecodeRequest(_ context.Context, request *http.Request, forwardHeaders []string) (Request, error) |
| 23 | + // DecodeResponse decodes a Response from an http response. |
| 24 | + // The original request is also passed as a parameter this is useful for implementation that needs the request |
| 25 | + // to merge result or build the result correctly. |
| 26 | + DecodeResponse(context.Context, *http.Response, Request) (Response, error) |
| 27 | + // EncodeRequest encodes a Request into an http request. |
| 28 | + EncodeRequest(context.Context, Request) (*http.Request, error) |
| 29 | + // EncodeResponse encodes a Response into an http response. |
| 30 | + EncodeResponse(context.Context, Response) (*http.Response, error) |
| 31 | +} |
| 32 | + |
| 33 | +// Merger is used by middlewares making multiple requests to merge back all responses into a single one. |
| 34 | +type Merger interface { |
| 35 | + // MergeResponse merges responses from multiple requests into a single Response |
| 36 | + MergeResponse(...Response) (Response, error) |
| 37 | +} |
| 38 | + |
| 39 | +// Response represents a query range response. |
| 40 | +type Response interface { |
| 41 | + proto.Message |
| 42 | + // HTTPHeaders returns the HTTP headers in the response. |
| 43 | + HTTPHeaders() map[string][]string |
| 44 | +} |
| 45 | + |
| 46 | +// Request represents a query range request that can be process by middlewares. |
| 47 | +type Request interface { |
| 48 | + // GetStart returns the start timestamp of the request in milliseconds. |
| 49 | + GetStart() int64 |
| 50 | + // GetEnd returns the end timestamp of the request in milliseconds. |
| 51 | + GetEnd() int64 |
| 52 | + // GetStep returns the step of the request in milliseconds. |
| 53 | + GetStep() int64 |
| 54 | + // GetQuery returns the query of the request. |
| 55 | + GetQuery() string |
| 56 | + // WithStartEnd clone the current request with different start and end timestamp. |
| 57 | + WithStartEnd(startTime int64, endTime int64) Request |
| 58 | + // WithQuery clone the current request with a different query. |
| 59 | + WithQuery(string) Request |
| 60 | + proto.Message |
| 61 | + // LogToSpan writes information about this request to an OpenTracing span |
| 62 | + LogToSpan(opentracing.Span) |
| 63 | + // GetStats returns the stats of the request. |
| 64 | + GetStats() string |
| 65 | + // WithStats clones the current `PrometheusRequest` with a new stats. |
| 66 | + WithStats(stats string) Request |
| 67 | +} |
| 68 | + |
| 69 | +// UnmarshalJSON implements json.Unmarshaler. |
| 70 | +func (s *SampleStream) UnmarshalJSON(data []byte) error { |
| 71 | + var stream struct { |
| 72 | + Metric model.Metric `json:"metric"` |
| 73 | + Values []cortexpb.Sample `json:"values"` |
| 74 | + } |
| 75 | + if err := json.Unmarshal(data, &stream); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + s.Labels = cortexpb.FromMetricsToLabelAdapters(stream.Metric) |
| 79 | + s.Samples = stream.Values |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// MarshalJSON implements json.Marshaler. |
| 84 | +func (s *SampleStream) MarshalJSON() ([]byte, error) { |
| 85 | + stream := struct { |
| 86 | + Metric model.Metric `json:"metric"` |
| 87 | + Values []cortexpb.Sample `json:"values"` |
| 88 | + }{ |
| 89 | + Metric: cortexpb.FromLabelAdaptersToMetric(s.Labels), |
| 90 | + Values: s.Samples, |
| 91 | + } |
| 92 | + return json.Marshal(stream) |
| 93 | +} |
| 94 | + |
| 95 | +func PrometheusResponseQueryableSamplesStatsPerStepJsoniterDecode(ptr unsafe.Pointer, iter *jsoniter.Iterator) { |
| 96 | + if !iter.ReadArray() { |
| 97 | + iter.ReportError("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", "expected [") |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + t := model.Time(iter.ReadFloat64() * float64(time.Second/time.Millisecond)) |
| 102 | + |
| 103 | + if !iter.ReadArray() { |
| 104 | + iter.ReportError("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", "expected ,") |
| 105 | + return |
| 106 | + } |
| 107 | + v := iter.ReadInt64() |
| 108 | + |
| 109 | + if iter.ReadArray() { |
| 110 | + iter.ReportError("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", "expected ]") |
| 111 | + } |
| 112 | + |
| 113 | + *(*PrometheusResponseQueryableSamplesStatsPerStep)(ptr) = PrometheusResponseQueryableSamplesStatsPerStep{ |
| 114 | + TimestampMs: int64(t), |
| 115 | + Value: v, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func PrometheusResponseQueryableSamplesStatsPerStepJsoniterEncode(ptr unsafe.Pointer, stream *jsoniter.Stream) { |
| 120 | + stats := (*PrometheusResponseQueryableSamplesStatsPerStep)(ptr) |
| 121 | + stream.WriteArrayStart() |
| 122 | + stream.WriteFloat64(float64(stats.TimestampMs) / float64(time.Second/time.Millisecond)) |
| 123 | + stream.WriteMore() |
| 124 | + stream.WriteInt64(stats.Value) |
| 125 | + stream.WriteArrayEnd() |
| 126 | +} |
| 127 | + |
| 128 | +func init() { |
| 129 | + jsoniter.RegisterTypeEncoderFunc("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", PrometheusResponseQueryableSamplesStatsPerStepJsoniterEncode, func(unsafe.Pointer) bool { return false }) |
| 130 | + jsoniter.RegisterTypeDecoderFunc("tripperware.PrometheusResponseQueryableSamplesStatsPerStep", PrometheusResponseQueryableSamplesStatsPerStepJsoniterDecode) |
| 131 | +} |
0 commit comments