Skip to content

Commit 932b43d

Browse files
committed
refactor: print trace id
1 parent 0bcb168 commit 932b43d

26 files changed

+713
-699
lines changed

cmd/mcp-gateway/main.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,14 @@ func run() {
181181
logger.Fatal("Failed to initialize auth service", zap.Error(err))
182182
}
183183

184-
// Create server instance
185-
server, err := core.NewServer(
186-
logger,
187-
cfg.Port,
188-
store,
189-
sessionStore,
190-
a,
191-
core.WithForwardConfig(cfg.Forward),
192-
core.WithTraceCapture(cfg.Tracing.Capture),
193-
)
194-
if err != nil {
195-
logger.Fatal("Failed to create server", zap.Error(err))
196-
}
197-
198-
// Enable gin tracing middleware if tracing is configured
184+
// Initialize tracing BEFORE creating server if enabled
185+
var tracingServiceName string
199186
if cfg.Tracing.Enabled {
200187
if cfg.Tracing.ServiceName == "" {
201188
cfg.Tracing.ServiceName = "mcp-gateway"
202189
}
190+
tracingServiceName = cfg.Tracing.ServiceName
191+
203192
shutdown, err := trace.InitTracing(ctx, &cfg.Tracing, logger)
204193
if err != nil {
205194
logger.Error("Failed to initialize tracing", zap.Error(err))
@@ -217,10 +206,22 @@ func run() {
217206
zap.String("endpoint", cfg.Tracing.Endpoint),
218207
zap.String("protocol", cfg.Tracing.Protocol),
219208
)
220-
221209
}
210+
}
222211

223-
server.EnableTracing(cfg.Tracing.ServiceName)
212+
// Create server instance with tracing enabled from the start
213+
server, err := core.NewServer(
214+
logger,
215+
cfg.Port,
216+
store,
217+
sessionStore,
218+
a,
219+
core.WithForwardConfig(cfg.Forward),
220+
core.WithTraceCapture(cfg.Tracing.Capture),
221+
core.WithTracing(tracingServiceName), // Register OTel middleware early
222+
)
223+
if err != nil {
224+
logger.Fatal("Failed to create server", zap.Error(err))
224225
}
225226

226227
err = server.RegisterRoutes(ctx)

cmd/mcp-gateway/main_more_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,4 @@ func TestCommandTimeout(t *testing.T) {
200200
case <-time.After(timeout):
201201
t.Error("command took too long to complete")
202202
}
203-
}
203+
}
Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,70 @@
11
package backend
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"net/http"
7-
"net/http/httptest"
8-
"testing"
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
99
)
1010

1111
func TestHTTPServer_UserRoutes(t *testing.T) {
12-
s := NewHTTPServer()
12+
s := NewHTTPServer()
1313

14-
// Create user
15-
body := map[string]any{
16-
"username": "u1",
17-
"email": "[email protected]",
18-
}
19-
bb, _ := json.Marshal(body)
20-
w := httptest.NewRecorder()
21-
r := httptest.NewRequest(http.MethodPost, "/users", bytes.NewReader(bb))
22-
r.Header.Set("Content-Type", "application/json")
23-
s.router.ServeHTTP(w, r)
24-
if w.Code != http.StatusCreated {
25-
t.Fatalf("create user status = %d", w.Code)
26-
}
14+
// Create user
15+
body := map[string]any{
16+
"username": "u1",
17+
"email": "[email protected]",
18+
}
19+
bb, _ := json.Marshal(body)
20+
w := httptest.NewRecorder()
21+
r := httptest.NewRequest(http.MethodPost, "/users", bytes.NewReader(bb))
22+
r.Header.Set("Content-Type", "application/json")
23+
s.router.ServeHTTP(w, r)
24+
if w.Code != http.StatusCreated {
25+
t.Fatalf("create user status = %d", w.Code)
26+
}
2727

28-
// Get by email
29-
w = httptest.NewRecorder()
30-
r = httptest.NewRequest(http.MethodGet, "/users/email/[email protected]", nil)
31-
s.router.ServeHTTP(w, r)
32-
if w.Code != http.StatusOK {
33-
t.Fatalf("get user status = %d", w.Code)
34-
}
28+
// Get by email
29+
w = httptest.NewRecorder()
30+
r = httptest.NewRequest(http.MethodGet, "/users/email/[email protected]", nil)
31+
s.router.ServeHTTP(w, r)
32+
if w.Code != http.StatusOK {
33+
t.Fatalf("get user status = %d", w.Code)
34+
}
3535

36-
// Update preferences
37-
prefs := map[string]any{
38-
"isPublic": true,
39-
"showEmail": false,
40-
"theme": "dark",
41-
"tags": []string{"a", "b"},
42-
"settings": map[string]any{"k": "v"},
43-
"notifications": []Notification{{Type: "email", Channel: "system", Enabled: true, Frequency: 0}},
44-
}
45-
pb, _ := json.Marshal(prefs)
46-
w = httptest.NewRecorder()
47-
r = httptest.NewRequest(http.MethodPut, "/users/[email protected]/preferences", bytes.NewReader(pb))
48-
r.Header.Set("Content-Type", "application/json")
49-
s.router.ServeHTTP(w, r)
50-
if w.Code != http.StatusOK {
51-
t.Fatalf("update prefs status = %d", w.Code)
52-
}
36+
// Update preferences
37+
prefs := map[string]any{
38+
"isPublic": true,
39+
"showEmail": false,
40+
"theme": "dark",
41+
"tags": []string{"a", "b"},
42+
"settings": map[string]any{"k": "v"},
43+
"notifications": []Notification{{Type: "email", Channel: "system", Enabled: true, Frequency: 0}},
44+
}
45+
pb, _ := json.Marshal(prefs)
46+
w = httptest.NewRecorder()
47+
r = httptest.NewRequest(http.MethodPut, "/users/[email protected]/preferences", bytes.NewReader(pb))
48+
r.Header.Set("Content-Type", "application/json")
49+
s.router.ServeHTTP(w, r)
50+
if w.Code != http.StatusOK {
51+
t.Fatalf("update prefs status = %d", w.Code)
52+
}
5353

54-
// Upload avatar missing url -> 400
55-
w = httptest.NewRecorder()
56-
r = httptest.NewRequest(http.MethodPost, "/users/[email protected]/avatar", nil)
57-
s.router.ServeHTTP(w, r)
58-
if w.Code != http.StatusBadRequest {
59-
t.Fatalf("avatar missing url status = %d", w.Code)
60-
}
54+
// Upload avatar missing url -> 400
55+
w = httptest.NewRecorder()
56+
r = httptest.NewRequest(http.MethodPost, "/users/[email protected]/avatar", nil)
57+
s.router.ServeHTTP(w, r)
58+
if w.Code != http.StatusBadRequest {
59+
t.Fatalf("avatar missing url status = %d", w.Code)
60+
}
6161

62-
// Upload avatar with url
63-
w = httptest.NewRecorder()
64-
r = httptest.NewRequest(http.MethodPost, "/users/[email protected]/avatar", bytes.NewBufferString("url=https://img"))
65-
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
66-
s.router.ServeHTTP(w, r)
67-
if w.Code != http.StatusOK {
68-
t.Fatalf("avatar ok status = %d", w.Code)
69-
}
62+
// Upload avatar with url
63+
w = httptest.NewRecorder()
64+
r = httptest.NewRequest(http.MethodPost, "/users/[email protected]/avatar", bytes.NewBufferString("url=https://img"))
65+
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
66+
s.router.ServeHTTP(w, r)
67+
if w.Code != http.StatusOK {
68+
t.Fatalf("avatar ok status = %d", w.Code)
69+
}
7070
}
71-
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
package backend
22

33
import (
4-
"context"
5-
"encoding/json"
6-
"testing"
4+
"context"
5+
"encoding/json"
6+
"testing"
77

8-
"github.com/mark3labs/mcp-go/mcp"
9-
"github.com/stretchr/testify/assert"
8+
"github.com/mark3labs/mcp-go/mcp"
9+
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestNewMCPServer_And_GenerateResources(t *testing.T) {
13-
srv := NewMCPServer()
14-
if srv == nil {
15-
t.Fatal("expected server instance")
16-
}
13+
srv := NewMCPServer()
14+
if srv == nil {
15+
t.Fatal("expected server instance")
16+
}
1717

18-
rs := generateResources()
19-
if len(rs) != 100 {
20-
t.Fatalf("expected 100 resources, got %d", len(rs))
21-
}
18+
rs := generateResources()
19+
if len(rs) != 100 {
20+
t.Fatalf("expected 100 resources, got %d", len(rs))
21+
}
2222
}
2323

2424
func TestHandleSimpleAndComplexPrompts(t *testing.T) {
25-
// simple
26-
out, err := handleSimplePrompt(context.Background(), mcp.GetPromptRequest{})
27-
assert.NoError(t, err)
28-
assert.GreaterOrEqual(t, len(out.Messages), 1)
25+
// simple
26+
out, err := handleSimplePrompt(context.Background(), mcp.GetPromptRequest{})
27+
assert.NoError(t, err)
28+
assert.GreaterOrEqual(t, len(out.Messages), 1)
2929

30-
// complex with args
31-
req := mcp.GetPromptRequest{Params: mcp.GetPromptParams{Arguments: map[string]string{
32-
"temperature": "0.7",
33-
"style": "short",
34-
}}}
35-
out2, err := handleComplexPrompt(context.Background(), req)
36-
assert.NoError(t, err)
37-
assert.GreaterOrEqual(t, len(out2.Messages), 2)
30+
// complex with args
31+
req := mcp.GetPromptRequest{Params: mcp.GetPromptParams{Arguments: map[string]string{
32+
"temperature": "0.7",
33+
"style": "short",
34+
}}}
35+
out2, err := handleComplexPrompt(context.Background(), req)
36+
assert.NoError(t, err)
37+
assert.GreaterOrEqual(t, len(out2.Messages), 2)
3838
}
3939

4040
func TestHandleLongRunningOperation_ZeroSteps_NoProgress(t *testing.T) {
41-
// Provide steps=0 to avoid progress notifications and timing
42-
args, _ := json.Marshal(map[string]any{"duration": 0, "steps": 0})
43-
req := mcp.CallToolRequest{Params: mcp.CallToolParams{Arguments: json.RawMessage(args), Meta: &mcp.Meta{}}}
44-
out, err := handleLongRunningOperationTool(context.Background(), req)
45-
assert.NoError(t, err)
46-
assert.NotNil(t, out)
41+
// Provide steps=0 to avoid progress notifications and timing
42+
args, _ := json.Marshal(map[string]any{"duration": 0, "steps": 0})
43+
req := mcp.CallToolRequest{Params: mcp.CallToolParams{Arguments: json.RawMessage(args), Meta: &mcp.Meta{}}}
44+
out, err := handleLongRunningOperationTool(context.Background(), req)
45+
assert.NoError(t, err)
46+
assert.NotNil(t, out)
4747
}

internal/apiserver/handler/auth_missing_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestHandler_UpdateUserTenants_Success(t *testing.T) {
214214
req, _ := http.NewRequest(http.MethodPut, "/users/tenants", nil)
215215
r.ServeHTTP(w, req)
216216
assert.Equal(t, http.StatusOK, w.Code)
217-
assert.Contains(t, f.removedIds, uint(1)) // removed tenant 1
217+
assert.Contains(t, f.removedIds, uint(1)) // removed tenant 1
218218
assert.Contains(t, f.addedPairs, [2]uint{5, 3}) // added tenant 3 to user 5
219219
}
220220

@@ -272,4 +272,4 @@ func TestHandler_UpdateUserTenants_UserNotFound(t *testing.T) {
272272
req, _ := http.NewRequest(http.MethodPut, "/users/tenants", nil)
273273
r.ServeHTTP(w, req)
274274
assert.Equal(t, http.StatusInternalServerError, w.Code)
275-
}
275+
}

0 commit comments

Comments
 (0)