datadog 2.10.0 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -1
  3. data/ext/datadog_profiling_native_extension/collectors_stack.c +3 -3
  4. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +44 -1
  5. data/ext/datadog_profiling_native_extension/extconf.rb +4 -0
  6. data/ext/datadog_profiling_native_extension/gvl_profiling_helper.c +2 -0
  7. data/ext/datadog_profiling_native_extension/gvl_profiling_helper.h +0 -8
  8. data/ext/datadog_profiling_native_extension/heap_recorder.c +1 -1
  9. data/ext/datadog_profiling_native_extension/private_vm_api_access.c +56 -0
  10. data/ext/datadog_profiling_native_extension/private_vm_api_access.h +7 -0
  11. data/ext/datadog_profiling_native_extension/profiling.c +7 -0
  12. data/ext/libdatadog_api/crashtracker.c +4 -4
  13. data/ext/libdatadog_extconf_helpers.rb +1 -1
  14. data/lib/datadog/appsec/configuration/settings.rb +64 -11
  15. data/lib/datadog/appsec/contrib/active_record/patcher.rb +0 -3
  16. data/lib/datadog/appsec/contrib/devise/configuration.rb +76 -0
  17. data/lib/datadog/appsec/contrib/devise/event.rb +4 -7
  18. data/lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb +16 -21
  19. data/lib/datadog/appsec/contrib/devise/patcher/registration_controller_patch.rb +8 -15
  20. data/lib/datadog/appsec/contrib/devise/patcher/rememberable_patch.rb +1 -1
  21. data/lib/datadog/appsec/contrib/devise/patcher.rb +0 -3
  22. data/lib/datadog/appsec/contrib/devise/tracking.rb +1 -1
  23. data/lib/datadog/appsec/contrib/excon/integration.rb +41 -0
  24. data/lib/datadog/appsec/contrib/excon/patcher.rb +28 -0
  25. data/lib/datadog/appsec/contrib/excon/ssrf_detection_middleware.rb +43 -0
  26. data/lib/datadog/appsec/contrib/faraday/connection_patch.rb +22 -0
  27. data/lib/datadog/appsec/contrib/faraday/integration.rb +42 -0
  28. data/lib/datadog/appsec/contrib/faraday/patcher.rb +53 -0
  29. data/lib/datadog/appsec/contrib/faraday/rack_builder_patch.rb +22 -0
  30. data/lib/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rb +42 -0
  31. data/lib/datadog/appsec/contrib/graphql/gateway/watcher.rb +10 -12
  32. data/lib/datadog/appsec/contrib/graphql/patcher.rb +0 -3
  33. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +65 -73
  34. data/lib/datadog/appsec/contrib/rack/patcher.rb +0 -3
  35. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +20 -25
  36. data/lib/datadog/appsec/contrib/rails/patcher.rb +0 -3
  37. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +38 -49
  38. data/lib/datadog/appsec/contrib/sinatra/patcher.rb +0 -3
  39. data/lib/datadog/appsec/monitor/gateway/watcher.rb +19 -25
  40. data/lib/datadog/appsec/remote.rb +4 -0
  41. data/lib/datadog/appsec.rb +2 -0
  42. data/lib/datadog/core/configuration/components.rb +7 -1
  43. data/lib/datadog/core/configuration/ext.rb +1 -1
  44. data/lib/datadog/core/configuration/option_definition.rb +2 -0
  45. data/lib/datadog/core/configuration/settings.rb +22 -6
  46. data/lib/datadog/core/encoding.rb +16 -0
  47. data/lib/datadog/core/environment/agent_info.rb +77 -0
  48. data/lib/datadog/core/remote/transport/http/api.rb +13 -18
  49. data/lib/datadog/core/remote/transport/http/config.rb +0 -18
  50. data/lib/datadog/core/remote/transport/http/negotiation.rb +1 -18
  51. data/lib/datadog/core/remote/transport/http.rb +7 -12
  52. data/lib/datadog/core/remote/transport/negotiation.rb +13 -1
  53. data/lib/datadog/core/telemetry/event.rb +5 -0
  54. data/lib/datadog/core/transport/http/adapters/unix_socket.rb +1 -1
  55. data/lib/datadog/{tracing → core}/transport/http/api/instance.rb +1 -1
  56. data/lib/datadog/{tracing → core}/transport/http/api/spec.rb +1 -1
  57. data/lib/datadog/{tracing → core}/transport/http/builder.rb +37 -17
  58. data/lib/datadog/core/transport/response.rb +4 -0
  59. data/lib/datadog/di/code_tracker.rb +15 -8
  60. data/lib/datadog/di/component.rb +1 -0
  61. data/lib/datadog/di/configuration/settings.rb +14 -0
  62. data/lib/datadog/di/contrib.rb +2 -0
  63. data/lib/datadog/di/logger.rb +30 -0
  64. data/lib/datadog/di/probe.rb +3 -6
  65. data/lib/datadog/di/probe_manager.rb +5 -2
  66. data/lib/datadog/di/probe_notifier_worker.rb +15 -4
  67. data/lib/datadog/di/remote.rb +3 -3
  68. data/lib/datadog/di/utils.rb +91 -0
  69. data/lib/datadog/di.rb +3 -0
  70. data/lib/datadog/profiling/component.rb +2 -8
  71. data/lib/datadog/profiling/load_native_extension.rb +1 -33
  72. data/lib/datadog/tracing/configuration/ext.rb +1 -0
  73. data/lib/datadog/tracing/contrib/extensions.rb +14 -0
  74. data/lib/datadog/tracing/contrib/graphql/configuration/error_extension_env_parser.rb +21 -0
  75. data/lib/datadog/tracing/contrib/graphql/configuration/settings.rb +11 -0
  76. data/lib/datadog/tracing/contrib/graphql/ext.rb +5 -0
  77. data/lib/datadog/tracing/contrib/graphql/unified_trace.rb +102 -11
  78. data/lib/datadog/tracing/contrib/rack/header_collection.rb +11 -1
  79. data/lib/datadog/tracing/contrib/rack/middlewares.rb +1 -1
  80. data/lib/datadog/tracing/contrib/span_attribute_schema.rb +6 -1
  81. data/lib/datadog/tracing/transport/http/api.rb +11 -2
  82. data/lib/datadog/tracing/transport/http/traces.rb +0 -3
  83. data/lib/datadog/tracing/transport/http.rb +12 -7
  84. data/lib/datadog/tracing/transport/serializable_trace.rb +8 -4
  85. data/lib/datadog/tracing/transport/traces.rb +25 -8
  86. data/lib/datadog/version.rb +1 -1
  87. metadata +23 -28
  88. data/ext/datadog_profiling_loader/datadog_profiling_loader.c +0 -142
  89. data/ext/datadog_profiling_loader/extconf.rb +0 -60
  90. data/lib/datadog/appsec/contrib/graphql/reactive/multiplex.rb +0 -46
  91. data/lib/datadog/appsec/contrib/patcher.rb +0 -12
  92. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +0 -69
  93. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +0 -47
  94. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +0 -53
  95. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +0 -53
  96. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +0 -48
  97. data/lib/datadog/appsec/monitor/reactive/set_user.rb +0 -45
  98. data/lib/datadog/appsec/reactive/address_hash.rb +0 -22
  99. data/lib/datadog/appsec/reactive/engine.rb +0 -47
  100. data/lib/datadog/appsec/reactive/subscriber.rb +0 -19
  101. data/lib/datadog/core/remote/transport/http/api/instance.rb +0 -39
  102. data/lib/datadog/core/remote/transport/http/api/spec.rb +0 -21
  103. data/lib/datadog/core/remote/transport/http/builder.rb +0 -219
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Contrib
6
- module Rack
7
- module Reactive
8
- # Dispatch data from a Rack request to the WAF context
9
- module Request
10
- ADDRESSES = [
11
- 'request.headers',
12
- 'request.uri.raw',
13
- 'request.query',
14
- 'request.cookies',
15
- 'request.client_ip',
16
- 'server.request.method'
17
- ].freeze
18
- private_constant :ADDRESSES
19
-
20
- def self.publish(engine, gateway_request)
21
- catch(:block) do
22
- engine.publish('request.query', gateway_request.query)
23
- engine.publish('request.headers', gateway_request.headers)
24
- engine.publish('request.uri.raw', gateway_request.fullpath)
25
- engine.publish('request.cookies', gateway_request.cookies)
26
- engine.publish('request.client_ip', gateway_request.client_ip)
27
- engine.publish('server.request.method', gateway_request.method)
28
-
29
- nil
30
- end
31
- end
32
-
33
- def self.subscribe(engine, context)
34
- engine.subscribe(*ADDRESSES) do |*values|
35
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
36
-
37
- headers = values[0]
38
- headers_no_cookies = headers.dup.tap { |h| h.delete('cookie') }
39
- uri_raw = values[1]
40
- query = values[2]
41
- cookies = values[3]
42
- client_ip = values[4]
43
- request_method = values[5]
44
-
45
- persistent_data = {
46
- 'server.request.cookies' => cookies,
47
- 'server.request.query' => query,
48
- 'server.request.uri.raw' => uri_raw,
49
- 'server.request.headers' => headers,
50
- 'server.request.headers.no_cookies' => headers_no_cookies,
51
- 'http.client_ip' => client_ip,
52
- 'server.request.method' => request_method,
53
- }
54
-
55
- waf_timeout = Datadog.configuration.appsec.waf_timeout
56
- result = context.run_waf(persistent_data, {}, waf_timeout)
57
-
58
- next unless result.match?
59
-
60
- yield result
61
- throw(:block, true) unless result.actions.empty?
62
- end
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Contrib
6
- module Rack
7
- module Reactive
8
- # Dispatch data from a Rack request to the WAF context
9
- module RequestBody
10
- ADDRESSES = [
11
- 'request.body',
12
- ].freeze
13
- private_constant :ADDRESSES
14
-
15
- def self.publish(engine, gateway_request)
16
- catch(:block) do
17
- # params have been parsed from the request body
18
- engine.publish('request.body', gateway_request.form_hash)
19
-
20
- nil
21
- end
22
- end
23
-
24
- def self.subscribe(engine, context)
25
- engine.subscribe(*ADDRESSES) do |*values|
26
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
27
- body = values[0]
28
-
29
- persistent_data = {
30
- 'server.request.body' => body,
31
- }
32
-
33
- waf_timeout = Datadog.configuration.appsec.waf_timeout
34
- result = context.run_waf(persistent_data, {}, waf_timeout)
35
-
36
- next unless result.match?
37
-
38
- yield result
39
- throw(:block, true) unless result.actions.empty?
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Contrib
6
- module Rack
7
- module Reactive
8
- # Dispatch data from a Rack response to the WAF context
9
- module Response
10
- ADDRESSES = [
11
- 'response.status',
12
- 'response.headers',
13
- ].freeze
14
- private_constant :ADDRESSES
15
-
16
- def self.publish(engine, gateway_response)
17
- catch(:block) do
18
- engine.publish('response.status', gateway_response.status)
19
- engine.publish('response.headers', gateway_response.headers)
20
-
21
- nil
22
- end
23
- end
24
-
25
- def self.subscribe(engine, context)
26
- engine.subscribe(*ADDRESSES) do |*values|
27
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
28
-
29
- response_status = values[0]
30
- response_headers = values[1]
31
- response_headers_no_cookies = response_headers.dup.tap { |h| h.delete('set-cookie') }
32
-
33
- persistent_data = {
34
- 'server.response.status' => response_status.to_s,
35
- 'server.response.headers' => response_headers,
36
- 'server.response.headers.no_cookies' => response_headers_no_cookies,
37
- }
38
-
39
- waf_timeout = Datadog.configuration.appsec.waf_timeout
40
- result = context.run_waf(persistent_data, {}, waf_timeout)
41
-
42
- next unless result.match?
43
-
44
- yield result
45
- throw(:block, true) unless result.actions.empty?
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../request'
4
-
5
- module Datadog
6
- module AppSec
7
- module Contrib
8
- module Rails
9
- module Reactive
10
- # Dispatch data from a Rails request to the WAF context
11
- module Action
12
- ADDRESSES = [
13
- 'rails.request.body',
14
- 'rails.request.route_params',
15
- ].freeze
16
- private_constant :ADDRESSES
17
-
18
- def self.publish(engine, gateway_request)
19
- catch(:block) do
20
- # params have been parsed from the request body
21
- engine.publish('rails.request.body', gateway_request.parsed_body)
22
- engine.publish('rails.request.route_params', gateway_request.route_params)
23
-
24
- nil
25
- end
26
- end
27
-
28
- def self.subscribe(engine, context)
29
- engine.subscribe(*ADDRESSES) do |*values|
30
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
31
- body = values[0]
32
- path_params = values[1]
33
-
34
- persistent_data = {
35
- 'server.request.body' => body,
36
- 'server.request.path_params' => path_params,
37
- }
38
-
39
- waf_timeout = Datadog.configuration.appsec.waf_timeout
40
- result = context.run_waf(persistent_data, {}, waf_timeout)
41
-
42
- next unless result.match?
43
-
44
- yield result
45
- throw(:block, true) unless result.actions.empty?
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end
52
- end
53
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Contrib
6
- module Sinatra
7
- module Reactive
8
- # Dispatch data from a Sinatra request to the WAF context
9
- module Routed
10
- ADDRESSES = [
11
- 'sinatra.request.route_params',
12
- ].freeze
13
- private_constant :ADDRESSES
14
-
15
- def self.publish(engine, data)
16
- _request, route_params = data
17
-
18
- catch(:block) do
19
- engine.publish('sinatra.request.route_params', route_params.params)
20
-
21
- nil
22
- end
23
- end
24
-
25
- def self.subscribe(engine, context)
26
- engine.subscribe(*ADDRESSES) do |*values|
27
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
28
- path_params = values[0]
29
-
30
- persistent_data = {
31
- 'server.request.path_params' => path_params,
32
- }
33
-
34
- waf_timeout = Datadog.configuration.appsec.waf_timeout
35
- result = context.run_waf(persistent_data, {}, waf_timeout)
36
-
37
- next unless result.match?
38
-
39
- yield result
40
- throw(:block, true) unless result.actions.empty?
41
- end
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Monitor
6
- module Reactive
7
- # Dispatch data from Datadog::Kit::Identity.set_user to the WAF context
8
- module SetUser
9
- ADDRESSES = [
10
- 'usr.id',
11
- ].freeze
12
- private_constant :ADDRESSES
13
-
14
- def self.publish(engine, user)
15
- catch(:block) do
16
- engine.publish('usr.id', user.id)
17
-
18
- nil
19
- end
20
- end
21
-
22
- def self.subscribe(engine, context)
23
- engine.subscribe(*ADDRESSES) do |*values|
24
- Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
25
-
26
- user_id = values[0]
27
-
28
- persistent_data = {
29
- 'usr.id' => user_id,
30
- }
31
-
32
- waf_timeout = Datadog.configuration.appsec.waf_timeout
33
- result = context.run_waf(persistent_data, {}, waf_timeout)
34
-
35
- next unless result.match?
36
-
37
- yield result
38
- throw(:block, true) unless result.actions.empty?
39
- end
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Reactive
6
- # AddressHash for Reactive Engine
7
- class AddressHash < Hash
8
- def self.new(*arguments, &block)
9
- super { |h, k| h[k] = [] }
10
- end
11
-
12
- def addresses
13
- keys.flatten
14
- end
15
-
16
- def with(address)
17
- keys.select { |k| k.include?(address) }
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'address_hash'
4
- require_relative 'subscriber'
5
-
6
- module Datadog
7
- module AppSec
8
- module Reactive
9
- # Reactive Engine
10
- class Engine
11
- def initialize
12
- @data = {}
13
- @subscribers = AddressHash.new
14
- end
15
-
16
- def subscribe(*addresses, &block)
17
- @subscribers[addresses.freeze] << Subscriber.new(&block)
18
- end
19
-
20
- def publish(address, value)
21
- # check if someone has address subscribed
22
- if @subscribers.addresses.include?(address)
23
-
24
- # someone will be interested, set value
25
- @data[address] = value
26
-
27
- # find candidates i.e address groups that contain the just posted address
28
- @subscribers.with(address).each do |addresses|
29
- # find targets to the address group containing the posted address
30
- subscribers = @subscribers[addresses]
31
-
32
- # is all data for the targets available?
33
- if (addresses - @data.keys).empty?
34
- hash = addresses.each_with_object({}) { |a, h| h[a] = @data[a] }
35
- subscribers.each { |s| s.call(*hash.values) }
36
- end
37
- end
38
- end
39
- end
40
-
41
- private
42
-
43
- attr_reader :subscribers, :data
44
- end
45
- end
46
- end
47
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module AppSec
5
- module Reactive
6
- # Reactive Engine subscriber
7
- class Subscriber
8
- def initialize(&block)
9
- @block = block
10
- freeze
11
- end
12
-
13
- def call(*args)
14
- @block.call(*args)
15
- end
16
- end
17
- end
18
- end
19
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module Core
5
- module Remote
6
- module Transport
7
- module HTTP
8
- module API
9
- # An API configured with adapter and routes
10
- class Instance
11
- attr_reader \
12
- :adapter,
13
- :headers,
14
- :spec
15
-
16
- def initialize(spec, adapter, options = {})
17
- @spec = spec
18
- @adapter = adapter
19
- @headers = options.fetch(:headers, {})
20
- end
21
-
22
- def encoder
23
- spec.encoder
24
- end
25
-
26
- def call(env)
27
- # Add headers to request env, unless empty.
28
- env.headers.merge!(headers) unless headers.empty?
29
-
30
- # Send request env to the adapter.
31
- adapter.call(env)
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datadog
4
- module Core
5
- module Remote
6
- module Transport
7
- module HTTP
8
- module API
9
- # Specification for an HTTP API
10
- # Defines behaviors without specific configuration details.
11
- class Spec
12
- def initialize
13
- yield(self) if block_given?
14
- end
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,219 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../../configuration/agent_settings_resolver'
4
- require_relative '../../../transport/http/adapters/registry'
5
- require_relative '../../../transport/http/api/map'
6
-
7
- # TODO: Decouple standard transport/http/api/instance
8
- #
9
- # Separate classes are needed because transport/http/trace includes
10
- # Trace::API::Instance which closes over and uses a single spec, which is
11
- # negotiated as either /v3 or /v4 for the whole API at the spec level, but we
12
- # need an independent toplevel path at the endpoint level.
13
- #
14
- # Separate classes are needed because of `include Trace::API::Instance`.
15
- #
16
- # Below should be:
17
- # require_relative '../../../../datadog/core/transport/http/api/instance'
18
- require_relative 'api/instance'
19
-
20
- # TODO: Decouple transport/http/client
21
- #
22
- # The standard one does `include Transport::HTTP::Statistics` and performs
23
- # stats updates, which may or may not be desirable in general.
24
- #
25
- # Below should be:
26
- # require_relative '../../../../datadog/core/transport/http/client'
27
- require_relative 'client'
28
-
29
- # TODO: Decouple transport/http/builder
30
- #
31
- # This class is duplicated even though it is tantalisingly close to the
32
- # one in datadog/core/transport mostly because it refers to a different
33
- # `API::Instance` in `#api_instance_class` but also because it operates on a
34
- # different `HTTP::Client`, as well as de-hardcoding the transport class
35
- # `Transport::Traces::Transport` in `#to_transport`
36
- #
37
- # Additionally, its design makes it so there can be only one (API, Client,
38
- # Transport) triplet, requiring a new transport instance for any varying
39
- # element of the triplet.
40
-
41
- module Datadog
42
- module Core
43
- module Remote
44
- module Transport
45
- module HTTP
46
- # Builds new instances of Transport::HTTP::Client
47
- class Builder
48
- REGISTRY = Datadog::Core::Transport::HTTP::Adapters::Registry.new
49
-
50
- attr_reader \
51
- :apis,
52
- :api_options,
53
- :default_adapter,
54
- :default_api,
55
- :default_headers
56
-
57
- def initialize
58
- # Global settings
59
- @default_adapter = nil
60
- @default_headers = {}
61
-
62
- # Client settings
63
- @apis = Datadog::Core::Transport::HTTP::API::Map.new
64
- @default_api = nil
65
-
66
- # API settings
67
- @api_options = {}
68
-
69
- yield(self) if block_given?
70
- end
71
-
72
- def adapter(config, *args, **kwargs)
73
- @default_adapter = case config
74
- when Core::Configuration::AgentSettingsResolver::AgentSettings
75
- registry_klass = REGISTRY.get(config.adapter)
76
- raise UnknownAdapterError, config.adapter if registry_klass.nil?
77
-
78
- registry_klass.build(config)
79
- when Symbol
80
- registry_klass = REGISTRY.get(config)
81
- raise UnknownAdapterError, config if registry_klass.nil?
82
-
83
- registry_klass.new(*args, **kwargs)
84
- else
85
- config
86
- end
87
- end
88
-
89
- def headers(values = {})
90
- @default_headers.merge!(values)
91
- end
92
-
93
- # Adds a new API to the client
94
- # Valid options:
95
- # - :adapter
96
- # - :default
97
- # - :fallback
98
- # - :headers
99
- def api(key, spec, options = {})
100
- options = options.dup
101
-
102
- # Copy spec into API map
103
- @apis[key] = spec
104
-
105
- # Apply as default API, if specified to do so.
106
- @default_api = key if options.delete(:default) || @default_api.nil?
107
-
108
- # Save all other settings for initialization
109
- (@api_options[key] ||= {}).merge!(options)
110
- end
111
-
112
- def default_api=(key)
113
- raise UnknownApiError, key unless @apis.key?(key)
114
-
115
- @default_api = key
116
- end
117
-
118
- def to_transport(klass)
119
- raise NoDefaultApiError if @default_api.nil?
120
-
121
- klass.new(to_api_instances, @default_api)
122
- end
123
-
124
- def to_api_instances
125
- raise NoApisError if @apis.empty?
126
-
127
- @apis.inject(Datadog::Core::Transport::HTTP::API::Map.new) do |instances, (key, spec)|
128
- instances.tap do
129
- api_options = @api_options[key].dup
130
-
131
- # Resolve the adapter to use for this API
132
- adapter = api_options.delete(:adapter) || @default_adapter
133
- raise NoAdapterForApiError, key if adapter.nil?
134
-
135
- # Resolve fallback and merge headers
136
- fallback = api_options.delete(:fallback)
137
- api_options[:headers] = @default_headers.merge((api_options[:headers] || {}))
138
-
139
- # Add API::Instance with all settings
140
- instances[key] = api_instance_class.new(
141
- spec,
142
- adapter,
143
- api_options
144
- )
145
-
146
- # Configure fallback, if provided.
147
- instances.with_fallbacks(key => fallback) unless fallback.nil?
148
- end
149
- end
150
- end
151
-
152
- def api_instance_class
153
- API::Instance
154
- end
155
-
156
- # Raised when the API key does not match known APIs.
157
- class UnknownApiError < StandardError
158
- attr_reader :key
159
-
160
- def initialize(key)
161
- super()
162
-
163
- @key = key
164
- end
165
-
166
- def message
167
- "Unknown transport API '#{key}'!"
168
- end
169
- end
170
-
171
- # Raised when the identifier cannot be matched to an adapter.
172
- class UnknownAdapterError < StandardError
173
- attr_reader :type
174
-
175
- def initialize(type)
176
- super()
177
-
178
- @type = type
179
- end
180
-
181
- def message
182
- "Unknown transport adapter '#{type}'!"
183
- end
184
- end
185
-
186
- # Raised when an adapter cannot be resolved for an API instance.
187
- class NoAdapterForApiError < StandardError
188
- attr_reader :key
189
-
190
- def initialize(key)
191
- super()
192
-
193
- @key = key
194
- end
195
-
196
- def message
197
- "No adapter resolved for transport API '#{key}'!"
198
- end
199
- end
200
-
201
- # Raised when built without defining APIs.
202
- class NoApisError < StandardError
203
- def message
204
- 'No APIs configured for transport!'
205
- end
206
- end
207
-
208
- # Raised when client built without defining a default API.
209
- class NoDefaultApiError < StandardError
210
- def message
211
- 'No default API configured for transport!'
212
- end
213
- end
214
- end
215
- end
216
- end
217
- end
218
- end
219
- end