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

Auto-instrumentation with service_name from environmental variable #2455

Merged
merged 19 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Environment variable service_name for pg
  • Loading branch information
TonyCTHsu committed Dec 6, 2022
commit 3d4593f6b70eb23915fa982e8634eb423c284ac9
9 changes: 8 additions & 1 deletion lib/datadog/tracing/contrib/pg/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class Settings < Contrib::Configuration::Settings
o.lazy
end

option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
option :service_name do |o|
o.default do
ENV.fetch(Ext::ENV_SERVICE_NAME) do
Ext::DEFAULT_PEER_SERVICE_NAME
end
end
o.lazy
end

option :comment_propagation do |o|
o.default do
Expand Down
1 change: 1 addition & 0 deletions lib/datadog/tracing/contrib/pg/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Pg
# @public_api Changing resource names, tag names, or environment variables creates breaking changes.
module Ext
ENV_ENABLED = 'DD_TRACE_PG_ENABLED'.freeze
ENV_SERVICE_NAME = 'DD_TRACE_PG_SERVICE_NAME'.freeze
ENV_ANALYTICS_ENABLED = 'DD_TRACE_PG_ANALYTICS_ENABLED'.freeze
ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_PG_ANALYTICS_SAMPLE_RATE'.freeze
DEFAULT_PEER_SERVICE_NAME = 'pg'.freeze
Expand Down
76 changes: 76 additions & 0 deletions spec/datadog/tracing/contrib/pg/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'datadog/tracing/contrib/support/spec_helper'
require 'datadog/tracing/contrib/analytics_examples'
require 'datadog/tracing/contrib/sql_comment_propagation_examples'
require 'datadog/tracing/contrib/environment_service_name_examples'

require 'datadog/tracing/contrib/propagation/sql_comment/mode'

Expand Down Expand Up @@ -124,6 +125,10 @@
it_behaves_like 'measured span for integration', false do
before { exec }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -138,6 +143,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('column "invalid" does not exist'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -217,6 +226,10 @@
it_behaves_like 'measured span for integration', false do
before { exec_params }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -233,6 +246,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('could not determine data type of parameter $1'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -305,6 +322,10 @@
it_behaves_like 'measured span for integration', false do
before { exec_prepared }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -316,6 +337,11 @@
include('ERROR') & include('prepared statement "invalid prepared select 1" does not exist')
)
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
subject { conn.exec_prepared('invalid prepared select 1', ['INVALID']) }
end
end
end

Expand Down Expand Up @@ -395,6 +421,10 @@
it_behaves_like 'measured span for integration', false do
before { async_exec }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -410,6 +440,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('column "invalid" does not exist'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -493,6 +527,10 @@
it_behaves_like 'measured span for integration', false do
before { async_exec_params }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -508,6 +546,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('could not determine data type of parameter $1'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -585,6 +627,10 @@
it_behaves_like 'measured span for integration', false do
before { async_exec_prepared }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -596,6 +642,11 @@
include('ERROR') & include('prepared statement "invalid prepared select 1" does not exist')
)
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
subject { conn.async_exec_prepared('invalid prepared select 1', ['INVALID']) }
end
end
end

Expand Down Expand Up @@ -681,6 +732,10 @@
it_behaves_like 'measured span for integration', false do
before { sync_exec }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -694,6 +749,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('column "invalid" does not exist'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -774,6 +833,10 @@
it_behaves_like 'measured span for integration', false do
before { sync_exec_params }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -790,6 +853,10 @@
expect(span).to have_error
expect(span).to have_error_message(include('ERROR') & include('could not determine data type of parameter $1'))
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
end
end
end

Expand Down Expand Up @@ -865,6 +932,10 @@
it_behaves_like 'measured span for integration', false do
before { sync_exec_prepared }
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME' do
let(:configuration_options) { {} }
end
end

context 'when a failed query is made' do
Expand All @@ -876,6 +947,11 @@
include('ERROR') & include('prepared statement "invalid prepared select 1" does not exist')
)
end

it_behaves_like 'environment service name', 'DD_TRACE_PG_SERVICE_NAME', error: PG::Error do
let(:configuration_options) { {} }
subject { conn.sync_exec_prepared('invalid prepared select 1', ['INVALID']) }
end
end
end
end
Expand Down