Skip to content

Commit

Permalink
feat(trace): support mqtt 5 trace
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <huangjy@emqx.io>
  • Loading branch information
ngjaying committed Nov 21, 2024
1 parent 300310a commit 376ddd7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion internal/io/mqtt/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/lf-edge/ekuiper/v2/internal/conf"
"github.com/lf-edge/ekuiper/v2/internal/pkg/util"
"github.com/lf-edge/ekuiper/v2/internal/topo/node/tracenode"
"github.com/lf-edge/ekuiper/v2/pkg/cast"
"github.com/lf-edge/ekuiper/v2/pkg/connection"
)
Expand Down Expand Up @@ -115,7 +116,16 @@ func (ms *Sink) Collect(ctx api.StreamContext, item api.RawTuple) error {
}
}
}

traced, _, span := tracenode.TraceInput(ctx, item, fmt.Sprintf("%s_emit", ctx.GetOpId()))
if traced {
defer span.End()
traceID := span.SpanContext().TraceID()
spanID := span.SpanContext().SpanID()
if props == nil {
props = make(map[string]string)
}
props["traceparent"] = tracenode.BuildTraceParentId(traceID, spanID)
}
ctx.GetLogger().Debugf("publishing to topic %s", tpc)
return ms.cli.Publish(ctx, tpc, ms.adconf.Qos, ms.adconf.Retained, item.Raw(), props)
}
Expand Down
9 changes: 7 additions & 2 deletions internal/io/mqtt/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ func (ms *SourceConnector) Subscribe(ctx api.StreamContext, ingest api.BytesInge

func (ms *SourceConnector) onMessage(ctx api.StreamContext, msg any, ingest api.BytesIngest) {
rcvTime := timex.GetNow()
payload, meta, _ := ms.cli.ParseMsg(ctx, msg)
payload, meta, props := ms.cli.ParseMsg(ctx, msg)
if ms.eof != nil && ms.eofPayload != nil && bytes.Equal(ms.eofPayload, payload) {
ms.eof(ctx)
return
}
// TODO property trace
// extract trace id
if props != nil {
if tid, ok := props["traceparent"]; ok {
meta["traceId"] = tid
}
}
ingest(ctx, payload, meta, rcvTime)
}

Expand Down

0 comments on commit 376ddd7

Please sign in to comment.