| 176 | } |
| 177 | |
| 178 | func (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 { |