refactor: refactor trace in redis & sql & mongo (#1865)
* refactor: refactor tracing in redis & sql & mongo Signed-off-by: chenquan <chenquan.dev@gmail.com> * fix: fix some tests Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: add missing content Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: adjust `log` and `return` Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: reformat code Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: reformat code Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: reformat code Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: simpler span name Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: fix a bug Signed-off-by: chenquan <chenquan.dev@gmail.com> * refactor: fix a bug Signed-off-by: chenquan <chenquan.dev@gmail.com>
This commit is contained in:
37
core/stores/mon/trace.go
Normal file
37
core/stores/mon/trace.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mon
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/trace"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
oteltrace "go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
var mongoCmdAttributeKey = attribute.Key("mongo.cmd")
|
||||
|
||||
func startSpan(ctx context.Context, cmd string) (context.Context, oteltrace.Span) {
|
||||
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
||||
ctx, span := tracer.Start(ctx,
|
||||
spanName,
|
||||
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
|
||||
)
|
||||
span.SetAttributes(mongoCmdAttributeKey.String(cmd))
|
||||
return ctx, span
|
||||
}
|
||||
|
||||
func endSpan(span oteltrace.Span, err error) {
|
||||
defer span.End()
|
||||
|
||||
if err == nil || err == mongo.ErrNoDocuments ||
|
||||
err == mongo.ErrNilValue || err == mongo.ErrNilDocument {
|
||||
span.SetStatus(codes.Ok, "")
|
||||
return
|
||||
}
|
||||
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
}
|
||||
Reference in New Issue
Block a user