|
| 1 | +// Package nrpgx5 instruments https://github.com/jackc/pgx/v5. |
| 2 | +// |
| 3 | +// Use this package to instrument your PostgreSQL calls using the pgx |
| 4 | +// library. |
| 5 | +// |
| 6 | +// This are the steps to instrument your pgx calls without using `database/sql`: |
| 7 | +// if you want to use `database/sql`, you can use `nrpgx` package instead |
| 8 | +// |
| 9 | +// to instrument your pgx calls: |
| 10 | +// you can set the tracer in the pgx.Config like this |
| 11 | +// ```go |
| 12 | +// import ( |
| 13 | +// "github.com/jackc/pgx/v5" |
| 14 | +// "github.com/newrelic/go-agent/v3/integrations/nrpgx5" |
| 15 | +// "github.com/newrelic/go-agent/v3/newrelic" |
| 16 | +// ) |
| 17 | +// |
| 18 | +// func main() { |
| 19 | +// cfg, err := pgx.ParseConfig("postgres://postgres:postgres@localhost:5432") |
| 20 | +// if err != nil { |
| 21 | +// panic(err) |
| 22 | +// } |
| 23 | +// |
| 24 | +// cfg.Tracer = nrpgx5.NewTracer() |
| 25 | +// conn, err := pgx.ConnectConfig(context.Background(), cfg) |
| 26 | +// if err != nil { |
| 27 | +// panic(err) |
| 28 | +// } |
| 29 | +// ... |
| 30 | +// ``` |
| 31 | +// or you can set the tracer in the pgxpool.Config like this |
| 32 | +// ```go |
| 33 | +// import ( |
| 34 | +// "github.com/jackc/pgx/v5/pgxpool" |
| 35 | +// "github.com/newrelic/go-agent/v3/integrations/nrpgx5" |
| 36 | +// "github.com/newrelic/go-agent/v3/newrelic" |
| 37 | +// ) |
| 38 | +// |
| 39 | +// func main() { |
| 40 | +// cfg, err := pgxpool.ParseConfig("postgres://postgres:postgres@localhost:5432") |
| 41 | +// if err != nil { |
| 42 | +// panic(err) |
| 43 | +// } |
| 44 | +// |
| 45 | +// cfg.ConnConfig.Tracer = nrpgx5.NewTracer() |
| 46 | +// db, err := pgxpool.NewWithConfig(context.Background(), cfg) |
| 47 | +// if err != nil { |
| 48 | +// panic(err) |
| 49 | +// } |
| 50 | +// ... |
| 51 | +// ``` |
| 52 | + |
| 53 | +package nrpgx5 |
| 54 | + |
| 55 | +import ( |
| 56 | + "context" |
| 57 | + "strconv" |
| 58 | + |
| 59 | + "github.com/jackc/pgx/v5" |
| 60 | + "github.com/newrelic/go-agent/v3/internal" |
| 61 | + "github.com/newrelic/go-agent/v3/newrelic" |
| 62 | + "github.com/newrelic/go-agent/v3/newrelic/sqlparse" |
| 63 | +) |
| 64 | + |
| 65 | +func init() { |
| 66 | + internal.TrackUsage("integration", "driver", "nrpgx5") |
| 67 | +} |
| 68 | + |
| 69 | +type ( |
| 70 | + Tracer struct { |
| 71 | + BaseSegment newrelic.DatastoreSegment |
| 72 | + ParseQuery func(segment *newrelic.DatastoreSegment, query string) |
| 73 | + } |
| 74 | + |
| 75 | + nrPgxSegmentType string |
| 76 | +) |
| 77 | + |
| 78 | +const ( |
| 79 | + querySegmentKey nrPgxSegmentType = "nrPgx5Segment" |
| 80 | + prepareSegmentKey nrPgxSegmentType = "prepareNrPgx5Segment" |
| 81 | + batchSegmentKey nrPgxSegmentType = "batchNrPgx5Segment" |
| 82 | +) |
| 83 | + |
| 84 | +func NewTracer() *Tracer { |
| 85 | + return &Tracer{ |
| 86 | + ParseQuery: sqlparse.ParseQuery, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TraceConnectStart is called at the beginning of Connect and ConnectConfig calls. The returned context is used for |
| 91 | +// the rest of the call and will be passed to TraceConnectEnd. // implement pgx.ConnectTracer |
| 92 | +func (t *Tracer) TraceConnectStart(ctx context.Context, data pgx.TraceConnectStartData) context.Context { |
| 93 | + t.BaseSegment = newrelic.DatastoreSegment{ |
| 94 | + Product: newrelic.DatastorePostgres, |
| 95 | + Host: data.ConnConfig.Host, |
| 96 | + PortPathOrID: strconv.FormatUint(uint64(data.ConnConfig.Port), 10), |
| 97 | + DatabaseName: data.ConnConfig.Database, |
| 98 | + } |
| 99 | + |
| 100 | + return ctx |
| 101 | +} |
| 102 | + |
| 103 | +// TraceConnectEnd method // implement pgx.ConnectTracer |
| 104 | +func (Tracer) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEndData) {} |
| 105 | + |
| 106 | +// TraceQueryStart is called at the beginning of Query, QueryRow, and Exec calls. The returned context is used for the |
| 107 | +// rest of the call and will be passed to TraceQueryEnd. //implement pgx.QueryTracer |
| 108 | +func (t *Tracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context { |
| 109 | + segment := t.BaseSegment |
| 110 | + segment.StartTime = newrelic.FromContext(ctx).StartSegmentNow() |
| 111 | + segment.ParameterizedQuery = data.SQL |
| 112 | + segment.QueryParameters = t.getQueryParameters(data.Args) |
| 113 | + |
| 114 | + // fill Operation and Collection |
| 115 | + t.ParseQuery(&segment, data.SQL) |
| 116 | + |
| 117 | + return context.WithValue(ctx, querySegmentKey, &segment) |
| 118 | +} |
| 119 | + |
| 120 | +// TraceQueryEnd method implement pgx.QueryTracer. It will try to get segment from context and end it. |
| 121 | +func (t *Tracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) { |
| 122 | + segment, ok := ctx.Value(querySegmentKey).(*newrelic.DatastoreSegment) |
| 123 | + if !ok { |
| 124 | + return |
| 125 | + } |
| 126 | + segment.End() |
| 127 | +} |
| 128 | + |
| 129 | +func (t *Tracer) getQueryParameters(args []interface{}) map[string]interface{} { |
| 130 | + result := map[string]interface{}{} |
| 131 | + for i, arg := range args { |
| 132 | + result["$"+strconv.Itoa(i)] = arg |
| 133 | + } |
| 134 | + return result |
| 135 | +} |
| 136 | + |
| 137 | +// TraceBatchStart is called at the beginning of SendBatch calls. The returned context is used for the |
| 138 | +// rest of the call and will be passed to TraceBatchQuery and TraceBatchEnd. // implement pgx.BatchTracer |
| 139 | +func (t *Tracer) TraceBatchStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchStartData) context.Context { |
| 140 | + segment := t.BaseSegment |
| 141 | + segment.StartTime = newrelic.FromContext(ctx).StartSegmentNow() |
| 142 | + segment.Operation = "batch" |
| 143 | + segment.Collection = "" |
| 144 | + |
| 145 | + return context.WithValue(ctx, batchSegmentKey, &segment) |
| 146 | +} |
| 147 | + |
| 148 | +// TraceBatchQuery implement pgx.BatchTracer. In this method we will get query and store it in segment. |
| 149 | +func (t *Tracer) TraceBatchQuery(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchQueryData) { |
| 150 | + segment, ok := ctx.Value(batchSegmentKey).(*newrelic.DatastoreSegment) |
| 151 | + if !ok { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + segment.ParameterizedQuery += data.SQL + "\n" |
| 156 | +} |
| 157 | + |
| 158 | +// TraceBatchEnd implement pgx.BatchTracer. In this method we will get segment from context and fill it with |
| 159 | +func (t *Tracer) TraceBatchEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceBatchEndData) { |
| 160 | + segment, ok := ctx.Value(batchSegmentKey).(*newrelic.DatastoreSegment) |
| 161 | + if !ok { |
| 162 | + return |
| 163 | + } |
| 164 | + segment.End() |
| 165 | +} |
| 166 | + |
| 167 | +// TracePrepareStart is called at the beginning of Prepare calls. The returned context is used for the |
| 168 | +// rest of the call and will be passed to TracePrepareEnd. // implement pgx.PrepareTracer |
| 169 | +// The Query and QueryRow will call prepare. Fill this function will make the datastore segment called twice. |
| 170 | +// So this function woudln't do anything and just return the context. |
| 171 | +func (t *Tracer) TracePrepareStart(ctx context.Context, conn *pgx.Conn, data pgx.TracePrepareStartData) context.Context { |
| 172 | + return ctx |
| 173 | +} |
| 174 | + |
| 175 | +// TracePrepareEnd implement pgx.PrepareTracer. In this function nothing happens. |
| 176 | +func (t *Tracer) TracePrepareEnd(ctx context.Context, conn *pgx.Conn, data pgx.TracePrepareEndData) { |
| 177 | +} |
0 commit comments