ddtrace 0.53.0 → 0.54.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -11
  3. data/ddtrace.gemspec +5 -2
  4. data/docs/GettingStarted.md +40 -3
  5. data/docs/ProfilingDevelopment.md +2 -2
  6. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +86 -0
  7. data/ext/ddtrace_profiling_native_extension/clock_id.h +4 -0
  8. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +52 -0
  9. data/ext/ddtrace_profiling_native_extension/clock_id_noop.c +14 -0
  10. data/ext/ddtrace_profiling_native_extension/extconf.rb +111 -3
  11. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +35 -0
  12. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.h +3 -0
  13. data/ext/ddtrace_profiling_native_extension/profiling.c +6 -1
  14. data/lib/datadog/ci/contrib/cucumber/formatter.rb +1 -0
  15. data/lib/datadog/ci/contrib/rspec/example.rb +1 -0
  16. data/lib/datadog/ci/ext/environment.rb +26 -21
  17. data/lib/datadog/ci/ext/test.rb +1 -0
  18. data/lib/datadog/ci/test.rb +5 -1
  19. data/lib/ddtrace/buffer.rb +28 -16
  20. data/lib/ddtrace/configuration/agent_settings_resolver.rb +27 -16
  21. data/lib/ddtrace/context.rb +10 -2
  22. data/lib/ddtrace/contrib/delayed_job/plugin.rb +2 -2
  23. data/lib/ddtrace/contrib/mongodb/instrumentation.rb +1 -1
  24. data/lib/ddtrace/contrib/mongodb/integration.rb +5 -0
  25. data/lib/ddtrace/contrib/rails/configuration/settings.rb +7 -0
  26. data/lib/ddtrace/contrib/rails/framework.rb +3 -2
  27. data/lib/ddtrace/contrib/redis/instrumentation.rb +90 -0
  28. data/lib/ddtrace/contrib/redis/patcher.rb +2 -84
  29. data/lib/ddtrace/contrib/resque/integration.rb +1 -5
  30. data/lib/ddtrace/ext/priority.rb +6 -4
  31. data/lib/ddtrace/ext/profiling.rb +1 -1
  32. data/lib/ddtrace/metrics.rb +2 -2
  33. data/lib/ddtrace/profiling/collectors/stack.rb +45 -45
  34. data/lib/ddtrace/profiling/encoding/profile.rb +1 -1
  35. data/lib/ddtrace/profiling/events/stack.rb +8 -8
  36. data/lib/ddtrace/profiling/native_extension.rb +23 -1
  37. data/lib/ddtrace/profiling/pprof/builder.rb +8 -2
  38. data/lib/ddtrace/profiling/pprof/stack_sample.rb +13 -16
  39. data/lib/ddtrace/profiling/pprof/template.rb +2 -2
  40. data/lib/ddtrace/profiling/tasks/setup.rb +21 -12
  41. data/lib/ddtrace/profiling/trace_identifiers/ddtrace.rb +9 -8
  42. data/lib/ddtrace/profiling/trace_identifiers/helper.rb +2 -2
  43. data/lib/ddtrace/profiling.rb +0 -2
  44. data/lib/ddtrace/sampler.rb +18 -8
  45. data/lib/ddtrace/sampling/rule_sampler.rb +13 -1
  46. data/lib/ddtrace/utils/time.rb +6 -0
  47. data/lib/ddtrace/version.rb +1 -1
  48. metadata +14 -9
  49. data/lib/ddtrace/profiling/ext/cpu.rb +0 -67
  50. data/lib/ddtrace/profiling/ext/cthread.rb +0 -156
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d503fa2075462a3264c15a5e05ed1bc19afae88994c6df987dd32c6ee4f500c
4
- data.tar.gz: 93a4cfaa1b395f1f968b1e87525df21b44c3a2fd63458013a1d506c9ae3b28e2
3
+ metadata.gz: 98fd0b93fb5cc2fbca0156ea0ef37ae12936ccc2718538b61fb2c04d07f26896
4
+ data.tar.gz: a64b9a3977f32c7b6417b411bf5ebd1648085bc2fdc4ba7adeffa95de590bace
5
5
  SHA512:
6
- metadata.gz: 63918fc0a3b1d50a76842f6b500ae80300c6df9afd873628230a70a17b83b8971904fe2e1b1ff71cb44143eb6ef62b294f4958188c4db24cb12ee61a2069ab34
7
- data.tar.gz: 5de75c82e3030e4c617a0b06b590574f5389d428eba9294e8b1a5221bd7f4e3b0bfbc40295cb86d1fb91af1a199585015ea6d6d4e3080dc475e15e8bba1288f0
6
+ metadata.gz: 67e2065aa016d1fc18b4a2d193379173a78e6c3fcf47e1f74f45fa096e26feda6f6f1b5da286051730abbe9b6b5331ceb62d48ff09af9ab932bc8b97c8b3efd2
7
+ data.tar.gz: dcde2f41ef22a3a2b2ab48038cd61021d63848cdfaea4aa535290447e81648a73fd0f9640b39be041273d6aa1fb3829e2a4781d39976398cf4b3f9f1826a41b1
data/CHANGELOG.md CHANGED
@@ -2,6 +2,43 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.54.0] - 2021-11-17
6
+
7
+ Release notes: https://github.com/DataDog/dd-trace-rb/releases/tag/v0.54.0
8
+
9
+ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.53.0...v0.54.0
10
+
11
+ ### Added
12
+
13
+ - MongoDB service name resolver when using multi cluster ([#1423][]) ([@skcc321][])
14
+ - Service name override for ActiveJob in Rails configuration ([#1703][], [#1770][]) ([@hatstand][])
15
+ - Profiler: Expose profile duration and start to the UI ([#1709][])
16
+ - Profiler: Gather CPU time without monkey patching Thread ([#1735][], [#1740][])
17
+ - Profiler: Link profiler samples to individual web requests ([#1688][])
18
+ - Profiler: Capture threads with empty backtrace ([#1719][])
19
+ - CI-App: Memoize environment tags to improve performance ([#1762][])
20
+ - CI-App: `test.framework_version` tag for rspec and cucumber ([#1713][])
21
+
22
+ ### Changed
23
+
24
+ - Set minimum version of dogstatsd-ruby 5 series to 5.3 ([#1717][])
25
+ - Use USER_KEEP/USER_REJECT for RuleSampler decisions ([#1769][])
26
+
27
+ ### Fixed
28
+
29
+ - "private method `ruby2_keywords' called" errors ([#1712][], [#1714][])
30
+ - Configuration warning when Agent port is a String ([#1720][])
31
+ - Ensure internal trace buffer respects its maximum size ([#1715][])
32
+ - Remove erroneous maximum resque version support ([#1761][])
33
+ - CI-App: Environment variables parsing precedence ([#1745][], [#1763][])
34
+ - CI-App: GitHub Metadata Extraction ([#1771][])
35
+ - Profiler: Missing thread id for natively created threads ([#1718][])
36
+ - Docs: Active Job integration example code ([#1721][]) ([@y-yagi][])
37
+
38
+ ### Refactored
39
+
40
+ - Redis client patch to use prepend ([#1743][]) ([@justinhoward][])
41
+
5
42
  ## [0.53.0] - 2021-10-06
6
43
 
7
44
  Release notes: https://github.com/DataDog/dd-trace-rb/releases/tag/v0.53.0
@@ -17,6 +54,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.52.0...v0.53.0
17
54
  - Runtime Metrics: Global VM cache statistics ([#1680][])
18
55
  - Automatically send traces to agent Unix socket if present ([#1700][])
19
56
  - CI-App: User Provided Git Metadata ([#1662][])
57
+ - ActionMailer integration ([#1280][])
20
58
 
21
59
  ### Changed
22
60
 
@@ -475,26 +513,26 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.39.0...v0.40.0
475
513
  ### Fixed
476
514
 
477
515
  - Sinatra nested modular applications possibly leaking spans ([#1035][], [#1145][])
478
-
516
+
479
517
  * **BREAKING** for nested modular Sinatra applications only:
480
518
  ```ruby
481
519
  class Nested < Sinatra::Base
482
520
  end
483
-
521
+
484
522
  class TopLevel < Sinatra::Base
485
523
  use Nested # Nesting happens here
486
524
  end
487
525
  ```
488
526
  * Non-breaking for classic applications nor modular non-nested applications.
489
-
527
+
490
528
  Fixes issues introduced by [#1015][] (in 0.35.0), when we first introduced Sinatra support for modular applications.
491
-
529
+
492
530
  The main issue we had to solve for modular support is how to handle nested applications, as only one application is actually responsible for handling the route. A naive implementation would cause the creation of nested `sinatra.request` spans, even for applications that did not handle the request. This is technically correct, as Sinatra is traversing that middleware, accruing overhead, but that does not aligned with our existing behavior of having a single `sinatra.request` span.
493
-
531
+
494
532
  While trying to achieve backwards-compatibility, we had to resort to a solution that turned out brittle: `sinatra.request` spans had to start in one middleware level and finished it in another. This allowed us to only capture the `sinatra.request` for the matching route, and skip the non-matching one. This caused unexpected issues on some user setups, specially around Sinatra middleware that created spans in between the initialization and closure of `sinatra.request` spans.
495
-
533
+
496
534
  This change now address these implementation issues by creating multiple `sinatra.request`, one for each traversed Sinatra application, even non-matching ones. This instrumentation is more correct, but at the cost of being a breaking change for nested modular applications.
497
-
535
+
498
536
  Please see [#1145][] for more information, and example screenshots on how traces for affected applications will look like.
499
537
 
500
538
  - Rack/Rails span error propagation with `rescue_from` ([#1155][], [#1162][])
@@ -1350,7 +1388,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.14.1...v0.14.2
1350
1388
 
1351
1389
  ### Fixed
1352
1390
 
1353
- - Sampling priority from request headers not being used ([#521][])
1391
+ - Sampling priority from request headers not being used ([#521][])
1354
1392
 
1355
1393
  ## [0.14.1] - 2018-08-21
1356
1394
 
@@ -1496,9 +1534,9 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.12.1...v0.13.0
1496
1534
  - Hash quantization into core library ([#410][])
1497
1535
  - MongoDB integration to use Hash quantization library ([#463][])
1498
1536
 
1499
- ### Changed
1537
+ ### Changed
1500
1538
 
1501
- - Hash quantization truncates arrays with nested objects ([#463][])
1539
+ - Hash quantization truncates arrays with nested objects ([#463][])
1502
1540
 
1503
1541
  ## [0.13.0.beta1] - 2018-05-09
1504
1542
 
@@ -1847,7 +1885,8 @@ Release notes: https://github.com/DataDog/dd-trace-rb/releases/tag/v0.3.1
1847
1885
 
1848
1886
  Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
1849
1887
 
1850
- [Unreleased]: https://github.com/DataDog/dd-trace-rb/compare/v0.53.0...master
1888
+ [Unreleased]: https://github.com/DataDog/dd-trace-rb/compare/v0.54.0...master
1889
+ [0.54.0]: https://github.com/DataDog/dd-trace-rb/compare/v0.53.0...v0.54.0
1851
1890
  [0.53.0]: https://github.com/DataDog/dd-trace-rb/compare/v0.52.0...v0.53.0
1852
1891
  [0.52.0]: https://github.com/DataDog/dd-trace-rb/compare/v0.51.1...v0.52.0
1853
1892
  [0.51.1]: https://github.com/DataDog/dd-trace-rb/compare/v0.51.0...v0.51.1
@@ -2439,6 +2478,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2439
2478
  [#1277]: https://github.com/DataDog/dd-trace-rb/issues/1277
2440
2479
  [#1278]: https://github.com/DataDog/dd-trace-rb/issues/1278
2441
2480
  [#1279]: https://github.com/DataDog/dd-trace-rb/issues/1279
2481
+ [#1280]: https://github.com/DataDog/dd-trace-rb/issues/1280
2442
2482
  [#1281]: https://github.com/DataDog/dd-trace-rb/issues/1281
2443
2483
  [#1283]: https://github.com/DataDog/dd-trace-rb/issues/1283
2444
2484
  [#1284]: https://github.com/DataDog/dd-trace-rb/issues/1284
@@ -2516,6 +2556,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2516
2556
  [#1420]: https://github.com/DataDog/dd-trace-rb/issues/1420
2517
2557
  [#1421]: https://github.com/DataDog/dd-trace-rb/issues/1421
2518
2558
  [#1422]: https://github.com/DataDog/dd-trace-rb/issues/1422
2559
+ [#1423]: https://github.com/DataDog/dd-trace-rb/issues/1423
2519
2560
  [#1426]: https://github.com/DataDog/dd-trace-rb/issues/1426
2520
2561
  [#1427]: https://github.com/DataDog/dd-trace-rb/issues/1427
2521
2562
  [#1428]: https://github.com/DataDog/dd-trace-rb/issues/1428
@@ -2601,12 +2642,34 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2601
2642
  [#1684]: https://github.com/DataDog/dd-trace-rb/issues/1684
2602
2643
  [#1685]: https://github.com/DataDog/dd-trace-rb/issues/1685
2603
2644
  [#1687]: https://github.com/DataDog/dd-trace-rb/issues/1687
2645
+ [#1688]: https://github.com/DataDog/dd-trace-rb/issues/1688
2604
2646
  [#1694]: https://github.com/DataDog/dd-trace-rb/issues/1694
2605
2647
  [#1695]: https://github.com/DataDog/dd-trace-rb/issues/1695
2606
2648
  [#1699]: https://github.com/DataDog/dd-trace-rb/issues/1699
2607
2649
  [#1700]: https://github.com/DataDog/dd-trace-rb/issues/1700
2608
2650
  [#1702]: https://github.com/DataDog/dd-trace-rb/issues/1702
2651
+ [#1703]: https://github.com/DataDog/dd-trace-rb/issues/1703
2609
2652
  [#1706]: https://github.com/DataDog/dd-trace-rb/issues/1706
2653
+ [#1709]: https://github.com/DataDog/dd-trace-rb/issues/1709
2654
+ [#1712]: https://github.com/DataDog/dd-trace-rb/issues/1712
2655
+ [#1713]: https://github.com/DataDog/dd-trace-rb/issues/1713
2656
+ [#1714]: https://github.com/DataDog/dd-trace-rb/issues/1714
2657
+ [#1715]: https://github.com/DataDog/dd-trace-rb/issues/1715
2658
+ [#1717]: https://github.com/DataDog/dd-trace-rb/issues/1717
2659
+ [#1718]: https://github.com/DataDog/dd-trace-rb/issues/1718
2660
+ [#1719]: https://github.com/DataDog/dd-trace-rb/issues/1719
2661
+ [#1720]: https://github.com/DataDog/dd-trace-rb/issues/1720
2662
+ [#1721]: https://github.com/DataDog/dd-trace-rb/issues/1721
2663
+ [#1735]: https://github.com/DataDog/dd-trace-rb/issues/1735
2664
+ [#1740]: https://github.com/DataDog/dd-trace-rb/issues/1740
2665
+ [#1743]: https://github.com/DataDog/dd-trace-rb/issues/1743
2666
+ [#1745]: https://github.com/DataDog/dd-trace-rb/issues/1745
2667
+ [#1761]: https://github.com/DataDog/dd-trace-rb/issues/1761
2668
+ [#1762]: https://github.com/DataDog/dd-trace-rb/issues/1762
2669
+ [#1763]: https://github.com/DataDog/dd-trace-rb/issues/1763
2670
+ [#1769]: https://github.com/DataDog/dd-trace-rb/issues/1769
2671
+ [#1770]: https://github.com/DataDog/dd-trace-rb/issues/1770
2672
+ [#1771]: https://github.com/DataDog/dd-trace-rb/issues/1771
2610
2673
  [@AdrianLC]: https://github.com/AdrianLC
2611
2674
  [@Azure7111]: https://github.com/Azure7111
2612
2675
  [@BabyGroot]: https://github.com/BabyGroot
@@ -2669,6 +2732,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2669
2732
  [@gkampjes]: https://github.com/gkampjes
2670
2733
  [@gottfrois]: https://github.com/gottfrois
2671
2734
  [@guizmaii]: https://github.com/guizmaii
2735
+ [@hatstand]: https://github.com/hatstand
2672
2736
  [@hawknewton]: https://github.com/hawknewton
2673
2737
  [@hs-bguven]: https://github.com/hs-bguven
2674
2738
  [@illdelph]: https://github.com/illdelph
@@ -2678,6 +2742,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2678
2742
  [@jfrancoist]: https://github.com/jfrancoist
2679
2743
  [@joeyAghion]: https://github.com/joeyAghion
2680
2744
  [@jpaulgs]: https://github.com/jpaulgs
2745
+ [@justinhoward]: https://github.com/justinhoward
2681
2746
  [@jvalanen]: https://github.com/jvalanen
2682
2747
  [@kelvin-acosta]: https://github.com/kelvin-acosta
2683
2748
  [@kexoth]: https://github.com/kexoth
@@ -2715,6 +2780,7 @@ Git diff: https://github.com/DataDog/dd-trace-rb/compare/v0.3.0...v0.3.1
2715
2780
  [@senny]: https://github.com/senny
2716
2781
  [@shayonj]: https://github.com/shayonj
2717
2782
  [@sinsoku]: https://github.com/sinsoku
2783
+ [@skcc321]: https://github.com/skcc321
2718
2784
  [@soulcutter]: https://github.com/soulcutter
2719
2785
  [@stefanahman]: https://github.com/stefanahman
2720
2786
  [@steveh]: https://github.com/steveh
data/ddtrace.gemspec CHANGED
@@ -46,8 +46,11 @@ Gem::Specification.new do |spec|
46
46
  spec.add_dependency 'msgpack', '< 1.4'
47
47
  end
48
48
 
49
- # Used by the profiler
50
- spec.add_dependency 'ffi', '~> 1.0'
49
+ # Used by the profiler native extension to support older Rubies (see NativeExtensionDesign.md for notes)
50
+ #
51
+ # Because we only use this for older Rubies, and we consider it "feature-complete" for those older Rubies,
52
+ # we're pinning it at the latest available version and will manually bump the dependency as needed.
53
+ spec.add_dependency 'debase-ruby_core_source', '= 0.10.12'
51
54
 
52
55
  spec.extensions = ['ext/ddtrace_profiling_native_extension/extconf.rb']
53
56
  end
@@ -135,13 +135,18 @@ To contribute, check out the [contribution guidelines][contribution docs] and [d
135
135
 
136
136
  *EOL* indicates support is no longer provided.
137
137
 
138
+ ### Microsoft Windows support
139
+
140
+ Using `ddtrace ` on Microsoft Windows is currently unsupported. We'll still accept community contributions and issues,
141
+ but will consider them as having low priority.
142
+
138
143
  ## Installation
139
144
 
140
145
  The following steps will help you quickly start tracing your Ruby application.
141
146
 
142
147
  ### Configure the Datadog Agent for APM
143
148
 
144
- Before downloading tracing on your application, install the Datadog Agent. The Ruby APM tracer sends trace data through the Datadog Agent.
149
+ Before downloading tracing on your application, [install the Datadog Agent on the host](https://docs.datadoghq.com/agent/). The Ruby APM tracer sends trace data through the Datadog Agent.
145
150
 
146
151
  Install and configure the Datadog Agent to receive traces from your now instrumented application. By default the Datadog Agent is enabled in your `datadog.yaml` file under `apm_enabled: true` and listens for trace traffic at `localhost:8126`. For containerized environments, follow the steps below to enable trace collection within the Datadog Agent.
147
152
 
@@ -540,7 +545,7 @@ Where `options` is an optional `Hash` that accepts the following parameters:
540
545
  Most of the time, Active Job is set up as part of Rails, but it can be activated separately:
541
546
 
542
547
  ```ruby
543
- require 'activejob'
548
+ require 'active_job'
544
549
  require 'ddtrace'
545
550
 
546
551
  Datadog.configure do |c|
@@ -1217,6 +1222,37 @@ Where `options` is an optional `Hash` that accepts the following parameters:
1217
1222
  | `quantize` | Hash containing options for quantization. May include `:show` with an Array of keys to not quantize (or `:all` to skip quantization), or `:exclude` with Array of keys to exclude entirely. | `{ show: [:collection, :database, :operation] }` |
1218
1223
  | `service_name` | Service name used for `mongo` instrumentation | `'mongodb'` |
1219
1224
 
1225
+ **Configuring trace settings per connection**
1226
+
1227
+ You can configure trace settings per connection by using the `describes` option:
1228
+
1229
+ ```ruby
1230
+ # Provide a `:describes` option with a connection key.
1231
+ # Any of the following keys are acceptable and equivalent to one another.
1232
+ # If a block is provided, it yields a Settings object that
1233
+ # accepts any of the configuration options listed above.
1234
+
1235
+ Datadog.configure do |c|
1236
+ # Network connection string
1237
+ c.use :mongo, describes: '127.0.0.1:27017', service_name: 'mongo-primary'
1238
+
1239
+ # Network connection regular expression
1240
+ c.use :mongo, describes: /localhost.*/, service_name: 'mongo-secondary'
1241
+ end
1242
+
1243
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'artists')
1244
+ collection = client[:people]
1245
+ collection.insert_one({ name: 'Steve' })
1246
+ # Traced call will belong to `mongo-primary` service
1247
+
1248
+ client = Mongo::Client.new([ 'localhost:27017' ], :database => 'artists')
1249
+ collection = client[:people]
1250
+ collection.insert_one({ name: 'Steve' })
1251
+ # Traced call will belong to `mongo-secondary` service
1252
+ ```
1253
+
1254
+ When multiple `describes` configurations match a connection, the latest configured rule that matches will be applied.
1255
+
1220
1256
  ### MySQL2
1221
1257
 
1222
1258
  The MySQL2 integration traces any SQL command sent through `mysql2` gem.
@@ -1471,6 +1507,7 @@ Where `options` is an optional `Hash` that accepts the following parameters:
1471
1507
  | `database_service` | Database service name used when tracing database activity | `'<app_name>-<adapter_name>'` |
1472
1508
  | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) so that this service trace is connected with a trace of another service if tracing headers are received | `true` |
1473
1509
  | `exception_controller` | Class or Module which identifies a custom exception controller class. Tracer provides improved error behavior when it can identify custom exception controllers. By default, without this option, it 'guesses' what a custom exception controller looks like. Providing this option aids this identification. | `nil` |
1510
+ | `job_service` | Service name used when tracing ActiveJob activity. | `<app_name>-active_job` |
1474
1511
  | `middleware` | Add the trace middleware to the Rails application. Set to `false` if you don't want the middleware to load. | `true` |
1475
1512
  | `middleware_names` | Enables any short-circuited middleware requests to display the middleware name as a resource for the trace. | `false` |
1476
1513
  | `service_name` | Service name used when tracing application requests (on the `rack` level) | `'<app_name>'` (inferred from your Rails application namespace) |
@@ -2461,7 +2498,7 @@ The tracer and its integrations can produce some additional metrics that can pro
2461
2498
  To configure your application for metrics collection:
2462
2499
 
2463
2500
  1. [Configure your Datadog agent for StatsD](https://docs.datadoghq.com/developers/dogstatsd/#setup)
2464
- 2. Add `gem 'dogstatsd-ruby', '~> 5.2'` to your Gemfile
2501
+ 2. Add `gem 'dogstatsd-ruby', '~> 5.3'` to your Gemfile
2465
2502
 
2466
2503
  #### For application runtime
2467
2504
 
@@ -97,9 +97,9 @@ The profiler backend links a trace covering a given time interval to the profile
97
97
  whenever they share the same `runtime-id`.
98
98
 
99
99
  To further enable filtering of a profile to show only samples related to a given trace/span, each sample taken by the
100
- profiler is tagged with the trace_id and span_id for the given trace/span.
100
+ profiler is tagged with the `local root span id` and `span id` for the given trace/span.
101
101
 
102
- This is done using the `Datadog::Profiling::TraceIdentifiers::Helper` that retrieves a trace_id and span_id, if
102
+ This is done using the `Datadog::Profiling::TraceIdentifiers::Helper` that retrieves a `root_span_id` and `span_id`, if
103
103
  available, from the supported tracers. This helper is called by the `Collectors::Stack` during sampling.
104
104
 
105
105
  Note that if a given trace executes too fast, it's possible that the profiler will not contain any samples for that
@@ -0,0 +1,86 @@
1
+ # Profiling Native Extension Design
2
+
3
+ The profiling native extension is used to implement features which are expensive (in terms of resources) or otherwise
4
+ impossible to implement using Ruby code.
5
+
6
+ This extension is quite coupled with MRI Ruby ("C Ruby") internals, and is not intended to support other rubies such as
7
+ JRuby or TruffleRuby. When below we say "Ruby", read it as "MRI Ruby".
8
+
9
+ ## Disabling
10
+
11
+ The profiling native extension can be disabled by setting `DD_PROFILING_NO_EXTENSION=true` when installing or running
12
+ the gem. Setting `DD_PROFILING_NO_EXTENSION` at installation time skips compilation of the extension entirely.
13
+
14
+ (If you're a customer and needed to use this, please tell us why on <https://github.com/DataDog/dd-trace-rb/issues/new>.)
15
+
16
+ Currently the profiler can still "limp along" when the native extension is disabled, but the plan is to require it
17
+ in future releases -- e.g. disabling the extension will disable profiling entirely.
18
+
19
+ ## Safety
20
+
21
+ The profiling native extension is (and must always be) designed to **not cause failures** during gem installation, even
22
+ if some features, Ruby versions, or operating systems are not supported.
23
+
24
+ E.g. the extension must cleanly build on Ruby 2.1 (or the oldest Ruby version we support at the time) on Windows,
25
+ even if at run time it will effectively do nothing for such a setup.
26
+
27
+ We have a CI setup to help validate this, but this is really important to keep in mind when adding to or changing the
28
+ existing codebase.
29
+
30
+ ## Usage of private VM headers
31
+
32
+ To implement some of the features below, we sometimes require access to private Ruby header files (that describe VM
33
+ internal types, structures and functions).
34
+
35
+ Because these private header files are not included in regular Ruby installations, we have two different workarounds:
36
+
37
+ 1. for Ruby versions >= 2.6 we make use use the Ruby private MJIT header
38
+ 2. for Ruby versions < 2.6 (legacy Rubies) we make use of the `debase-ruby_core_source` gem
39
+
40
+ Functions which make use of these headers are defined in the <private_vm_api_acccess.c> file.
41
+
42
+ **Important Note**: Our medium/long-term plan is to stop relying on all private Ruby headers, and instead request and
43
+ contribute upstream changes so that they become official public VM APIs.
44
+
45
+ ### Approach 1: Using the Ruby private MJIT header
46
+
47
+ Ruby versions >= 2.6 introduced a JIT compiler called MJIT. This compiler does not directly generate machine code;
48
+ instead it generates C code and uses the system C compiler to turn it into machine code.
49
+
50
+ The generated C code `#include`s a private header -- which we reference as "the MJIT header" everywhere.
51
+ The MJIT header gets shipped with all MJIT-enabled Rubies and includes the layout of many internal VM structures;
52
+ and of course the intention is that it is only used by the Ruby MJIT compiler.
53
+
54
+ This header is placed inside the `include/` directory in a Ruby installation, and is named for that specific Ruby
55
+ version. e.g. `rb_mjit_min_header-2.7.4.h`.
56
+
57
+ ### Approach 2: Using the `debase-ruby_core_source` gem
58
+
59
+ The [`debase-ruby_core_source`](https://github.com/ruby-debug/debase-ruby_core_source) contains almost no code;
60
+ instead, it just contains per-Ruby-version folders with the private VM headers (`.h`) files for that version.
61
+
62
+ Thus, even though a regular Ruby installation does not include these files, we can access the copy inside this gem.
63
+
64
+ ## Feature: Getting thread CPU-time clock_ids
65
+
66
+ * **OS support**: Linux
67
+ * **Ruby support**: 2.6+
68
+
69
+ To enable CPU-time profiling, we use the `pthread_getcpuclockid(pthread_t thread, clockid_t *clockid)` C function to
70
+ obtain a `clockid_t` that can then be used with the `Process.clock_gettime` method (or directly with the
71
+ `clock_gettime()` C function).
72
+
73
+ The challenge with using `pthread_getcpuclockid()` is that we need to get the `pthread_t` for a given Ruby `Thread`
74
+ object. We previously did this with a weird combination of monkey patching and `pthread_self()` (effectively patching
75
+ every `Thread` to run `pthread_self()` at initialization time and stash that value somewhere), but this had a number
76
+ of downsides.
77
+
78
+ The approach we use in the profiling native extension is to reach inside the internal structure of the `Thread` object,
79
+ and extract the `pthread_t` that Ruby itself keeps, but does not expose. This is implemented in the `pthread_id_for()`
80
+ function in `private_vm_api_acccess.c`. Thus, using this trick we can at any point in execution go from a `Thread`
81
+ object into the `clockid_t` that we need.
82
+
83
+ Note that `pthread_getcpuclockid()` is not available on macOS (nor, obviously, on Windows), and hence this feature
84
+ is currently Linux-specific. Thus, in the <clock_id_from_pthread.c> file we implement the feature for supported Ruby
85
+ setups but if something is missing we instead compile in <clock_id_noop.c> that includes a no-op implementation of the
86
+ feature.
@@ -0,0 +1,4 @@
1
+ #pragma once
2
+
3
+ void self_test_clock_id();
4
+ VALUE clock_id_for(VALUE self, VALUE thread);
@@ -0,0 +1,52 @@
1
+ #include "extconf.h"
2
+
3
+ // This file is only compiled on systems where pthread_getcpuclockid() is available;
4
+ // Otherwise we compile clock_id_noop.c
5
+ #ifdef HAVE_PTHREAD_GETCPUCLOCKID
6
+
7
+ #include <pthread.h>
8
+ #include <time.h>
9
+ #include <errno.h>
10
+
11
+ #include <ruby.h>
12
+
13
+ #ifdef RUBY_2_1_WORKAROUND
14
+ #include <thread_native.h>
15
+ #else
16
+ #include <ruby/thread_native.h>
17
+ #endif
18
+
19
+ #include "private_vm_api_access.h"
20
+
21
+ #include "clock_id.h"
22
+
23
+ // Validate that our home-cooked pthread_id_for() matches pthread_self() for the current thread
24
+ void self_test_clock_id() {
25
+ rb_nativethread_id_t expected_pthread_id = pthread_self();
26
+ rb_nativethread_id_t actual_pthread_id = pthread_id_for(rb_thread_current());
27
+
28
+ if (expected_pthread_id != actual_pthread_id) rb_raise(rb_eRuntimeError, "pthread_id_for() self-test failed");
29
+ }
30
+
31
+ VALUE clock_id_for(VALUE self, VALUE thread) {
32
+ rb_nativethread_id_t thread_id = pthread_id_for(thread);
33
+
34
+ clockid_t clock_id;
35
+ int error = pthread_getcpuclockid(thread_id, &clock_id);
36
+
37
+ if (error == 0) {
38
+ return CLOCKID2NUM(clock_id);
39
+ } else {
40
+ switch(error) {
41
+ // The more specific error messages are based on the pthread_getcpuclockid(3) man page
42
+ case ENOENT:
43
+ rb_exc_raise(rb_syserr_new(error, "Failed to get clock_id for given thread: Per-thread CPU time clocks are not supported by the system."));
44
+ case ESRCH:
45
+ rb_exc_raise(rb_syserr_new(error, "Failed to get clock_id for given thread: No thread could be found."));
46
+ default:
47
+ rb_exc_raise(rb_syserr_new(error, "Failed to get clock_id for given thread"));
48
+ }
49
+ }
50
+ }
51
+
52
+ #endif
@@ -0,0 +1,14 @@
1
+ #include "extconf.h"
2
+
3
+ // This file is the dual of clock_id_from_pthread.c for systems where that info
4
+ // is not available.
5
+ #ifndef HAVE_PTHREAD_GETCPUCLOCKID
6
+
7
+ #include <ruby.h>
8
+
9
+ #include "clock_id.h"
10
+
11
+ void self_test_clock_id() { } // Nothing to check
12
+ VALUE clock_id_for(VALUE self, VALUE thread) { return Qnil; } // Nothing to return
13
+
14
+ #endif
@@ -1,28 +1,136 @@
1
- # typed: false
1
+ # typed: ignore
2
+
2
3
  def skip_building_extension?
3
4
  # We don't support JRuby for profiling, and JRuby doesn't support native extensions, so let's just skip this entire
4
5
  # thing so that JRuby users of dd-trace-rb aren't impacted.
5
6
  on_jruby = RUBY_ENGINE == 'jruby'
6
7
 
8
+ # We don't officially support TruffleRuby for dd-trace-rb at all BUT let's not break adventurous customers that
9
+ # want to give it a try.
10
+ on_truffleruby = RUBY_ENGINE == 'truffleruby'
11
+
12
+ # Microsoft Windows is unsupported, so let's not build the extension there.
13
+ on_windows = Gem.win_platform?
14
+
7
15
  # Experimental toggle to disable building the extension.
8
16
  # Disabling the extension will lead to the profiler not working in future releases.
9
17
  # If you needed to use this, please tell us why on <https://github.com/DataDog/dd-trace-rb/issues/new>.
10
18
  disabled_via_env = ENV['DD_PROFILING_NO_EXTENSION'].to_s.downcase == 'true'
11
19
 
12
- on_jruby || disabled_via_env
20
+ on_jruby || on_truffleruby || on_windows || disabled_via_env
21
+ end
22
+
23
+ # IMPORTANT: When adding flags, remember that our customers compile with a wide range of gcc/clang versions, so
24
+ # doublecheck that what you're adding can be reasonably expected to exist on their systems.
25
+ def add_compiler_flag(flag)
26
+ $CFLAGS << ' ' << flag
13
27
  end
14
28
 
15
29
  if skip_building_extension?
30
+ # rubocop:disable Style/StderrPuts
31
+ $stderr.puts(%(
32
+ +------------------------------------------------------------------------------+
33
+ | Skipping build of profiling native extension and replacing it with a no-op |
34
+ | Makefile |
35
+ +------------------------------------------------------------------------------+
36
+
37
+ ))
38
+
16
39
  File.write('Makefile', 'all install clean: # dummy makefile that does nothing')
17
40
  return
18
41
  end
19
42
 
43
+ $stderr.puts(%(
44
+ +------------------------------------------------------------------------------+
45
+ | **Preparing to build the ddtrace native extension...** |
46
+ | |
47
+ | If you run into any failures during this step, you can set the |
48
+ | `DD_PROFILING_NO_EXTENSION` environment variable to `true` e.g. |
49
+ | `$ DD_PROFILING_NO_EXTENSION=true bundle install` to skip this step. |
50
+ | |
51
+ | Disabling the extension will lead to the ddtrace profiling features not |
52
+ | working in future releases. |
53
+ | If you needed to use this, please tell us why on |
54
+ | <https://github.com/DataDog/dd-trace-rb/issues/new> so we can fix it :\) |
55
+ | |
56
+ | Thanks for using ddtrace! You rock! |
57
+ +------------------------------------------------------------------------------+
58
+
59
+ ))
60
+ # rubocop:enable Style/StderrPuts
61
+
20
62
  # NOTE: we MUST NOT require 'mkmf' before we check the #skip_building_extension? because the require triggers checks
21
63
  # that may fail on an environment not properly setup for building Ruby extensions.
22
64
  require 'mkmf'
23
65
 
66
+ # Gets really noisy when we include the MJIT header, let's omit it
67
+ add_compiler_flag '-Wno-unused-function'
68
+
69
+ # Allow defining variables at any point in a function
70
+ add_compiler_flag '-Wno-declaration-after-statement'
71
+
72
+ # If we forget to include a Ruby header, the function call may still appear to work, but then
73
+ # cause a segfault later. Let's ensure that never happens.
74
+ add_compiler_flag '-Werror-implicit-function-declaration'
75
+
76
+ if RUBY_PLATFORM.include?('linux')
77
+ # Supposedly, the correct way to do this is
78
+ # ```
79
+ # have_library 'pthread'
80
+ # have_func 'pthread_getcpuclockid'
81
+ # ```
82
+ # but it broke the build on Windows and on older Ruby versions (2.1 and 2.2)
83
+ # so instead we just assume that we have the function we need on Linux, and nowhere else
84
+ $defs << '-DHAVE_PTHREAD_GETCPUCLOCKID'
85
+ end
86
+
87
+ # Older Rubies don't have the MJIT header, used by the JIT compiler, so we need to use a different approach
88
+ CAN_USE_MJIT_HEADER = RUBY_VERSION >= '2.6'
89
+
24
90
  # Tag the native extension library with the Ruby version and Ruby platform.
25
91
  # This makes it easier for development (avoids "oops I forgot to rebuild when I switched my Ruby") and ensures that
26
92
  # the wrong library is never loaded.
27
93
  # When requiring, we need to use the exact same string, including the version and the platform.
28
- create_makefile "ddtrace_profiling_native_extension.#{RUBY_VERSION}_#{RUBY_PLATFORM}"
94
+ EXTENSION_NAME = "ddtrace_profiling_native_extension.#{RUBY_VERSION}_#{RUBY_PLATFORM}".freeze
95
+
96
+ if CAN_USE_MJIT_HEADER
97
+ $defs << '-DUSE_MJIT_HEADER'
98
+
99
+ # NOTE: This needs to come after all changes to $defs
100
+ create_header
101
+
102
+ # The MJIT header is always (afaik?) suffixed with the exact Ruby VM version,
103
+ # including patch (e.g. 2.7.2). Thus, we add to the header file a definition
104
+ # containing the exact file, so that it can be used in a #include in the C code.
105
+ header_contents =
106
+ File.read($extconf_h)
107
+ .sub('#endif',
108
+ <<-EXTCONF_H.strip
109
+ #define RUBY_MJIT_HEADER "rb_mjit_min_header-#{RUBY_VERSION}.h"
110
+
111
+ #endif
112
+ EXTCONF_H
113
+ )
114
+ File.open($extconf_h, 'w') { |file| file.puts(header_contents) }
115
+
116
+ create_makefile EXTENSION_NAME
117
+ else
118
+ # On older Rubies, we use the debase-ruby_core_source gem to get access to private VM headers.
119
+ # This gem ships source code copies of these VM headers for the different Ruby VM versions;
120
+ # see https://github.com/ruby-debug/debase-ruby_core_source for details
121
+
122
+ thread_native_for_ruby_2_1 = proc { true }
123
+ if RUBY_VERSION < '2.2'
124
+ # This header became public in Ruby 2.2, but we need to pull it from the private headers folder for 2.1
125
+ thread_native_for_ruby_2_1 = proc { have_header('thread_native.h') }
126
+ $defs << '-DRUBY_2_1_WORKAROUND'
127
+ end
128
+
129
+ create_header
130
+
131
+ require 'debase/ruby_core_source'
132
+ dir_config('ruby') # allow user to pass in non-standard core include directory
133
+
134
+ Debase::RubyCoreSource
135
+ .create_makefile_with_core(proc { have_header('vm_core.h') && thread_native_for_ruby_2_1.call }, EXTENSION_NAME)
136
+ end
@@ -0,0 +1,35 @@
1
+ #include "extconf.h"
2
+
3
+ // This file exports functions used to access private Ruby VM APIs and internals.
4
+ // To do this, it imports a few VM internal (private) headers.
5
+ //
6
+ // **Important Note**: Our medium/long-term plan is to stop relying on all private Ruby headers, and instead request and
7
+ // contribute upstream changes so that they become official public VM APIs.
8
+ //
9
+ // In the meanwhile, be very careful when changing things here :)
10
+
11
+ #ifdef USE_MJIT_HEADER
12
+ // Pick up internal structures from the private Ruby MJIT header file
13
+ #include RUBY_MJIT_HEADER
14
+ #else
15
+ // On older Rubies, use a copy of the VM internal headers shipped in the debase-ruby_core_source gem
16
+ #include <vm_core.h>
17
+ #endif
18
+
19
+ // MRI has a similar rb_thread_ptr() function which we can't call it directly
20
+ // because Ruby does not expose the thread_data_type publicly.
21
+ // Instead, we have our own version of that function, and we lazily initialize the thread_data_type pointer
22
+ // from a known-correct object: the current thread.
23
+ //
24
+ // Note that beyond returning the rb_thread_struct*, rb_check_typeddata() raises an exception
25
+ // if the argument passed in is not actually a `Thread` instance.
26
+ static inline struct rb_thread_struct *thread_struct_from_object(VALUE thread) {
27
+ static const rb_data_type_t *thread_data_type = NULL;
28
+ if (thread_data_type == NULL) thread_data_type = RTYPEDDATA_TYPE(rb_thread_current());
29
+
30
+ return (struct rb_thread_struct *) rb_check_typeddata(thread, thread_data_type);
31
+ }
32
+
33
+ rb_nativethread_id_t pthread_id_for(VALUE thread) {
34
+ return thread_struct_from_object(thread)->thread_id;
35
+ }
@@ -0,0 +1,3 @@
1
+ #pragma once
2
+
3
+ rb_nativethread_id_t pthread_id_for(VALUE thread);