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

chore(middleware/tracing): upgrade otel semconv #3416

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

flc1125
Copy link
Contributor

@flc1125 flc1125 commented Sep 5, 2024

Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Sep 5, 2024
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
@flc1125
Copy link
Contributor Author

flc1125 commented Sep 11, 2024

或者整个v2版本:github.com/go-kratos/kratos/v2/middleware/tracing/v2,增加一些自定义的设定span补充的逻辑?

@shenqidebaozi

草稿版:

package v2

import (
	"context"

	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/propagation"
	"go.opentelemetry.io/otel/trace"

	"github.com/go-kratos/kratos/v2/middleware"
)

var defaultPropagator = propagation.NewCompositeTextMapPropagator(Metadata{}, propagation.Baggage{}, propagation.TraceContext{})

type Handler interface {
	Start(ctx context.Context, req interface{}, span trace.Span) context.Context
	End(ctx context.Context, span trace.Span, reply interface{}, err error)
}

type Tracer struct {
	tracerName     string
	spanKind       trace.SpanKind
	tracerProvider trace.TracerProvider
	propagator     propagation.TextMapPropagator
	handlers       []Handler
}

type Option func(*Tracer)

func WithTracerProvider(provider trace.TracerProvider) Option {
	return func(t *Tracer) {
		t.tracerProvider = provider
	}
}

func WithTracerName(name string) Option {
	return func(t *Tracer) {
		t.tracerName = name
	}
}

func WithSpanKind(kind trace.SpanKind) Option {
	return func(t *Tracer) {
		t.spanKind = kind
	}
}

func WithPropagator(propagator propagation.TextMapPropagator) Option {
	return func(t *Tracer) {
		t.propagator = propagator
	}
}

func WithHandlers(handlers ...Handler) Option {
	return func(t *Tracer) {
		t.handlers = append(t.handlers, handlers...)
	}
}

func WithOptions(opts ...Option) Option {
	return func(t *Tracer) {
		for _, o := range opts {
			o(t)
		}
	}
}

func NewTracer(opts ...Option) *Tracer {
	t := &Tracer{
		tracerName:     "kratos",
		spanKind:       trace.SpanKindInternal,
		tracerProvider: otel.GetTracerProvider(),
		propagator:     defaultPropagator,
		handlers: []Handler{
			DefaultHandler, // default handler
		},
	}
	return t
}

func (t *Tracer) Start(ctx context.Context, req interface{}) (context.Context, trace.Span) {
	ctx, span := t.tracerProvider.Tracer(t.tracerName).Start(ctx, "", trace.WithSpanKind(t.spanKind))
	for _, h := range t.handlers {
		h.Start(ctx, req, span)
	}
	return ctx, span
}

func (t *Tracer) End(ctx context.Context, span trace.Span, reply interface{}, err error) {
	for _, h := range t.handlers {
		h.End(ctx, span, reply, err)
	}
}

func Server(opts ...Option) middleware.Middleware {
	tracer := NewTracer(
		WithTracerName("kratos"),
		WithSpanKind(trace.SpanKindServer),
		WithOptions(opts...),
	)
	return func(handler middleware.Handler) middleware.Handler {
		return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
			ctx, span := tracer.Start(ctx, req)
			defer func() { tracer.End(ctx, span, reply, err) }()
			return handler(ctx, req)
		}
	}
}

@shenqidebaozi
Copy link
Member

There is no need to add V2 version

@shenqidebaozi
Copy link
Member

Retention is only to avoid affecting the query configuration that users have already used

@flc1125
Copy link
Contributor Author

flc1125 commented Sep 11, 2024

Retention is only to avoid affecting the query configuration that users have already used

OK。后面我在研究看看,如何在当前版本下,支持增加自定义处理 Handler 的支持。接口按如下定义OK没:

type Handler interface {
	Start(ctx context.Context, span trace.Span, req interface{}) context.Context
	End(ctx context.Context, span trace.Span, reply interface{}, err error) context.Context
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants