-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/jackc/pgx: implement Connect/Batch/CopyFrom/Prepare tracers
- Loading branch information
Showing
5 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// TraceBatchStart marks the start of a batch, implementing pgx.BatchTracer | ||
func (t *tracer) TraceBatchStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceBatchStartData) context.Context { | ||
opts := []ddtrace.StartSpanOption{ | ||
ddtracer.ServiceName(t.serviceName), | ||
ddtracer.SpanType(ext.SpanTypeSQL), | ||
ddtracer.StartTime(time.Now()), | ||
ddtracer.Tag("sql.query_type", "Batch"), | ||
ddtracer.Tag(ext.ResourceName, "pgx.batch"), | ||
} | ||
for key, tag := range t.tags { | ||
opts = append(opts, ddtracer.Tag(key, tag)) | ||
} | ||
if !math.IsNaN(t.analyticsRate) { | ||
opts = append(opts, ddtracer.Tag(ext.EventSampleRate, t.analyticsRate)) | ||
} | ||
_, ctx = ddtracer.StartSpanFromContext(ctx, "pgx.batch", opts...) | ||
|
||
return ctx | ||
} | ||
|
||
// TraceBatchQuery traces the query of a batch, implementing pgx.BatchTracer | ||
func (t *tracer) TraceBatchQuery(ctx context.Context, c *pgx.Conn, data pgx.TraceBatchQueryData) { | ||
opts := []ddtrace.StartSpanOption{ | ||
ddtracer.ServiceName(t.serviceName), | ||
ddtracer.SpanType(ext.SpanTypeSQL), | ||
ddtracer.StartTime(time.Now()), | ||
ddtracer.Tag(ext.ResourceName, data.SQL), | ||
} | ||
if t.traceArgs { | ||
opts = append(opts, ddtracer.Tag("sql.args", data.Args)) | ||
} | ||
for key, tag := range t.tags { | ||
opts = append(opts, ddtracer.Tag(key, tag)) | ||
} | ||
if !math.IsNaN(t.analyticsRate) { | ||
opts = append(opts, ddtracer.Tag(ext.EventSampleRate, t.analyticsRate)) | ||
} | ||
ddtracer.StartSpanFromContext(ctx, "pgx.batch_query", opts...) | ||
} | ||
|
||
// TraceBatchEnd marks the end of a batch, implementing pgx.BatchTracer | ||
func (t *tracer) TraceBatchEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceBatchEndData) { | ||
span, exists := ddtracer.SpanFromContext(ctx) | ||
if !exists { | ||
return | ||
} | ||
|
||
if data.Err != nil { | ||
span.SetTag(ext.Error, data.Err) | ||
} | ||
span.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// TraceConnectStart marks the start of a pgx connect operation, implementing pgx.ConnectTracer | ||
func (t *tracer) TraceConnectStart(ctx context.Context, _ pgx.TraceConnectStartData) context.Context { | ||
opts := []ddtrace.StartSpanOption{ | ||
ddtracer.ServiceName(t.serviceName), | ||
ddtracer.SpanType(ext.SpanTypeSQL), | ||
ddtracer.StartTime(time.Now()), | ||
ddtracer.Tag("sql.query_type", "Connect"), | ||
} | ||
for key, tag := range t.tags { | ||
opts = append(opts, ddtracer.Tag(key, tag)) | ||
} | ||
if !math.IsNaN(t.analyticsRate) { | ||
opts = append(opts, ddtracer.Tag(ext.EventSampleRate, t.analyticsRate)) | ||
} | ||
_, ctx = ddtracer.StartSpanFromContext(ctx, "pgx.connect", opts...) | ||
|
||
return ctx | ||
} | ||
|
||
// TraceConnectEnd marks the end of a pgx connect operation, implementing pgx.ConnectTracer | ||
func (t *tracer) TraceConnectEnd(ctx context.Context, data pgx.TraceConnectEndData) { | ||
span, exists := ddtracer.SpanFromContext(ctx) | ||
if !exists { | ||
return | ||
} | ||
|
||
if data.Err != nil { | ||
span.SetTag(ext.Error, data.Err) | ||
} | ||
span.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// TraceCopyFromStart marks the start of a CopyFrom query, implementing pgx.CopyFromTracer | ||
func (t *tracer) TraceCopyFromStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceCopyFromStartData) context.Context { | ||
opts := []ddtrace.StartSpanOption{ | ||
ddtracer.ServiceName(t.serviceName), | ||
ddtracer.SpanType(ext.SpanTypeSQL), | ||
ddtracer.StartTime(time.Now()), | ||
ddtracer.Tag("sql.query_type", "Query"), | ||
ddtracer.Tag(ext.ResourceName, "pgx.copyfrom"), | ||
ddtracer.Tag("pgx.table_name", data.TableName), | ||
ddtracer.Tag("pgx.columns", data.ColumnNames), | ||
} | ||
for key, tag := range t.tags { | ||
opts = append(opts, ddtracer.Tag(key, tag)) | ||
} | ||
if !math.IsNaN(t.analyticsRate) { | ||
opts = append(opts, ddtracer.Tag(ext.EventSampleRate, t.analyticsRate)) | ||
} | ||
|
||
_, ctx = ddtracer.StartSpanFromContext(ctx, "pgx.copyfrom", opts...) | ||
|
||
return ctx | ||
} | ||
|
||
// TraceCopyFromEnd marks the end of a CopyFrom query, implementing pgx.CopyFromTracer | ||
func (t *tracer) TraceCopyFromEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceCopyFromEndData) { | ||
span, exists := ddtracer.SpanFromContext(ctx) | ||
if !exists { | ||
return | ||
} | ||
|
||
if t.traceStatus { | ||
span.SetTag("pgx.status", data.CommandTag.String()) | ||
} | ||
|
||
if data.Err != nil { | ||
span.SetTag(ext.Error, data.Err) | ||
} | ||
span.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// TracePrepareStart marks the start of a pgx prepare operation, implementing pgx.PrepareTracer | ||
func (t *tracer) TracePrepareStart(ctx context.Context, _ *pgx.Conn, data pgx.TracePrepareStartData) context.Context { | ||
opts := []ddtrace.StartSpanOption{ | ||
ddtracer.ServiceName(t.serviceName), | ||
ddtracer.SpanType(ext.SpanTypeSQL), | ||
ddtracer.StartTime(time.Now()), | ||
ddtracer.Tag("sql.query_type", "Prepare"), | ||
ddtracer.Tag("pgx.prepared_statement_name", data.Name), | ||
ddtracer.Tag(ext.ResourceName, data.SQL), | ||
} | ||
for key, tag := range t.tags { | ||
opts = append(opts, ddtracer.Tag(key, tag)) | ||
} | ||
if !math.IsNaN(t.analyticsRate) { | ||
opts = append(opts, ddtracer.Tag(ext.EventSampleRate, t.analyticsRate)) | ||
} | ||
_, ctx = ddtracer.StartSpanFromContext(ctx, "pgx.prepare", opts...) | ||
|
||
return ctx | ||
} | ||
|
||
// TracePrepareEnd marks the end of a pgx prepare operation, implementing pgx.PrepareTracer | ||
func (t *tracer) TracePrepareEnd(ctx context.Context, _ *pgx.Conn, data pgx.TracePrepareEndData) { | ||
span, exists := ddtracer.SpanFromContext(ctx) | ||
if !exists { | ||
return | ||
} | ||
|
||
span.SetTag("pgx.already_prepared", data.AlreadyPrepared) | ||
|
||
if data.Err != nil { | ||
span.SetTag(ext.Error, data.Err) | ||
} | ||
span.Finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters