mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:51:50 +00:00
This change allows clients to provide an interaction ID, essentially a trace but maybe simpler for some scenarios, and have that be propagated throughout Sourcegraph systems implicitly. We can automatically add this to events (telemetry V2 and Cody Gateway). The implementation closely follows the patterns we have for `internal/requestclient` and `internal/actor` If we land this mechanism, I'll follow up with an change that automatically injects these IDs into telemetry events within a context as well. If clients send a bunch of completion requests to the backend with this header, the interaction ID will automatically be added to the completion events recorded by the backend, like [these ones](https://sourcegraph.com/search?q=context:global+repo:sourcegraph/sourcegraph+.Record(...,+%22cody.completions%22,+...)&patternType=structural&sm=1&groupBy=repo), and any future events. Similarly, if clients set this header on outbound requests when recording events in the new telemetry (go/telemetry-v2), they'll get added as well - we can also consider adding an interaction ID as an explicit argument in the `recordEvent` GraphQL mutation to take precedence over the context interaction ID.
39 lines
900 B
Go
39 lines
900 B
Go
package requestinteraction
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/sourcegraph/log"
|
|
)
|
|
|
|
type requestInteractionKey struct{}
|
|
|
|
// Interaction carries information about the interaction associated with a
|
|
// request - a sort of manually instrumented trace.
|
|
type Interaction struct {
|
|
// ID identifies the interaction
|
|
ID string
|
|
}
|
|
|
|
func FromContext(ctx context.Context) *Interaction {
|
|
ip, ok := ctx.Value(requestInteractionKey{}).(*Interaction)
|
|
if !ok || ip == nil {
|
|
return nil
|
|
}
|
|
return ip
|
|
}
|
|
|
|
// WithClient adds client IP information to context for propagation.
|
|
func WithClient(ctx context.Context, client *Interaction) context.Context {
|
|
return context.WithValue(ctx, requestInteractionKey{}, client)
|
|
}
|
|
|
|
func (c *Interaction) LogFields() []log.Field {
|
|
if c == nil {
|
|
return []log.Field{log.String("requestInteraction", "<nil>")}
|
|
}
|
|
return []log.Field{
|
|
log.String("requestInteraction.id", c.ID),
|
|
}
|
|
}
|