我想在GO中创建一个自定义插件,然后将其加载到Krakend(Text)中。
在此插件中,我想生成一个跟踪跨度上下文(root)并注入每个传入请求的标头,然后传播到另一个后端API。 我有此代码:
import (
"context"
"fmt"
"net/http"
// jaegerPropagator "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"
otelTrace "go.opentelemetry.io/otel/trace"
)
func newTraceProvider() (*trace.TracerProvider, error) {
traceExporter, err := stdouttrace.New(
stdouttrace.WithPrettyPrint())
if err != nil {
return nil, err
}
traceProvider := trace.NewTracerProvider(
trace.WithBatcher(traceExporter),
)
return traceProvider, nil
}
// This method takes an "http.Handler" and returns another "http.Handler" that modifies the incoming request
func (req registrable) registerHandlers(ctx context.Context, extra map[string]interface{}, handler http.Handler) (http.Handler, error) {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
// Set up trace provider.
tracerProvider, err := newTraceProvider()
fmt.Println(err)
tracer := tracerProvider.Tracer(nameTracer)
// Start a new server span using the tracer and the incoming request context
newCtx, span := tracer.Start(
ctx,
"Root-Server-Span",
otelTrace.WithSpanKind(otelTrace.SpanKindServer),
)
defer span.End() // Ensure the span is ended
carrier := propagation.HeaderCarrier(req.Header)
otel.GetTextMapPropagator().Inject(newCtx, carrier)
// Call the next handler in the chain with the new context
handler.ServeHTTP(w, req.WithContext(newCtx))
}), nil
}
插件成功加载,但不能注入标题。 有人在那里知道上述并可以帮助我吗?
我忘了将插件的名称放在克拉肯德。
问题解决了!!!