Skip to content

Commit

Permalink
Environment variable service_name for pg
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Dec 6, 2022
1 parent b7133eb commit 3d4593f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
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

0 comments on commit 3d4593f

Please sign in to comment.