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

Add Postgres Integration #2054

Merged
merged 14 commits into from
Jun 2, 2022
Merged

Add Postgres Integration #2054

merged 14 commits into from
Jun 2, 2022

Conversation

jennchenn
Copy link
Member

@jennchenn jennchenn commented May 27, 2022

Description

This PR adds new instrumentation for the pg gem (>=v0.18.4). [GitHub Repo] [Ruby Gem]

Support was added for the following methods:

  • #exec
  • #exec_params
  • #exec_prepared
  • #async_exec
  • #async_exec_params (>=v1.1.0)
  • #async_exec_prepared (>=v1.1.0)
  • #sync_exec (>=v1.1.0)
  • #sync_exec_params (>=v1.1.0)
  • #sync_exec_prepared (>=v1.1.0)

Examples

These changes were tested in a sample rails application that make queries to a postgres database directly using the pg gem. Some sample traces are shown below:
image
image
image

Performance Benchmarks

The time it takes to execute a query using the pg gem when the tracer is enabled vs. when it is not was compared. The results for each of the supported methods are shown in the table below for the following queries: SELECT 1;, SELECT * FROM <table with 100 rows>; and SELECT * FROM <table with 1000 rows>;

With tracing disabled:

image

With tracing enabled:

image

Though there is a performance difference between the two, the overhead from having the tracer enabled for the pg gem seems to be on the same order as with other gems.

Comment on lines +14 to +16
def self.included(base)
base.prepend(InstanceMethods)
end
Copy link
Member

@marcotc marcotc May 27, 2022

Choose a reason for hiding this comment

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

@delner I've seen this pattern before and I'm not sure why we do this (include to then prepend).
Do you know of any reason to not directly prepend our instrumentation module?

Copy link
Contributor

Choose a reason for hiding this comment

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

It necessary when patching a blend of class and instance methods. As a matter of convenience, it also removes the ambiguity of when to include and when to prepend by encapsulating the correct behavior in the module. Moreover, it means you can also apply multiple modules in one include call. See Workers::Polling as an example.

Here, it's not strictly necessary, and I'm agnostic to whether we repeat the pattern or not.

Comment on lines 23 to 26
Tracing.trace(Ext::SPAN_EXEC, service: service) do |span|
span.resource = sql
span.type = Tracing::Metadata::Ext::SQL::TYPE
span.service = Ext::DEFAULT_PEER_SERVICE_NAME
Copy link
Member

Choose a reason for hiding this comment

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

Minor change: let's try to move as many values to the trace method call arguments (e.g. type be passed a a keyword argument to #trace for example).

This reduces the amount of times the span is mutated, and can allow for better introspection of the Span at initialization time if any parties are interested in inspecting it (e.g. sampling could do that in the future).

There's a minor performance improvement as well, as we save a method call (e.g. span.type = Tracing::Metadata::Ext::SQL::TYPE).

Copy link
Contributor

Choose a reason for hiding this comment

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

Especially a good point regarding sampling behavior.

@codecov-commenter
Copy link

codecov-commenter commented Jun 1, 2022

Codecov Report

Merging #2054 (8d9bc1b) into master (61a1391) will increase coverage by 0.03%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2054      +/-   ##
==========================================
+ Coverage   97.47%   97.51%   +0.03%     
==========================================
  Files        1035     1042       +7     
  Lines       52663    53384     +721     
==========================================
+ Hits        51334    52055     +721     
  Misses       1329     1329              
Impacted Files Coverage Δ
lib/datadog/tracing/contrib.rb 100.00% <100.00%> (ø)
...tadog/tracing/contrib/pg/configuration/settings.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/contrib/pg/ext.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/contrib/pg/instrumentation.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/contrib/pg/integration.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/contrib/pg/patcher.rb 100.00% <100.00%> (ø)
lib/datadog/tracing/metadata/ext.rb 100.00% <100.00%> (ø)
...pec/datadog/tracing/contrib/pg/integration_spec.rb 100.00% <100.00%> (ø)
spec/datadog/tracing/contrib/pg/patcher_spec.rb 100.00% <100.00%> (ø)

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Appraisals Outdated Show resolved Hide resolved
docs/GettingStarted.md Outdated Show resolved Hide resolved
@jennchenn jennchenn changed the title Add Postgres Integration [POC] Add Postgres Integration Jun 2, 2022
@jennchenn jennchenn marked this pull request as ready for review June 2, 2022 15:17
@jennchenn jennchenn requested a review from a team June 2, 2022 15:17
@delner delner added integrations Involves tracing integrations feature Involves a product feature labels Jun 2, 2022
@delner delner added this to In review in Active work Jun 2, 2022
@delner delner added this to the 1.2.0 milestone Jun 2, 2022
Appraisals Outdated Show resolved Hide resolved
Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

🚀

@jennchenn jennchenn merged commit 625f115 into master Jun 2, 2022
@jennchenn jennchenn deleted the jenn/postgres-integration branch June 2, 2022 22:09
Active work automation moved this from In review to Merged & awaiting release Jun 2, 2022
@ivoanjo ivoanjo moved this from Merged & awaiting release to Released in Active work Jul 11, 2022
@ivoanjo
Copy link
Member

ivoanjo commented Jul 11, 2022

This just got released as part of v1.2.0 -- @jennchenn your first PR is now in the hands of customers! Good job! :)

@pedrocunha
Copy link
Contributor

Hi @jennchenn, does this integration only work for explicit calls to pg gem or will it also provide a dependency breakdown when calling pg through activerecord on rails environments?

@marcotc
Copy link
Member

marcotc commented Oct 6, 2022

@pedrocunha, calls invoking the pg gem from any source will be instrumented, including indirect calls from activerecord.

Is there any issue you see today with the pg monitoring?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Involves a product feature integrations Involves tracing integrations
Projects
Active work
  
Released
Development

Successfully merging this pull request may close these issues.

None yet

6 participants