Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access current request context from Config.ConnConfig.OnNotice #2122

Open
anazibinurasheed opened this issue Sep 13, 2024 · 1 comment
Open

Comments

@anazibinurasheed
Copy link

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

@anazibinurasheed anazibinurasheed changed the title Failed to trace the logs from DB via Raise Notice. Couldn't find a way to access current request context from Config.ConnConfig.OnNotice. Failed to trace the logs from DB via Raise Notice. Couldn't find a way to access current request context from Config.ConnConfig.OnNotice. Sep 13, 2024
@jackc jackc changed the title Failed to trace the logs from DB via Raise Notice. Couldn't find a way to access current request context from Config.ConnConfig.OnNotice. Access current request context from Config.ConnConfig.OnNotice Sep 13, 2024
@jackc
Copy link
Owner

jackc commented Sep 13, 2024

This might be possible, but it also could be difficult and a significant change.

A notice could be received at any time. Not just while receiving query results. In addition, the notice may not have any connection to a query that happened to be executing at that time.

That's why it does not include a ctx now. However, it should be possible to store a currentCtx on *pgconn.PgConn`. This would have to be set and cleared by every method that potentially could receive a notice while it was executing.

I'm not opposed to this change, but it may be a fair amount of work, and it's not something I will get to anytime soon.


But as far as your immediate need, I think you can work around the issue by using https://pkg.go.dev/github.com/jackc/pgx/[email protected]/pgconn#PgConn.CustomData. You could use TraceQueryStart to set your current request context and TraceQueryEnd to clear it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants