rest otel support (#943)

master
zhoushuguang 3 years ago committed by GitHub
parent f669e1226c
commit 280e837c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx" "github.com/tal-tech/go-zero/core/syncx"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
@ -53,7 +54,17 @@ func StartAgent(c Config) {
otel.SetTracerProvider(tp) otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
otel.SetErrorHandler(otelErrHandler{})
enabled.Set(true) enabled.Set(true)
}) })
} }
// errHandler handing otel errors.
type otelErrHandler struct{}
var _ otel.ErrorHandler = otelErrHandler{}
func (o otelErrHandler) Handle(err error) {
logx.Errorf("[otel] error: %v", err)
}

@ -1,8 +1,6 @@
package opentelemetry package opentelemetry
const ( const TraceName = "go-zero"
TraceName = "go-zero"
)
// A Config is a opentelemetry config. // A Config is a opentelemetry config.
type Config struct { type Config struct {

@ -45,6 +45,9 @@ func (sc ServiceConf) SetUp() error {
if len(sc.Log.ServiceName) == 0 { if len(sc.Log.ServiceName) == 0 {
sc.Log.ServiceName = sc.Name sc.Log.ServiceName = sc.Name
} }
if len(sc.OpenTelemetry.Name) == 0 {
sc.OpenTelemetry.Name = sc.Name
}
if err := logx.SetUp(sc.Log); err != nil { if err := logx.SetUp(sc.Log); err != nil {
return err return err
} }

@ -108,6 +108,7 @@ func (s *engine) bindRoute(fr featuredRoutes, router httpx.Router, metrics *stat
route Route, verifier func(chain alice.Chain) alice.Chain) error { route Route, verifier func(chain alice.Chain) alice.Chain) error {
chain := alice.New( chain := alice.New(
handler.TracingHandler, handler.TracingHandler,
handler.OtelHandler(route.Path),
s.getLogHandler(), s.getLogHandler(),
handler.PrometheusHandler(route.Path), handler.PrometheusHandler(route.Path),
handler.MaxConns(s.conf.MaxConns), handler.MaxConns(s.conf.MaxConns),

@ -0,0 +1,36 @@
package handler
import (
"net/http"
"github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
oteltrace "go.opentelemetry.io/otel/trace"
)
func OtelHandler(path string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
if !opentelemetry.Enabled() {
return next
}
propagator := otel.GetTextMapPropagator()
tracer := otel.GetTracerProvider().Tracer(opentelemetry.TraceName)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
spanCtx, span := tracer.Start(
ctx,
path,
oteltrace.WithSpanKind(oteltrace.SpanKindServer),
oteltrace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", path, r)...),
)
defer span.End()
next.ServeHTTP(w, r.WithContext(spanCtx))
})
}
}

@ -0,0 +1,49 @@
package handler
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
func TestOtelHandler(t *testing.T) {
opentelemetry.StartAgent(opentelemetry.Config{
Name: "go-zero-test",
Endpoint: "http://localhost:14268/api/traces",
Batcher: "jaeger",
Sampler: 1.0,
})
ts := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header))
spanCtx := trace.SpanContextFromContext(ctx)
assert.Equal(t, true, spanCtx.IsValid())
}),
)
defer ts.Close()
client := ts.Client()
err := func(ctx context.Context) error {
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
defer span.End()
req, _ := http.NewRequest("GET", ts.URL, nil)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
res, err := client.Do(req)
assert.Equal(t, err, nil)
_ = res.Body.Close()
return nil
}(context.Background())
assert.Equal(t, err, nil)
}

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/tal-tech/go-zero/core/opentelemetry" "github.com/tal-tech/go-zero/core/opentelemetry"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"

Loading…
Cancel
Save