-
Notifications
You must be signed in to change notification settings - Fork 375
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
Fix Rails caching stale instance of tracer #1064
Conversation
6f171e2
to
e4882a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
so this fixes my problem:
however, it breaks the ability to set a custom tracer instance on the rails integration: diff --git a/spec/ddtrace/contrib/rails/database_spec.rb b/spec/ddtrace/contrib/rails/database_spec.rb
index fe090973..ac2a2836 100644
--- a/spec/ddtrace/contrib/rails/database_spec.rb
+++ b/spec/ddtrace/contrib/rails/database_spec.rb
@@ -6,9 +6,13 @@ RSpec.describe 'Rails database' do
let(:database_service) { adapter_name }
+ let(:rails_configuration) do
+ { database_service: database_service }
+ end
+
before do
Datadog.configure do |c|
- c.use :rails, database_service: database_service
+ c.use :rails, rails_configuration
end
end
@@ -23,6 +27,21 @@ RSpec.describe 'Rails database' do
after { Article.delete_all }
+ context 'with an explicit tracer instance' do
+ let(:use_mock_tracer) { false }
+
+ let(:rails_configuration) do
+ { database_service: database_service, tracer: tracer }
+ end
+
+ it 'has the correct tracer instance', :aggregate_failures do
+ expect(Datadog.configuration[:rails].tracer).to equal(tracer)
+ expect(Datadog.configuration[:active_record].tracer).to equal(tracer)
+ expect(tracer).not_to equal(Datadog.tracer)
+ expect(span.tracer).to equal tracer
+ end
+ end
+
context 'with ActiveRecord query' do
subject! { Article.count }
diff --git a/spec/support/tracer_helpers.rb b/spec/support/tracer_helpers.rb
index 6271015c..6f22f7ca 100644
--- a/spec/support/tracer_helpers.rb
+++ b/spec/support/tracer_helpers.rb
@@ -5,8 +5,9 @@ require 'support/faux_writer'
# rubocop:disable Metrics/ModuleLength
module TracerHelpers
shared_context 'global test tracer' do
- before { Datadog.configure { |c| c.tracer.instance = tracer } }
- after { Datadog.configure { |c| c.tracer.instance = nil } }
+ let(:use_mock_tracer) { true }
+ before { Datadog.configure { |c| c.tracer.instance = tracer } if use_mock_tracer }
+ after { Datadog.configure { |c| c.tracer.instance = nil } if use_mock_tracer }
end
|
@delner also this logic needs to be changed or removed: dd-trace-rb/lib/ddtrace/contrib/rails/framework.rb Lines 47 to 50 in 0625ccc
|
@fledman Good catch on the updating the tracer check. I'll make that amendment. I think we're moving towards removing the |
@delner |
I tested this branch with one of our services and I'm sorry to say that I had the same issues mentioned in #1048 where spans for some integrations (GraphQL) were disconnected from the rest of the trace (that issue was from v0.35.2). I did not get down to the level of debugging which tracer instance each integration was getting. It was not just the GraphQL spans that were disconnected. As mentioned in this comment #1048 (comment), when testing v0.36.0 we saw issues with other integrations. I saw the same again testing this branch. We're keeping this service on v0.34.2 for now. |
@tjwp |
@tjwp diff --git a/lib/ddtrace/contrib/graphql/patcher.rb b/lib/ddtrace/contrib/graphql/patcher.rb
index ffb2da4a..e13237d2 100644
--- a/lib/ddtrace/contrib/graphql/patcher.rb
+++ b/lib/ddtrace/contrib/graphql/patcher.rb
@@ -22,7 +22,6 @@ module Datadog
end
def patch_schema!(schema)
- tracer = get_option(:tracer)
service_name = get_option(:service_name)
analytics_enabled = Contrib::Analytics.enabled?(get_option(:analytics_enabled))
analytics_sample_rate = get_option(:analytics_sample_rate)
@@ -30,7 +29,6 @@ module Datadog
if schema.respond_to?(:use)
schema.use(
::GraphQL::Tracing::DataDogTracing,
- tracer: tracer,
service: service_name,
analytics_enabled: analytics_enabled,
analytics_sample_rate: analytics_sample_rate
@@ -39,7 +37,6 @@ module Datadog
schema.define do
use(
::GraphQL::Tracing::DataDogTracing,
- tracer: tracer,
service: service_name,
analytics_enabled: analytics_enabled,
analytics_sample_rate: analytics_sample_rate
diff --git a/spec/ddtrace/contrib/graphql/tracer_spec.rb b/spec/ddtrace/contrib/graphql/tracer_spec.rb
index 2fbdceff..061f57e1 100644
--- a/spec/ddtrace/contrib/graphql/tracer_spec.rb
+++ b/spec/ddtrace/contrib/graphql/tracer_spec.rb
@@ -16,6 +16,8 @@ RSpec.describe 'GraphQL patcher' do
let(:root_span) { spans.find { |s| s.parent.nil? } }
RSpec.shared_examples 'Schema patcher' do
+ include_context 'global test tracer'
+
before(:each) do
remove_patch!(:graphql)
Datadog.configure do |c|
|
Looks like this might be superseded by #1079 which includes most of these changes and should fix the same problem. Might want to close this one in favor of that. |
Just came to say that 0.37.0 releases #1079 and fixes this issue for me! |
Okay 0.37.0 is out and already has these changes; going to close this one. |
Per #1061, Rails is ending up with a stale instance of the tracer, which can cause traces or certain settings to look as if they've disappeared. This is because when Rails instrumentation applied itself in
after_initialize
, it would set the tracer on Rails components explicitly, causing it to be cached.This issue was exposed in 0.36.0 when we introduced lazy patching, which necessitated the rebuilding of trace components when activating Rails instrumentation.
In this pull request, we remove the caching of the tracer instance during the instrumentation activation phase, such that Rails components will always delegate to the current global tracer instead.