今日已办
watermill
将 key 设置到 message 中
修改 watermill-kafka
源码 将 key 设置到 message.metadata中
接入 otel-sdk
- 添加 middleware
resolveUpstreamCtx
解析上游上下文,开启根Span - 添加 middleware
middleware.InstantAck
- 马上ACK,使得多条消息可以平行处理(走middleware 和 handler 的逻辑)
// Package pubsub
// @Author xzx 2023/8/12 10:01:00
package pubsub
import (
"context"
"encoding/json"
"github.com/ThreeDotsLabs/watermill/message"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"profile/internal/connector"
"profile/internal/log"
"profile/internal/otelclient"
"profile/internal/schema"
"profile/internal/schema/performance"
"profile/internal/state"
"profile/internal/watermill/watermillkafka"
)
// consumeCtxData
// @Description: Data collection of a message processing context
// @Author xzx 2023-08-17 13:36:12
type consumeCtxData struct {
Status int
Event schema.Event
RootSpan trace.Span
RootSpanCtx context.Context
AppID string // API 上报
FetchScenario string // API 上报
}
// resolveUpstreamCtx
// @Description
// @Author xzx 2023-08-18 11:15:09
// @Param h
// @Return message.HandlerFunc
func resolveUpstreamCtx(h message.HandlerFunc) message.HandlerFunc {
return func(msg *message.Message) ([]*message.Message, error) {
var data consumeCtxData
// get upstream producer's W3C trace context via propagation
headerCarrier := make(propagation.HeaderCarrier)
headerCarrier.Set("Traceparent", msg.Metadata.Get("Traceparent"))
upstreamProducerCtx := otel.GetTextMapPropagator().Extract(msg.Context(), headerCarrier)
// set traceID to consumer context
consumerCtx := trace.ContextWithRemoteSpanContext(msg.Context(),
trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.SpanContextFromContext(upstreamProducerCtx).TraceID(),
}))
//start tracing
data.RootSpanCtx, data.RootSpan = otelclient.ConsumerTracer.Start(consumerCtx, "Profile-Consumer",
trace.WithSpanKind(trace.SpanKindConsumer),
trace.WithLinks(trace.LinkFromContext(upstreamProducerCtx, semconv.OpentracingRefTypeFollowsFrom)))
msg.SetContext(context.WithValue(msg.Context(), "data", &data))
return h(msg)
}
}
// unpackKafkaMessage
// @Description
//