Skip to content

Commit 16ba7c2

Browse files
authored
chore: adjust the tracing (#8)
1 parent 31fbb39 commit 16ba7c2

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

Diff for: multiple.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717

1818
const spanName = "multipleSql"
1919

20-
var DBTypeAttributeKey = attribute.Key("multipleSql.leader")
21-
var followerDBSqlAttributeKey = attribute.Key("multipleSql.follower_db")
2220
var (
23-
leaderTypeAttributeKey = DBTypeAttributeKey.String("leader")
24-
followerTypeAttributeKey = DBTypeAttributeKey.String("follower")
21+
DBTypeAttributeKey = attribute.Key("multiple_sql.type")
22+
followerDBSqlAttributeKey = attribute.Key("multiple_sql.follower_db")
23+
leaderTypeAttributeKey = DBTypeAttributeKey.String("leader")
24+
followerTypeAttributeKey = DBTypeAttributeKey.String("follower")
25+
sqlDriverAttributeKey = attribute.Key("sql.driver")
2526
)
2627

2728
var _ sqlx.SqlConn = (*multipleSqlConn)(nil)
@@ -44,6 +45,7 @@ type (
4445
followers []sqlx.SqlConn
4546
conf DBConf
4647
accept func(error) bool
48+
driveName string
4749
}
4850
)
4951

@@ -60,6 +62,7 @@ func NewMultipleSqlConn(driverName string, conf DBConf, opts ...SqlOption) sqlx.
6062
enableFollower: len(followers) != 0,
6163
followers: followers,
6264
conf: conf,
65+
driveName: driverName,
6366
}
6467

6568
for _, opt := range opts {
@@ -85,7 +88,7 @@ func (m *multipleSqlConn) Exec(query string, args ...any) (sql.Result, error) {
8588
}
8689

8790
func (m *multipleSqlConn) ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error) {
88-
ctx, span := startSpanWithLeader(ctx)
91+
ctx, span := m.startSpanWithLeader(ctx)
8992
defer span.End()
9093
return m.leader.ExecCtx(ctx, query, args...)
9194
}
@@ -95,7 +98,7 @@ func (m *multipleSqlConn) Prepare(query string) (sqlx.StmtSession, error) {
9598
}
9699

97100
func (m *multipleSqlConn) PrepareCtx(ctx context.Context, query string) (sqlx.StmtSession, error) {
98-
ctx, span := startSpanWithLeader(ctx)
101+
ctx, span := m.startSpanWithLeader(ctx)
99102
defer span.End()
100103
return m.leader.PrepareCtx(ctx, query)
101104
}
@@ -108,9 +111,9 @@ func (m *multipleSqlConn) QueryRowCtx(ctx context.Context, v any, query string,
108111
db := m.getQueryDB(query)
109112
var span oteltrace.Span
110113
if db.leader {
111-
ctx, span = startSpanWithLeader(ctx)
114+
ctx, span = m.startSpanWithLeader(ctx)
112115
} else {
113-
ctx, span = startSpanWithFollower(ctx, db.followerDB)
116+
ctx, span = m.startSpanWithFollower(ctx, db.followerDB)
114117
}
115118
defer span.End()
116119

@@ -127,9 +130,9 @@ func (m *multipleSqlConn) QueryRowPartialCtx(ctx context.Context, v any, query s
127130
db := m.getQueryDB(query)
128131
var span oteltrace.Span
129132
if db.leader {
130-
ctx, span = startSpanWithLeader(ctx)
133+
ctx, span = m.startSpanWithLeader(ctx)
131134
} else {
132-
ctx, span = startSpanWithFollower(ctx, db.followerDB)
135+
ctx, span = m.startSpanWithFollower(ctx, db.followerDB)
133136
}
134137
defer span.End()
135138

@@ -146,9 +149,9 @@ func (m *multipleSqlConn) QueryRowsCtx(ctx context.Context, v any, query string,
146149
db := m.getQueryDB(query)
147150
var span oteltrace.Span
148151
if db.leader {
149-
ctx, span = startSpanWithLeader(ctx)
152+
ctx, span = m.startSpanWithLeader(ctx)
150153
} else {
151-
ctx, span = startSpanWithFollower(ctx, db.followerDB)
154+
ctx, span = m.startSpanWithFollower(ctx, db.followerDB)
152155
}
153156
defer span.End()
154157

@@ -165,9 +168,9 @@ func (m *multipleSqlConn) QueryRowsPartialCtx(ctx context.Context, v any, query
165168
db := m.getQueryDB(query)
166169
var span oteltrace.Span
167170
if db.leader {
168-
ctx, span = startSpanWithLeader(ctx)
171+
ctx, span = m.startSpanWithLeader(ctx)
169172
} else {
170-
ctx, span = startSpanWithFollower(ctx, db.followerDB)
173+
ctx, span = m.startSpanWithFollower(ctx, db.followerDB)
171174
}
172175
defer span.End()
173176

@@ -187,7 +190,7 @@ func (m *multipleSqlConn) Transact(fn func(sqlx.Session) error) error {
187190
}
188191

189192
func (m *multipleSqlConn) TransactCtx(ctx context.Context, fn func(context.Context, sqlx.Session) error) error {
190-
ctx, span := startSpanWithLeader(ctx)
193+
ctx, span := m.startSpanWithLeader(ctx)
191194
defer span.End()
192195
return m.leader.TransactCtx(ctx, fn)
193196
}
@@ -254,20 +257,21 @@ func (m *multipleSqlConn) startFollowerHeartbeat(ctx context.Context) {
254257
}
255258
}
256259

257-
func startSpan(ctx context.Context) (context.Context, oteltrace.Span) {
260+
func (m *multipleSqlConn) startSpan(ctx context.Context) (context.Context, oteltrace.Span) {
258261
tracer := trace.TracerFromContext(ctx)
259262
ctx, span := tracer.Start(ctx, spanName, oteltrace.WithSpanKind(oteltrace.SpanKindClient))
263+
span.SetAttributes(sqlDriverAttributeKey.String(m.driveName))
260264
return ctx, span
261265
}
262266

263-
func startSpanWithLeader(ctx context.Context) (context.Context, oteltrace.Span) {
264-
ctx, span := startSpan(ctx)
267+
func (m *multipleSqlConn) startSpanWithLeader(ctx context.Context) (context.Context, oteltrace.Span) {
268+
ctx, span := m.startSpan(ctx)
265269
span.SetAttributes(leaderTypeAttributeKey)
266270
return ctx, span
267271
}
268272

269-
func startSpanWithFollower(ctx context.Context, db int) (context.Context, oteltrace.Span) {
270-
ctx, span := startSpan(ctx)
273+
func (m *multipleSqlConn) startSpanWithFollower(ctx context.Context, db int) (context.Context, oteltrace.Span) {
274+
ctx, span := m.startSpan(ctx)
271275
span.SetAttributes(followerTypeAttributeKey)
272276
span.SetAttributes(followerDBSqlAttributeKey.Int(db))
273277
return ctx, span

0 commit comments

Comments
 (0)