Class: Contrast::Agent::Telemetry::Client
- Inherits:
-
Utils::NetHttpBase
- Object
- Utils::NetHttpBase
- Contrast::Agent::Telemetry::Client
- Includes:
- Components::Logger::InstanceMethods
- Defined in:
- lib/contrast/agent/telemetry/client.rb
Overview
This module creates a Net::HTTP client and initiates a connection to the provided result
Constant Summary collapse
- ENDPOINT =
/TelemetryEvent.path
'api/v1/telemetry/metrics'
- EXCEPTIONS =
/Telemetry::Exception::Event.path
'api/v1/telemetry/exceptions'
- SERVICE_NAME =
'Telemetry'
Instance Attribute Summary
Attributes inherited from Utils::NetHttpBase
Instance Method Summary collapse
-
#build_request(event) ⇒ Net::HTTP::Post
This method will be responsible for building the request.
-
#handle_response(res) ⇒ Object
This method will handle the response from the tenant.
-
#initialize_connection(url) ⇒ Net::HTTP?
This method initializes the Net::HTTP client we’ll need.
-
#send_request(event, connection) ⇒ Object
This method will create the actual request and send it.
-
#valid_event?(event) ⇒ Boolean
This method will be responsible for validating the event.
Methods included from Components::Logger::InstanceMethods
Methods inherited from Utils::NetHttpBase
#connection_verified?, last_error, last_error=
Instance Method Details
#build_request(event) ⇒ Net::HTTP::Post
This method will be responsible for building the request. Because the telemetry collector expects to receive multiple events in a single request, we must always wrap the event in an array, even if there is only one.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/contrast/agent/telemetry/client.rb', line 35 def build_request event return unless valid_event?(event) header = { 'User-Agent' => "<#{ Contrast::Utils::ObjectShare::RUBY }>-<#{ Contrast::Agent::VERSION }>", 'Content-Type' => 'application/json' } request = Net::HTTP::Post.new(build_path(event), header) request.body = get_event_json(event) request end |
#handle_response(res) ⇒ Object
This method will handle the response from the tenant
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/contrast/agent/telemetry/client.rb', line 61 def handle_response res status_code = res.code.to_i ready_after = if res.to_hash.keys.map(&:downcase).include?('ready-after') res['Ready-After'] else 60 end logger.debug('[Telemetry] received response.', response_code: status_code) ready_after if status_code == 429 end |
#initialize_connection(url) ⇒ Net::HTTP?
This method initializes the Net::HTTP client we’ll need. it will validate the connection and make the first request. If connection is valid and response is available then the open connection is returned.
26 27 28 |
# File 'lib/contrast/agent/telemetry/client.rb', line 26 def initialize_connection url super(SERVICE_NAME, url, use_proxy: false, use_custom_cert: false) end |
#send_request(event, connection) ⇒ Object
This method will create the actual request and send it
50 51 52 53 54 55 56 |
# File 'lib/contrast/agent/telemetry/client.rb', line 50 def send_request event, connection return if connection.nil? || event.nil? return unless valid_event?(event) req = build_request(event) connection.request(req) end |
#valid_event?(event) ⇒ Boolean
This method will be responsible for validating the event. Valid if event is of a known Contrast::Agent::Telemetry type
76 77 78 79 80 81 82 83 84 |
# File 'lib/contrast/agent/telemetry/client.rb', line 76 def valid_event? event return true if event.cs__is_a?(Contrast::Agent::Telemetry::Event) return true if event.cs__is_a?(Contrast::Agent::Telemetry::StartupMetricsEvent) return true if event.cs__is_a?(Contrast::Agent::Telemetry::Exception::Event) return true if event.cs__is_a?(Contrast::Agent::Telemetry::InputAnalysisCacheEvent) return true if event.cs__is_a?(Contrast::Agent::Telemetry::InputAnalysisEncodingEvent) false end |