-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
flc1125
wants to merge
6
commits into
go-kratos:main
Choose a base branch
from
flc1125:otel-deps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed-off-by: Flc゛ <[email protected]>
dosubot
bot
added
the
size:M
This PR changes 30-99 lines, ignoring generated files.
label
Sep 5, 2024
Signed-off-by: Flc゛ <[email protected]>
Signed-off-by: Flc゛ <[email protected]>
或者整个v2版本: 草稿版: 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)
}
}
} |
There is no need to add V2 version |
Retention is only to avoid affecting the query configuration that users have already used |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description (what this PR does / why we need it):
Upgrade otel semconv
https://github.com/open-telemetry/opentelemetry-specification/blob/v1.24.0/specification/trace/semantic_conventions/http.md