Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Oct 17, 2024
1 parent 4ed21d0 commit 84bc831
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 2 additions & 0 deletions otel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type (
Span interface {
TraceID() string

Relation(span Span)

Msg(msg string, attributes ...KeyValue)

End(attributes ...KeyValue)
Expand Down
2 changes: 2 additions & 0 deletions otel/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
noopSpan struct{}
)

func (noopSpan) Relation(Span) {}

func (noopSpan) TraceID() string {
return ""
}
Expand Down
31 changes: 16 additions & 15 deletions otel/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package otel
import (
"context"

otelTrace "go.opentelemetry.io/otel/trace"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/kv"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
Expand Down Expand Up @@ -206,16 +204,17 @@ func databaseSQL(cfg Config) (t trace.DatabaseSQL) {
}
t.OnTxExec = func(info trace.DatabaseSQLTxExecStartInfo) func(trace.DatabaseSQLTxExecDoneInfo) {
if cfg.Details()&trace.DatabaseSQLTxEvents != 0 {
if !isStmtCall(*info.Context) {
*info.Context = otelTrace.ContextWithSpan(*info.Context, otelTrace.SpanFromContext(info.TxContext))
}
start := childSpanWithReplaceCtx(
info.Context,
info.Call.FunctionID(),
kv.String("query", info.Query),
kv.String("transaction_id", safeID(info.Tx)),
)

if !isStmtCall(*info.Context) {
start.Relation(cfg.SpanFromContext(info.TxContext))
}

return func(info trace.DatabaseSQLTxExecDoneInfo) {
finish(
start,
Expand All @@ -228,16 +227,17 @@ func databaseSQL(cfg Config) (t trace.DatabaseSQL) {
}
t.OnTxQuery = func(info trace.DatabaseSQLTxQueryStartInfo) func(trace.DatabaseSQLTxQueryDoneInfo) {
if cfg.Details()&trace.DatabaseSQLTxEvents != 0 {
if !isStmtCall(*info.Context) {
*info.Context = otelTrace.ContextWithSpan(*info.Context, otelTrace.SpanFromContext(info.TxContext))
}
start := childSpanWithReplaceCtx(
info.Context,
info.Call.FunctionID(),
kv.String("query", info.Query),
kv.String("transaction_id", safeID(info.Tx)),
)

if !isStmtCall(*info.Context) {
start.Relation(cfg.SpanFromContext(info.TxContext))
}

return func(info trace.DatabaseSQLTxQueryDoneInfo) {
finish(
start,
Expand All @@ -250,7 +250,6 @@ func databaseSQL(cfg Config) (t trace.DatabaseSQL) {
}
t.OnTxPrepare = func(info trace.DatabaseSQLTxPrepareStartInfo) func(trace.DatabaseSQLTxPrepareDoneInfo) {
if cfg.Details()&trace.DatabaseSQLTxEvents != 0 {
*info.Context = otelTrace.ContextWithSpan(*info.Context, otelTrace.SpanFromContext(info.TxContext))
start := childSpanWithReplaceCtx(
info.Context,
info.Call.FunctionID(),
Expand All @@ -270,15 +269,16 @@ func databaseSQL(cfg Config) (t trace.DatabaseSQL) {
}
t.OnStmtExec = func(info trace.DatabaseSQLStmtExecStartInfo) func(trace.DatabaseSQLStmtExecDoneInfo) {
if cfg.Details()&trace.DatabaseSQLStmtEvents != 0 {
*info.Context = markStmtCall(
otelTrace.ContextWithSpan(*info.Context, otelTrace.SpanFromContext(info.StmtContext)),
)
start := childSpanWithReplaceCtx(
info.Context,
info.Call.FunctionID(),
kv.String("query", info.Query),
)

start.Relation(cfg.SpanFromContext(info.StmtContext))

*info.Context = markStmtCall(*info.Context)

return func(info trace.DatabaseSQLStmtExecDoneInfo) {
finish(
start,
Expand All @@ -291,15 +291,16 @@ func databaseSQL(cfg Config) (t trace.DatabaseSQL) {
}
t.OnStmtQuery = func(info trace.DatabaseSQLStmtQueryStartInfo) func(trace.DatabaseSQLStmtQueryDoneInfo) {
if cfg.Details()&trace.DatabaseSQLStmtEvents != 0 {
*info.Context = markStmtCall(
otelTrace.ContextWithSpan(*info.Context, otelTrace.SpanFromContext(info.StmtContext)),
)
start := childSpanWithReplaceCtx(
info.Context,
info.Call.FunctionID(),
kv.String("query", info.Query),
)

start.Relation(cfg.SpanFromContext(info.StmtContext))

*info.Context = markStmtCall(*info.Context)

return func(info trace.DatabaseSQLStmtQueryDoneInfo) {
finish(
start,
Expand Down

0 comments on commit 84bc831

Please sign in to comment.