From 3d4593f6b70eb23915fa982e8634eb423c284ac9 Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Tue, 6 Dec 2022 14:49:29 +0100 Subject: [PATCH] Environment variable service_name for `pg` --- .../contrib/pg/configuration/settings.rb | 9 ++- lib/datadog/tracing/contrib/pg/ext.rb | 1 + .../tracing/contrib/pg/patcher_spec.rb | 76 +++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/lib/datadog/tracing/contrib/pg/configuration/settings.rb b/lib/datadog/tracing/contrib/pg/configuration/settings.rb index d779a74406..dcb2f29eb6 100644 --- a/lib/datadog/tracing/contrib/pg/configuration/settings.rb +++ b/lib/datadog/tracing/contrib/pg/configuration/settings.rb @@ -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 diff --git a/lib/datadog/tracing/contrib/pg/ext.rb b/lib/datadog/tracing/contrib/pg/ext.rb index 799ecf1f8e..32e7970cb6 100644 --- a/lib/datadog/tracing/contrib/pg/ext.rb +++ b/lib/datadog/tracing/contrib/pg/ext.rb @@ -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 diff --git a/spec/datadog/tracing/contrib/pg/patcher_spec.rb b/spec/datadog/tracing/contrib/pg/patcher_spec.rb index c6dfa882fa..f1f49ff29e 100644 --- a/spec/datadog/tracing/contrib/pg/patcher_spec.rb +++ b/spec/datadog/tracing/contrib/pg/patcher_spec.rb @@ -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' @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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