tracer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package pgx
  2. import (
  3. "context"
  4. "github.com/jackc/pgx/v5/pgconn"
  5. )
  6. // QueryTracer traces Query, QueryRow, and Exec.
  7. type QueryTracer interface {
  8. // TraceQueryStart is called at the beginning of Query, QueryRow, and Exec calls. The returned context is used for the
  9. // rest of the call and will be passed to TraceQueryEnd.
  10. TraceQueryStart(ctx context.Context, conn *Conn, data TraceQueryStartData) context.Context
  11. TraceQueryEnd(ctx context.Context, conn *Conn, data TraceQueryEndData)
  12. }
  13. type TraceQueryStartData struct {
  14. SQL string
  15. Args []any
  16. }
  17. type TraceQueryEndData struct {
  18. CommandTag pgconn.CommandTag
  19. Err error
  20. }
  21. // BatchTracer traces SendBatch.
  22. type BatchTracer interface {
  23. // TraceBatchStart is called at the beginning of SendBatch calls. The returned context is used for the
  24. // rest of the call and will be passed to TraceBatchQuery and TraceBatchEnd.
  25. TraceBatchStart(ctx context.Context, conn *Conn, data TraceBatchStartData) context.Context
  26. TraceBatchQuery(ctx context.Context, conn *Conn, data TraceBatchQueryData)
  27. TraceBatchEnd(ctx context.Context, conn *Conn, data TraceBatchEndData)
  28. }
  29. type TraceBatchStartData struct {
  30. Batch *Batch
  31. }
  32. type TraceBatchQueryData struct {
  33. SQL string
  34. Args []any
  35. CommandTag pgconn.CommandTag
  36. Err error
  37. }
  38. type TraceBatchEndData struct {
  39. Err error
  40. }
  41. // CopyFromTracer traces CopyFrom.
  42. type CopyFromTracer interface {
  43. // TraceCopyFromStart is called at the beginning of CopyFrom calls. The returned context is used for the
  44. // rest of the call and will be passed to TraceCopyFromEnd.
  45. TraceCopyFromStart(ctx context.Context, conn *Conn, data TraceCopyFromStartData) context.Context
  46. TraceCopyFromEnd(ctx context.Context, conn *Conn, data TraceCopyFromEndData)
  47. }
  48. type TraceCopyFromStartData struct {
  49. TableName Identifier
  50. ColumnNames []string
  51. }
  52. type TraceCopyFromEndData struct {
  53. CommandTag pgconn.CommandTag
  54. Err error
  55. }
  56. // PrepareTracer traces Prepare.
  57. type PrepareTracer interface {
  58. // TracePrepareStart is called at the beginning of Prepare calls. The returned context is used for the
  59. // rest of the call and will be passed to TracePrepareEnd.
  60. TracePrepareStart(ctx context.Context, conn *Conn, data TracePrepareStartData) context.Context
  61. TracePrepareEnd(ctx context.Context, conn *Conn, data TracePrepareEndData)
  62. }
  63. type TracePrepareStartData struct {
  64. Name string
  65. SQL string
  66. }
  67. type TracePrepareEndData struct {
  68. AlreadyPrepared bool
  69. Err error
  70. }
  71. // ConnectTracer traces Connect and ConnectConfig.
  72. type ConnectTracer interface {
  73. // TraceConnectStart is called at the beginning of Connect and ConnectConfig calls. The returned context is used for
  74. // the rest of the call and will be passed to TraceConnectEnd.
  75. TraceConnectStart(ctx context.Context, data TraceConnectStartData) context.Context
  76. TraceConnectEnd(ctx context.Context, data TraceConnectEndData)
  77. }
  78. type TraceConnectStartData struct {
  79. ConnConfig *ConnConfig
  80. }
  81. type TraceConnectEndData struct {
  82. Conn *Conn
  83. Err error
  84. }