Skip to content

Commit

Permalink
Merge pull request #2700 from DataDog/ivoanjo/profiling-typechecking
Browse files Browse the repository at this point in the history
Add type signatures for a bunch of profiling classes
  • Loading branch information
ivoanjo authored Mar 20, 2023
2 parents e96d8fe + c0b33ac commit 1720689
Show file tree
Hide file tree
Showing 40 changed files with 588 additions and 70 deletions.
15 changes: 2 additions & 13 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,16 @@ 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'
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'
Expand All @@ -192,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'
Expand Down Expand Up @@ -668,4 +655,6 @@ target :ddtrace do
library 'gem'
library 'rails'
library 'sinatra'
library 'google-protobuf'
library 'protobuf-cucumber'
end
3 changes: 2 additions & 1 deletion lib/datadog/profiling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 <https://docs.datadoghq.com/help/>.' \
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/profiling/collectors/idle_sampling_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/profiling/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 28 additions & 26 deletions lib/datadog/profiling/ext.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 0 additions & 18 deletions lib/datadog/profiling/stack_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sig/datadog/core/buffer/random.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Datadog
module Core
module Buffer
class Random
end
end
end
end
8 changes: 8 additions & 0 deletions sig/datadog/core/buffer/thread_safe.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Datadog
module Core
module Buffer
class ThreadSafe < Datadog::Core::Buffer::Random
end
end
end
end
2 changes: 2 additions & 0 deletions sig/datadog/core/configuration.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 1 addition & 9 deletions sig/datadog/core/configuration/components.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/core/worker.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Datadog
module Core
class Worker
end
end
end
21 changes: 21 additions & 0 deletions sig/datadog/core/workers/polling.rbs
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions sig/datadog/profiling.rbs
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions sig/datadog/profiling/collectors/code_provenance.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Datadog
module Profiling
module Collectors
class CodeProvenance
def refresh: () -> self
def generate_json: () -> ::String
end
end
end
end
20 changes: 20 additions & 0 deletions sig/datadog/profiling/collectors/cpu_and_wall_time_worker.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Datadog
module Profiling
module Collectors
class CpuAndWallTimeWorker
def self._native_allocation_count: () -> ::Integer?

def initialize: (
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,
?idle_sampling_helper: Datadog::Profiling::Collectors::IdleSamplingHelper,
) -> void
end
end
end
end
8 changes: 8 additions & 0 deletions sig/datadog/profiling/collectors/dynamic_sampling_rate.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Datadog
module Profiling
module Collectors
module DynamicSamplingRate
end
end
end
end
28 changes: 28 additions & 0 deletions sig/datadog/profiling/collectors/idle_sampling_helper.rbs
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions sig/datadog/profiling/collectors/old_stack.rbs
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 1720689

Please sign in to comment.