You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
901 B
Go
39 lines
901 B
Go
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.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)
|
|
}
|