• 腾讯mini项目-【指标监控服务重构】2023-08-01


    今日已办

    合并 Traefik 和 Profile 的 Trace

    Traceparent Header 理解有误

    image-20230731211105189

    Trace Context (w3.org)

    image-20230801141706444

    故需要解析 TraceHeader 才能获取trace_id、parent_id

    func (profileCtx *ProfileContext) UnpackKafkaMessage(ctx context.Context) (needBreak bool, tpsStatus string, contextErr error) {
    	var traceID trace.TraceID
    	var parentID trace.SpanID
    	headers := profileCtx.msg.Headers
    	for _, h := range headers {
    		key := h.Key
    		value := string(h.Value)
    		if key == "Traceparent" {
                // eg: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
    			split := strings.Split(value, "-")
    			traceID, _ = trace.TraceIDFromHex(split[1])
    			parentID, _ = trace.SpanIDFromHex(split[2])
    			break
    		}
    	}
    	log.Logger.Info("[UnpackKafkaItem] parse header traceparent",
    		zap.String("traceId", traceID.String()),
    		zap.String("parentID", parentID.String()),
    	)
    	//otel.GetTextMapPropagator().Extract(profileCtx.Ctx, header)
    	ctx = trace.ContextWithRemoteSpanContext(ctx,
    		trace.NewSpanContext(trace.SpanContextConfig{
    			TraceID: traceID,
    		}))
    	var span trace.Span
    	profileCtx.Ctx, span = consumerTracer.Start(ctx, "UnpackKafkaMessage")
    	//profileCtx.Ctx, span = consumerTracer.Start(profileCtx.Ctx, "UnpackKafkaMessage")
    	defer span.End()
    	// ...
    	return
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    image-20230801145012158

    image-20230801144721174

    image-20230801145233913

    调研上下文传递 Propagator

    参考 passthrough

    Venus 服务中初始化 TextMapPropagator

    func initPassthroughGlobals() {
    	// We explicitly DO NOT set the global TracerProvider using otel.SetTracerProvider().
    	// The unset TracerProvider returns a "non-recording" span, but still passes through context.
    	log.Logger().Info("Register a global TextMapPropagator, but do not register a global TracerProvider to be in \"passthrough\" mode.")
    	log.Logger().Info("The \"passthrough\" mode propagates the TraceContext and Baggage, but does not record spans.")
    	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    再根据 Fiber 的 Context 和 Header 来解包出 Context ,创建 Span

    var (
    	traceparent    string
    	producerTracer = otel.Tracer("venus-producer",
    		trace.WithInstrumentationAttributes(attribute.String("venus.work", "producer")))
    )
    
    func SplitAndValidate(c *fiber.Ctx) error {
    	traceparent = c.Get("Traceparent", "default")
    	log.Logger().Info("Traceparent", zap.Any("Traceparent", traceparent))
    	log.Logger().Debug("split and validate", zap.String("client", c.IP()), zap.String("agent", string(c.Context().UserAgent())))
    	header := make(propagation.HeaderCarrier)
    	for k, v := range c.GetReqHeaders() {
    		header.Set(k, v)
    	}
    	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
    	ctx := otel.GetTextMapPropagator().Extract(c.Context(), header)
    	_, span := producerTracer.Start(ctx, "SplitAndValidate")
    	defer span.End()
    	// ...
    	return c.Next()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    image-20230801151310593

    Venus中注入,ctx 为 Kafka 的 WriteMessages 的,携带 TraceParentHeader

    【感觉没有必要,确实我移除这部分代码正常运行】

    1. 因为 kafka 没有上下文机制,是使用 header 来传播 TraceParent
    2. TraceParent 包含了 trace-id、parent-id共4个字段
    3. Extract方法应该可以根据这个TraceParent的值来解析出tracespan的关系

    image-20230801162219272

    以相同的方式在 Profile 中处理,关于traceID、parentID的代码就可以移除了

    image-20230801162426841

    image-20230801162008177

    现在的 venus 和 profile 是同一级,是因为 traefik 传播的 traceparent 没有修改,parent-id 是相同的,应该需要 venus 将最后一个 span 的 span_id 更新到 traceparent 的 parent-id 的部分,然后再用 kafka 的 header 传播下来

    image-20230801162012420

    image-20230801162501552

    明日待办

    1. 组长会议汇报进度和问题
  • 相关阅读:
    Spring request工具类
    Vue中修改 elementui组件样式 不生效
    mysql部署
    .NET 的 Native AOT 现在是什么样的?
    JAVA核酸预约检测管理系统毕业设计 开题报告
    举个栗子~Tableau 技巧(237):用多节点瀑布图分阶段查看数据
    IDEA使用技巧
    Unity射线实现碰撞检测(不需要rigbody组件)
    【PHP设计模式03】抽象工厂模式
    第十二届蓝桥杯物联网试题(国赛)
  • 原文地址:https://blog.csdn.net/xzx18822942899/article/details/132878919