From 0ec6be0881dc980a21d7cf5500d68beace43cf3c Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Fri, 17 Mar 2023 16:37:17 +0000 Subject: [PATCH 1/3] Enable typechecking for `lib/datadog/profiling.rb` --- Steepfile | 3 +- lib/datadog/profiling.rb | 3 +- sig/datadog/profiling.rbs | 37 +++++++++++++++++++ .../collectors/cpu_and_wall_time_worker.rbs | 9 +++++ vendor/rbs/gem/0/gem.rbs | 1 + .../rbs/google-protobuf/0/google-protobuf.rbs | 4 ++ .../protobuf-cucumber/0/protobuf-cucumber.rbs | 2 + 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 sig/datadog/profiling.rbs create mode 100644 sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs create mode 100644 vendor/rbs/google-protobuf/0/google-protobuf.rbs create mode 100644 vendor/rbs/protobuf-cucumber/0/protobuf-cucumber.rbs diff --git a/Steepfile b/Steepfile index 07fc8be7423..b2369ddc722 100644 --- a/Steepfile +++ b/Steepfile @@ -164,7 +164,6 @@ target :ddtrace do ignore 'lib/datadog/opentracer/thread_local_scope.rb' ignore 'lib/datadog/opentracer/thread_local_scope_manager.rb' ignore 'lib/datadog/opentracer/tracer.rb' - ignore 'lib/datadog/profiling.rb' ignore 'lib/datadog/profiling/backtrace_location.rb' ignore 'lib/datadog/profiling/buffer.rb' ignore 'lib/datadog/profiling/collectors/code_provenance.rb' @@ -668,4 +667,6 @@ target :ddtrace do library 'gem' library 'rails' library 'sinatra' + library 'google-protobuf' + library 'protobuf-cucumber' end diff --git a/lib/datadog/profiling.rb b/lib/datadog/profiling.rb index 33596bbe1e4..d54676f32c8 100644 --- a/lib/datadog/profiling.rb +++ b/lib/datadog/profiling.rb @@ -113,6 +113,7 @@ def self.allocation_count # rubocop:disable Lint/DuplicateMethods, Lint/NestedMe private_class_method def self.protobuf_already_loaded? defined?(::Google::Protobuf) && !defined?(::Protobuf) + !!(defined?(::Google::Protobuf) && !defined?(::Protobuf)) end private_class_method def self.protobuf_failed_to_load? @@ -157,7 +158,7 @@ def self.allocation_count # rubocop:disable Lint/DuplicateMethods, Lint/NestedMe unless success if exception 'There was an error loading the profiling native extension due to ' \ - "'#{exception.class.name} #{exception.message}' at '#{exception.backtrace.first}'" + "'#{exception.class.name} #{exception.message}' at '#{Array(exception.backtrace).first}'" else 'The profiling native extension did not load correctly. ' \ 'For help solving this issue, please contact Datadog support at .' \ diff --git a/sig/datadog/profiling.rbs b/sig/datadog/profiling.rbs new file mode 100644 index 00000000000..350f7972f5e --- /dev/null +++ b/sig/datadog/profiling.rbs @@ -0,0 +1,37 @@ +module Datadog + module Profiling + GOOGLE_PROTOBUF_MINIMUM_VERSION: ::Gem::Version + + self.@protobuf_loaded: bool + + def self.supported?: () -> bool + + def self.unsupported_reason: () -> ::String? + + def self.start_if_enabled: () -> bool + + def self.allocation_count: () -> ::Integer? + + def self.replace_noop_allocation_count: () -> void + + def self.native_library_compilation_skipped?: () -> ::String? + + def self.try_reading_skipped_reason_file: (?untyped file_api) -> ::String? + + def self.protobuf_gem_unavailable?: () -> ::String? + + def self.protobuf_version_unsupported?: () -> ::String? + + def self.protobuf_already_loaded?: () -> bool + + def self.protobuf_failed_to_load?: () -> ::String? + + def self.protobuf_loaded_successfully?: () -> bool + + def self.native_library_failed_to_load?: () -> ::String? + + def self.try_loading_native_library: () -> [bool, ::Exception?] + + def self.load_profiling: () -> bool + end +end diff --git a/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs new file mode 100644 index 00000000000..1818ad980a7 --- /dev/null +++ b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs @@ -0,0 +1,9 @@ +module Datadog + module Profiling + module Collectors + class CpuAndWallTimeWorker + def self._native_allocation_count: () -> ::Integer? + end + end + end +end diff --git a/vendor/rbs/gem/0/gem.rbs b/vendor/rbs/gem/0/gem.rbs index 35ef94ca87b..18607f633f0 100644 --- a/vendor/rbs/gem/0/gem.rbs +++ b/vendor/rbs/gem/0/gem.rbs @@ -11,4 +11,5 @@ end class Gem::BasicSpecification def platform: () -> Gem::Platform + def version: () -> String end diff --git a/vendor/rbs/google-protobuf/0/google-protobuf.rbs b/vendor/rbs/google-protobuf/0/google-protobuf.rbs new file mode 100644 index 00000000000..5bba81afffe --- /dev/null +++ b/vendor/rbs/google-protobuf/0/google-protobuf.rbs @@ -0,0 +1,4 @@ +module Google + module Protobuf + end +end diff --git a/vendor/rbs/protobuf-cucumber/0/protobuf-cucumber.rbs b/vendor/rbs/protobuf-cucumber/0/protobuf-cucumber.rbs new file mode 100644 index 00000000000..0c5bc869255 --- /dev/null +++ b/vendor/rbs/protobuf-cucumber/0/protobuf-cucumber.rbs @@ -0,0 +1,2 @@ +module Protobuf +end From 0b278907a6ad1955fda50da20dbcb46e634c2a05 Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Fri, 17 Mar 2023 19:06:16 +0000 Subject: [PATCH 2/3] Add type signatures for a bunch of profiling classes **What does this PR do?**: This PR adds type signatures and enables type checking for a number of profiling classes (see `Steepfile` for list of files that are no longer ignored). **Motivation**: This was discussed/requested in #2697. **Additional Notes**: I wish the steep errors were a bit more user-friendly. The `StackRecorder#clear` method actually should not exist anymore, and would actually break because it called `_native_clear` which was deleted in #2367. (Ooops) Other than that, there's a few minor changes to code to make code more type-checkable, but nothing of notice. **How to test the change?**: Validate that CI is still green and typechecking passes. --- Steepfile | 12 ---- .../collectors/idle_sampling_helper.rb | 2 +- lib/datadog/profiling/component.rb | 2 +- lib/datadog/profiling/ext.rb | 54 ++++++++-------- lib/datadog/profiling/stack_recorder.rb | 18 ------ sig/datadog/core/buffer/random.rbs | 8 +++ sig/datadog/core/buffer/thread_safe.rbs | 8 +++ sig/datadog/core/configuration.rbs | 2 + sig/datadog/core/configuration/components.rbs | 10 +-- sig/datadog/core/worker.rbs | 6 ++ sig/datadog/core/workers/polling.rbs | 21 +++++++ .../profiling/collectors/code_provenance.rbs | 10 +++ .../collectors/cpu_and_wall_time_worker.rbs | 10 +++ .../collectors/dynamic_sampling_rate.rbs | 8 +++ .../collectors/idle_sampling_helper.rbs | 28 +++++++++ .../profiling/collectors/old_stack.rbs | 23 +++++++ sig/datadog/profiling/collectors/stack.rbs | 8 +++ .../profiling/collectors/thread_context.rbs | 32 ++++++++++ sig/datadog/profiling/component.rbs | 33 ++++++++++ sig/datadog/profiling/event.rbs | 6 ++ sig/datadog/profiling/events/stack.rbs | 11 ++++ sig/datadog/profiling/exporter.rbs | 40 ++++++++++++ sig/datadog/profiling/ext.rbs | 41 +++++++++++++ sig/datadog/profiling/flush.rbs | 25 ++++++++ sig/datadog/profiling/http_transport.rbs | 61 +++++++++++++++++++ .../profiling/load_native_extension.rbs | 2 +- sig/datadog/profiling/old_recorder.rbs | 9 +++ sig/datadog/profiling/preload.rbs | 0 sig/datadog/profiling/profiler.rbs | 20 ++++++ sig/datadog/profiling/scheduler.rbs | 19 ++++++ sig/datadog/profiling/stack_recorder.rbs | 27 ++++++++ sig/datadog/profiling/tag_builder.rbs | 7 +++ sig/datadog/profiling/tasks/setup.rbs | 9 +++ .../profiling/trace_identifiers/ddtrace.rbs | 8 +++ .../profiling/trace_identifiers/helper.rbs | 16 +++++ 35 files changed, 528 insertions(+), 68 deletions(-) create mode 100644 sig/datadog/core/buffer/random.rbs create mode 100644 sig/datadog/core/buffer/thread_safe.rbs create mode 100644 sig/datadog/core/worker.rbs create mode 100644 sig/datadog/core/workers/polling.rbs create mode 100644 sig/datadog/profiling/collectors/code_provenance.rbs create mode 100644 sig/datadog/profiling/collectors/dynamic_sampling_rate.rbs create mode 100644 sig/datadog/profiling/collectors/idle_sampling_helper.rbs create mode 100644 sig/datadog/profiling/collectors/old_stack.rbs create mode 100644 sig/datadog/profiling/collectors/stack.rbs create mode 100644 sig/datadog/profiling/collectors/thread_context.rbs create mode 100644 sig/datadog/profiling/component.rbs create mode 100644 sig/datadog/profiling/event.rbs create mode 100644 sig/datadog/profiling/events/stack.rbs create mode 100644 sig/datadog/profiling/exporter.rbs create mode 100644 sig/datadog/profiling/ext.rbs create mode 100644 sig/datadog/profiling/flush.rbs create mode 100644 sig/datadog/profiling/http_transport.rbs create mode 100644 sig/datadog/profiling/old_recorder.rbs create mode 100644 sig/datadog/profiling/preload.rbs create mode 100644 sig/datadog/profiling/profiler.rbs create mode 100644 sig/datadog/profiling/scheduler.rbs create mode 100644 sig/datadog/profiling/stack_recorder.rbs create mode 100644 sig/datadog/profiling/tag_builder.rbs create mode 100644 sig/datadog/profiling/tasks/setup.rbs create mode 100644 sig/datadog/profiling/trace_identifiers/ddtrace.rbs create mode 100644 sig/datadog/profiling/trace_identifiers/helper.rbs diff --git a/Steepfile b/Steepfile index b2369ddc722..76b85b3d8b3 100644 --- a/Steepfile +++ b/Steepfile @@ -168,20 +168,12 @@ target :ddtrace do ignore 'lib/datadog/profiling/buffer.rb' ignore 'lib/datadog/profiling/collectors/code_provenance.rb' ignore 'lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb' - ignore 'lib/datadog/profiling/collectors/dynamic_sampling_rate.rb' - ignore 'lib/datadog/profiling/collectors/idle_sampling_helper.rb' ignore 'lib/datadog/profiling/collectors/old_stack.rb' ignore 'lib/datadog/profiling/collectors/stack.rb' - ignore 'lib/datadog/profiling/collectors/thread_context.rb' - ignore 'lib/datadog/profiling/component.rb' ignore 'lib/datadog/profiling/encoding/profile.rb' ignore 'lib/datadog/profiling/event.rb' ignore 'lib/datadog/profiling/events/stack.rb' - ignore 'lib/datadog/profiling/exporter.rb' - ignore 'lib/datadog/profiling/ext.rb' ignore 'lib/datadog/profiling/ext/forking.rb' - ignore 'lib/datadog/profiling/flush.rb' - ignore 'lib/datadog/profiling/http_transport.rb' ignore 'lib/datadog/profiling/old_recorder.rb' ignore 'lib/datadog/profiling/pprof/builder.rb' ignore 'lib/datadog/profiling/pprof/converter.rb' @@ -191,16 +183,12 @@ target :ddtrace do ignore 'lib/datadog/profiling/pprof/stack_sample.rb' ignore 'lib/datadog/profiling/pprof/string_table.rb' ignore 'lib/datadog/profiling/pprof/template.rb' - ignore 'lib/datadog/profiling/preload.rb' - ignore 'lib/datadog/profiling/profiler.rb' ignore 'lib/datadog/profiling/scheduler.rb' - ignore 'lib/datadog/profiling/stack_recorder.rb' ignore 'lib/datadog/profiling/tag_builder.rb' ignore 'lib/datadog/profiling/tasks/exec.rb' ignore 'lib/datadog/profiling/tasks/help.rb' ignore 'lib/datadog/profiling/tasks/setup.rb' ignore 'lib/datadog/profiling/trace_identifiers/ddtrace.rb' - ignore 'lib/datadog/profiling/trace_identifiers/helper.rb' ignore 'lib/datadog/tracing.rb' ignore 'lib/datadog/tracing/analytics.rb' ignore 'lib/datadog/tracing/buffer.rb' diff --git a/lib/datadog/profiling/collectors/idle_sampling_helper.rb b/lib/datadog/profiling/collectors/idle_sampling_helper.rb index edbd9af3a0e..b5815890a83 100644 --- a/lib/datadog/profiling/collectors/idle_sampling_helper.rb +++ b/lib/datadog/profiling/collectors/idle_sampling_helper.rb @@ -47,7 +47,7 @@ def start true end - def stop(*_) + def stop(*_unused) @start_stop_mutex.synchronize do Datadog.logger.debug('Requesting IdleSamplingHelper thread shut down') diff --git a/lib/datadog/profiling/component.rb b/lib/datadog/profiling/component.rb index c0514cd3fa4..0c3e09d082b 100644 --- a/lib/datadog/profiling/component.rb +++ b/lib/datadog/profiling/component.rb @@ -2,7 +2,7 @@ module Datadog module Profiling - # Profiling component + # Responsible for wiring up the Profiler for execution module Component # Passing in a `nil` tracer is supported and will disable the following profiling features: # * Code Hotspots panel in the trace viewer, as well as scoping a profile down to a span diff --git a/lib/datadog/profiling/ext.rb b/lib/datadog/profiling/ext.rb index 7c643df0165..1c32f002ecb 100644 --- a/lib/datadog/profiling/ext.rb +++ b/lib/datadog/profiling/ext.rb @@ -1,41 +1,43 @@ +# frozen_string_literal: true + module Datadog module Profiling module Ext - ENV_ENABLED = 'DD_PROFILING_ENABLED'.freeze - ENV_UPLOAD_TIMEOUT = 'DD_PROFILING_UPLOAD_TIMEOUT'.freeze - ENV_MAX_FRAMES = 'DD_PROFILING_MAX_FRAMES'.freeze - ENV_AGENTLESS = 'DD_PROFILING_AGENTLESS'.freeze - ENV_ENDPOINT_COLLECTION_ENABLED = 'DD_PROFILING_ENDPOINT_COLLECTION_ENABLED'.freeze + ENV_ENABLED = 'DD_PROFILING_ENABLED' + ENV_UPLOAD_TIMEOUT = 'DD_PROFILING_UPLOAD_TIMEOUT' + ENV_MAX_FRAMES = 'DD_PROFILING_MAX_FRAMES' + ENV_AGENTLESS = 'DD_PROFILING_AGENTLESS' + ENV_ENDPOINT_COLLECTION_ENABLED = 'DD_PROFILING_ENDPOINT_COLLECTION_ENABLED' # TODO: Consider removing this once the Ruby-based pprof encoding is removed and replaced by libdatadog module Pprof - LABEL_KEY_LOCAL_ROOT_SPAN_ID = 'local root span id'.freeze - LABEL_KEY_SPAN_ID = 'span id'.freeze - LABEL_KEY_THREAD_ID = 'thread id'.freeze - LABEL_KEY_TRACE_ENDPOINT = 'trace endpoint'.freeze + LABEL_KEY_LOCAL_ROOT_SPAN_ID = 'local root span id' + LABEL_KEY_SPAN_ID = 'span id' + LABEL_KEY_THREAD_ID = 'thread id' + LABEL_KEY_TRACE_ENDPOINT = 'trace endpoint' SAMPLE_VALUE_NO_VALUE = 0 - VALUE_TYPE_CPU = 'cpu-time'.freeze - VALUE_TYPE_WALL = 'wall-time'.freeze - VALUE_UNIT_NANOSECONDS = 'nanoseconds'.freeze + VALUE_TYPE_CPU = 'cpu-time' + VALUE_TYPE_WALL = 'wall-time' + VALUE_UNIT_NANOSECONDS = 'nanoseconds' end module Transport module HTTP - FORM_FIELD_TAG_ENV = 'env'.freeze - FORM_FIELD_TAG_HOST = 'host'.freeze - FORM_FIELD_TAG_LANGUAGE = 'language'.freeze - FORM_FIELD_TAG_PID = 'process_id'.freeze - FORM_FIELD_TAG_PROFILER_VERSION = 'profiler_version'.freeze - FORM_FIELD_TAG_RUNTIME = 'runtime'.freeze - FORM_FIELD_TAG_RUNTIME_ENGINE = 'runtime_engine'.freeze - FORM_FIELD_TAG_RUNTIME_ID = 'runtime-id'.freeze - FORM_FIELD_TAG_RUNTIME_PLATFORM = 'runtime_platform'.freeze - FORM_FIELD_TAG_RUNTIME_VERSION = 'runtime_version'.freeze - FORM_FIELD_TAG_SERVICE = 'service'.freeze - FORM_FIELD_TAG_VERSION = 'version'.freeze + FORM_FIELD_TAG_ENV = 'env' + FORM_FIELD_TAG_HOST = 'host' + FORM_FIELD_TAG_LANGUAGE = 'language' + FORM_FIELD_TAG_PID = 'process_id' + FORM_FIELD_TAG_PROFILER_VERSION = 'profiler_version' + FORM_FIELD_TAG_RUNTIME = 'runtime' + FORM_FIELD_TAG_RUNTIME_ENGINE = 'runtime_engine' + FORM_FIELD_TAG_RUNTIME_ID = 'runtime-id' + FORM_FIELD_TAG_RUNTIME_PLATFORM = 'runtime_platform' + FORM_FIELD_TAG_RUNTIME_VERSION = 'runtime_version' + FORM_FIELD_TAG_SERVICE = 'service' + FORM_FIELD_TAG_VERSION = 'version' - PPROF_DEFAULT_FILENAME = 'rubyprofile.pprof'.freeze - CODE_PROVENANCE_FILENAME = 'code-provenance.json'.freeze + PPROF_DEFAULT_FILENAME = 'rubyprofile.pprof' + CODE_PROVENANCE_FILENAME = 'code-provenance.json' end end end diff --git a/lib/datadog/profiling/stack_recorder.rb b/lib/datadog/profiling/stack_recorder.rb index 8f42e93c830..ccbe710dafd 100644 --- a/lib/datadog/profiling/stack_recorder.rb +++ b/lib/datadog/profiling/stack_recorder.rb @@ -48,24 +48,6 @@ def serialize! end end - def clear - status, result = @no_concurrent_synchronize_mutex.synchronize { self.class._native_clear(self) } - - if status == :ok - finish_timestamp = result - - Datadog.logger.debug { "Cleared profile at #{finish_timestamp}" } - - finish_timestamp - else - error_message = result - - Datadog.logger.error("Failed to clear profiling data: #{error_message}") - - nil - end - end - def reset_after_fork self.class._native_reset_after_fork(self) end diff --git a/sig/datadog/core/buffer/random.rbs b/sig/datadog/core/buffer/random.rbs new file mode 100644 index 00000000000..72a00f8541f --- /dev/null +++ b/sig/datadog/core/buffer/random.rbs @@ -0,0 +1,8 @@ +module Datadog + module Core + module Buffer + class Random + end + end + end +end diff --git a/sig/datadog/core/buffer/thread_safe.rbs b/sig/datadog/core/buffer/thread_safe.rbs new file mode 100644 index 00000000000..52f836466df --- /dev/null +++ b/sig/datadog/core/buffer/thread_safe.rbs @@ -0,0 +1,8 @@ +module Datadog + module Core + module Buffer + class ThreadSafe < Datadog::Core::Buffer::Random + end + end + end +end diff --git a/sig/datadog/core/configuration.rbs b/sig/datadog/core/configuration.rbs index 2213cf9418f..c8e569c694b 100644 --- a/sig/datadog/core/configuration.rbs +++ b/sig/datadog/core/configuration.rbs @@ -4,6 +4,8 @@ module Datadog def tracer: () -> Datadog::Tracing::Tracer def logger: () -> Datadog::Core::Logger + + def configuration: () -> Datadog::Core::Configuration::Settings end end end diff --git a/sig/datadog/core/configuration/components.rbs b/sig/datadog/core/configuration/components.rbs index 003d8040ad8..8da3d9621cd 100644 --- a/sig/datadog/core/configuration/components.rbs +++ b/sig/datadog/core/configuration/components.rbs @@ -12,7 +12,7 @@ module Datadog def self.build_tracer: (untyped settings, untyped agent_settings) -> untyped - def self.build_profiler: (untyped settings, untyped agent_settings, untyped tracer) -> (nil | untyped) + def self.build_telemetry: (untyped settings) -> untyped private @@ -22,14 +22,6 @@ module Datadog def self.build_tracer_test_mode_options: (untyped tracer_options, untyped settings, untyped agent_settings) -> untyped - def self.build_profiler_recorder: (untyped settings) -> untyped - - def self.build_profiler_collectors: (untyped settings, untyped recorder, untyped trace_identifiers_helper) -> ::Array[untyped] - - def self.build_profiler_exporters: (untyped settings, untyped agent_settings) -> ::Array[untyped] - - def self.build_profiler_scheduler: (untyped settings, untyped recorder, untyped exporters) -> untyped - public attr_reader health_metrics: untyped diff --git a/sig/datadog/core/worker.rbs b/sig/datadog/core/worker.rbs new file mode 100644 index 00000000000..7c59eb58d60 --- /dev/null +++ b/sig/datadog/core/worker.rbs @@ -0,0 +1,6 @@ +module Datadog + module Core + class Worker + end + end +end diff --git a/sig/datadog/core/workers/polling.rbs b/sig/datadog/core/workers/polling.rbs new file mode 100644 index 00000000000..7f4d8f9c55b --- /dev/null +++ b/sig/datadog/core/workers/polling.rbs @@ -0,0 +1,21 @@ +module Datadog + module Core + module Workers + module Polling + SHUTDOWN_TIMEOUT: 1 + + def self.included: (Class | Module base) -> void + + module PrependedMethods + def perform: (*untyped args) -> untyped + end + + def stop: (?bool force_stop, ?::Integer timeout) -> untyped + + def enabled?: () -> bool + + def enabled=: (bool value) -> bool + end + end + end +end diff --git a/sig/datadog/profiling/collectors/code_provenance.rbs b/sig/datadog/profiling/collectors/code_provenance.rbs new file mode 100644 index 00000000000..e4309b32944 --- /dev/null +++ b/sig/datadog/profiling/collectors/code_provenance.rbs @@ -0,0 +1,10 @@ +module Datadog + module Profiling + module Collectors + class CodeProvenance + def refresh: () -> self + def generate_json: () -> ::String + end + end + end +end diff --git a/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs index 1818ad980a7..9d79f68cb97 100644 --- a/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs +++ b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs @@ -3,6 +3,16 @@ module Datadog module Collectors class CpuAndWallTimeWorker def self._native_allocation_count: () -> ::Integer? + + def initialize: ( + recorder: Datadog::Profiling::StackRecorder, + max_frames: ::Integer, + tracer: Datadog::Tracing::Tracer?, + gc_profiling_enabled: bool, + allocation_counting_enabled: bool, + ?thread_context_collector: Datadog::Profiling::Collectors::ThreadContext, + ?idle_sampling_helper: Datadog::Profiling::Collectors::IdleSamplingHelper, + ) -> void end end end diff --git a/sig/datadog/profiling/collectors/dynamic_sampling_rate.rbs b/sig/datadog/profiling/collectors/dynamic_sampling_rate.rbs new file mode 100644 index 00000000000..56ae90e625e --- /dev/null +++ b/sig/datadog/profiling/collectors/dynamic_sampling_rate.rbs @@ -0,0 +1,8 @@ +module Datadog + module Profiling + module Collectors + module DynamicSamplingRate + end + end + end +end diff --git a/sig/datadog/profiling/collectors/idle_sampling_helper.rbs b/sig/datadog/profiling/collectors/idle_sampling_helper.rbs new file mode 100644 index 00000000000..f5b3ca0ccf1 --- /dev/null +++ b/sig/datadog/profiling/collectors/idle_sampling_helper.rbs @@ -0,0 +1,28 @@ +module Datadog + module Profiling + module Collectors + class IdleSamplingHelper + @worker_thread: untyped + @start_stop_mutex: ::Thread::Mutex + + private + + attr_accessor failure_exception: ::Exception? + + public + + def initialize: () -> void + + def start: () -> (nil | true) + + def stop: (*untyped _unused) -> void + + def self._native_stop: (Datadog::Profiling::Collectors::IdleSamplingHelper self_instance) -> true + + def self._native_reset: (Datadog::Profiling::Collectors::IdleSamplingHelper self_instance) -> true + + def self._native_idle_sampling_loop: (Datadog::Profiling::Collectors::IdleSamplingHelper self_instance) -> true + end + end + end +end diff --git a/sig/datadog/profiling/collectors/old_stack.rbs b/sig/datadog/profiling/collectors/old_stack.rbs new file mode 100644 index 00000000000..16d0b7d558e --- /dev/null +++ b/sig/datadog/profiling/collectors/old_stack.rbs @@ -0,0 +1,23 @@ +module Datadog + module Profiling + module Collectors + class OldStack < Core::Worker + include Core::Workers::Polling + + def initialize: ( + OldRecorder recorder, + max_frames: ::Integer, + trace_identifiers_helper: Datadog::Profiling::TraceIdentifiers::Helper, + ?ignore_thread: ::Proc?, + ?max_time_usage_pct: ::Float, + ?max_threads_sampled: ::Integer, + ?thread_api: untyped, + ?cpu_time_provider: untyped, + ?fork_policy: untyped, + ?interval: ::Float, + ?enabled: bool, + ) -> void + end + end + end +end diff --git a/sig/datadog/profiling/collectors/stack.rbs b/sig/datadog/profiling/collectors/stack.rbs new file mode 100644 index 00000000000..847652f8f5d --- /dev/null +++ b/sig/datadog/profiling/collectors/stack.rbs @@ -0,0 +1,8 @@ +module Datadog + module Profiling + module Collectors + class Stack + end + end + end +end diff --git a/sig/datadog/profiling/collectors/thread_context.rbs b/sig/datadog/profiling/collectors/thread_context.rbs new file mode 100644 index 00000000000..bd22adede79 --- /dev/null +++ b/sig/datadog/profiling/collectors/thread_context.rbs @@ -0,0 +1,32 @@ +module Datadog + module Profiling + module Collectors + class ThreadContext + def initialize: ( + recorder: Datadog::Profiling::StackRecorder, + max_frames: ::Integer, + tracer: Datadog::Tracing::Tracer? + ) -> void + + def self._native_initialize: ( + Datadog::Profiling::Collectors::ThreadContext collector_instance, + Datadog::Profiling::StackRecorder recorder_instance, + ::Integer max_frames, + ::Symbol? tracer_context_key, + ) -> void + + def inspect: () -> ::String + + def self._native_inspect: (Datadog::Profiling::Collectors::ThreadContext collector_instance) -> ::String + + def reset_after_fork: () -> true + + def self._native_reset_after_fork: (Datadog::Profiling::Collectors::ThreadContext collector_instance) -> true + + private + + def safely_extract_context_key_from: (untyped tracer) -> ::Symbol? + end + end + end +end diff --git a/sig/datadog/profiling/component.rbs b/sig/datadog/profiling/component.rbs new file mode 100644 index 00000000000..a2df83544a1 --- /dev/null +++ b/sig/datadog/profiling/component.rbs @@ -0,0 +1,33 @@ +module Datadog + module Profiling + module Component + def self.build_profiler_component: ( + settings: untyped, + agent_settings: Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings, + optional_tracer: Datadog::Tracing::Tracer?, + ) -> Datadog::Profiling::Profiler? + + def self.build_profiler_old_recorder: (untyped settings) -> Datadog::Profiling::OldRecorder + + def self.build_profiler_exporter: ( + untyped settings, + (Datadog::Profiling::StackRecorder | Datadog::Profiling::OldRecorder) recorder, + ) -> Datadog::Profiling::Exporter + + def self.build_profiler_oldstack_collector: ( + untyped settings, + Datadog::Profiling::OldRecorder old_recorder, + Datadog::Tracing::Tracer? tracer, + ) -> Datadog::Profiling::Collectors::OldStack + + def self.build_profiler_transport: ( + untyped settings, + Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings + ) -> untyped + + def self.enable_gc_profiling?: (untyped settings) -> bool + + def self.print_new_profiler_warnings: () -> void + end + end +end diff --git a/sig/datadog/profiling/event.rbs b/sig/datadog/profiling/event.rbs new file mode 100644 index 00000000000..04c587ed7f9 --- /dev/null +++ b/sig/datadog/profiling/event.rbs @@ -0,0 +1,6 @@ +module Datadog + module Profiling + class Event + end + end +end diff --git a/sig/datadog/profiling/events/stack.rbs b/sig/datadog/profiling/events/stack.rbs new file mode 100644 index 00000000000..7cd9dfe74c9 --- /dev/null +++ b/sig/datadog/profiling/events/stack.rbs @@ -0,0 +1,11 @@ +module Datadog + module Profiling + module Events + class Stack < Event + end + + class StackSample < Stack + end + end + end +end diff --git a/sig/datadog/profiling/exporter.rbs b/sig/datadog/profiling/exporter.rbs new file mode 100644 index 00000000000..af43a9b3980 --- /dev/null +++ b/sig/datadog/profiling/exporter.rbs @@ -0,0 +1,40 @@ +module Datadog + module Profiling + class Exporter + PROFILE_DURATION_THRESHOLD_SECONDS: 1 + + private + + attr_reader pprof_recorder: (Datadog::Profiling::StackRecorder | Datadog::Profiling::OldRecorder) + + attr_reader code_provenance_collector: Datadog::Profiling::Collectors::CodeProvenance? + + attr_reader minimum_duration_seconds: ::Integer + + attr_reader time_provider: untyped + + attr_reader last_flush_finish_at: ::Time? + + attr_reader created_at: ::Time + + public + + def initialize: ( + pprof_recorder: (Datadog::Profiling::StackRecorder | Datadog::Profiling::OldRecorder), + code_provenance_collector: Datadog::Profiling::Collectors::CodeProvenance?, + ?minimum_duration_seconds: ::Integer, + ?time_provider: untyped + ) -> void + + def flush: () -> Datadog::Profiling::Flush? + + def can_flush?: () -> bool + + def reset_after_fork: () -> void + + private + + def duration_below_threshold?: (::Time start, ::Time finish) -> bool + end + end +end diff --git a/sig/datadog/profiling/ext.rbs b/sig/datadog/profiling/ext.rbs new file mode 100644 index 00000000000..dc6ee20c3d8 --- /dev/null +++ b/sig/datadog/profiling/ext.rbs @@ -0,0 +1,41 @@ +module Datadog + module Profiling + module Ext + ENV_ENABLED: "DD_PROFILING_ENABLED" + ENV_UPLOAD_TIMEOUT: "DD_PROFILING_UPLOAD_TIMEOUT" + ENV_MAX_FRAMES: "DD_PROFILING_MAX_FRAMES" + ENV_AGENTLESS: "DD_PROFILING_AGENTLESS" + ENV_ENDPOINT_COLLECTION_ENABLED: "DD_PROFILING_ENDPOINT_COLLECTION_ENABLED" + + module Pprof + LABEL_KEY_LOCAL_ROOT_SPAN_ID: "local root span id" + LABEL_KEY_SPAN_ID: "span id" + LABEL_KEY_THREAD_ID: "thread id" + LABEL_KEY_TRACE_ENDPOINT: "trace endpoint" + SAMPLE_VALUE_NO_VALUE: 0 + VALUE_TYPE_CPU: "cpu-time" + VALUE_TYPE_WALL: "wall-time" + VALUE_UNIT_NANOSECONDS: "nanoseconds" + end + + module Transport + module HTTP + FORM_FIELD_TAG_ENV: "env" + FORM_FIELD_TAG_HOST: "host" + FORM_FIELD_TAG_LANGUAGE: "language" + FORM_FIELD_TAG_PID: "process_id" + FORM_FIELD_TAG_PROFILER_VERSION: "profiler_version" + FORM_FIELD_TAG_RUNTIME: "runtime" + FORM_FIELD_TAG_RUNTIME_ENGINE: "runtime_engine" + FORM_FIELD_TAG_RUNTIME_ID: "runtime-id" + FORM_FIELD_TAG_RUNTIME_PLATFORM: "runtime_platform" + FORM_FIELD_TAG_RUNTIME_VERSION: "runtime_version" + FORM_FIELD_TAG_SERVICE: "service" + FORM_FIELD_TAG_VERSION: "version" + PPROF_DEFAULT_FILENAME: "rubyprofile.pprof" + CODE_PROVENANCE_FILENAME: "code-provenance.json" + end + end + end + end +end diff --git a/sig/datadog/profiling/flush.rbs b/sig/datadog/profiling/flush.rbs new file mode 100644 index 00000000000..323ba9e468d --- /dev/null +++ b/sig/datadog/profiling/flush.rbs @@ -0,0 +1,25 @@ +module Datadog + module Profiling + EventGroup: untyped + + class Flush + attr_reader start: ::Time + attr_reader finish: ::Time + attr_reader pprof_file_name: ::String + attr_reader pprof_data: ::String + attr_reader code_provenance_file_name: ::String + attr_reader code_provenance_data: ::String? + attr_reader tags_as_array: Array[[::String, ::String]] + + def initialize: ( + start: ::Time, + finish: ::Time, + pprof_file_name: ::String, + pprof_data: ::String, + code_provenance_file_name: ::String, + code_provenance_data: ::String?, + tags_as_array: Array[[::String, ::String]] + ) -> void + end + end +end diff --git a/sig/datadog/profiling/http_transport.rbs b/sig/datadog/profiling/http_transport.rbs new file mode 100644 index 00000000000..d636ced2139 --- /dev/null +++ b/sig/datadog/profiling/http_transport.rbs @@ -0,0 +1,61 @@ +module Datadog + module Profiling + class HttpTransport + type exporter_configuration_array = [:agentless | :agent, untyped] + + @upload_timeout_milliseconds: ::Integer + @exporter_configuration: exporter_configuration_array + + def initialize: ( + agent_settings: Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings, + site: ::String?, + api_key: ::String?, + upload_timeout_seconds: ::Integer, + ) -> void + + def export: (Datadog::Profiling::Flush flush) -> bool + + def self.log_failure_to_process_tag: (::String failure_details) -> void + + private + + def base_url_from: (Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings) -> ::String + + def validate_agent_settings: (Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings agent_settings) -> void + + def agentless?: (::String? site, ::String? api_key) -> bool + + def validate_exporter: (exporter_configuration_array exporter_configuration) -> [:ok | :error, ::String?] + + def self._native_validate_exporter: (exporter_configuration_array exporter_configuration) -> [:ok | :error, ::String?] + + def do_export: ( + exporter_configuration: exporter_configuration_array, + upload_timeout_milliseconds: ::Integer, + start_timespec_seconds: ::Integer, + start_timespec_nanoseconds: ::Integer, + finish_timespec_seconds: ::Integer, + finish_timespec_nanoseconds: ::Integer, + pprof_file_name: ::String, + pprof_data: ::String, + code_provenance_file_name: ::String, + code_provenance_data: ::String?, + tags_as_array: Array[[::String, ::String]], + ) -> [:ok | :error, ::Integer | ::String] + + def self._native_do_export: ( + exporter_configuration_array exporter_configuration, + ::Integer upload_timeout_milliseconds, + ::Integer start_timespec_seconds, + ::Integer start_timespec_nanoseconds, + ::Integer finish_timespec_seconds, + ::Integer finish_timespec_nanoseconds, + ::String pprof_file_name, + ::String pprof_data, + ::String code_provenance_file_name, + ::String? code_provenance_data, + Array[[::String, ::String]] tags_as_array, + ) -> [:ok | :error, ::Integer | ::String] + end + end +end diff --git a/sig/datadog/profiling/load_native_extension.rbs b/sig/datadog/profiling/load_native_extension.rbs index a4c710049f6..ede9c60bc86 100644 --- a/sig/datadog/profiling/load_native_extension.rbs +++ b/sig/datadog/profiling/load_native_extension.rbs @@ -1,7 +1,7 @@ module Datadog module Profiling module Loader - def self._native_load: (String ruby_path, String ruby_init_name) -> ::Array[untyped] + def self._native_load: (String ruby_path, String ruby_init_name) -> [:ok | :error, ::String?] end end end diff --git a/sig/datadog/profiling/old_recorder.rbs b/sig/datadog/profiling/old_recorder.rbs new file mode 100644 index 00000000000..07a8e6e1ec2 --- /dev/null +++ b/sig/datadog/profiling/old_recorder.rbs @@ -0,0 +1,9 @@ +module Datadog + module Profiling + class OldRecorder + def initialize: (Array[untyped] event_classes, ::Integer max_size, ?last_flush_time: ::Time) -> void + + def serialize: () -> [::Time, ::Time, ::String]? + end + end +end diff --git a/sig/datadog/profiling/preload.rbs b/sig/datadog/profiling/preload.rbs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sig/datadog/profiling/profiler.rbs b/sig/datadog/profiling/profiler.rbs new file mode 100644 index 00000000000..9bf5815951f --- /dev/null +++ b/sig/datadog/profiling/profiler.rbs @@ -0,0 +1,20 @@ +module Datadog + module Profiling + class Profiler + include Datadog::Core::Utils::Forking + + attr_reader collectors: Array[untyped] + + attr_reader scheduler: Datadog::Profiling::Scheduler + + def initialize: ( + [Datadog::Profiling::Collectors::OldStack | Datadog::Profiling::Collectors::CpuAndWallTimeWorker] collectors, + Datadog::Profiling::Scheduler scheduler, + ) -> void + + def start: () -> void + + def shutdown!: () -> void + end + end +end diff --git a/sig/datadog/profiling/scheduler.rbs b/sig/datadog/profiling/scheduler.rbs new file mode 100644 index 00000000000..faf579a3903 --- /dev/null +++ b/sig/datadog/profiling/scheduler.rbs @@ -0,0 +1,19 @@ +module Datadog + module Profiling + class Scheduler < Core::Worker + include Core::Workers::Polling + + def initialize: ( + exporter: Datadog::Profiling::Exporter, + transport: Datadog::Profiling::HttpTransport, + ?fork_policy: untyped, + ?interval: ::Integer, + ?enabled: bool, + ) -> void + + def start: () -> void + + def reset_after_fork: () -> void + end + end +end diff --git a/sig/datadog/profiling/stack_recorder.rbs b/sig/datadog/profiling/stack_recorder.rbs new file mode 100644 index 00000000000..a3ff67b7e38 --- /dev/null +++ b/sig/datadog/profiling/stack_recorder.rbs @@ -0,0 +1,27 @@ +module Datadog + module Profiling + class StackRecorder + @no_concurrent_synchronize_mutex: ::Thread::Mutex + + def initialize: (cpu_time_enabled: bool, alloc_samples_enabled: bool) -> void + + def self._native_initialize: ( + Datadog::Profiling::StackRecorder recorder_instance, + bool cpu_time_enabled, + bool alloc_samples_enabled, + ) -> true + + def serialize: () -> untyped + + def self._native_serialize: (Datadog::Profiling::StackRecorder recorder_instance) -> [:ok | :error, untyped] + + def serialize!: () -> String + + def clear: () -> ::Time? + + def reset_after_fork: () -> true + + def self._native_reset_after_fork: (Datadog::Profiling::StackRecorder recorder_instance) -> true + end + end +end diff --git a/sig/datadog/profiling/tag_builder.rbs b/sig/datadog/profiling/tag_builder.rbs new file mode 100644 index 00000000000..dc4563bdd9e --- /dev/null +++ b/sig/datadog/profiling/tag_builder.rbs @@ -0,0 +1,7 @@ +module Datadog + module Profiling + module TagBuilder + def self.call: (settings: untyped) -> untyped + end + end +end diff --git a/sig/datadog/profiling/tasks/setup.rbs b/sig/datadog/profiling/tasks/setup.rbs new file mode 100644 index 00000000000..691864d1f33 --- /dev/null +++ b/sig/datadog/profiling/tasks/setup.rbs @@ -0,0 +1,9 @@ +module Datadog + module Profiling + module Tasks + class Setup + def run: () -> void + end + end + end +end diff --git a/sig/datadog/profiling/trace_identifiers/ddtrace.rbs b/sig/datadog/profiling/trace_identifiers/ddtrace.rbs new file mode 100644 index 00000000000..e95f28b0949 --- /dev/null +++ b/sig/datadog/profiling/trace_identifiers/ddtrace.rbs @@ -0,0 +1,8 @@ +module Datadog + module Profiling + module TraceIdentifiers + class Ddtrace + end + end + end +end diff --git a/sig/datadog/profiling/trace_identifiers/helper.rbs b/sig/datadog/profiling/trace_identifiers/helper.rbs new file mode 100644 index 00000000000..7cfbe932ac1 --- /dev/null +++ b/sig/datadog/profiling/trace_identifiers/helper.rbs @@ -0,0 +1,16 @@ +module Datadog + module Profiling + module TraceIdentifiers + class Helper + @endpoint_collection_enabled: bool + @supported_apis: Array[untyped] + + DEFAULT_SUPPORTED_APIS: ::Array[untyped] + + def initialize: (tracer: Datadog::Tracing::Tracer?, endpoint_collection_enabled: bool, ?supported_apis: untyped) -> void + + def trace_identifiers_for: (::Thread thread) -> untyped + end + end + end +end From c0b33ac2cbee5531bbaeaf12c73257a9ab73a7ce Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Mon, 20 Mar 2023 12:23:51 +0000 Subject: [PATCH 3/3] Update typechecking signatures to account for PR #2698 --- sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs | 1 + sig/datadog/profiling/collectors/thread_context.rbs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs index 9d79f68cb97..543e3616900 100644 --- a/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs +++ b/sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs @@ -8,6 +8,7 @@ module Datadog recorder: Datadog::Profiling::StackRecorder, max_frames: ::Integer, tracer: Datadog::Tracing::Tracer?, + endpoint_collection_enabled: bool, gc_profiling_enabled: bool, allocation_counting_enabled: bool, ?thread_context_collector: Datadog::Profiling::Collectors::ThreadContext, diff --git a/sig/datadog/profiling/collectors/thread_context.rbs b/sig/datadog/profiling/collectors/thread_context.rbs index bd22adede79..815bd9a16c5 100644 --- a/sig/datadog/profiling/collectors/thread_context.rbs +++ b/sig/datadog/profiling/collectors/thread_context.rbs @@ -5,7 +5,8 @@ module Datadog def initialize: ( recorder: Datadog::Profiling::StackRecorder, max_frames: ::Integer, - tracer: Datadog::Tracing::Tracer? + tracer: Datadog::Tracing::Tracer?, + endpoint_collection_enabled: bool, ) -> void def self._native_initialize: ( @@ -13,6 +14,7 @@ module Datadog Datadog::Profiling::StackRecorder recorder_instance, ::Integer max_frames, ::Symbol? tracer_context_key, + bool endpoint_collection_enabled, ) -> void def inspect: () -> ::String