|
6 | 6 | "fmt"
|
7 | 7 | "net/http"
|
8 | 8 | "net/http/httptest"
|
| 9 | + "strings" |
9 | 10 | "testing"
|
10 | 11 |
|
11 | 12 | "github.com/labstack/echo/v4"
|
@@ -101,11 +102,14 @@ func (tr *mockTracer) currentSpan() *mockSpan {
|
101 | 102 | }
|
102 | 103 |
|
103 | 104 | func (tr *mockTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
|
104 |
| - tr.hasStartSpanWithOption = (len(opts) > 0) |
| 105 | + tr.hasStartSpanWithOption = len(opts) > 0 |
105 | 106 | if tr.span != nil {
|
| 107 | + tr.span.opName = operationName |
106 | 108 | return tr.span
|
107 | 109 | }
|
108 |
| - return createSpan(tr) |
| 110 | + span := createSpan(tr) |
| 111 | + span.opName = operationName |
| 112 | + return span |
109 | 113 | }
|
110 | 114 |
|
111 | 115 | func (tr *mockTracer) Inject(sm opentracing.SpanContext, format interface{}, carrier interface{}) error {
|
@@ -325,3 +329,58 @@ func TestTraceWithoutLimitHTTPBody(t *testing.T) {
|
325 | 329 | assert.Equal(t, "123456789012345678901234567890", tracer.currentSpan().getLog("http.req.body"))
|
326 | 330 | assert.Equal(t, "Hi 123456789012345678901234567890", tracer.currentSpan().getLog("http.resp.body"))
|
327 | 331 | }
|
| 332 | + |
| 333 | +func TestTraceWithDefaultOperationName(t *testing.T) { |
| 334 | + tracer := createMockTracer() |
| 335 | + |
| 336 | + e := echo.New() |
| 337 | + e.Use(Trace(tracer)) |
| 338 | + |
| 339 | + e.GET("/trace", func(c echo.Context) error { |
| 340 | + return c.String(http.StatusOK, "Hi") |
| 341 | + }) |
| 342 | + |
| 343 | + req := httptest.NewRequest(http.MethodGet, "/trace", nil) |
| 344 | + rec := httptest.NewRecorder() |
| 345 | + e.ServeHTTP(rec, req) |
| 346 | + |
| 347 | + assert.Equal(t, "HTTP GET URL: /trace", tracer.currentSpan().getOpName()) |
| 348 | +} |
| 349 | + |
| 350 | +func TestTraceWithCustomOperationName(t *testing.T) { |
| 351 | + tracer := createMockTracer() |
| 352 | + |
| 353 | + e := echo.New() |
| 354 | + e.Use(TraceWithConfig(TraceConfig{ |
| 355 | + Tracer: tracer, |
| 356 | + ComponentName: "EchoTracer", |
| 357 | + OperationNameFunc: func(ctx echo.Context) string { |
| 358 | + // This is an example of operation name customization |
| 359 | + // In most cases default formatting is more than enough |
| 360 | + req := ctx.Request() |
| 361 | + opName := "HTTP " + req.Method |
| 362 | + |
| 363 | + path := ctx.Path() |
| 364 | + paramNames := ctx.ParamNames() |
| 365 | + |
| 366 | + for _, name := range paramNames { |
| 367 | + from := ":" + name |
| 368 | + to := "{" + name + "}" |
| 369 | + path = strings.ReplaceAll(path, from, to) |
| 370 | + } |
| 371 | + |
| 372 | + return opName + " " + path |
| 373 | + }, |
| 374 | + })) |
| 375 | + |
| 376 | + e.GET("/trace/:traceID/spans/:spanID", func(c echo.Context) error { |
| 377 | + return c.String(http.StatusOK, "Hi") |
| 378 | + }) |
| 379 | + |
| 380 | + req := httptest.NewRequest(http.MethodGet, "/trace/123456/spans/123", nil) |
| 381 | + rec := httptest.NewRecorder() |
| 382 | + e.ServeHTTP(rec, req) |
| 383 | + |
| 384 | + assert.Equal(t, true, tracer.currentSpan().isFinished()) |
| 385 | + assert.Equal(t, "HTTP GET /trace/{traceID}/spans/{spanID}", tracer.currentSpan().getOpName()) |
| 386 | +} |
0 commit comments