Skip to content

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented Aug 28, 2025

Fixes #3465

Async query execution, introduced in Rails 7.0.0 and expanded in Rails 7.1.0, now correctly parents the async spans to the context where the query resolution happens.
This context is the place where the results from the async query were consumed. For example: async_relation = Model.load_async schedules the async query, but the query span is only attached to the active trace at relation.load time). This only affects parenting, as the execution time of the async span is always accurate.

The active trace, started in a thread, will now see spans from a background async thread attached to it.

Change log entry
Yes. Add support for ActiveRecord async queries

Additional Notes:

How to test the change?

**What does this PR do?**
Adds support for Rails load_async feature by implementing the publish method and
enhancing the ActiveSupport notifications subscription system with proper callback
handling and error management.

**Motivation:**
Rails load_async feature requires support for the publish method in ActiveSupport
notifications to handle asynchronous event processing. The existing subscription
system needed enhancements to properly support this Rails feature.

**Change log entry**
Added support for Rails load_async feature in ActiveSupport notifications subscription

**Additional Notes:**
- Implements publish method for complete event lifecycle handling
- Adds robust error handling for callbacks and handlers
- Includes comprehensive test coverage (38 examples, 0 failures)
- Ensures system continues functioning when individual callbacks fail

**How to test the change?**
Run: bundle exec rspec spec/datadog/tracing/contrib/active_support/notifications/subscription_spec.rb
Tests verify load_async functionality including:
- publish method event lifecycle
- Handler and Callbacks error handling
- Integration with Rails async event processing
@github-actions github-actions bot added integrations Involves tracing integrations tracing labels Aug 28, 2025
Copy link

datadog-official bot commented Aug 28, 2025

⚠️ Tests

⚠️ Warnings

❄️ 1 New flaky test detected

ActiveRecord instrumentation when query is made with adapter supporting background execution parents the database span to the calling context from rspec (Datadog)
undefined method \`load_async' for #<ActiveRecord::Relation []>

Failure/Error: relation = Article.limit(1).load_async

NoMethodError:
  undefined method \`load_async' for #<ActiveRecord::Relation []>
/usr/local/bundle/gems/activerecord-6.1.7.6/lib/active_record/relation/delegation.rb:110:in \`method_missing'
./spec/datadog/tracing/contrib/active_record/tracer_spec.rb:165:in \`block (5 levels) in <top (required)>'
./lib/datadog/tracing/trace_operation.rb:243:in \`block in measure'
./lib/datadog/tracing/span_operation.rb:167:in \`measure'
...

🧪 5 Tests failed

ActiveRecord instrumentation when query is made and service_name behaves like schema version span service name env var testing service name test with integration service name is expected to have 1 item from rspec (Datadog)
Requested the only span, but 0 spans are available

Failure/Error: expect(spans).to have(1).item, "Requested the only span, but #{spans.size} spans are available"
  Requested the only span, but 0 spans are available
Shared Example Group: "schema version span" called from ./spec/datadog/tracing/contrib/active_record/tracer_spec.rb:61
./spec/support/tracer_helpers.rb:99:in \`span'
./spec/datadog/tracing/contrib/span_attribute_schema_examples.rb:40:in \`block (4 levels) in <top (required)>'
./spec/datadog/tracing/contrib/span_attribute_schema_examples.rb:34:in \`block (5 levels) in <top (required)>'
/usr/local/bundle/gems/climate_control-1.2.0/lib/climate_control.rb:24:in \`block in modify'
/usr/local/bundle/gems/climate_control-1.2.0/lib/climate_control.rb:15:in \`synchronize'
...
ActiveRecord instrumentation when query is made and service_name behaves like schema version span service name env var testing test the default is expected to have 1 item from rspec (Datadog)
Requested the only span, but 0 spans are available

Failure/Error: expect(spans).to have(1).item, "Requested the only span, but #{spans.size} spans are available"
  Requested the only span, but 0 spans are available
Shared Example Group: "schema version span" called from ./spec/datadog/tracing/contrib/active_record/tracer_spec.rb:61
./spec/support/tracer_helpers.rb:99:in \`span'
./spec/datadog/tracing/contrib/span_attribute_schema_examples.rb:26:in \`block (4 levels) in <top (required)>'
./spec/datadog/tracing/contrib/span_attribute_schema_examples.rb:21:in \`block (5 levels) in <top (required)>'
/usr/local/bundle/gems/climate_control-1.2.0/lib/climate_control.rb:24:in \`block in modify'
/usr/local/bundle/gems/climate_control-1.2.0/lib/climate_control.rb:15:in \`synchronize'
...
ActiveRecord instrumentation when query is made and service_name is not set is expected to have 1 item from rspec (Datadog)
Requested the only span, but 0 spans are available

Failure/Error: expect(spans).to have(1).item, "Requested the only span, but #{spans.size} spans are available"
  Requested the only span, but 0 spans are available
./spec/support/tracer_helpers.rb:99:in \`span'
./spec/datadog/tracing/contrib/active_record/tracer_spec.rb:64:in \`block (5 levels) in <top (required)>'
./spec/datadog/tracing/contrib/active_record/tracer_spec.rb:31:in \`block (2 levels) in <top (required)>'
./spec/datadog/tracing/contrib/support/tracer_helpers.rb:96:in \`block (2 levels) in <module:TracerHelpers>'
./spec/spec_helper.rb:272:in \`block (2 levels) in <top (required)>'
./spec/spec_helper.rb:154:in \`block (2 levels) in <top (required)>'
...
View all
This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2ea0f22 | Docs | Was this helpful? Give us feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrations Involves tracing integrations tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Active record async queries are considered separate traces
1 participant