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

contrib/jackc/pgx: add initial support #1840

Closed
wants to merge 2 commits into from

Conversation

statik
Copy link

@statik statik commented Mar 29, 2023

What does this PR do?

The content of this PR is from #1537. I'm trying to help move this contribution forward in response to the request for help in the original PR. DD support asked me to get a PR that is not in draft state so that the tests can run.

This PR fixes #697

Motivation

Describe how to test/QA your changes

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Changed code has unit tests for its functionality.
  • If this interacts with the agent in a new way, a system test has been added.

@statik statik requested a review from a team March 29, 2023 15:19
@statik statik requested a review from a team as a code owner March 29, 2023 15:19
@statik statik changed the title contrib/jackc/pgx: add API and implement QueryTracer contrib/jackc/pgx: add initial support Mar 29, 2023
@statik
Copy link
Author

statik commented Mar 30, 2023

Here is a quick sample app that uses this tracing support: https://github.com/statik/pgx-ddtrace-example

To set up for the sample app I run the data dog agent and Postgres on my dev machine. Then I run go build and then run the app. I see pgx traces showing up in DataDog.

Copy link
Contributor

@zarirhamza zarirhamza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked it over but firstly this integration is missing testing and an example_test.go expected in all libraries. You can refer to the contrib/database/sql folder for examples of unit and integration tests utilizing our pre-existing postgres setup.

}

// TraceArgs will report the arguments of the queries, if on is set to true
func TraceArgs(on bool) Option {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the library separates the SQL statement and the arguments. However, we already inherently obfuscate the arguments from the statement to prevent security leaks and we wouldn't the same issues by exposing the arguments as a tag, so this field should be omitted

}

// TraceStatus will report the status of the queries, if on is set to true
func TraceStatus(on bool) Option {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that this information is gated behind a boolean? It seems like a person would always want this information so we should either always set it as a tag or omit it as well

)

// TracedConn returns a traced *pgx.Conn
// note that connect trace are not effective this way
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

}

// TracedPool returns a traced *pgxpool.Pool for concurrent use
// note that connect traces are not effective this way
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean

ddtracer.Tag("sql.query_type", "Query"),
ddtracer.Tag(ext.ResourceName, data.SQL),
}
if t.traceArgs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned before, should be omitted

}

if t.traceStatus {
span.SetTag("pq.status", data.CommandTag.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be pgx so we know that it's from this library

ddtracer.SpanType(ext.SpanTypeSQL),
ddtracer.StartTime(time.Now()),
ddtracer.Tag("sql.query_type", "Query"),
ddtracer.Tag(ext.ResourceName, data.SQL),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ddtracer.Tag(ext.ResourceName, data.SQL),
ddtracer.Tag(ext.ResourceName, data.SQL),
ddtracer.Tag(ext.Component, "jackc/pgx"),
ddtracer.Tag(ext.SpanKind, ext.SpanKindClient),

Component and SpanKind tags are necessary for all components

}

// TracedPoolWithConfig returns a traced *pgxpool.Pool with config, for concurrent use
func TracedPoolWithConfig(ctx context.Context, config *pgxpool.Config, options ...Option) (*pgxpool.Pool, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the effort on implementing this.

Out of curiosity, what happens if someone is trying to use TraceLog?

May I suggest that the library could check if the config already has a Tracer set and wrap it if true?

for _, opt := range options {
opt(t)
}
p.Config().ConnConfig.Tracer = t
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidentally left a comment on the old draft regarding this.
#1537 (comment)

During our implementation of dd-tracer-go and pgx, we had to do something like:

config := p.Config()
config.ConnConfig.Tracer = t
poolWithTracer, err := pgxpool.NewWithConfig(ctx, config)

Pretty new to Go so I can't tell you exactly why, but the pointer reference from p.Config() was different than p, so editing that config didn't affect the original pool, so recreating the pool with the updated config was necessary to get things working.

@devwells
Copy link

@statik are you still able to work on this PR?

@statik
Copy link
Author

statik commented Jul 27, 2023

@devwells unfortunately I no longer have time to work on this, closing.

@statik statik closed this Jul 27, 2023
@statik statik deleted the feat-support-pgx branch July 27, 2023 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal: integrate with github.com/jackc/pgx
6 participants