/
model_extra.go
712 lines (607 loc) · 19.8 KB
/
model_extra.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/query_range.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package querymiddleware
import (
"bytes"
stdjson "encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
v1 "github.com/prometheus/prometheus/web/api/v1"
"github.com/grafana/mimir/pkg/mimirpb"
)
var (
json = jsoniter.Config{
EscapeHTML: false, // No HTML in our responses.
SortMapKeys: true,
ValidateJsonRawMessage: true,
}.Froze()
)
// newEmptyPrometheusResponse returns an empty successful Prometheus query range response.
func newEmptyPrometheusResponse() *PrometheusResponse {
return &PrometheusResponse{
Status: statusSuccess,
Data: &PrometheusData{
ResultType: model.ValMatrix.String(),
Result: []SampleStream{},
},
}
}
type PrometheusRangeQueryRequest struct {
Path string
Start int64
End int64
Step int64
Timeout time.Duration
Query string
Options Options
// ID of the request used to correlate downstream requests and responses.
ID int64
// Hints that could be optionally attached to the request to pass down the stack.
// These hints can be used to optimize the query execution.
Hints *Hints
}
func (r *PrometheusRangeQueryRequest) GetPath() string {
return r.Path
}
func (r *PrometheusRangeQueryRequest) GetStart() int64 {
return r.Start
}
func (r *PrometheusRangeQueryRequest) GetEnd() int64 {
return r.End
}
func (r *PrometheusRangeQueryRequest) GetStep() int64 {
return r.Step
}
func (r *PrometheusRangeQueryRequest) GetTimeout() time.Duration {
return r.Timeout
}
func (r *PrometheusRangeQueryRequest) GetQuery() string {
return r.Query
}
func (r *PrometheusRangeQueryRequest) GetOptions() Options {
return r.Options
}
func (r *PrometheusRangeQueryRequest) GetID() int64 {
return r.ID
}
func (r *PrometheusRangeQueryRequest) GetHints() *Hints {
return r.Hints
}
type PrometheusInstantQueryRequest struct {
Path string
Time int64
Query string
Options Options
// ID of the request used to correlate downstream requests and responses.
ID int64
// Hints that could be optionally attached to the request to pass down the stack.
// These hints can be used to optimize the query execution.
Hints *Hints
}
func (r *PrometheusInstantQueryRequest) GetPath() string {
return r.Path
}
func (r *PrometheusInstantQueryRequest) GetTime() int64 {
return r.Time
}
func (r *PrometheusInstantQueryRequest) GetQuery() string {
return r.Query
}
func (r *PrometheusInstantQueryRequest) GetOptions() Options {
return r.Options
}
func (r *PrometheusInstantQueryRequest) GetID() int64 {
return r.ID
}
func (r *PrometheusInstantQueryRequest) GetHints() *Hints {
return r.Hints
}
type Hints struct {
// Total number of queries that are expected to be executed to serve the original request.
TotalQueries int32
// Estimated total number of series that a request might return.
CardinalityEstimate *EstimatedSeriesCount
}
func (h *Hints) GetCardinalityEstimate() *EstimatedSeriesCount {
return h.CardinalityEstimate
}
func (h *Hints) GetTotalQueries() int32 {
return h.TotalQueries
}
func (h *Hints) GetEstimatedSeriesCount() uint64 {
if x := h.GetCardinalityEstimate(); x != nil {
return x.EstimatedSeriesCount
}
return 0
}
type EstimatedSeriesCount struct {
EstimatedSeriesCount uint64
}
// WithID clones the current `PrometheusRangeQueryRequest` with the provided ID.
func (r *PrometheusRangeQueryRequest) WithID(id int64) MetricsQueryRequest {
newRequest := *r
newRequest.ID = id
return &newRequest
}
// WithStartEnd clones the current `PrometheusRangeQueryRequest` with a new `start` and `end` timestamp.
func (r *PrometheusRangeQueryRequest) WithStartEnd(start int64, end int64) MetricsQueryRequest {
newRequest := *r
newRequest.Start = start
newRequest.End = end
return &newRequest
}
// WithQuery clones the current `PrometheusRangeQueryRequest` with a new query.
func (r *PrometheusRangeQueryRequest) WithQuery(query string) MetricsQueryRequest {
newRequest := *r
newRequest.Query = query
return &newRequest
}
// WithTotalQueriesHint clones the current `PrometheusRangeQueryRequest` with an
// added Hint value for TotalQueries.
func (r *PrometheusRangeQueryRequest) WithTotalQueriesHint(totalQueries int32) MetricsQueryRequest {
newRequest := *r
if newRequest.Hints == nil {
newRequest.Hints = &Hints{TotalQueries: totalQueries}
} else {
*newRequest.Hints = *(r.Hints)
newRequest.Hints.TotalQueries = totalQueries
}
return &newRequest
}
// WithEstimatedSeriesCountHint clones the current `PrometheusRangeQueryRequest`
// with an added Hint value for EstimatedCardinality.
func (r *PrometheusRangeQueryRequest) WithEstimatedSeriesCountHint(count uint64) MetricsQueryRequest {
newRequest := *r
if newRequest.Hints == nil {
newRequest.Hints = &Hints{
CardinalityEstimate: &EstimatedSeriesCount{count},
}
} else {
*newRequest.Hints = *(r.Hints)
newRequest.Hints.CardinalityEstimate = &EstimatedSeriesCount{count}
}
return &newRequest
}
// AddSpanTags writes the current `PrometheusRangeQueryRequest` parameters to the specified span tags
// ("attributes" in OpenTelemetry parlance).
func (r *PrometheusRangeQueryRequest) AddSpanTags(sp opentracing.Span) {
sp.SetTag("query", r.GetQuery())
sp.SetTag("start", timestamp.Time(r.GetStart()).String())
sp.SetTag("end", timestamp.Time(r.GetEnd()).String())
sp.SetTag("step_ms", r.GetStep())
}
func (r *PrometheusInstantQueryRequest) GetStart() int64 {
return r.GetTime()
}
func (r *PrometheusInstantQueryRequest) GetEnd() int64 {
return r.GetTime()
}
func (r *PrometheusInstantQueryRequest) GetStep() int64 {
return 0
}
func (r *PrometheusInstantQueryRequest) WithID(id int64) MetricsQueryRequest {
newRequest := *r
newRequest.ID = id
return &newRequest
}
func (r *PrometheusInstantQueryRequest) WithStartEnd(startTime int64, _ int64) MetricsQueryRequest {
newRequest := *r
newRequest.Time = startTime
return &newRequest
}
func (r *PrometheusInstantQueryRequest) WithQuery(s string) MetricsQueryRequest {
newRequest := *r
newRequest.Query = s
return &newRequest
}
func (r *PrometheusInstantQueryRequest) WithTotalQueriesHint(totalQueries int32) MetricsQueryRequest {
newRequest := *r
if newRequest.Hints == nil {
newRequest.Hints = &Hints{TotalQueries: totalQueries}
} else {
*newRequest.Hints = *(r.Hints)
newRequest.Hints.TotalQueries = totalQueries
}
return &newRequest
}
func (r *PrometheusInstantQueryRequest) WithEstimatedSeriesCountHint(count uint64) MetricsQueryRequest {
newRequest := *r
if newRequest.Hints == nil {
newRequest.Hints = &Hints{
CardinalityEstimate: &EstimatedSeriesCount{count},
}
} else {
*newRequest.Hints = *(r.Hints)
newRequest.Hints.CardinalityEstimate = &EstimatedSeriesCount{count}
}
return &newRequest
}
// AddSpanTags writes query information about the current `PrometheusInstantQueryRequest`
// to a span's tag ("attributes" in OpenTelemetry parlance).
func (r *PrometheusInstantQueryRequest) AddSpanTags(sp opentracing.Span) {
sp.SetTag("query", r.GetQuery())
sp.SetTag("time", timestamp.Time(r.GetTime()).String())
}
func (r *PrometheusLabelNamesQueryRequest) GetLabelName() string {
return ""
}
func (r *PrometheusLabelNamesQueryRequest) GetStartOrDefault() int64 {
if r.GetStart() == 0 {
return v1.MinTime.UnixMilli()
}
return r.GetStart()
}
func (r *PrometheusLabelNamesQueryRequest) GetEndOrDefault() int64 {
if r.GetEnd() == 0 {
return v1.MaxTime.UnixMilli()
}
return r.GetEnd()
}
func (r *PrometheusLabelValuesQueryRequest) GetStartOrDefault() int64 {
if r.GetStart() == 0 {
return v1.MinTime.UnixMilli()
}
return r.GetStart()
}
func (r *PrometheusLabelValuesQueryRequest) GetEndOrDefault() int64 {
if r.GetEnd() == 0 {
return v1.MaxTime.UnixMilli()
}
return r.GetEnd()
}
// AddSpanTags writes query information about the current `PrometheusLabelNamesQueryRequest`
// to a span's tag ("attributes" in OpenTelemetry parlance).
func (r *PrometheusLabelNamesQueryRequest) AddSpanTags(sp opentracing.Span) {
sp.SetTag("matchers", fmt.Sprintf("%v", r.GetLabelMatcherSets()))
sp.SetTag("start", timestamp.Time(r.GetStart()).String())
sp.SetTag("end", timestamp.Time(r.GetEnd()).String())
}
// AddSpanTags writes query information about the current `PrometheusLabelNamesQueryRequest`
// to a span's tag ("attributes" in OpenTelemetry parlance).
func (r *PrometheusLabelValuesQueryRequest) AddSpanTags(sp opentracing.Span) {
sp.SetTag("label", fmt.Sprintf("%v", r.GetLabelName()))
sp.SetTag("matchers", fmt.Sprintf("%v", r.GetLabelMatcherSets()))
sp.SetTag("start", timestamp.Time(r.GetStart()).String())
sp.SetTag("end", timestamp.Time(r.GetEnd()).String())
}
type PrometheusLabelNamesQueryRequest struct {
Path string
Start int64
End int64
// labelMatcherSets is a repeated field here in order to enable the representation
// of labels queries which have not yet been split; the prometheus querier code
// will eventually split requests like `?match[]=up&match[]=process_start_time_seconds{job="prometheus"}`
// into separate queries, one for each matcher set
LabelMatcherSets []string
// ID of the request used to correlate downstream requests and responses.
ID int64
// Limit the number of label names returned. A value of 0 means no limit
Limit uint64
}
func (r *PrometheusLabelNamesQueryRequest) GetPath() string {
return r.Path
}
func (r *PrometheusLabelNamesQueryRequest) GetStart() int64 {
return r.Start
}
func (r *PrometheusLabelNamesQueryRequest) GetEnd() int64 {
return r.End
}
func (r *PrometheusLabelNamesQueryRequest) GetLabelMatcherSets() []string {
return r.LabelMatcherSets
}
func (r *PrometheusLabelNamesQueryRequest) GetID() int64 {
return r.ID
}
func (r *PrometheusLabelNamesQueryRequest) GetLimit() uint64 {
return r.Limit
}
type PrometheusLabelValuesQueryRequest struct {
Path string
LabelName string
Start int64
End int64
// labelMatcherSets is a repeated field here in order to enable the representation
// of labels queries which have not yet been split; the prometheus querier code
// will eventually split requests like `?match[]=up&match[]=process_start_time_seconds{job="prometheus"}`
// into separate queries, one for each matcher set
LabelMatcherSets []string
// ID of the request used to correlate downstream requests and responses.
ID int64
// Limit the number of label values returned. A value of 0 means no limit.
Limit uint64
}
func (r *PrometheusLabelValuesQueryRequest) GetLabelName() string {
return r.LabelName
}
func (r *PrometheusLabelValuesQueryRequest) GetStart() int64 {
return r.Start
}
func (r *PrometheusLabelValuesQueryRequest) GetEnd() int64 {
return r.End
}
func (r *PrometheusLabelValuesQueryRequest) GetLabelMatcherSets() []string {
return r.LabelMatcherSets
}
func (r *PrometheusLabelValuesQueryRequest) GetID() int64 {
return r.ID
}
func (r *PrometheusLabelValuesQueryRequest) GetLimit() uint64 {
return r.Limit
}
func (d *PrometheusData) UnmarshalJSON(b []byte) error {
v := struct {
Type model.ValueType `json:"resultType"`
Result stdjson.RawMessage `json:"result"`
}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
d.ResultType = v.Type.String()
switch v.Type {
case model.ValString:
var sss stringSampleStreams
if err := json.Unmarshal(v.Result, &sss); err != nil {
return err
}
d.Result = sss
return nil
case model.ValScalar:
var sss scalarSampleStreams
if err := json.Unmarshal(v.Result, &sss); err != nil {
return err
}
d.Result = sss
return nil
case model.ValVector:
var vss []vectorSampleStream
if err := json.Unmarshal(v.Result, &vss); err != nil {
return err
}
d.Result = fromVectorSampleStreams(vss)
return nil
case model.ValMatrix:
return json.Unmarshal(v.Result, &d.Result)
default:
return fmt.Errorf("unsupported value type %q", v.Type)
}
}
func (d *PrometheusData) MarshalJSON() ([]byte, error) {
if d == nil {
return []byte("null"), nil
}
switch d.ResultType {
case model.ValString.String():
return json.Marshal(struct {
Type model.ValueType `json:"resultType"`
Result stringSampleStreams `json:"result"`
}{
Type: model.ValString,
Result: d.Result,
})
case model.ValScalar.String():
return json.Marshal(struct {
Type model.ValueType `json:"resultType"`
Result scalarSampleStreams `json:"result"`
}{
Type: model.ValScalar,
Result: d.Result,
})
case model.ValVector.String():
return json.Marshal(struct {
Type model.ValueType `json:"resultType"`
Result []vectorSampleStream `json:"result"`
}{
Type: model.ValVector,
Result: asVectorSampleStreams(d.Result),
})
case model.ValMatrix.String():
type plain *PrometheusData
return json.Marshal(plain(d))
default:
return nil, fmt.Errorf("can't marshal prometheus result type %q", d.ResultType)
}
}
type stringSampleStreams []SampleStream
func (sss stringSampleStreams) MarshalJSON() ([]byte, error) {
if len(sss) != 1 {
return nil, fmt.Errorf("string sample streams should have exactly one stream, got %d", len(sss))
}
ss := sss[0]
if len(ss.Labels) != 1 || ss.Labels[0].Name != "value" {
return nil, fmt.Errorf("string sample stream should have exactly one label called value, got %d: %v", len(ss.Labels), ss.Labels)
}
l := ss.Labels[0]
if len(ss.Samples) != 1 {
return nil, fmt.Errorf("string sample stream should have exactly one sample, got %d", len(ss.Samples))
}
s := ss.Samples[0]
return json.Marshal(model.String{Value: l.Value, Timestamp: model.Time(s.TimestampMs)})
}
func (sss *stringSampleStreams) UnmarshalJSON(b []byte) error {
var sv model.String
if err := json.Unmarshal(b, &sv); err != nil {
return err
}
*sss = []SampleStream{{
Labels: []mimirpb.LabelAdapter{{Name: "value", Value: sv.Value}},
Samples: []mimirpb.Sample{{TimestampMs: int64(sv.Timestamp)}},
}}
return nil
}
type scalarSampleStreams []SampleStream
func (sss scalarSampleStreams) MarshalJSON() ([]byte, error) {
if len(sss) != 1 {
return nil, fmt.Errorf("scalar sample streams should have exactly one stream, got %d", len(sss))
}
ss := sss[0]
if len(ss.Samples) != 1 {
return nil, fmt.Errorf("scalar sample stream should have exactly one sample, got %d", len(ss.Samples))
}
s := ss.Samples[0]
return json.Marshal(model.Scalar{
Timestamp: model.Time(s.TimestampMs),
Value: model.SampleValue(s.Value),
})
}
func (sss *scalarSampleStreams) UnmarshalJSON(b []byte) error {
var sv model.Scalar
if err := json.Unmarshal(b, &sv); err != nil {
return err
}
*sss = []SampleStream{{
Samples: []mimirpb.Sample{{TimestampMs: int64(sv.Timestamp), Value: float64(sv.Value)}},
}}
return nil
}
// asVectorSampleStreams converts a slice of SampleStream into a slice of vectorSampleStream.
// This can be done as vectorSampleStream is defined as a SampleStream.
func asVectorSampleStreams(ss []SampleStream) []vectorSampleStream {
return *(*[]vectorSampleStream)(unsafe.Pointer(&ss))
}
// fromVectorSampleStreams is the inverse of asVectorSampleStreams.
func fromVectorSampleStreams(vss []vectorSampleStream) []SampleStream {
return *(*[]SampleStream)(unsafe.Pointer(&vss))
}
type vectorSampleStream SampleStream
func (vs *vectorSampleStream) UnmarshalJSON(b []byte) error {
s := model.Sample{}
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s.Histogram != nil {
return errors.New("cannot unmarshal native histogram from JSON")
}
*vs = vectorSampleStream{
Labels: mimirpb.FromMetricsToLabelAdapters(s.Metric),
Samples: []mimirpb.Sample{{TimestampMs: int64(s.Timestamp), Value: float64(s.Value)}},
}
return nil
}
func (vs vectorSampleStream) MarshalJSON() ([]byte, error) {
if (len(vs.Samples) == 1) == (len(vs.Histograms) == 1) { // not XOR
return nil, fmt.Errorf("vector sample stream should have exactly one sample or one histogram, got %d samples and %d histograms", len(vs.Samples), len(vs.Histograms))
}
var sample model.Sample
if len(vs.Samples) == 1 {
sample = model.Sample{
Metric: mimirpb.FromLabelAdaptersToMetric(vs.Labels),
Timestamp: model.Time(vs.Samples[0].TimestampMs),
Value: model.SampleValue(vs.Samples[0].Value),
}
} else {
sample = model.Sample{
Metric: mimirpb.FromLabelAdaptersToMetric(vs.Labels),
Timestamp: model.Time(vs.Histograms[0].TimestampMs),
Histogram: mimirpb.FromFloatHistogramToPromHistogram(vs.Histograms[0].Histogram.ToPrometheusModel()),
}
}
return json.Marshal(sample)
}
// UnmarshalJSON implements json.Unmarshaler.
func (s *SampleStream) UnmarshalJSON(data []byte) error {
var stream struct {
Metric model.Metric `json:"metric"`
Values []mimirpb.Sample `json:"values"`
Histograms []mimirpb.SampleHistogramPair `json:"histograms"`
}
if err := json.Unmarshal(data, &stream); err != nil {
return err
}
s.Labels = mimirpb.FromMetricsToLabelAdapters(stream.Metric)
if len(stream.Values) > 0 {
s.Samples = stream.Values
}
if len(stream.Histograms) > 0 {
return fmt.Errorf("cannot unmarshal native histograms from JSON, but stream contains %d histograms", len(stream.Histograms))
}
return nil
}
// MarshalJSON implements json.Marshaler.
func (s *SampleStream) MarshalJSON() ([]byte, error) {
var histograms []mimirpb.SampleHistogramPair
if len(s.Histograms) > 0 {
histograms = make([]mimirpb.SampleHistogramPair, len(s.Histograms))
}
for i, h := range s.Histograms {
histograms[i] = mimirpb.SampleHistogramPair{
Timestamp: h.TimestampMs,
Histogram: mimirpb.FromFloatHistogramToSampleHistogram(h.Histogram.ToPrometheusModel()),
}
}
stream := struct {
Metric model.Metric `json:"metric"`
Values []mimirpb.Sample `json:"values,omitempty"`
Histograms []mimirpb.SampleHistogramPair `json:"histograms,omitempty"`
}{
Metric: mimirpb.FromLabelAdaptersToMetric(s.Labels),
Values: s.Samples,
Histograms: histograms,
}
return json.Marshal(stream)
}
type byFirstTime []*PrometheusResponse
func (a byFirstTime) Len() int { return len(a) }
func (a byFirstTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byFirstTime) Less(i, j int) bool { return a[i].minTime() < a[j].minTime() }
func (resp *PrometheusResponse) minTime() int64 {
result := resp.Data.Result
if len(result) == 0 {
return -1
}
if len(result[0].Samples) == 0 {
return -1
}
return result[0].Samples[0].TimestampMs
}
// EncodeCachedHTTPResponse encodes the input http.Response into CachedHTTPResponse.
// The input res.Body is replaced in this function, so that it can be safely consumed again.
func EncodeCachedHTTPResponse(cacheKey string, res *http.Response) (*CachedHTTPResponse, error) {
// Read the response.
body, err := readResponseBody(res)
if err != nil {
return nil, err
}
// Since we've already consumed the response Body we have to replace it on the response,
// otherwise the caller will get a response with a closed Body.
res.Body = io.NopCloser(bytes.NewBuffer(body))
// When preallocating the slice we assume that header as 1 value (which is the most common case).
headers := make([]*CachedHTTPHeader, 0, len(res.Header))
for name, values := range res.Header {
for _, value := range values {
headers = append(headers, &CachedHTTPHeader{
Name: name,
Value: value,
})
}
}
return &CachedHTTPResponse{
CacheKey: cacheKey,
StatusCode: int32(res.StatusCode),
Body: body,
Headers: headers,
}, nil
}
func DecodeCachedHTTPResponse(res *CachedHTTPResponse) *http.Response {
headers := http.Header{}
for _, header := range res.Headers {
headers[header.Name] = append(headers[header.Name], header.Value)
}
return &http.Response{
StatusCode: int(res.StatusCode),
Body: io.NopCloser(bytes.NewReader(res.Body)),
Header: headers,
ContentLength: int64(len(res.Body)),
}
}