Skip to content

Commit

Permalink
fix(contrib/labstack/echo.v4): sync with main
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Sep 2, 2024
1 parent bb94c3e commit 4d3fad6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
46 changes: 32 additions & 14 deletions contrib/labstack/echo.v4/echotrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package echo

import (
"errors"
"fmt"
"math"
"net/http"
Expand Down Expand Up @@ -38,24 +37,34 @@ func Middleware(opts ...Option) echo.MiddlewareFunc {
for _, fn := range opts {
fn.apply(cfg)
}
instr.Logger().Debug("contrib/labstack/echo: Configuring Middleware: %#v", cfg)
spanOpts := []tracer.StartSpanOption{
tracer.ServiceName(cfg.serviceName),
instr.Logger().Debug("contrib/labstack/echo.v4: Configuring Middleware: %#v", cfg)
spanOpts := make([]tracer.StartSpanOption, 0, 3+len(cfg.tags))
spanOpts = append(spanOpts, tracer.ServiceName(cfg.serviceName))
for k, v := range cfg.tags {
spanOpts = append(spanOpts, tracer.Tag(k, v))
}
spanOpts = append(spanOpts,
tracer.Tag(ext.Component, instrumentation.PackageLabstackEchoV4),
tracer.Tag(ext.SpanKind, ext.SpanKindServer),
}
)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// If we have an ignoreRequestFunc, use it to see if we proceed with tracing
if cfg.ignoreRequestFunc != nil && cfg.ignoreRequestFunc(c) {
return next(c)
}

request := c.Request()
resource := request.Method + " " + c.Path()
route := c.Path()
resource := request.Method + " " + route
opts := options.Copy(spanOpts) // opts must be a copy of spanOpts, locally scoped, to avoid races.
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
opts = append(opts,
tracer.ResourceName(resource),
tracer.Tag(ext.HTTPRoute, route),
httptrace.HeaderTagsFromRequest(request, cfg.headerTags))
// TODO: Should this also have an `http.route` tag like the v4 library does?

var finishOpts []tracer.FinishOption
if cfg.noDebugStack {
Expand All @@ -70,18 +79,15 @@ func Middleware(opts ...Option) echo.MiddlewareFunc {
// pass the span through the request context
c.SetRequest(request.WithContext(ctx))

// Use AppSec if enabled by user
if instr.AppSecEnabled() {
next = withAppSec(next, span)
}

// serve the request to the next middleware
err := next(c)
if err != nil {
if err != nil && !shouldIgnoreError(cfg, err) {
// It is impossible to determine what the final status code of a request is in echo.
// This is the best we can do.
var echoErr *echo.HTTPError
if errors.As(err, &echoErr) {
if echoErr, ok := cfg.translateError(err); ok {
if cfg.isStatusError(echoErr.Code) {
finishOpts = append(finishOpts, tracer.WithError(err))
}
Expand All @@ -95,16 +101,28 @@ func Middleware(opts ...Option) echo.MiddlewareFunc {
}
} else if status := c.Response().Status; status > 0 {
if cfg.isStatusError(status) {
finishOpts = append(finishOpts, tracer.WithError(fmt.Errorf("%d: %s", status, http.StatusText(status))))
if statusErr := errorFromStatusCode(status); !shouldIgnoreError(cfg, statusErr) {
finishOpts = append(finishOpts, tracer.WithError(statusErr))
}
}
span.SetTag(ext.HTTPCode, strconv.Itoa(status))
} else {
if cfg.isStatusError(200) {
finishOpts = append(finishOpts, tracer.WithError(fmt.Errorf("%d: %s", 200, http.StatusText(200))))
if statusErr := errorFromStatusCode(200); !shouldIgnoreError(cfg, statusErr) {
finishOpts = append(finishOpts, tracer.WithError(statusErr))
}
}
span.SetTag(ext.HTTPCode, "200")
}
return err
}
}
}

func errorFromStatusCode(statusCode int) error {
return fmt.Errorf("%d: %s", statusCode, http.StatusText(statusCode))
}

func shouldIgnoreError(cfg *config, err error) bool {
return cfg.errCheck != nil && !cfg.errCheck(err)
}
4 changes: 2 additions & 2 deletions contrib/labstack/echo.v4/echotrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,10 @@ func TestWithErrorCheck(t *testing.T) {

span := spans[0]
if tt.wantErr == nil {
assert.NotContains(t, span.Tags(), ext.Error)
assert.NotContains(t, span.Tags(), ext.ErrorMsg)
return
}
assert.Equal(t, tt.wantErr, span.Tag(ext.Error))
assert.Equal(t, tt.wantErr.Error(), span.Tag(ext.ErrorMsg))
})
}
}
Expand Down

0 comments on commit 4d3fad6

Please sign in to comment.