上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

opentracing-gorm SQL追踪源码分析

[复制链接]
  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    638

    主题

    5234

    回帖

    6105

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6105

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2023-8-8 17:15:39 | 显示全部楼层 | 阅读模式

    在https://github.com/go-programming-tour-book/blog-service/blob/fc62a2672eb7ac8c5eb08ef1710e46a74bb19d90/internal/service/service.go

    可以看到每次初始化代码之后都会调用otgorm.WithContext

    type Service struct {
        ctx context.Context
        dao *dao.Dao
    }
    
    func New(ctx context.Context) Service {
        svc := Service{ctx: ctx}
        svc.dao = dao.New(otgorm.WithContext(svc.ctx, global.DBEngine))
        return svc
    }

    我们看看otgorm.WithContext做了什么

    代码在https://github.com/eddycjy/opentracing-gorm/blob/master/otgorm.go

    这里调用了opentracing.SpanFromContext从context中读取parentSpan,判断是否存在,如果存在则设置到dao数据库实例上

    const (
        parentSpanGormKey = "opentracing:parent.span"
        spanGormKey       = "opentracing:span"
    )
    
    // SetSpanToGorm sets span to gorm settings, returns cloned DB
    func WithContext(ctx context.Context, db *gorm.DB) *gorm.DB {
        if ctx == nil {
            return db
        }
        parentSpan := opentracing.SpanFromContext(ctx)
        if parentSpan == nil {
            return db
        }
        return db.Set(parentSpanGormKey, parentSpan)
    }

    SpanFromContext是openTracing的函数

    https://github.com/opentracing/opentracing-go/blob/10b1cf09e00bdc84234b8c7a4b4d4e4afe64de87/gocontext.go#L26

    主要调用ctx.Value通过activeSpanKeyjian

    
    func SpanFromContext(ctx context.Context) Span {
        val := ctx.Value(activeSpanKey)
        if sp, ok := val.(Span); ok {
            return sp
        }
        return nil
    }

    这里基本的逻辑已经清晰了,每次初始化链接的时候都会把ctx中的span注入到对应的dao实例上

    那倒是在哪里注入的,一般我们会在中间的拦截器层调用StartSpanFromContextWithTracer

    span, newCtx = opentracing.StartSpanFromContextWithTracer(
                    c.Request.Context(),
                    global.Tracer,
                    c.Request.URL.Path,
                    opentracing.ChildOf(spanCtx),
                    opentracing.Tag{Key: string(ext.Component), Value: "HTTP"},
                )

    会返回一个新的newCtx,然后通过withContext设置上下文关系

    我们可以看看StartSpanFromContextWithTracer的源码

    func StartSpanFromContextWithTracer(ctx context.Context, tracer Tracer, operationName string, opts ...StartSpanOption) (Span, context.Context) {
        if parentSpan := SpanFromContext(ctx); parentSpan != nil {
            opts = append(opts, ChildOf(parentSpan.Context()))
        }
        span := tracer.StartSpan(operationName, opts...)
        return span, ContextWithSpan(ctx, span)
    }

    这里主要调用了ContextWithSpan做了一层封装,我们再去看ContextWithSpan

    发现通过withValue设置进去了对应的span

    var activeSpanKey = contextKey{}
    
    // ContextWithSpan returns a new `context.Context` that holds a reference to
    // the span. If span is nil, a new context without an active span is returned.
    func ContextWithSpan(ctx context.Context, span Span) context.Context {
        if span != nil {
            if tracerWithHook, ok := span.Tracer().(TracerContextWithSpanExtension); ok {
                ctx = tracerWithHook.ContextWithSpanHook(ctx, span)
            }
        }
        return context.WithValue(ctx, activeSpanKey, span)
    }

    到这里我们就了解了全部过程

    拦截器中通过StartSpanFromContextWithTracer创建的时候会自动注入到ctx
    然后在初始化dao层的时候ctx中的span并注入
    每次调用gorm都会触发钩子
    钩子再对其sql进行进一步处理实现sql跟踪效果

    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    296

    主题

    3950

    回帖

    3858

    积分

    管理员

    积分
    3858

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-8-9 09:38:15 | 显示全部楼层
    哥哥都看啥去了。。。。知道这是干啥的东西么
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    638

    主题

    5234

    回帖

    6105

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6105

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2023-8-9 15:05:20 | 显示全部楼层
    王一之 发表于 2023-8-9 09:38
    哥哥都看啥去了。。。。知道这是干啥的东西么

    jaeger链路追踪,grpc用的
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    296

    主题

    3950

    回帖

    3858

    积分

    管理员

    积分
    3858

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2023-8-9 15:07:04 | 显示全部楼层
    李恒道 发表于 2023-8-9 15:05
    jaeger链路追踪,grpc用的

    链路追踪干啥的,这个和grpc也没啥关系
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表