Skip to content

Commit

Permalink
feat: capture Postgres error code as span attribute (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
marselester authored Jan 19, 2024
1 parent de93250 commit ab9f011
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand All @@ -26,6 +27,9 @@ const (
BatchSizeKey = attribute.Key("pgx.batch.size")
// PrepareStmtNameKey represents the prepared statement name.
PrepareStmtNameKey = attribute.Key("pgx.prepare_stmt.name")
// SQLStateKey represents PostgreSQL error code,
// see https://www.postgresql.org/docs/current/errcodes-appendix.html.
SQLStateKey = attribute.Key("pgx.sql_state")
)

// Tracer is a wrapper around the pgx tracer interfaces which instrument
Expand Down Expand Up @@ -79,6 +83,11 @@ func recordError(span trace.Span, err error) {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
span.SetAttributes(SQLStateKey.String(pgErr.Code))
}
}
}

Expand Down Expand Up @@ -307,7 +316,7 @@ func (t *Tracer) TracePrepareStart(ctx context.Context, conn *pgx.Conn, data pgx
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(t.attrs...),
}

if data.Name != "" {
trace.WithAttributes(PrepareStmtNameKey.String(data.Name))
}
Expand Down

0 comments on commit ab9f011

Please sign in to comment.