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.
38 lines
834 B
Go
38 lines
834 B
Go
3 years ago
|
package sqlx
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/trace"
|
||
|
"go.opentelemetry.io/otel"
|
||
|
"go.opentelemetry.io/otel/attribute"
|
||
|
"go.opentelemetry.io/otel/codes"
|
||
|
oteltrace "go.opentelemetry.io/otel/trace"
|
||
|
)
|
||
|
|
||
|
var sqlAttributeKey = attribute.Key("sql.method")
|
||
|
|
||
|
func startSpan(ctx context.Context, method string) (context.Context, oteltrace.Span) {
|
||
|
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
|
||
|
start, span := tracer.Start(ctx,
|
||
|
spanName,
|
||
|
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
|
||
|
)
|
||
|
span.SetAttributes(sqlAttributeKey.String(method))
|
||
|
|
||
|
return start, span
|
||
|
}
|
||
|
|
||
|
func endSpan(span oteltrace.Span, err error) {
|
||
|
defer span.End()
|
||
|
|
||
|
if err == nil || err == sql.ErrNoRows {
|
||
|
span.SetStatus(codes.Ok, "")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
span.SetStatus(codes.Error, err.Error())
|
||
|
span.RecordError(err)
|
||
|
}
|