From 3e1dd58fdcb4ec4619f292430bfd1d9da3314a8f Mon Sep 17 00:00:00 2001 From: Teppei Shintani Date: Sun, 16 Mar 2025 22:41:49 +0900 Subject: [PATCH 1/6] Add RBS for `Datadog::Tracing.correlation` I am developing a Rails application utilizing types with Steep/RBS. Since the type definitions for the datadog gem were incomplete, I added the missing type definitions. --- sig/datadog/tracing.rbs | 1 + sig/datadog/tracing/correlation.rbs | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sig/datadog/tracing.rbs b/sig/datadog/tracing.rbs index b386889caf9..7f2942052c9 100644 --- a/sig/datadog/tracing.rbs +++ b/sig/datadog/tracing.rbs @@ -4,6 +4,7 @@ module Datadog def self.active_trace: -> TraceSegment? def self.active_span: -> SpanOperation? + def self.correlation: () -> Correlation::Identifier def self.enabled?: -> bool diff --git a/sig/datadog/tracing/correlation.rbs b/sig/datadog/tracing/correlation.rbs index a385bb68db8..002abf5b08d 100644 --- a/sig/datadog/tracing/correlation.rbs +++ b/sig/datadog/tracing/correlation.rbs @@ -1,7 +1,14 @@ module Datadog module Tracing module Correlation - Identifier: Struct[untyped] + class Identifier + attr_reader env: String? + attr_reader service: String + attr_reader span_id: String + attr_reader version: String + + def trace_id: () -> String + end def self?.identifier_from_context: (untyped context) -> Struct[untyped] # Identifier end From 7c1b8f5014f910fedcfb5e0e6a177d020ea57a27 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 27 Mar 2025 16:05:14 -0400 Subject: [PATCH 2/6] enable type checking of the changed files --- Steepfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Steepfile b/Steepfile index 8f111e0a68d..e8071a11e70 100644 --- a/Steepfile +++ b/Steepfile @@ -115,7 +115,6 @@ target :datadog do ignore 'lib/datadog/profiling/scheduler.rb' ignore 'lib/datadog/profiling/tag_builder.rb' ignore 'lib/datadog/profiling/tasks/setup.rb' - ignore 'lib/datadog/tracing.rb' ignore 'lib/datadog/tracing/buffer.rb' ignore 'lib/datadog/tracing/client_ip.rb' ignore 'lib/datadog/tracing/component.rb' @@ -123,7 +122,6 @@ target :datadog do ignore 'lib/datadog/tracing/configuration/settings.rb' ignore 'lib/datadog/tracing/context.rb' ignore 'lib/datadog/tracing/contrib/' - ignore 'lib/datadog/tracing/correlation.rb' ignore 'lib/datadog/tracing/diagnostics/environment_logger.rb' ignore 'lib/datadog/tracing/diagnostics/health.rb' ignore 'lib/datadog/tracing/distributed/datadog.rb' From ba3bf08ee7083a23471cdf044744bc85349ca9db Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 27 Mar 2025 16:09:51 -0400 Subject: [PATCH 3/6] regenerate these files keeping the existing types --- sig/datadog/tracing.rbs | 21 ++++++++++++---- sig/datadog/tracing/correlation.rbs | 39 ++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/sig/datadog/tracing.rbs b/sig/datadog/tracing.rbs index 7f2942052c9..5d7c630b36a 100644 --- a/sig/datadog/tracing.rbs +++ b/sig/datadog/tracing.rbs @@ -1,13 +1,24 @@ module Datadog module Tracing - def self.continue_trace!: (TraceDigest) ? { (untyped) -> untyped } -> untyped + type on_error = ^(SpanOperation span_op, Exception error) -> void - def self.active_trace: -> TraceSegment? - def self.active_span: -> SpanOperation? + def self.trace: (untyped name, ?continue_from: untyped?, ?on_error: untyped?, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?, ?id: untyped?) { (?) -> untyped } -> untyped + def self.continue_trace!: (TraceDigest digest) { (?) -> untyped } -> untyped + def self.logger: () -> untyped + def self.active_trace: () -> TraceSegment? + def self.active_span: () -> SpanOperation? + def self.keep!: () -> untyped + def self.reject!: () -> untyped def self.correlation: () -> Correlation::Identifier + def self.log_correlation: () -> ("" | untyped) + def self.baggage: () -> untyped + def self.shutdown!: () -> (nil | untyped) + def self.before_flush: (*untyped processors) { (?) -> untyped } -> untyped + def self.enabled?: () -> bool - def self.enabled?: -> bool + private + def self.components: () -> untyped - type on_error = ^(SpanOperation span_op, Exception error) -> void + def self.tracer: () -> untyped end end diff --git a/sig/datadog/tracing/correlation.rbs b/sig/datadog/tracing/correlation.rbs index 002abf5b08d..2f5d2f22df4 100644 --- a/sig/datadog/tracing/correlation.rbs +++ b/sig/datadog/tracing/correlation.rbs @@ -2,16 +2,49 @@ module Datadog module Tracing module Correlation class Identifier + @env: String? + + @service: String + + @span_id: String + + @trace_id: String + + @version: String + + @to_h: untyped + + @log_format: untyped + + LOG_ATTR_ENV: "dd.env" + + LOG_ATTR_SERVICE: "dd.service" + + LOG_ATTR_SPAN_ID: "dd.span_id" + + LOG_ATTR_TRACE_ID: "dd.trace_id" + + LOG_ATTR_VERSION: "dd.version" + + LOG_ATTR_SOURCE: "ddsource" + attr_reader env: String? + attr_reader service: String + attr_reader span_id: String + attr_reader version: String + def initialize: (?env: untyped?, ?service: untyped?, ?span_id: untyped?, ?trace_id: untyped?, ?version: untyped?) -> void - def trace_id: () -> String + def to_h: () -> untyped + def to_log_format: () -> untyped + + def trace_id: () -> untyped end + def self?.identifier_from_digest: (untyped digest) -> untyped - def self?.identifier_from_context: (untyped context) -> Struct[untyped] # Identifier + def self?.format_trace_id: (untyped trace_id) -> untyped end end end - From 9d54d42816024efd9984e887730ed97b41ec5198 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 27 Mar 2025 16:12:21 -0400 Subject: [PATCH 4/6] regenerate --- sig/datadog/tracing/trace_segment.rbs | 99 +++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/sig/datadog/tracing/trace_segment.rbs b/sig/datadog/tracing/trace_segment.rbs index fd3a8239c99..f3fa386beef 100644 --- a/sig/datadog/tracing/trace_segment.rbs +++ b/sig/datadog/tracing/trace_segment.rbs @@ -1,73 +1,164 @@ module Datadog module Tracing class TraceSegment - TAG_NAME: ::String - TAG_RESOURCE: ::String - TAG_SERVICE: ::String + @id: untyped + + @root_span_id: untyped + + @spans: untyped + @meta: untyped + + @metrics: untyped + @agent_sample_rate: untyped + + @hostname: untyped + + @lang: untyped + + @name: untyped + + @origin: untyped + + @process_id: untyped + + @rate_limiter_rate: untyped + + @resource: untyped + + @rule_sample_rate: untyped + + @runtime_id: untyped + + @sample_rate: untyped + + @sampling_decision_maker: untyped + + @sampling_priority: untyped + + @service: untyped + + @profiling_enabled: untyped + + @apm_tracing_enabled: untyped + + TAG_NAME: "name" + + TAG_RESOURCE: "resource" + + TAG_SERVICE: "service" attr_reader id: untyped + attr_reader spans: untyped + attr_reader agent_sample_rate: untyped + attr_reader hostname: untyped + attr_reader lang: untyped + attr_reader name: untyped + attr_reader origin: untyped + attr_reader process_id: untyped + attr_reader rate_limiter_rate: untyped + attr_reader resource: untyped + attr_reader rule_sample_rate: untyped + attr_reader runtime_id: untyped + attr_reader sample_rate: untyped + attr_reader sampling_decision_maker: untyped + attr_reader sampling_priority: untyped + attr_reader service: untyped + attr_reader profiling_enabled: untyped - attr_reader apm_tracing_enabled: untyped + attr_reader apm_tracing_enabled: untyped def initialize: (untyped spans, ?agent_sample_rate: untyped?, ?hostname: untyped?, ?id: untyped?, ?lang: untyped?, ?name: untyped?, ?origin: untyped?, ?process_id: untyped?, ?rate_limiter_rate: untyped?, ?resource: untyped?, ?root_span_id: untyped?, ?rule_sample_rate: untyped?, ?runtime_id: untyped?, ?sample_rate: untyped?, ?sampling_priority: untyped?, ?service: untyped?, ?tags: untyped?, ?metrics: untyped?, ?profiling_enabled: untyped?, ?apm_tracing_enabled: untyped?) -> void + def any?: () -> untyped + def count: () -> untyped + def empty?: () -> untyped + def length: () -> untyped + def size: () -> untyped def keep!: () -> void def reject!: () -> void def sampled?: () -> untyped + def high_order_tid: () -> untyped attr_reader root_span_id: untyped + attr_reader meta: untyped + attr_reader metrics: untyped private attr_writer agent_sample_rate: untyped + attr_writer hostname: untyped + attr_writer lang: untyped + attr_writer name: untyped + attr_writer origin: untyped + attr_writer process_id: untyped + attr_writer rate_limiter_rate: untyped + attr_writer resource: untyped + attr_writer rule_sample_rate: untyped + attr_writer runtime_id: untyped + attr_writer sample_rate: untyped + attr_writer sampling_priority: untyped + attr_writer service: untyped def agent_sample_rate_tag: () -> untyped + def hostname_tag: () -> untyped + def lang_tag: () -> untyped + def name_tag: () -> untyped + def origin_tag: () -> untyped + def process_id_tag: () -> untyped + def rate_limiter_rate_tag: () -> untyped + def resource_tag: () -> untyped + def rule_sample_rate_tag: () -> untyped + def runtime_id_tag: () -> untyped + def sample_rate_tag: () -> untyped + def sampling_decision_maker_tag: () -> untyped + def sampling_priority_tag: () -> untyped + def service_tag: () -> untyped end end From 1769d01bcf53e299e84e9ff1fb11ba023e5deac2 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 27 Mar 2025 16:13:32 -0400 Subject: [PATCH 5/6] make steep happy --- lib/datadog/tracing.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/datadog/tracing.rb b/lib/datadog/tracing.rb index e18d8660f90..d956588d97a 100644 --- a/lib/datadog/tracing.rb +++ b/lib/datadog/tracing.rb @@ -81,7 +81,7 @@ def active_span # @public_api def keep! trace = active_trace - active_trace.keep! if trace + trace.keep! if trace end # (see Datadog::Tracing::TraceSegment#reject!) @@ -89,7 +89,7 @@ def keep! # @public_api def reject! trace = active_trace - active_trace.reject! if trace + trace.reject! if trace end # (see Datadog::Tracing::Tracer#active_correlation) From 641d1b952316d7f9f0ba2535c9a1a5076f68c165 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 27 Mar 2025 16:16:30 -0400 Subject: [PATCH 6/6] block argument to continue_trace! is optional --- sig/datadog/tracing.rbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sig/datadog/tracing.rbs b/sig/datadog/tracing.rbs index 5d7c630b36a..27f0e47592c 100644 --- a/sig/datadog/tracing.rbs +++ b/sig/datadog/tracing.rbs @@ -3,7 +3,7 @@ module Datadog type on_error = ^(SpanOperation span_op, Exception error) -> void def self.trace: (untyped name, ?continue_from: untyped?, ?on_error: untyped?, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?, ?id: untyped?) { (?) -> untyped } -> untyped - def self.continue_trace!: (TraceDigest digest) { (?) -> untyped } -> untyped + def self.continue_trace!: (TraceDigest digest) ?{ (?) -> untyped } -> untyped def self.logger: () -> untyped def self.active_trace: () -> TraceSegment? def self.active_span: () -> SpanOperation?