weixin_39972019
weixin_39972019
2020-12-09 00:25

gin version

This is more a query than an issue.

It would be useful to have a gin (https://github.com/gin-gonic/gin) version of this middleware.

It's not clear that the approach used for gorilla (https://github.com/opentracing-contrib/go-gorilla/blob/master/gorilla/server.go) would work for gin though. This is because the middleware signatures are a bit different.

Would it be ok to reuse/tweak the code here to produce a separate gin version?

The gin middleware specific part would look as follows:


// Middleware is a gin native version of the equivalent middleware in:          
//   https://github.com/opentracing-contrib/go-stdlib/                          
func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc {   
    opts := mwOptions{                                                          
        opNameFunc: func(r *http.Request) string {                              
            return "HTTP " + r.Method                                           
        },                                                                      
        spanObserver: func(span opentracing.Span, r *http.Request) {},          
    }                                                                           
    for _, opt := range options {                                               
        opt(&opts)                                                              
    }                                                                           

    return func(c *gin.Context) {                                               
        carrier := opentracing.HTTPHeadersCarrier(c.Request.Header)             
        ctx, _ := tr.Extract(opentracing.HTTPHeaders, carrier)                  
        op := opts.opNameFunc(c.Request)                                        
        sp := tr.StartSpan(op, ext.RPCServerOption(ctx))                        
        ext.HTTPMethod.Set(sp, c.Request.Method)                                
        ext.HTTPUrl.Set(sp, c.Request.URL.String())                             
        opts.spanObserver(sp, c.Request)                                        

        // set component name, use "net/http" if caller does not specify        
        componentName := opts.componentName                                     
        if componentName == "" {                                                
            componentName = defaultComponentName                                
        }                                                                       
        ext.Component.Set(sp, componentName)                                    
        c.Request = c.Request.WithContext(                                      
            opentracing.ContextWithSpan(c.Request.Context(), sp))               

        c.Next()                                                                

        ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status()))                   
        sp.Finish()                                                             
    }                                                                           
} 

该提问来源于开源项目:opentracing-contrib/go-stdlib

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39917090 weixin_39917090 5月前

    This repo is intended for Go std library. Why not create a separate repo for gin?

    点赞 评论 复制链接分享
  • weixin_39972019 weixin_39972019 5月前

    I created a test repo here: https://github.com/stuart-mclaren/go-gin-test

    If that looks reasonable to you (in particular the code reuse in server.go) I could look at creating a (suitably licensed) opentracing-contrib/go-gin repo.

    点赞 评论 复制链接分享
  • weixin_39917090 weixin_39917090 5月前

    tbh I am not worried much about code reuse, in this particular case creating cross-project dependencies is more trouble. Feel free to add your repo to the OT Registry (see the website), it doesn't need to go to opentracing-contrib just yet (we can always move it later).

    点赞 评论 复制链接分享
  • weixin_39972019 weixin_39972019 5月前

    Proposed wiki update: https://github.com/opentracing/opentracing.io/pull/364

    Thanks for your help.

    点赞 评论 复制链接分享

相关推荐