Skip to content

Commit 4150ed0

Browse files
authored
fix: set org header for AI Fix (#106)
1 parent 055a65f commit 4150ed0

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

llm/api_client.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
4343
}
4444
}
4545

46-
responseBody, err := d.submitRequest(ctx, u, requestBody)
46+
responseBody, err := d.submitRequest(span.Context(), u, requestBody, "")
4747
if err != nil {
4848
return Explanations{}, err
4949
}
@@ -62,17 +62,19 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
6262
return explains, nil
6363
}
6464

65-
func (d *DeepCodeLLMBindingImpl) submitRequest(ctx context.Context, url *url.URL, requestBody []byte) ([]byte, error) {
65+
func (d *DeepCodeLLMBindingImpl) submitRequest(ctx context.Context, url *url.URL, requestBody []byte, orgId string) ([]byte, error) {
6666
logger := d.logger.With().Str("method", "submitRequest").Logger()
6767
logger.Trace().Str("payload body: %s\n", string(requestBody)).Msg("Marshaled payload")
68+
span := d.instrumentor.StartSpan(ctx, "code.SubmitRequest")
69+
defer span.Finish()
6870

6971
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), bytes.NewBuffer(requestBody))
7072
if err != nil {
7173
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request")
7274
return nil, err
7375
}
7476

75-
d.addDefaultHeaders(req)
77+
d.addDefaultHeaders(req, span.GetTraceId(), orgId)
7678

7779
resp, err := d.httpClientFunc().Do(req) //nolint:bodyclose // this seems to be a false positive
7880
if err != nil {
@@ -123,11 +125,11 @@ func (d *DeepCodeLLMBindingImpl) explainRequestBody(options *ExplainOptions) ([]
123125

124126
var failed = AutofixStatus{Message: "FAILED"}
125127

126-
func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, requestId string, options AutofixOptions) (AutofixResponse, AutofixStatus, error) {
128+
func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, options AutofixOptions) (AutofixResponse, AutofixStatus, error) {
127129
span := d.instrumentor.StartSpan(ctx, "code.RunAutofix")
128130
defer span.Finish()
129131

130-
logger := d.logger.With().Str("method", "code.RunAutofix").Str("requestId", requestId).Logger()
132+
logger := d.logger.With().Str("method", "code.RunAutofix").Str("requestId", span.GetTraceId()).Logger()
131133

132134
endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/suggestions", options.Host))
133135
if err != nil {
@@ -142,7 +144,7 @@ func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, requestId strin
142144
}
143145

144146
logger.Info().Msg("Started obtaining autofix Response")
145-
responseBody, err := d.submitRequest(ctx, endpoint, requestBody)
147+
responseBody, err := d.submitRequest(span.Context(), endpoint, requestBody, options.CodeRequestContext.Org.PublicId)
146148
logger.Info().Msg("Finished obtaining autofix Response")
147149

148150
if err != nil {
@@ -199,11 +201,11 @@ func (d *DeepCodeLLMBindingImpl) autofixRequestBody(options *AutofixOptions) ([]
199201
return requestBody, err
200202
}
201203

202-
func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, requestId string, options AutofixFeedbackOptions) error {
204+
func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, options AutofixFeedbackOptions) error {
203205
span := d.instrumentor.StartSpan(ctx, "code.SubmitAutofixFeedback")
204206
defer span.Finish()
205207

206-
logger := d.logger.With().Str("method", "code.SubmitAutofixFeedback").Str("requestId", requestId).Logger()
208+
logger := d.logger.With().Str("method", "code.SubmitAutofixFeedback").Str("requestId", span.GetTraceId()).Logger()
207209

208210
endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/event", options.Host))
209211
if err != nil {
@@ -218,7 +220,7 @@ func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, requ
218220
}
219221

220222
logger.Info().Msg("Started obtaining autofix Response")
221-
_, err = d.submitRequest(ctx, endpoint, requestBody)
223+
_, err = d.submitRequest(span.Context(), endpoint, requestBody, options.CodeRequestContext.Org.PublicId)
222224
logger.Info().Msg("Finished obtaining autofix Response")
223225

224226
return err
@@ -257,7 +259,14 @@ func prepareDiffs(diffs []string) []string {
257259
return encodedDiffs
258260
}
259261

260-
func (d *DeepCodeLLMBindingImpl) addDefaultHeaders(req *http.Request) {
262+
func (d *DeepCodeLLMBindingImpl) addDefaultHeaders(req *http.Request, requestId string, orgId string) {
263+
// if requestId is empty it will be enriched from the Gateway
264+
if len(requestId) > 0 {
265+
req.Header.Set("snyk-request-id", requestId)
266+
}
267+
if len(orgId) > 0 {
268+
req.Header.Set("snyk-org-name", orgId)
269+
}
261270
req.Header.Set("Cache-Control", "private, max-age=0, no-cache")
262271
req.Header.Set("Content-Type", "application/json")
263272
}

llm/api_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func TestAddDefaultHeadersWithExistingHeaders(t *testing.T) {
265265
d := &DeepCodeLLMBindingImpl{} // Initialize your struct if needed
266266
req := &http.Request{Header: http.Header{"Existing-Header": {"existing-value"}}}
267267

268-
d.addDefaultHeaders(req)
268+
d.addDefaultHeaders(req, "", "")
269269

270270
cacheControl := req.Header.Get("Cache-Control")
271271
contentType := req.Header.Get("Content-Type")

llm/binding.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,28 @@ type DeepCodeLLMBindingImpl struct {
6363
instrumentor observability.Instrumentor
6464
}
6565

66-
func (d *DeepCodeLLMBindingImpl) SubmitAutofixFeedback(ctx context.Context, requestId string, options AutofixFeedbackOptions) error {
66+
func (d *DeepCodeLLMBindingImpl) SubmitAutofixFeedback(ctx context.Context, fixId string, options AutofixFeedbackOptions) error {
6767
method := "SubmitAutofixFeedback"
6868
span := d.instrumentor.StartSpan(ctx, method)
6969
defer d.instrumentor.Finish(span)
70-
logger := d.logger.With().Str("method", method).Str("requestId", requestId).Logger()
70+
logger := d.logger.With().Str("method", method).Str("fixId", fixId).Logger()
7171
logger.Info().Msg("Started submitting autofix feedback")
7272
defer logger.Info().Msg("Finished submitting autofix feedback")
7373

74-
err := d.submitAutofixFeedback(ctx, requestId, options)
74+
err := d.submitAutofixFeedback(span.Context(), options)
7575
return err
7676
}
7777

78-
func (d *DeepCodeLLMBindingImpl) GetAutofixDiffs(ctx context.Context, requestId string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, status AutofixStatus, err error) {
78+
func (d *DeepCodeLLMBindingImpl) GetAutofixDiffs(ctx context.Context, _ string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, status AutofixStatus, err error) {
7979
method := "GetAutofixDiffs"
8080
span := d.instrumentor.StartSpan(ctx, method)
8181
defer d.instrumentor.Finish(span)
82+
requestId := span.GetTraceId()
8283
logger := d.logger.With().Str("method", method).Str("requestId", requestId).Logger()
8384
logger.Info().Msg("Started obtaining autofix diffs")
8485
defer logger.Info().Msg("Finished obtaining autofix diffs")
8586

86-
autofixResponse, status, err := d.runAutofix(ctx, requestId, options)
87+
autofixResponse, status, err := d.runAutofix(span.Context(), options)
8788
if err != nil {
8889
return nil, status, err
8990
}

0 commit comments

Comments
 (0)