2022-05-12 23:32:34 -05:00
|
|
|
package mon
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2024-04-27 20:43:45 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/errorx"
|
2022-05-12 23:32:34 -05:00
|
|
|
"github.com/zeromicro/go-zero/core/trace"
|
2025-08-09 21:21:53 +08:00
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
2022-05-12 23:32:34 -05:00
|
|
|
"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) {
|
2023-02-22 17:21:58 +08:00
|
|
|
tracer := trace.TracerFromContext(ctx)
|
2023-02-26 09:35:59 +08:00
|
|
|
ctx, span := tracer.Start(ctx, spanName, oteltrace.WithSpanKind(oteltrace.SpanKindClient))
|
2022-05-12 23:32:34 -05:00
|
|
|
span.SetAttributes(mongoCmdAttributeKey.String(cmd))
|
2023-01-12 15:56:51 +08:00
|
|
|
|
2022-05-12 23:32:34 -05:00
|
|
|
return ctx, span
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func endSpan(span oteltrace.Span, err error) {
|
|
|
|
|
defer span.End()
|
|
|
|
|
|
2024-04-27 20:43:45 +08:00
|
|
|
if err == nil || errorx.In(err, mongo.ErrNoDocuments, mongo.ErrNilValue, mongo.ErrNilDocument) {
|
2022-05-12 23:32:34 -05:00
|
|
|
span.SetStatus(codes.Ok, "")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
|
span.RecordError(err)
|
|
|
|
|
}
|