jackc / jackc/pgx

Access current request context from `Config.ConnConfig.OnNotice`

Open
#2,122 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

### Failed to get `context.Context` from `Config.ConnConfig.OnNotice`

I have implemented log tracing mechanism using a correlationID and I am accessing it everywhere to relate the logs.

**Example**
```
func (tracer *PGXQueryTracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
log := logger.GetFromCtx(ctx)

log.Trace().
Str("Database Query Result", data.CommandTag.String()).
Err(data.Err).
Msg("Database Query ended")
}

```

> The above example will log the executing query by attaching a common ID used for the all logs in the current request. BTW easily we can track all the steps happened in a particular request. `

Only the place I am not able to track the raise notice logging from the db using correlationID is this place, The reason for it is I didn't get a way to access the current request context that we are passing in every query from pgconn.PgConn.

```
Config.ConnConfig.OnNotice = func(pc *pgconn.PgConn, n *pgconn.Notice) {
log := logger.GetFromCtx(context.Background()) // doing this

// log := logger.GetFromCtx(ctx) // want to do this

log.Trace().
Uint32("pid", pc.PID()).
Str("message", n.Message).
Str("severity", n.Severity).
Msg("Database notice")
}

```

@jackc

Contributor guide

Open the contributing guide

Research direction

Start by tracing how Config.ConnConfig.OnNotice is invoked through pgconn.PgConn and how query contexts are passed. Determine an API design that makes the current request context available to notice handling while preserving existing callback behavior. Done means notice logs can use the query's context rather than context.Background().

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.