mirror of https://github.com/jackc/pgx.git
feat: add 'MultiTracer' to go doc & cover it
parent
8e46d2117c
commit
3f270eec7d
2
doc.go
2
doc.go
|
@ -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.
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue