package pgxtraces
import (
)
type (
querySpanKey struct{}
batchSpanKey struct{}
copyFromSpanKey struct{}
prepareSpanKey struct{}
)
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceQueryStartData,
) context.Context {
:= sqlOperation(.SQL)
if !.allow(, ) {
return
}
var *pgx.ConnConfig
if != nil {
= .Config()
}
:= .connAttrs(, , )
if := sqlCollection(, .SQL); != "" {
= append(, traces.String(attrDBCollectionName, ))
}
if , := .queryText(.SQL); {
= append(, traces.String(attrDBQueryText, ))
}
:= .tracer.Start(
,
traces.FromContext(),
traces.SpanKindClient,
.spanName(, dbName()),
...,
)
return context.WithValue(, querySpanKey{}, )
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceQueryEndData,
) {
, := .Value(querySpanKey{}).(*traces.Span)
if == nil {
return
}
if .Err != nil {
setError(, .Err)
} else {
.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
}
.End()
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceBatchStartData,
) context.Context {
if !.allow(, opBatch) {
return
}
var *pgx.ConnConfig
if != nil {
= .Config()
}
:= .connAttrs(, , opBatch)
if .Batch != nil {
= append(, traces.Int(attrDBBatchSize, .Batch.Len()))
}
:= .tracer.Start(
,
traces.FromContext(),
traces.SpanKindClient,
.spanName(opBatch, dbName()),
...,
)
return context.WithValue(, batchSpanKey{}, )
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceBatchQueryData,
) {
:= sqlOperation(.SQL)
if !.allow(, ) {
return
}
:= traces.FromContext()
if , := .Value(batchSpanKey{}).(*traces.Span); && != nil {
= .Ref()
}
var *pgx.ConnConfig
if != nil {
= .Config()
}
:= .connAttrs(, , )
if := sqlCollection(, .SQL); != "" {
= append(, traces.String(attrDBCollectionName, ))
}
if , := .queryText(.SQL); {
= append(, traces.String(attrDBQueryText, ))
}
:= .tracer.Start(
,
,
traces.SpanKindClient,
.spanName(, dbName()),
...,
)
if !.ReadStartTime.IsZero() {
.SetStart(.ReadStartTime)
}
if .Err != nil {
setError(, .Err)
} else {
.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
}
.End()
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceBatchEndData,
) {
, := .Value(batchSpanKey{}).(*traces.Span)
if == nil {
return
}
setError(, .Err)
.End()
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceCopyFromStartData,
) context.Context {
if !.allow(, opCopy) {
return
}
var *pgx.ConnConfig
if != nil {
= .Config()
}
:= .connAttrs(, , opCopy)
if len(.TableName) > 0 {
= append(, traces.String(attrDBCollectionName, .TableName.Sanitize()))
}
:= .tracer.Start(
,
traces.FromContext(),
traces.SpanKindClient,
.spanName(opCopy, dbName()),
...,
)
return context.WithValue(, copyFromSpanKey{}, )
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TraceCopyFromEndData,
) {
, := .Value(copyFromSpanKey{}).(*traces.Span)
if == nil {
return
}
if .Err != nil {
setError(, .Err)
} else {
.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
}
.End()
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TracePrepareStartData,
) context.Context {
if !.allow(, opPrepare) {
return
}
var *pgx.ConnConfig
if != nil {
= .Config()
}
:= .connAttrs(, , opPrepare)
if , := .queryText(.SQL); {
= append(, traces.String(attrDBQueryText, ))
}
:= .tracer.Start(
,
traces.FromContext(),
traces.SpanKindClient,
.spanName(opPrepare, dbName()),
...,
)
return context.WithValue(, prepareSpanKey{}, )
}
func ( *Tracer) (
context.Context,
*pgx.Conn,
pgx.TracePrepareEndData,
) {
, := .Value(prepareSpanKey{}).(*traces.Span)
if == nil {
return
}
setError(, .Err)
.End()
}