Skip to content

Commit 6f1bc8c

Browse files
committed
review 2
1 parent 59934d9 commit 6f1bc8c

7 files changed

Lines changed: 32 additions & 23 deletions

bridge_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,11 @@ func setupInjectedToolTest(t *testing.T, fixture []byte, streaming bool, configu
974974

975975
recorderClient := &mockRecorderClient{}
976976

977-
// Setup MCP tools.
978-
tools := setupMCPServerProxiesForTest(t, testTracer)
977+
// Setup MCP proxies.
978+
proxies := setupMCPServerProxiesForTest(t, testTracer)
979979

980980
// Configure the bridge with injected tools.
981-
mcpMgr := mcp.NewServerProxyManager(tools, testTracer)
981+
mcpMgr := mcp.NewServerProxyManager(proxies, testTracer)
982982
require.NoError(t, mcpMgr.Init(ctx))
983983
b, err := configureFn(mockSrv.URL, recorderClient, mcpMgr)
984984
require.NoError(t, err)
@@ -1005,7 +1005,7 @@ func setupInjectedToolTest(t *testing.T, fixture []byte, streaming bool, configu
10051005
return mockSrv.callCount.Load() == 2
10061006
}, time.Second*10, time.Millisecond*50)
10071007

1008-
return recorderClient, tools, resp
1008+
return recorderClient, proxies, resp
10091009
}
10101010

10111011
func TestErrorHandling(t *testing.T) {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ require (
2727
)
2828

2929
require (
30-
github.com/google/go-cmp v0.7.0
3130
go.opentelemetry.io/otel v1.38.0
3231
go.opentelemetry.io/otel/sdk v1.38.0
3332
go.opentelemetry.io/otel/trace v1.38.0

intercept_anthropic_messages_blocking.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (i *AnthropicMessagesBlockingInterception) ProcessRequest(w http.ResponseWr
8888

8989
for {
9090
// TODO add outer loop span (https://github.com/coder/aibridge/issues/67)
91-
resp, err = i.traceNewMessage(ctx, svc, messages) // traces svc.New(ctx, msgParams) call
91+
resp, err = i.newMessage(ctx, svc, messages)
9292
if err != nil {
9393
if isConnError(err) {
9494
// Can't write a response, just error out.
@@ -298,7 +298,7 @@ func (i *AnthropicMessagesBlockingInterception) ProcessRequest(w http.ResponseWr
298298
return nil
299299
}
300300

301-
func (i *AnthropicMessagesBlockingInterception) traceNewMessage(ctx context.Context, svc anthropic.MessageService, msgParams anthropic.MessageNewParams) (_ *anthropic.Message, outErr error) {
301+
func (i *AnthropicMessagesBlockingInterception) newMessage(ctx context.Context, svc anthropic.MessageService, msgParams anthropic.MessageNewParams) (_ *anthropic.Message, outErr error) {
302302
ctx, span := i.tracer.Start(ctx, "Intercept.ProcessRequest.Upstream", trace.WithAttributes(tracing.InterceptionAttributesFromContext(ctx)...))
303303
defer tracing.EndSpanErr(span, &outErr)
304304

intercept_openai_chat_blocking.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (i *OpenAIBlockingChatInterception) ProcessRequest(w http.ResponseWriter, r
7676
var opts []option.RequestOption
7777
opts = append(opts, option.WithRequestTimeout(time.Second*60)) // TODO: configurable timeout
7878

79-
completion, err = i.traceChatCompletionsNew(ctx, svc, opts) // traces svc.New(ctx, i.req.ChatCompletionNewParams, opts...) call
79+
completion, err = i.newChatCompletion(ctx, svc, opts)
8080
if err != nil {
8181
break
8282
}
@@ -233,7 +233,7 @@ func (i *OpenAIBlockingChatInterception) ProcessRequest(w http.ResponseWriter, r
233233
return nil
234234
}
235235

236-
func (i *OpenAIBlockingChatInterception) traceChatCompletionsNew(ctx context.Context, svc openai.ChatCompletionService, opts []option.RequestOption) (_ *openai.ChatCompletion, outErr error) {
236+
func (i *OpenAIBlockingChatInterception) newChatCompletion(ctx context.Context, svc openai.ChatCompletionService, opts []option.RequestOption) (_ *openai.ChatCompletion, outErr error) {
237237
ctx, span := i.tracer.Start(ctx, "Intercept.ProcessRequest.Upstream", trace.WithAttributes(tracing.InterceptionAttributesFromContext(ctx)...))
238238
defer tracing.EndSpanErr(span, &outErr)
239239

mcp/tool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
maxSpanInputAttrLen = 100
18+
maxSpanInputAttrLen = 100 // truncates tool.Call span input attribute to first `maxSpanInputAttrLen` letters
1919
injectedToolPrefix = "bmcp" // "bridged MCP"
2020
injectedToolDelimiter = "_"
2121
)
@@ -63,7 +63,7 @@ func (t *Tool) Call(ctx context.Context, input any, tracer trace.Tracer) (_ *mcp
6363
} else {
6464
strJson := string(inputJson)
6565
if len(strJson) > maxSpanInputAttrLen {
66-
strJson = strJson[:100]
66+
strJson = strJson[:maxSpanInputAttrLen]
6767
}
6868
span.SetAttributes(attribute.String(tracing.MCPInput, strJson))
6969
}

trace_integration_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8+
"slices"
9+
"strings"
810
"testing"
911
"time"
1012

@@ -13,8 +15,6 @@ import (
1315
"github.com/coder/aibridge"
1416
"github.com/coder/aibridge/mcp"
1517
"github.com/coder/aibridge/tracing"
16-
"github.com/google/go-cmp/cmp"
17-
"github.com/google/go-cmp/cmp/cmpopts"
1818
"github.com/stretchr/testify/assert"
1919
"github.com/stretchr/testify/require"
2020
"github.com/tidwall/gjson"
@@ -670,13 +670,12 @@ func TestTracePassthrough(t *testing.T) {
670670
require.Len(t, spans, 1)
671671

672672
assert.Equal(t, spans[0].Name(), "Passthrough")
673-
attrs := []attribute.KeyValue{
674-
attribute.String(tracing.PassthroughURL, "/v1/models"),
673+
want := []attribute.KeyValue{
675674
attribute.String(tracing.PassthroughMethod, "GET"),
675+
attribute.String(tracing.PassthroughURL, "/v1/models"),
676676
}
677-
if attrDiff := cmp.Diff(spans[0].Attributes(), attrs, cmpopts.EquateComparable(attribute.KeyValue{}), cmpopts.SortSlices(cmpAttrKeyVal)); attrDiff != "" {
678-
t.Errorf("unexpectet attrs diff: %s", attrDiff)
679-
}
677+
got := slices.SortedFunc(slices.Values(spans[0].Attributes()), cmpAttrKeyVal)
678+
require.Equal(t, want, got)
680679
}
681680

682681
func TestNewServerProxyManagerTraces(t *testing.T) {
@@ -714,8 +713,8 @@ func TestNewServerProxyManagerTraces(t *testing.T) {
714713
verifyTraces(t, sr, []expectTrace{{"StreamableHTTPServerProxy.Init.fetchTools", 1, codes.Unset}}, attrs)
715714
}
716715

717-
func cmpAttrKeyVal(a attribute.KeyValue, b attribute.KeyValue) bool {
718-
return a.Key < b.Key
716+
func cmpAttrKeyVal(a attribute.KeyValue, b attribute.KeyValue) int {
717+
return strings.Compare(string(a.Key), string(b.Key))
719718
}
720719

721720
// checks counts of traces with given name, status and attributes
@@ -729,9 +728,9 @@ func verifyTraces(t *testing.T, spanRecorder *tracetest.SpanRecorder, expect []e
729728
continue
730729
}
731730
found++
732-
if attrDiff := cmp.Diff(s.Attributes(), attrs, cmpopts.EquateEmpty(), cmpopts.EquateComparable(attribute.KeyValue{}), cmpopts.SortSlices(cmpAttrKeyVal)); attrDiff != "" {
733-
t.Errorf("unexpectet attrs for span named: %v, diff: %s", e.name, attrDiff)
734-
}
731+
want := slices.SortedFunc(slices.Values(attrs), cmpAttrKeyVal)
732+
got := slices.SortedFunc(slices.Values(s.Attributes()), cmpAttrKeyVal)
733+
require.Equal(t, want, got)
735734
assert.Equalf(t, e.status, s.Status().Code, "unexpected status for trace naned: %v got: %v want: %v", e.name, s.Status().Code, e.status)
736735
}
737736
if found != e.count {

tracing/tracing.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ func RequestBridgeAttributesFromContext(ctx context.Context) []attribute.KeyValu
6363
return attrs
6464
}
6565

66+
// EndSpanErr ends given span and sets Error status if error is not nil
67+
// uses pointer to error because defer evaluates function arguments
68+
// when defer statement is executed not when deferred function is called
69+
//
70+
// example usage:
71+
//
72+
// func Example() (result any, outErr error) {
73+
// _, span := tracer.Start(...)
74+
// defer tracing.EndSpanErr(span, &outErr)
75+
//
76+
// }
6677
func EndSpanErr(span trace.Span, err *error) {
6778
if span == nil {
6879
return

0 commit comments

Comments
 (0)