/
types.go
63 lines (55 loc) · 1.47 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ydb
import (
"context"
"errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/ydb-platform/ydb-go-sdk/v3"
)
const (
errorAttribute = "error"
tracerID = "ydb-go-sdk"
version = "v" + ydb.Version
)
func logError(s trace.Span, err error, fields ...attribute.KeyValue) {
s.RecordError(err, trace.WithAttributes(append(fields, attribute.Bool(errorAttribute, true))...))
s.SetStatus(codes.Error, err.Error())
var ydbErr ydb.Error
if errors.As(err, &ydbErr) {
s.SetAttributes(
attribute.Int(errorAttribute+".ydb.code", int(ydbErr.Code())),
attribute.String(errorAttribute+".ydb.name", ydbErr.Name()),
)
}
}
func finish(s trace.Span, err error, fields ...attribute.KeyValue) {
if err != nil {
logError(s, err, fields...)
} else if len(fields) > 0 {
s.SetAttributes(fields...)
}
s.End()
}
func childSpanWithReplaceCtx(
tracer trace.Tracer,
ctx *context.Context,
operationName string,
fields ...attribute.KeyValue,
) (s trace.Span) {
fields = append(fields, attribute.String("ydb-go-sdk", version))
*ctx, s = childSpan(tracer, *ctx, operationName, fields...)
return s
}
func childSpan(
tracer trace.Tracer,
ctx context.Context, //nolint:revive
operationName string,
fields ...attribute.KeyValue,
) (context.Context, trace.Span) {
fields = append(fields, attribute.String("ydb-go-sdk", version))
return tracer.Start(ctx,
operationName,
trace.WithAttributes(fields...),
)
}