Skip to content

Commit

Permalink
fix: fix trace rest api propagation (#3214)
Browse files Browse the repository at this point in the history
Signed-off-by: Song Gao <disxiaofei@163.com>
  • Loading branch information
Yisaer authored Sep 20, 2024
1 parent 6b8665d commit 2d58919
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 8 additions & 2 deletions internal/server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/lf-edge/ekuiper/contract/v2/api"
"go.opentelemetry.io/otel/propagation"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -138,9 +139,14 @@ func jsonByteResponse(buffer bytes.Buffer, w http.ResponseWriter, logger api.Log
func traceMiddleware(next http.Handler) http.Handler {
t := tracer.GetTracer()
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
_, span := t.Start(context.Background(), req.URL.Path)
propagator := propagation.TraceContext{}
originCtx := context.Background()
ctx := propagator.Extract(originCtx, propagation.HeaderCarrier(req.Header))
if ctx != originCtx {
_, span := t.Start(ctx, req.URL.Path)
defer span.End()
}
next.ServeHTTP(w, req)
span.End()
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/tracer/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type SpanExporter struct {
spanStorage LocalSpanStorage
}

func NewSpanExporter(remoteCollector bool) (*SpanExporter, error) {
func NewSpanExporter(remoteCollector bool, remoteEndpoint string) (*SpanExporter, error) {
s := &SpanExporter{}
if remoteCollector {
exporter, err := otlptracehttp.New(context.Background(),
otlptracehttp.WithEndpoint(conf.Config.OpenTelemetry.RemoteEndpoint),
otlptracehttp.WithEndpoint(remoteEndpoint),
otlptracehttp.WithInsecure(),
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (g *GlobalTracerManager) SetTracer(enableRemote bool, serviceName, endpoint
g.ServiceName = serviceName
g.EnableRemoteEndpoint = enableRemote
g.RemoteEndpoint = endpoint
exporter, err := NewSpanExporter(enableRemote)
exporter, err := NewSpanExporter(enableRemote, endpoint)
if err != nil {
return err
}
Expand Down

0 comments on commit 2d58919

Please sign in to comment.