Skip to content

Commit bed5eb9

Browse files
fix(observability): avoid error status for expected pkgsite results (#25)
1 parent f4ad20b commit bed5eb9

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

internal/mcpserver/tools/register.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ func (s *service) result(ctx context.Context, result pkgsite.Result, page pkgsit
6262
resultAttrs := observability.ResultAttrs{FromCache: result.FromCache, UpstreamURLPresent: result.UpstreamURL != "", ResultCount: len(result.Items)}
6363
if result.Error != nil {
6464
resultAttrs.ErrorStatusCode = result.Error.StatusCode
65-
span.SetStatus(codes.Error, result.Error.Status)
65+
if unexpectedPkgsiteStatus(result.Error.StatusCode) {
66+
span.SetStatus(codes.Error, result.Error.Status)
67+
}
6668
}
6769
span.SetAttributes(resultAttrs.Attributes()...)
6870
opts := envelopeOptions{Source: pkgsite.DefaultBaseURL, UpstreamURL: result.UpstreamURL, ToolName: toolName, NextArgs: nextArgs}
@@ -74,6 +76,10 @@ func (s *service) result(ctx context.Context, result pkgsite.Result, page pkgsit
7476
return textResult(singleEnvelope(result, opts)), nil, nil
7577
}
7678

79+
func unexpectedPkgsiteStatus(statusCode int) bool {
80+
return statusCode >= 500
81+
}
82+
7783
func toolAttrs(toolName string, args map[string]any, result pkgsite.Result) observability.ToolAttrs {
7884
return observability.ToolAttrs{
7985
ToolName: toolName,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tools
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
)
7+
8+
func TestUnexpectedPkgsiteStatus(t *testing.T) {
9+
t.Parallel()
10+
11+
tests := []struct {
12+
status int
13+
want bool
14+
}{
15+
{status: 400, want: false},
16+
{status: 404, want: false},
17+
{status: 429, want: false},
18+
{status: 500, want: true},
19+
{status: 503, want: true},
20+
}
21+
22+
for _, tt := range tests {
23+
t.Run(strconv.Itoa(tt.status), func(t *testing.T) {
24+
t.Parallel()
25+
26+
if got := unexpectedPkgsiteStatus(tt.status); got != tt.want {
27+
t.Fatalf("unexpectedPkgsiteStatus(%d) = %t, want %t", tt.status, got, tt.want)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)