MCPcopy Index your code
hub / github.com/cortexproject/cortex / executeBackendRequests

Method executeBackendRequests

tools/querytee/proxy_endpoint.go:74–141  ·  view source on GitHub ↗
(r *http.Request, resCh chan *backendResponse)

Source from the content-addressed store, hash-verified

72}
73
74func (p *ProxyEndpoint) executeBackendRequests(r *http.Request, resCh chan *backendResponse) {
75 responses := make([]*backendResponse, 0, len(p.backends))
76
77 var (
78 wg = sync.WaitGroup{}
79 mtx = sync.Mutex{}
80 )
81 wg.Add(len(p.backends))
82
83 for _, b := range p.backends {
84
85 go func() {
86 defer wg.Done()
87
88 start := time.Now()
89 status, body, err := b.ForwardRequest(r)
90 elapsed := time.Since(start)
91
92 res := &backendResponse{
93 backend: b,
94 status: status,
95 body: body,
96 err: err,
97 }
98
99 // Log with a level based on the backend response.
100 lvl := level.Debug
101 if !res.succeeded() {
102 lvl = level.Warn
103 }
104
105 lvl(p.logger).Log("msg", "Backend response", "path", r.URL.Path, "query", r.URL.RawQuery, "backend", b.name, "status", status, "elapsed", elapsed)
106 p.metrics.requestDuration.WithLabelValues(res.backend.name, r.Method, p.routeName, strconv.Itoa(res.statusCode())).Observe(elapsed.Seconds())
107
108 // Keep track of the response if required.
109 if p.comparator != nil {
110 mtx.Lock()
111 responses = append(responses, res)
112 mtx.Unlock()
113 }
114
115 resCh <- res
116 }()
117 }
118
119 // Wait until all backend requests completed.
120 wg.Wait()
121 close(resCh)
122
123 // Compare responses.
124 if p.comparator != nil {
125 expectedResponse := responses[0]
126 actualResponse := responses[1]
127 if responses[1].backend.preferred {
128 expectedResponse, actualResponse = actualResponse, expectedResponse
129 }
130
131 result := comparisonSuccess

Callers 1

ServeHTTPMethod · 0.95

Calls 10

succeededMethod · 0.95
statusCodeMethod · 0.95
compareResponsesMethod · 0.95
DoneMethod · 0.80
ForwardRequestMethod · 0.80
AddMethod · 0.45
LogMethod · 0.45
WaitMethod · 0.45
ErrorMethod · 0.45
IncMethod · 0.45

Tested by

no test coverage detected