Class: Contrast::Agent::Telemetry::Client

Inherits:
Utils::NetHttpBase show all
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

#client_name

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

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.

Parameters:

Returns:

  • (Net::HTTP::Post)


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

Parameters:

  • res (Net::HTTPResponse)


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.

Parameters:

Returns:

  • (Net::HTTP, nil)

    Return open connection or nil



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

Parameters:



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

Parameters:

Returns:

  • (Boolean)


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