forked from DataDog/dd-trace-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
658 lines (570 loc) · 24.3 KB
/
Copy pathRakefile
File metadata and controls
658 lines (570 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
require "bundler/gem_tasks"
require "datadog/version"
require "standard/rake" if Gem.loaded_specs.key? "standard"
require "rspec/core/rake_task"
require "rake/extensiontask"
require "os"
if Gem.loaded_specs.key? "ruby_memcheck"
require "ruby_memcheck"
require "ruby_memcheck/rspec/rake_task"
RubyMemcheck.config(
# If there's an error, print the suppression for that error, to allow us to easily skip such an error if it's
# a false-positive / something in the VM we can't fix.
valgrind_generate_suppressions: true,
# This feature provides better quality data -- I couldn't get good output out of ruby_memcheck without it.
use_only_ruby_free_at_exit: true,
)
end
Dir.glob("tasks/*.rake").each { |r| import r }
TEST_METADATA = eval(File.read("Matrixfile")).freeze # rubocop:disable Security/Eval
CORE_WITH_LIBDATADOG_API = [
"spec/datadog/core/crashtracking/**/*_spec.rb",
"spec/datadog/core/process_discovery_spec.rb",
"spec/datadog/core/configuration/stable_config_spec.rb",
"spec/datadog/core/feature_flags_spec.rb",
"spec/datadog/core/ddsketch_spec.rb",
"spec/datadog/data_streams/**/*_spec.rb",
"spec/datadog/open_feature_spec.rb",
"spec/datadog/core/libdatadog_extconf_helpers_spec.rb",
"spec/datadog/core/datadog_ruby_common_spec.rb",
].freeze
# Native trace exporter / transport specs (spec/datadog/tracing/transport/native).
# They need the libdatadog_api extension but are deliberately kept OUT of
# CORE_WITH_LIBDATADOG_API: that pool also runs the fork-heavy ProcessDiscovery
# specs, and a live native exporter (SharedRuntime threads) present at fork time
# deadlocks the child until fork safety lands (#5835). Running them in their own
# task keeps them in a separate process.
NATIVE_TRANSPORT_SPECS = [
"spec/datadog/tracing/transport/native/**/*_spec.rb",
].freeze
DI_WITH_EXT = %w[
spec/datadog/di/*_spec.rb
spec/datadog/di/**/*_spec.rb
].freeze
# Data Streams Monitoring (DSM) requires libdatadog_api for DDSketch
# Add new instrumentation libraries here as they gain DSM support
DSM_ENABLED_LIBRARIES = [
:kafka,
:karafka
].freeze
# rubocop:disable Metrics/BlockLength
namespace :test do
desc "Run all tests"
task all: TEST_METADATA.map { |k, _| "test:#{k}" }
TEST_METADATA.each do |key, spec_metadata|
spec_task = "spec:#{key}"
desc "Run #{spec_task} tests"
task key, [:task_args] do |_, args|
spec_arguments = args.task_args
candidates = spec_metadata.select do |_group, rubies|
RuntimeMatcher.match?(rubies)
end
candidates.each_key do |group|
env = if group.empty?
{}
else
gemfile = AppraisalConversion.to_bundle_gemfile(group)
{"BUNDLE_GEMFILE" => gemfile}
end
command = "bundle check || bundle install && bundle exec rake #{spec_task}"
command += "'[#{spec_arguments}]'" if spec_arguments
Bundler.with_unbundled_env { sh(env, command) }
end
end
end
end
desc "Run RSpec"
namespace :spec do
# REMINDER: If adding a new task here, make sure also add it to the `Matrixfile`
task all: [:main, :benchmark, :custom_cop,
:graphql, :graphql_unified_trace_patcher, :graphql_trace_patcher, :graphql_tracing_patcher,
:rails, :railsredis, :railsredis_activesupport, :railsactivejob,
:elasticsearch, :http, :redis, :sidekiq, :sinatra, :hanami, :hanami_autoinstrument,
:profiling, :core_with_libdatadog_api, :native_transport, :"di:di_with_ext", :"di:ractors", :error_tracking, :open_feature, :core_with_rails, :environment, :ai_guard]
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:main) do |t, args|
t.pattern = "spec/**/*_spec.rb"
t.exclude_pattern = "spec/**/{appsec/integration,contrib,benchmark,redis,auto_instrument,opentelemetry,open_feature,profiling,error_tracking,rubocop,ai_guard}/**/*_spec.rb," \
" spec/**/{auto_instrument,opentelemetry,process,ai_guard}_spec.rb," \
" spec/**/*_rails_spec.rb," \
" spec/datadog/core/environment/execution_spec.rb," \
" spec/datadog/di/*_spec.rb," \
" spec/datadog/di/**/*_spec.rb," \
" spec/datadog/gem_packaging_spec.rb," \
+ CORE_WITH_LIBDATADOG_API.join(", ") + ", " \
+ NATIVE_TRANSPORT_SPECS.join(", ")
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:benchmark) do |t, args|
t.pattern = "spec/datadog/benchmark/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:custom_cop) do |t, args|
t.pattern = "spec/rubocop/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:graphql) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/graphql/**/*_spec.rb"
t.exclude_pattern = "spec/datadog/tracing/contrib/graphql/{unified_trace,trace,tracing}_patcher_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:graphql_unified_trace_patcher) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/graphql/unified_trace_patcher_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:graphql_trace_patcher) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/graphql/trace_patcher_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:graphql_tracing_patcher) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/graphql/tracing_patcher_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:opentelemetry) do |t, args|
t.pattern = "spec/datadog/opentelemetry/**/*_spec.rb,spec/datadog/opentelemetry_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:open_feature) do |t, args|
t.pattern = "spec/datadog/open_feature/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:rails) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*_spec.rb"
t.exclude_pattern = "spec/datadog/tracing/contrib/rails/**/*{active_job,disable_env,redis_cache,auto_instrument," \
"semantic_logger}*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railsredis) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*redis*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railsredis_activesupport) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*redis*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
# Flag used to tell specs the expected configuration (so that they break if they're not being setup correctly)
ENV["EXPECT_RAILS_ACTIVESUPPORT"] = "true"
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railsactivejob) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*active_job*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railsdisableenv) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*disable_env*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railsautoinstrument) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*auto_instrument*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
# Tests if Datadog::Tracing::Contrib::ActiveSupport::Cache::Redis::Patcher does not eager load
# ActiveSupport::Cache::RedisCacheStore when the version of Redis present is too old to be compatible.
# @see Datadog::Tracing::Contrib::ActiveSupport::Cache::Redis::Patcher#patch_redis_cache_store?
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:rails_old_redis) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/cache_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:hanami) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/hanami/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:hanami_autoinstrument) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/hanami/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
ENV["TEST_AUTO_INSTRUMENT"] = "true"
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:autoinstrument) do |t, args|
t.pattern = "spec/datadog/auto_instrument_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:yjit) do |t, args|
t.pattern = "spec/datadog/core/runtime/metrics_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
# rails_semantic_logger is the dog at the dog park that doesnt play nicely with other
# logging gems, aka it tries to bite/monkeypatch them, so we have to put it in its own appraisal and rake task
# in order to isolate its effects for rails logs auto injection
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:railssemanticlogger) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/rails/**/*rails_semantic_logger*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
RSpec::Core::RakeTask.new(:core_with_libdatadog_api) do |t, args|
t.pattern = CORE_WITH_LIBDATADOG_API.join(", ")
t.rspec_opts = args.to_a.join(" ")
end
desc "Run spec:core_with_libdatadog_api tests with memory leak checking"
if Gem.loaded_specs.key?("ruby_memcheck")
require "libdatadog"
# Temporary workaround to unblock moving to libdatadog v30. If you see this code here and we've moved past v30 already,
# we forgot to clean it up -- please do!
if Libdatadog::VERSION.start_with?("30.")
task :core_with_libdatadog_api_memcheck do
warn "Skipping memcheck for libdatadog v30 because of https://github.com/bytecodealliance/rustix/issues/1559" \
" (libdatadog v30 causes a crash when running inside valgrind)." \
" Libdatadog 31? 32? should include https://github.com/DataDog/libdatadog/pull/1859 and fix this issue."
end
else
RubyMemcheck::RSpec::RakeTask.new(:core_with_libdatadog_api_memcheck) do |t, args|
t.pattern = CORE_WITH_LIBDATADOG_API.join(", ")
t.rspec_opts = [*args.to_a, "-t ~memcheck_valgrind_skip"].join(" ")
end
end
else
task :core_with_libdatadog_api_memcheck do
raise "Memcheck requires the ruby_memcheck gem to be installed"
end
end
# These specs require both libdatadog_api and profiling native extensions:
# - libdatadog_api provides crashtracking, DDSketch, and other core features
# - profiling extension is needed for crashtracking runtime stack capture
Rake::Task["spec:core_with_libdatadog_api"].enhance([:compile])
Rake::Task["spec:core_with_libdatadog_api_memcheck"].enhance([:compile]) if Gem.loaded_specs.key?("ruby_memcheck")
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:native_transport) do |t, args|
t.pattern = NATIVE_TRANSPORT_SPECS.join(", ")
t.rspec_opts = args.to_a.join(" ")
end
# Native trace exporter / transport specs need the libdatadog_api extension;
# run in their own process (see NATIVE_TRANSPORT_SPECS) to avoid sharing with
# the fork-heavy ProcessDiscovery specs.
Rake::Task["spec:native_transport"].enhance([:compile])
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:core_with_rails) do |t, args|
t.pattern = "spec/datadog/core/environment/process_spec.rb," \
"spec/**/*_rails_spec.rb,"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:environment) do |t, args|
t.pattern = "spec/datadog/core/environment/execution_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:error_tracking) do |t, args|
t.pattern = "spec/datadog/error_tracking/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:contrib) do |t, args|
contrib_paths = [
"*",
"configuration/*",
"configuration/resolvers/*",
"registry/*",
"propagation/**/*",
].join(",")
t.pattern = "spec/**/contrib/{#{contrib_paths}}_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
# Datadog Tracing integrations
# REMINDER: If adding a new task here, make sure also add it to the `Matrixfile`
[
:action_cable,
:action_mailer,
:action_pack,
:action_view,
:active_model_serializers,
:active_record,
:active_support,
:aws,
:concurrent_ruby,
:dalli,
:delayed_job,
:elasticsearch,
:ethon,
:excon,
:faraday,
:grape,
:grpc,
:http,
:httpclient,
:httprb,
:kafka,
:karafka,
:lograge,
:mongodb,
:mysql2,
:opensearch,
:pg,
:presto,
:que,
:racecar,
:rack,
:rake,
:redis,
:resque,
:roda,
:rest_client,
:semantic_logger,
:sequel,
:shoryuken,
:sidekiq,
:sinatra,
:sneakers,
:stripe,
:sucker_punch,
:suite,
:trilogy,
:waterdrop
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/#{contrib}/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
end
# Ensure DSM-enabled contrib tests compile libdatadog_api before running (MRI Ruby only)
# If compilation fails (e.g., new Ruby version without prebuilt extension), tests will skip via DDSketch.supported?
unless RUBY_PLATFORM == "java"
task :compile_libdatadog_for_dsm do
Rake::Task["compile:libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"].invoke
rescue => e
# Compilation failed (likely unsupported Ruby version) - tests will skip gracefully
puts "Warning: libdatadog_api compilation failed: #{e.class}: #{e}"
puts "DSM tests will be skipped for this Ruby version"
end
DSM_ENABLED_LIBRARIES.each do |task_name|
Rake::Task["spec:#{task_name}"].enhance([:compile_libdatadog_for_dsm])
end
end
namespace :appsec do
task all: [
:main,
:active_record,
:rack,
:rails,
:sinatra,
:devise,
:graphql,
:faraday,
:excon,
:rest_client,
:aws_lambda,
:integration
]
# Datadog AppSec main specs
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:main) do |t, args|
t.pattern = "spec/datadog/appsec/**/*_spec.rb"
t.exclude_pattern = "spec/datadog/appsec/**/{integration,contrib,auto_instrument}/**/*_spec.rb," \
" spec/datadog/appsec/**/{auto_instrument,autoload}_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
# Datadog AppSec integration specs (syntetic rails application)
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:integration) do |t, args|
t.pattern = "spec/datadog/appsec/integration/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
# Datadog AppSec integrations
[
:active_record,
:rack,
:sinatra,
:rails,
:devise,
:graphql,
:faraday,
:excon,
:rest_client,
:aws_lambda
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
t.pattern = "spec/datadog/appsec/contrib/#{contrib}/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
end
end
task appsec: [:"appsec:all"]
namespace :ai_guard do
task all: [:main, :rack, :ruby_llm]
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:main) do |t, args|
t.pattern = "spec/datadog/ai_guard/**/*_spec.rb,spec/datadog/ai_guard_spec.rb"
t.exclude_pattern = "spec/datadog/ai_guard/contrib/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:rack) do |t, args|
t.pattern = "spec/datadog/ai_guard/contrib/rack/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:ruby_llm) do |t, args|
t.pattern = "spec/datadog/ai_guard/contrib/ruby_llm/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
end
task ai_guard: [:"ai_guard:main"]
namespace :di do
# Datadog DI integrations
[
:active_record,
:bootsnap,
:rails,
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
t.pattern = "spec/datadog/di/contrib/#{contrib}/**/*_spec.rb"
t.rspec_opts = args.to_a.join(" ")
end
end
# bootsnap tests require the DI C extension (DI::Component.build checks for it).
Rake::Task["di:bootsnap"].enhance(["compile:libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"])
# rubocop:disable Style/MultilineBlockChain
RSpec::Core::RakeTask.new(:di_with_ext) do |t, args|
t.pattern = DI_WITH_EXT.join(", ")
t.exclude_pattern = "spec/datadog/di/contrib/**/*_spec.rb"
t.rspec_opts = [*args.to_a, "-t ~ractors"].join(" ")
end.tap do |t|
Rake::Task[t.name].enhance(["compile:libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"])
end
# Ractor creation transitions the entire Ruby VM into multi-ractor mode permanently
# and can introduce order-dependent side effects on later examples. Splitting these
# examples into a dedicated task mirrors spec:profiling:ractors (see Rakefile around
# spec:profiling:main / :ractors) and keeps the regular DI job from mutating VM mode.
RSpec::Core::RakeTask.new(:ractors) do |t, args|
t.pattern = DI_WITH_EXT.join(", ")
t.exclude_pattern = "spec/datadog/di/contrib/**/*_spec.rb"
t.rspec_opts = [*args.to_a, "-t ractors"].join(" ")
end.tap do |t|
Rake::Task[t.name].enhance(["compile:libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"])
end
# rubocop:enable Style/MultilineBlockChain
end
namespace :profiling do
task all: [:main, :ractors]
task :compile_native_extensions do
# "bundle exec rake compile" currently only works on MRI Ruby on Linux
if RUBY_ENGINE == "ruby" && OS.linux? && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.3.0")
Rake::Task[:clean].invoke
Rake::Task[:compile].invoke
end
end
# Datadog Profiling main specs without Ractor creation
# NOTE: Ractor creation will transition the entire Ruby VM into multi-ractor mode. This cannot be undone
# and, as such, may introduce side-effects between tests and make them flaky depending on order of
# execution. By splitting in two separate suites, the side-effect impact should be mitigated as
# the non-ractor VM will never trigger the transition into multi-ractor mode.
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:main) do |t, args|
t.pattern = "spec/datadog/profiling/**/*_spec.rb,spec/datadog/profiling_spec.rb"
t.rspec_opts = [*args.to_a, "-t ~ractors"].join(" ")
end
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(:ractors) do |t, args|
t.pattern = "spec/datadog/profiling/**/*_spec.rb"
t.rspec_opts = [*args.to_a, "-t ractors"].join(" ")
end
desc "Run spec:profiling:main tests with memory leak checking"
if Gem.loaded_specs.key?("ruby_memcheck")
require "libdatadog"
# Temporary workaround to unblock moving to libdatadog v30. If you see this code here and we've moved past v30 already,
# we forgot to clean it up -- please do!
if Libdatadog::VERSION.start_with?("30.")
task :memcheck do
warn "Skipping memcheck for libdatadog v30 because of https://github.com/bytecodealliance/rustix/issues/1559" \
" (libdatadog v30 causes a crash when running inside valgrind)." \
" Libdatadog 31? 32? should include https://github.com/DataDog/libdatadog/pull/1859 and fix this issue."
end
else
RubyMemcheck::RSpec::RakeTask.new(:memcheck) do |t, args|
t.pattern = "spec/datadog/profiling/**/*_spec.rb,spec/datadog/profiling_spec.rb"
# Some of our specs use multi-threading + busy looping, or multiple processes, or are just really really slow.
# We skip running these when running under valgrind.
# (As a reminder, by default valgrind simulates a sequential/single-threaded execution).
#
# @ivoanjo: I previously tried https://github.com/Shopify/ruby_memcheck/issues/51 but in some cases valgrind
# would give incomplete output, causing a "FATAL: Premature end of data in tag valgrindoutput line 3" error in
# ruby_memcheck. I did not figure out why exactly.
t.rspec_opts = [*args.to_a, "-t ~ractors -t ~memcheck_valgrind_skip"].join(" ")
end
end
else
task :memcheck do
raise "Memcheck requires the ruby_memcheck gem to be installed"
end
end
# Make sure each profiling test suite has a dependency on compiled native extensions
Rake::Task[:all].prerequisite_tasks.each { |t| t.enhance([:compile_native_extensions]) }
end
task profiling: [:"profiling:all"]
end
# Jobs are parallelized if running in CI.
desc "CI task; it runs all tests for current version of Ruby"
task ci: "test:all"
namespace :coverage do
# Generates one global report for all tracer tests
task :report do
require "simplecov"
resultset_files = Dir["#{ENV.fetch("COVERAGE_DIR", "coverage")}/.resultset.json"] +
Dir["#{ENV.fetch("COVERAGE_DIR", "coverage")}/versions/**/.resultset.json"]
SimpleCov.collate resultset_files do
coverage_dir "#{ENV.fetch("COVERAGE_DIR", "coverage")}/report"
formatter SimpleCov::Formatter::HTMLFormatter
end
end
# Generates one report for each Ruby version
task :report_per_ruby_version do
require "simplecov"
require_relative "spec/support/simplecov_fix"
versions = Dir["#{ENV.fetch("COVERAGE_DIR", "coverage")}/versions/*"].map { |f| File.basename(f) }
versions.map do |version|
puts "Generating report for: #{version}"
SimpleCov.collate Dir["#{ENV.fetch("COVERAGE_DIR", "coverage")}/versions/#{version}/**/.resultset.json"] do
coverage_dir "#{ENV.fetch("COVERAGE_DIR", "coverage")}/report/versions/#{version}"
formatter SimpleCov::Formatter::HTMLFormatter
end
end
end
end
NATIVE_EXTS = [
Rake::ExtensionTask.new("libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}") do |ext|
ext.ext_dir = "ext/libdatadog_api"
end,
Rake::ExtensionTask.new("datadog_profiling_native_extension.#{RUBY_VERSION}_#{RUBY_PLATFORM}") do |ext|
ext.ext_dir = "ext/datadog_profiling_native_extension"
end,
].freeze
NATIVE_CLEAN = ::Rake::FileList[]
# DEV: Should we suggest this Rake task for native development onboarding?
namespace :native_dev do
compile_commands_tasks = NATIVE_EXTS.map do |ext|
tmp_dir_dd_native_dev = "#{ext.tmp_dir}/dd_native_dev"
directory tmp_dir_dd_native_dev
NATIVE_CLEAN << tmp_dir_dd_native_dev
compile_commands_task = file "#{ext.ext_dir}/compile_commands.json" => [tmp_dir_dd_native_dev] do |t|
puts "Generating #{t.name}"
root_dir = Dir.pwd
cmd = ext.make_makefile_cmd(root_dir, tmp_dir_dd_native_dev, "#{ext.ext_dir}/#{ext.config_script}", nil)
abs_ext_dir = (Pathname.new(root_dir) + ext.ext_dir).realpath
chdir tmp_dir_dd_native_dev do
sh(*cmd)
sh("make clean; bear -- make; make clean")
cp("compile_commands.json", "#{abs_ext_dir}/compile_commands.json")
end
end
NATIVE_CLEAN << compile_commands_task.name
compile_commands_task
end
desc "Setup dev environment for native extensions."
task setup: compile_commands_tasks
CLEAN.concat(NATIVE_CLEAN)
end
desc "Runs lint + main test suite"
task default: ["rubocop", "standard", "typecheck", "spec:main"]
desc "Runs the default task in parallel"
multitask fastdefault: ["rubocop", "standard", "typecheck", "spec:main"]