Class: Contrast::Agent::Telemetry::Base

Inherits:
WorkerThread show all
Extended by:
Components::Logger::InstanceMethods, Config::EnvVariables
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/agent/telemetry/base.rb

Overview

This class will initialize and hold everything needed for the telemetry

Constant Summary collapse

URL =

this is where we will send the data from the agents

'https://telemetry.ruby.contrastsecurity.com/'
SUGGESTED_TIMEOUT =

Suggested timeout after each send is to be 3 hours (10800 seconds)

10_800

Constants included from Config::EnvVariables

Config::EnvVariables::ENV_VARIABLES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::EnvVariables

return_value

Methods included from Components::Logger::InstanceMethods

cef_logger, logger

Methods inherited from WorkerThread

#clean_properties, #initialize, #running?

Constructor Details

This class inherits a constructor from Contrast::Agent::WorkerThread

Class Method Details

.application_idString

The unique identifier of this application based on Mac address and application name.

Returns:



31
32
33
# File 'lib/contrast/agent/telemetry/base.rb', line 31

def application_id
  Contrast::Agent::Telemetry::Identifier.application_id
end

.disable!Object



79
80
81
# File 'lib/contrast/agent/telemetry/base.rb', line 79

def disable!
  @enabled = false
end

.enabled?Boolean

If the feature as a whole is enabled or not, as determined by user opt-out

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/contrast/agent/telemetry/base.rb', line 45

def enabled?
  @enabled = telemetry_enabled? if @enabled.nil?
  @enabled
end

.instance_idString

The unique identifier of this instance, based on Mac address.

Returns:



38
39
40
# File 'lib/contrast/agent/telemetry/base.rb', line 38

def instance_id
  Contrast::Agent::Telemetry::Identifier.instance_id
end

.ip_opt_out?Boolean

In case of connection error, do not create the background thread or queue, as if the opt-out env var was set.

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/contrast/agent/telemetry/base.rb', line 54

def ip_opt_out?
  @_ip_opt_out ||= begin
    test_conn = Contrast::Agent::Telemetry::Client.new.initialize_connection(URL)

    if test_conn.nil? || Contrast::Utils::NetHttpBase.last_error
      # log if error:
      if defined?(Contrast) && defined?(Contrast::CONFIG) && defined?(Contrast::CONFIG)
        Contrast::CONFIG.proto_logger.warn('[Telemetry] connection error disabling...',
                                           error: Contrast::Utils::NetHttpBase.last_error)

      end
      # Disable telemetry:
      @enabled = false
      true
    else
      # Close the connection
      test_conn.finish if test_conn.started?
      false
    end
  end
rescue StandardError
  @enabled = false
  true
end

Instance Method Details

#attempt_to_start?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/contrast/agent/telemetry/base.rb', line 103

def attempt_to_start?
  return unless super

  unless cs__class.enabled?
    logger.info('[Telemetry] Telemetry service is disabled!')
    return false
  end

  logger.debug('[Telemetry] Attempting to start telemetry thread') unless running?
  true
end

#clientObject



99
100
101
# File 'lib/contrast/agent/telemetry/base.rb', line 99

def client
  @_client ||= Contrast::Agent::Reporting::Telemetry::Interface.new
end

#delete_queue!Object



135
136
137
138
# File 'lib/contrast/agent/telemetry/base.rb', line 135

def delete_queue!
  @_queue&.close
  @_queue = nil
end

#send_event(event) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/contrast/agent/telemetry/base.rb', line 123

def send_event event
  if ::Contrast::AGENT.disabled?
    logger.debug('[Telemetry] Attempted to queue event with Agent disabled', caller: caller, event: event)
    return
  end

  return unless cs__class.enabled?

  logger.debug('[Telemetry] Enqueued event for sending', event_type: event.cs__class)
  queue << event if event
end

#start_thread!Object



115
116
117
118
119
120
121
# File 'lib/contrast/agent/telemetry/base.rb', line 115

def start_thread!
  return unless attempt_to_start?
  return if running?

  logger.debug('[Telemetry] Starting background telemetry thread.')
  @_thread = create_thread
end

#stop!Object



140
141
142
143
144
145
146
147
148
# File 'lib/contrast/agent/telemetry/base.rb', line 140

def stop!
  return unless running?

  super
  delete_queue!
  Contrast::TELEMETRY_EXCEPTIONS&.clear
  Contrast::TELEMETRY_IA_CACHE&.clear
  Contrast::TELEMETRY_BASE64_HASH&.clear
end