Skip to content

Commit

Permalink
Increase type checking coverage (#4290)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc authored Jan 15, 2025
1 parent 9e3a3e3 commit 7fa295e
Show file tree
Hide file tree
Showing 22 changed files with 400 additions and 151 deletions.
31 changes: 5 additions & 26 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,12 @@ target :datadog do
ignore 'lib/datadog/tracing/sampling/rule.rb'
ignore 'lib/datadog/tracing/sampling/rule_sampler.rb'
ignore 'lib/datadog/tracing/sampling/span/rule.rb'
ignore 'lib/datadog/tracing/span.rb'
ignore 'lib/datadog/tracing/span_operation.rb'
ignore 'lib/datadog/tracing/sync_writer.rb'
ignore 'lib/datadog/tracing/trace_operation.rb'
ignore 'lib/datadog/tracing/tracer.rb'
ignore 'lib/datadog/tracing/transport/http.rb'
ignore 'lib/datadog/tracing/transport/http/api.rb'
ignore 'lib/datadog/tracing/transport/http/builder.rb'
ignore 'lib/datadog/tracing/transport/http/client.rb'
ignore 'lib/datadog/tracing/transport/http/statistics.rb'
ignore 'lib/datadog/tracing/transport/http/traces.rb'
ignore 'lib/datadog/tracing/transport/io/client.rb'
ignore 'lib/datadog/tracing/transport/io/traces.rb'
Expand All @@ -162,30 +158,13 @@ target :datadog do
library 'digest'
library 'zlib'
library 'time'
library 'pp'

# Load all dependency signatures from the `vendor/rbs` directory
repo_path 'vendor/rbs'
library 'cucumber'
library 'jruby'
library 'gem'
library 'rails'
library 'spring'
library 'sinatra'
library 'google-protobuf'
library 'protobuf-cucumber'
library 'minitest'
library 'mysql2'
library 'mysql2-aurora'
library 'concurrent-ruby'
library 'faraday'
library 'seahorse'
library 'excon'
library 'grpc'
library 'delayed_job'
library 'opentelemetry-api'
library 'passenger'
library 'webmock'
library 'graphql'
library 'datadog-ci'
Dir.children('vendor/rbs').each do |vendor_gem|
library vendor_gem
end

# ffi version 1.17 was shipped with invalid rbs types:
# https://github.com/ffi/ffi/issues/1107
Expand Down
16 changes: 12 additions & 4 deletions lib/datadog/tracing/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def stopped?

def duration
return @duration if @duration
return @end_time - @start_time if @start_time && @end_time

start_time = @start_time
end_time = @end_time
end_time - start_time if start_time && end_time
end

def set_error(e)
Expand All @@ -135,6 +138,8 @@ def to_s
# TODO: Change this to reflect attributes when serialization
# isn't handled by this method.
def to_hash
@meta['events'] = @events.map(&:to_hash).to_json unless @events.empty?

h = {
error: @status,
meta: @meta,
Expand All @@ -154,8 +159,6 @@ def to_hash
h[:duration] = duration_nano
end

h[:meta]['events'] = @events.map(&:to_hash).to_json unless @events.empty?

h
end

Expand Down Expand Up @@ -196,12 +199,17 @@ def pretty_print(q)
# Used for serialization
# @return [Integer] in nanoseconds since Epoch
def start_time_nano
@start_time.to_i * 1000000000 + @start_time.nsec
return unless (start_time = @start_time)

start_time.to_i * 1000000000 + start_time.nsec
end

# Used for serialization
# @return [Integer] in nanoseconds since Epoch
def duration_nano
duration = self.duration
return unless duration

(duration * 1e9).to_i
end

Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/tracing/span_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ def start(start_time = nil)
end

# Mark the span stopped at the current time
#
# steep:ignore:start
# Steep issue fixed in https://github.com/soutaro/steep/pull/1467
def stop(stop_time = nil)
# A span should not be stopped twice. Note that this is not thread-safe,
# stop is called from multiple threads, a given span might be stopped
Expand All @@ -221,6 +224,7 @@ def stop(stop_time = nil)

self
end
# steep:ignore:end

# Return whether the duration is started or not
def started?
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/core/configuration.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Datadog
module Core
module Configuration
def health_metrics: -> Diagnostics::Health::Metrics

def tracer: () -> Datadog::Tracing::Tracer

def logger: () -> Datadog::Core::Logger
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/diagnostics/health.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Diagnostics
module Health
class Metrics < Core::Metrics::Client
extend Tracing::Diagnostics::Health::Metrics
include Tracing::Diagnostics::Health::Metrics
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/tracing.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module Datadog

def self.active_trace: -> TraceSegment?
def self.active_span: -> SpanOperation?

type on_error = ^(SpanOperation span_op, Exception error) -> void
end
end
2 changes: 1 addition & 1 deletion sig/datadog/tracing/contrib/resque/resque_job.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Datadog

def forked?: () -> untyped

def span_options: () -> { service: untyped, on_error: untyped }
def span_options: () -> { service: untyped, on_error: on_error }

def datadog_configuration: () -> untyped
end
Expand Down
20 changes: 19 additions & 1 deletion sig/datadog/tracing/diagnostics/health.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ module Datadog
module Diagnostics
module Health
module Metrics
def self.extended: (untyped base) -> untyped
def api_errors: (untyped ?value) ?{ (untyped) -> untyped } -> void
def api_requests: (untyped ?value) ?{ (untyped) -> untyped } -> void
def api_responses: (untyped ?value) ?{ (untyped) -> untyped } -> void
def error_context_overflow: (untyped ?value) ?{ (untyped) -> untyped } -> void
def error_instrumentation_patch: (untyped ?value) ?{ (untyped) -> untyped } -> void
def error_span_finish: (untyped ?value) ?{ (untyped) -> untyped } -> void
def error_unfinished_spans: (untyped ?value) ?{ (untyped) -> untyped } -> void
def instrumentation_patched: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_accepted: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_accepted_lengths: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_dropped: (untyped ?value) ?{ (untyped) -> untyped } -> void
def traces_filtered: (untyped ?value) ?{ (untyped) -> untyped } -> void
def transport_trace_too_large: (untyped ?value) ?{ (untyped) -> untyped } -> void
def transport_chunked: (untyped ?value) ?{ (untyped) -> untyped } -> void
def writer_cpu_time: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_length: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_max_length: (untyped ?value) ?{ (untyped) -> untyped } -> void
def queue_spans: (untyped ?value) ?{ (untyped) -> untyped } -> void
def sampling_service_cache_length: (untyped ?value) ?{ (untyped) -> untyped } -> void
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/tracing/metadata.rbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Datadog
module Tracing
module Metadata
def self.included: (untyped base) -> untyped
include Metadata::Tagging
include Metadata::Errors
prepend Metadata::Analytics
end
end
end
55 changes: 52 additions & 3 deletions sig/datadog/tracing/span.rbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
module Datadog
module Tracing
class Span
attr_accessor span_id: Integer
include Metadata

def set_tag: (String key, ?untyped? value) -> void
attr_accessor end_time: (Time | nil)
attr_accessor id: Integer
attr_accessor meta: Hash[String, String]
attr_accessor metrics: Hash[String, Float]
attr_accessor name: String
attr_accessor parent_id: Integer
attr_accessor resource: String
attr_accessor service: (String | nil)
attr_accessor links: Array[untyped]
attr_accessor events: Array[untyped]
attr_accessor type: (String | nil)
attr_accessor start_time: (Time | nil)
attr_accessor status: Integer
attr_accessor trace_id: Integer
attr_writer duration: (Float | nil)

def initialize: (
String name,
?duration: (Float | nil),
?end_time: (Time | nil),
?id: (Integer | nil),
?meta: (Hash[String, String] | nil),
?metrics: (Hash[String, Float] | nil),
?parent_id: Integer,
?resource: String,
?service: (String | nil),
?start_time: (Time | nil),
?status: Integer,
?type: (String | nil),
?trace_id: (Integer | nil),
?service_entry: (bool | nil),
?links: (Array[untyped] | nil),
?events: (Array[untyped] | nil)
) -> void

def started?: -> bool
def stopped?: -> bool
def duration: -> (Float | nil)
def set_error: (Exception e) -> void
def ==: (Span other) -> bool
def to_s: -> String
def to_hash: -> Hash[Symbol, untyped]
def pretty_print: (PP q) -> void

private

def duration_nano: -> Integer?

def service_entry?: -> bool

def start_time_nano: -> Integer?
end
end
end

28 changes: 21 additions & 7 deletions sig/datadog/tracing/span_operation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Datadog

attr_reader span_events: untyped

attr_reader end_time: untyped
attr_reader end_time: ::Time

attr_reader id: untyped

Expand All @@ -22,15 +22,29 @@ module Datadog

attr_reader service: untyped

attr_reader start_time: untyped
attr_reader start_time: ::Time

attr_reader trace_id: untyped

attr_reader type: untyped

attr_accessor status: untyped

def initialize: (untyped name, ?child_of: untyped?, ?events: untyped?, ?on_error: untyped?, ?parent_id: ::Integer, ?resource: untyped, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?trace_id: untyped?, ?type: untyped?, ?links: untyped?, ?span_events: untyped?) -> void
def initialize: (
String name,
?events: Events,
?on_error: on_error,
?parent_id: Integer,
?resource: String,
?service: (String | nil),
?start_time: (Time | nil),
?tags: (Hash[String, (String|Numeric)] | nil),
?trace_id: (Integer | nil),
?type: (String | nil),
?links: (Array[SpanLink] | nil),
?span_events: (Array[SpanEvent] | nil),
?id: (Integer | nil)
) -> void

def name=: (untyped name) -> untyped

Expand All @@ -44,7 +58,7 @@ module Datadog

def start: (?untyped? start_time) -> self

def stop: (?untyped? stop_time) -> (nil | self)
def stop: (?untyped? stop_time) -> self?

def started?: () -> untyped

Expand All @@ -71,17 +85,17 @@ module Datadog
class Events
include Tracing::Events

DEFAULT_ON_ERROR: untyped
DEFAULT_ON_ERROR: on_error

attr_reader after_finish: untyped

attr_reader after_stop: untyped

attr_reader before_start: untyped

def initialize: (?on_error: untyped?) -> void
def initialize: (?on_error: on_error) -> void

def on_error: () -> untyped
def on_error: () -> OnError

class AfterFinish < Tracing::Event
def initialize: () -> void
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/tracing/trace_operation.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module Datadog
def resource: () -> untyped
def resource_override?: () -> bool
def service: () -> untyped
def measure: (untyped op_name, ?events: untyped?, ?on_error: untyped?, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?) { (untyped, untyped) -> untyped } -> untyped
def build_span: (untyped op_name, ?events: untyped?, ?on_error: untyped?, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?) -> untyped
def measure: (untyped op_name, ?events: untyped?, ?on_error: on_error, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?) { (untyped, untyped) -> untyped } -> untyped
def build_span: (untyped op_name, ?events: untyped?, ?on_error: on_error, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?) -> untyped
def flush!: () { (untyped) -> untyped } -> untyped
def to_digest: () -> untyped
def fork_clone: () -> untyped
Expand Down
14 changes: 8 additions & 6 deletions sig/datadog/tracing/transport/http/api.rbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Datadog
module Transport
module HTTP
module API
V4: "v0.4"
module Tracing
module Transport
module HTTP
module API
V4: "v0.4"

V3: "v0.3"
V3: "v0.3"

def self?.defaults: () -> untyped
def self?.defaults: () -> untyped
end
end
end
end
Expand Down
Loading

0 comments on commit 7fa295e

Please sign in to comment.