echo.Server.Handle are overridden in StartServer()

Created on 19 Jun 2018  路  6Comments  路  Source: labstack/echo

Hi, Im trying to enclose echo's handle function to add tracing support from OpenCensus:

var e = echo.New()

// Wrap the Echo handler in an OpenCensus handler.
var ocHandler = &ochttp.Handler{Handler: e, IsPublicEndpoint: true}
e.Server.Handler = ocHandler

But later when starting echo server by: e.Start(), echo again points e.Server.Handler to it self. Why do it need to do it twice?

https://github.com/labstack/echo/blob/d36ff729613dd8e825455c504bea0586c43ac03d/echo.go#L647
https://github.com/census-instrumentation/opencensus-go

wontfix

Most helpful comment

For people who find this thread via Google, here's what I did to integrate OpenCensus with Echo.

func NewCensus() echo.MiddlewareFunc {
    return echo.WrapMiddleware(func(h http.Handler) http.Handler {
        return &ochttp.Handler{
            Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                if span := trace.FromContext(r.Context()); span != nil {
                    // Do stuff
                }
                h.ServeHTTP(w, r)
            }),
        }
    })
}

All 6 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@niklas-karlsson how did you get by with this issue? I'm trying to fit in echo with opencensus tracing as well.

For people who find this thread via Google, here's what I did to integrate OpenCensus with Echo.

func NewCensus() echo.MiddlewareFunc {
    return echo.WrapMiddleware(func(h http.Handler) http.Handler {
        return &ochttp.Handler{
            Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                if span := trace.FromContext(r.Context()); span != nil {
                    // Do stuff
                }
                h.ServeHTTP(w, r)
            }),
        }
    })
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Hi guys! I created a middleware for it
https://github.com/faabiosr/echo-middleware

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arun0009 picture arun0009  路  3Comments

vishr picture vishr  路  3Comments

spielstein picture spielstein  路  3Comments

alexzorin picture alexzorin  路  3Comments

kyokomi picture kyokomi  路  3Comments