Skip to content

Commit

Permalink
Pass type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
lloeki committed Mar 15, 2023
1 parent a1e2611 commit 9230461
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ target :ddtrace do
library 'json'
library 'ipaddr'
library 'net-http'
library 'securerandom'
library 'base64'

# TODO: gem 'libddwaf'

Expand Down
3 changes: 0 additions & 3 deletions lib/datadog/core/transport/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ def initialize(apis, default_api)
@apis = apis

@client = HTTP::Client.new(current_api)

# TODO: this should go in the high level remote config client
@client_id = SecureRandom.uuid
end

##### there is only one transport! it's negotiation!
Expand Down
32 changes: 16 additions & 16 deletions lib/datadog/core/transport/http/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me

raise TypeError.new(Array, roots) unless roots.is_a?(Array)

@roots = roots.map do |e|
raise TypeError.new(String, e) unless e.is_a?(String)
@roots = roots.map do |root|
raise TypeError.new(String, root) unless root.is_a?(String)

begin
decoded = Base64.strict_decode64(e) # TODO: unprocessed, don't symbolize_names
decoded = begin
Base64.strict_decode64(root) # TODO: unprocessed, don't symbolize_names
rescue ArgumentError
raise DecodeError.new(:roots, e)
raise DecodeError.new(:roots, root)
end

begin
parsed = JSON.parse(decoded)
parsed = begin
JSON.parse(decoded)
rescue JSON::ParserError
raise ParseError.new(:roots, e)
raise ParseError.new(:roots, decoded)
end

# TODO: perform more processing to validate content. til then, no freeze
Expand All @@ -69,16 +69,16 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me
raise TypeError.new(String, targets) unless targets.is_a?(String)

@targets = begin
begin
decoded = Base64.strict_decode64(targets)
decoded = begin
Base64.strict_decode64(targets)
rescue ArgumentError
raise DecodeError.new(:targets, e)
raise DecodeError.new(:targets, targets)
end

begin
parsed = JSON.parse(decoded) # TODO: unprocessed, don't symbolize_names
parsed = begin
JSON.parse(decoded) # TODO: unprocessed, don't symbolize_names
rescue JSON::ParserError
raise ParseError.new(:targets, e)
raise ParseError.new(:targets, decoded)
end

# TODO: perform more processing to validate content. til then, no freeze
Expand All @@ -97,8 +97,8 @@ def initialize(http_response, options = {}) # rubocop:disable Metrics/AbcSize,Me

raise TypeError.new(String, raw) unless raw.is_a?(String)

begin
content = Base64.strict_decode64(raw)
content = begin
Base64.strict_decode64(raw)
rescue ArgumentError
raise DecodeError.new(:target_files, raw)
end
Expand Down
44 changes: 44 additions & 0 deletions sig/datadog/core/encoding.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Datadog
module Core
# Encoding module that encodes data for the AgentTransport
module Encoding
# Encoder interface that provides the logic to encode traces and service
# @abstract
module Encoder
def content_type: () -> untyped

# Concatenates a list of elements previously encoded by +#encode+.
def join: (untyped encoded_elements) -> untyped

# Serializes a single trace into a String suitable for network transmission.
def encode: (untyped _) -> untyped
end

# Encoder for the JSON format
module JSONEncoder
extend Encoder

CONTENT_TYPE: "application/json"

def self?.content_type: () -> untyped

def self?.encode: (untyped obj) -> untyped

def self?.join: (untyped encoded_data) -> ::String
end

# Encoder for the Msgpack format
module MsgpackEncoder
extend Encoder

CONTENT_TYPE: "application/msgpack"

def self?.content_type: () -> untyped

def self?.encode: (untyped obj) -> untyped

def self?.join: (untyped encoded_data) -> untyped
end
end
end
end
47 changes: 47 additions & 0 deletions sig/datadog/core/transport/config.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Datadog
module Core
module Transport
module Config
# Data transfer object for encoded traces
class EncodedParcel
include Datadog::Transport::Parcel

def count: () -> untyped
end

# Config request
class Request < Datadog::Transport::Request
end

# Config response
module Response
attr_reader roots: untyped

attr_reader targets: untyped

attr_reader target_files: untyped

attr_reader client_configs: untyped
end

# Config transport
class Transport
attr_reader client: untyped

attr_reader apis: untyped

attr_reader default_api: untyped

attr_reader current_api_id: untyped

def initialize: (untyped apis, untyped default_api) -> void

# ### there is only one transport! it's negotiation!
def send_config: (untyped payload) -> untyped

def current_api: () -> untyped
end
end
end
end
end
2 changes: 2 additions & 0 deletions sig/datadog/core/transport/http.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Datadog

def self?.root: (?agent_settings: untyped, **untyped options) { (untyped) -> untyped } -> untyped

def self?.v7: (?agent_settings: untyped, **untyped options) { (untyped) -> untyped } -> untyped

def self?.default_headers: () -> untyped

def self?.default_adapter: () -> untyped
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/core/transport/http/api.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Datadog
module HTTP
module API
ROOT: "root"
V7: "v0.7"

def self?.defaults: () -> untyped
end
Expand Down
89 changes: 89 additions & 0 deletions sig/datadog/core/transport/http/config.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Datadog
module Core
module Transport
module HTTP
# HTTP transport behavior for remote configuration
module Config
# Response from HTTP transport for remote configuration
class Response
include Datadog::Transport::HTTP::Response

include Core::Transport::Config::Response

def initialize: (untyped http_response, ?::Hash[untyped, untyped] options) -> void

# When an expected key is missing
class KeyError < StandardError
def initialize: (untyped key) -> void
end

# When an expected value type is incorrect
class TypeError < StandardError
def initialize: (untyped `type`, untyped value) -> void
end

# When value decoding fails
class DecodeError < StandardError
def initialize: (untyped key, untyped value) -> void
end

# When value parsing fails
class ParseError < StandardError
def initialize: (untyped key, untyped value) -> void
end
end

# Extensions for HTTP client
module Client : HTTP::Client
def send_config_payload: (untyped request) -> untyped
end

module API
# Extensions for HTTP API Spec
module Spec
attr_reader config: untyped

def config=: (untyped endpoint) -> untyped

def send_config: (untyped env) ?{ () -> untyped } -> untyped

# Raised when traces sent but no traces endpoint is defined
class NoConfigEndpointDefinedError < StandardError
attr_reader spec: untyped

def initialize: (untyped spec) -> void

def message: () -> "No config endpoint is defined for API specification!"
end
end

# Extensions for HTTP API Instance
module Instance : HTTP::API::Instance
def send_config: (untyped env) -> untyped

# Raised when traces sent to API that does not support traces
class ConfigNotSupportedError < StandardError
attr_reader spec: untyped

def initialize: (untyped spec) -> void

def message: () -> "Config not supported for this API!"
end
end

# Endpoint for remote configuration
class Endpoint < Datadog::Transport::HTTP::API::Endpoint
HEADER_CONTENT_TYPE: "Content-Type"

attr_reader encoder: untyped

def initialize: (untyped path, untyped encoder) -> void

def call: (untyped env) { (untyped) -> untyped } -> untyped
end
end
end
end
end
end
end

0 comments on commit 9230461

Please sign in to comment.