feat: add 'MultiTracer' to go doc & cover it

pull/2084/head
Stepan Rabotkin 2024-07-13 02:02:22 +03:00
parent 8e46d2117c
commit 3f270eec7d
No known key found for this signature in database
GPG Key ID: 9D45CFF1B900FBFF
3 changed files with 52 additions and 1 deletions

2
doc.go
View File

@ -175,7 +175,7 @@ notification is received or the context is canceled.
Tracing and Logging
pgx supports tracing by setting ConnConfig.Tracer.
pgx supports tracing by setting ConnConfig.Tracer. To combine several tracers you can use the MultiTracer.
In addition, the tracelog package provides the TraceLog type which lets a traditional logger act as a Tracer.

View File

@ -105,6 +105,8 @@ type TraceConnectEndData struct {
Err error
}
// MultiTracer can combine several tracers into one.
// You can use NewMultiTracer to automatically split tracers by interface.
type MultiTracer struct {
QueryTracers []QueryTracer
BatchTracers []BatchTracer
@ -113,6 +115,7 @@ type MultiTracer struct {
ConnectTracers []ConnectTracer
}
// NewMultiTracer returns new MultiTracer from tracers with automatically split tracers by interface.
func NewMultiTracer(tracers ...QueryTracer) *MultiTracer {
var t MultiTracer

View File

@ -566,3 +566,51 @@ func TestTraceConnect(t *testing.T) {
require.True(t, traceConnectStartCalled)
require.True(t, traceConnectEndCalled)
}
type testCopyTracer struct{}
func (tt *testCopyTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
return ctx
}
func (tt *testCopyTracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
}
func (tt *testCopyTracer) TraceCopyFromStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromStartData) context.Context {
return ctx
}
func (tt *testCopyTracer) TraceCopyFromEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceCopyFromEndData) {
}
func TestNewMultiTracer(t *testing.T) {
t.Parallel()
fullTracer := &testTracer{}
copyTracer := &testCopyTracer{}
mt := pgx.NewMultiTracer(fullTracer, copyTracer)
require.Equal(
t,
&pgx.MultiTracer{
QueryTracers: []pgx.QueryTracer{
fullTracer,
copyTracer,
},
BatchTracers: []pgx.BatchTracer{
fullTracer,
},
CopyFromTracers: []pgx.CopyFromTracer{
fullTracer,
copyTracer,
},
PrepareTracers: []pgx.PrepareTracer{
fullTracer,
},
ConnectTracers: []pgx.ConnectTracer{
fullTracer,
},
},
mt,
)
}