From 8244cc7764d3f537bc055b40a90ff15710ad3173 Mon Sep 17 00:00:00 2001 From: Stanley Nguyen Date: Tue, 6 Oct 2020 04:10:17 +0800 Subject: [PATCH] fix(echo): return nil from middleware to handle error only once (#377) --- CHANGELOG.md | 2 +- .../github.com/labstack/echo/otelecho/echo.go | 2 +- .../labstack/echo/otelecho/echo_test.go | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc5debc68d..87ce2652586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed +- The `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho.Middleware` no longer sends duplicate errors to the global `ErrorHandler`. (#377, #364) - The import comment in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` is now correctly quoted. (#379) ## [0.12.0] - 2020-09-25 @@ -124,7 +125,6 @@ This release upgrades its [go.opentelemetry.io/otel](https://github.com/open-tel - Update dependabot configuration to correctly check all included packages. (#131) - Update `RELEASING.md` with correct `tag.sh` command. (#130) - ## [0.8.0] - 2020-07-10 This release upgrades its [go.opentelemetry.io/otel](https://github.com/open-telemetry/opentelemetry-go/releases/tag/v0.8.0) dependency to v0.8.0, includes minor fixes, and new instrumentation. diff --git a/instrumentation/github.com/labstack/echo/otelecho/echo.go b/instrumentation/github.com/labstack/echo/otelecho/echo.go index 436ae939af3..1abe0a755ab 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/echo.go +++ b/instrumentation/github.com/labstack/echo/otelecho/echo.go @@ -89,7 +89,7 @@ func Middleware(service string, opts ...Option) echo.MiddlewareFunc { span.SetAttributes(attrs...) span.SetStatus(spanStatus, spanMessage) - return err + return nil } } } diff --git a/instrumentation/github.com/labstack/echo/otelecho/echo_test.go b/instrumentation/github.com/labstack/echo/otelecho/echo_test.go index c93e0003abe..87155821c3a 100644 --- a/instrumentation/github.com/labstack/echo/otelecho/echo_test.go +++ b/instrumentation/github.com/labstack/echo/otelecho/echo_test.go @@ -147,6 +147,23 @@ func TestError(t *testing.T) { assert.Equal(t, codes.Internal, span.Status) } +func TestErrorOnlyHandledOnce(t *testing.T) { + router := echo.New() + timesHandlingError := 0 + router.HTTPErrorHandler = func(e error, c echo.Context) { + timesHandlingError++ + } + router.Use(Middleware("test-service")) + router.GET("/", func(c echo.Context) error { + return errors.New("Mock Error") + }) + r := httptest.NewRequest(http.MethodGet, "/", nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, r) + + assert.Equal(t, 1, timesHandlingError) +} + func TestGetSpanNotInstrumented(t *testing.T) { router := echo.New() router.GET("/ping", func(c echo.Context) error {