Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest otel support #943

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/opentelemetry/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -53,7 +54,17 @@ func StartAgent(c Config) {

otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
otel.SetErrorHandler(otelErrHandler{})

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)
}
4 changes: 1 addition & 3 deletions core/opentelemetry/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package opentelemetry

const (
TraceName = "go-zero"
)
const TraceName = "go-zero"

// A Config is a opentelemetry config.
type Config struct {
Expand Down
3 changes: 3 additions & 0 deletions core/service/serviceconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (sc ServiceConf) SetUp() error {
if len(sc.Log.ServiceName) == 0 {
sc.Log.ServiceName = sc.Name
}
if len(sc.OpenTelemetry.Name) == 0 {
sc.OpenTelemetry.Name = sc.Name
}
if err := logx.SetUp(sc.Log); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions rest/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
chain := alice.New(
handler.TracingHandler,
handler.OtelHandler(route.Path),
s.getLogHandler(),
handler.PrometheusHandler(route.Path),
handler.MaxConns(s.conf.MaxConns),
Expand Down
36 changes: 36 additions & 0 deletions rest/handler/otelhandler.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}
49 changes: 49 additions & 0 deletions rest/handler/otelhandler_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions zrpc/internal/clientinterceptors/opentracinginterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/tal-tech/go-zero/core/opentelemetry"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
Expand Down
1 change: 1 addition & 0 deletions zrpc/internal/serverinterceptors/opentracinginterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/tal-tech/go-zero/core/opentelemetry"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/codes"
Expand Down