MCPcopy Create free account
hub / github.com/coder/coder / makeRequest

Method makeRequest

scaletest/bridge/run.go:178–253  ·  view source on GitHub ↗
(ctx context.Context, logger slog.Logger, url, token, model string, requestNum int)

Source from the content-addressed store, hash-verified

176}
177
178func (r *Runner) makeRequest(ctx context.Context, logger slog.Logger, url, token, model string, requestNum int) error {
179 start := r.clock.Now()
180
181 ctx = context.WithValue(ctx, tracingContextKey{}, tracingContext{
182 provider: r.cfg.Provider,
183 model: model,
184 stream: r.cfg.Stream,
185 requestNum: requestNum + 1,
186 mode: r.cfg.Mode,
187 })
188
189 // Set timeout per request
190 ctx, cancel := context.WithTimeout(ctx, r.cfg.HTTPTimeout)
191 defer cancel()
192
193 req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(r.cfg.RequestBody))
194 if err != nil {
195 return xerrors.Errorf("create request: %w", err)
196 }
197
198 req.Header.Set("Content-Type", "application/json")
199 if token != "" {
200 req.Header.Set("Authorization", "Bearer "+token)
201 }
202
203 logger.Debug(ctx, "making bridge request",
204 slog.F("url", url),
205 slog.F("request_num", requestNum+1),
206 slog.F("model", model),
207 )
208
209 resp, err := r.httpClient.Do(req)
210 if err != nil {
211 span := trace.SpanFromContext(req.Context())
212 if span.IsRecording() {
213 span.RecordError(err)
214 }
215 logger.Warn(ctx, "request failed during execution",
216 slog.F("request_num", requestNum+1),
217 slog.Error(err),
218 )
219 return xerrors.Errorf("execute request: %w", err)
220 }
221 defer resp.Body.Close()
222
223 span := trace.SpanFromContext(req.Context())
224 if span.IsRecording() {
225 span.SetAttributes(semconv.HTTPStatusCodeKey.Int(resp.StatusCode))
226 span.SetStatus(httpconv.ClientStatus(resp.StatusCode))
227 }
228
229 duration := r.clock.Since(start)
230 r.totalDuration += duration
231 r.cfg.Metrics.ObserveDuration(duration.Seconds())
232
233 if resp.StatusCode != http.StatusOK {
234 body, readErr := io.ReadAll(resp.Body)
235 if readErr != nil {

Callers 3

RunMethod · 0.95

Calls 12

IsRecordingMethod · 0.80
IntMethod · 0.80
SetMethod · 0.65
DoMethod · 0.65
ContextMethod · 0.65
CloseMethod · 0.65
ObserveDurationMethod · 0.65
ErrorfMethod · 0.45
ErrorMethod · 0.45
ReadAllMethod · 0.45

Tested by 2