-
Notifications
You must be signed in to change notification settings - Fork 12
/
tx.go
81 lines (69 loc) · 2.09 KB
/
tx.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2021 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package otlpsql
import (
"context"
"database/sql/driver"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// Compile time validation that our types implement the expected interfaces
var (
_ driver.Tx = otlpTx{}
)
// otlpTx implements driver.Tx
type otlpTx struct {
parent driver.Tx
ctx context.Context
options wrapper
}
func (t otlpTx) Commit() (err error) {
ctx := t.ctx
attrs := append([]attribute.KeyValue(nil), t.options.DefaultAttributes...)
onDeferWithErr := recordCallStats("go.sql.tx.commit", t.options.InstanceName)
defer func() {
// Invoking this function in a defer so that we can capture
// the value of err as set on function exit.
onDeferWithErr(ctx, err, attrs...)
}()
parentSpan := trace.SpanFromContext(ctx)
if !t.options.AllowRoot && !parentSpan.SpanContext().IsValid() {
// we already tested driver
return t.parent.Commit()
}
ctx, span := otel.Tracer("").Start(ctx, "sql:commit", trace.WithSpanKind(trace.SpanKindClient))
defer func() {
setSpanStatus(span, t.options, err)
span.SetAttributes(attrs...)
span.End()
}()
span.SetAttributes(attrs...)
err = t.parent.Commit()
return
}
func (t otlpTx) Rollback() (err error) {
ctx := t.ctx
var attrs []attribute.KeyValue
onDeferWithErr := recordCallStats("go.sql.tx.rollback", t.options.InstanceName)
defer func() {
// Invoking this function in a defer so that we can capture
// the value of err as set on function exit.
onDeferWithErr(ctx, err, attrs...)
}()
parentSpan := trace.SpanFromContext(ctx)
if !t.options.AllowRoot && !parentSpan.SpanContext().IsValid() {
// we already tested driver
return t.parent.Commit()
}
ctx, span := otel.Tracer("").Start(ctx, "sql:rollback", trace.WithSpanKind(trace.SpanKindClient))
defer func() {
setSpanStatus(span, t.options, err)
span.SetAttributes(attrs...)
span.End()
}()
span.SetAttributes(attrs...)
err = t.parent.Rollback()
return
}