Skip to content

Commit 0d8289d

Browse files
authored
Merge pull request #1227 from maximhq/01-03-fix_set_request_id_before_prehooks
fix: ensure request ID is set in context before PreHooks
2 parents d3aea78 + 69ea61d commit 0d8289d

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

core/bifrost.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,11 @@ func (bifrost *Bifrost) handleRequest(ctx context.Context, req *schemas.BifrostR
22172217

22182218
// Try the primary provider first
22192219
ctx = context.WithValue(ctx, schemas.BifrostContextKeyFallbackIndex, 0)
2220+
// Ensure request ID is set in context before PreHooks
2221+
if _, ok := ctx.Value(schemas.BifrostContextKeyRequestID).(string); !ok {
2222+
requestID := uuid.New().String()
2223+
ctx = context.WithValue(ctx, schemas.BifrostContextKeyRequestID, requestID)
2224+
}
22202225
primaryResult, primaryErr := bifrost.tryRequest(ctx, req)
22212226
if primaryErr != nil {
22222227
if primaryErr.Error != nil {
@@ -2310,6 +2315,11 @@ func (bifrost *Bifrost) handleStreamRequest(ctx context.Context, req *schemas.Bi
23102315

23112316
// Try the primary provider first
23122317
ctx = context.WithValue(ctx, schemas.BifrostContextKeyFallbackIndex, 0)
2318+
// Ensure request ID is set in context before PreHooks
2319+
if _, ok := ctx.Value(schemas.BifrostContextKeyRequestID).(string); !ok {
2320+
requestID := uuid.New().String()
2321+
ctx = context.WithValue(ctx, schemas.BifrostContextKeyRequestID, requestID)
2322+
}
23132323
primaryResult, primaryErr := bifrost.tryStreamRequest(ctx, req)
23142324

23152325
// Check if we should proceed with fallbacks

core/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- fix: ensure request ID is consistently set in context before PreHooks are executed

core/schemas/context.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ var reservedKeys = []any{
1919
BifrostContextKeySelectedKeyName,
2020
BifrostContextKeyNumberOfRetries,
2121
BifrostContextKeyFallbackIndex,
22-
BifrostContextKeyStreamEndIndicator,
2322
BifrostContextKeySkipKeySelection,
2423
BifrostContextKeyExtraHeaders,
2524
BifrostContextKeyURLPath,

plugins/maxim/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ func (plugin *Plugin) PreHook(ctx *schemas.BifrostContext, req *schemas.BifrostR
398398
ctx.SetValue(GenerationIDKey, generationID)
399399

400400
// Extract request ID from context, if not present, create a new one
401-
var ok bool
402-
requestID, ok = ctx.Value(schemas.BifrostContextKeyRequestID).(string)
401+
requestID, ok := ctx.Value(schemas.BifrostContextKeyRequestID).(string)
403402
if !ok || requestID == "" {
403+
// This should never happen since core/bifrost.go guarantees it's set before PreHooks
404404
requestID = uuid.New().String()
405-
ctx.SetValue(schemas.BifrostContextKeyRequestID, requestID)
405+
plugin.logger.Warn("%s request ID missing in PreHook, using fallback: %s", PluginLoggerPrefix, requestID)
406406
}
407407
}
408408

transports/changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
- fix: added missing logs filter checks in ui for live updates
1+
- fix: added missing logs filter checks in ui for live updates
2+
- fix: ensure request ID is consistently set in context before PreHooks are executed

0 commit comments

Comments
 (0)