Class: Contrast::Components::AppContext::Interface

Inherits:
Object
  • Object
show all
Includes:
ComponentBase, Interface
Defined in:
lib/contrast/components/app_context.rb

Overview

A wrapper build around the Common Agent Configuration project to allow for access of the values contained in its parent_configuration_spec.yaml. Specifically, this allows for querying the state of the Application, including the Client, Process, and Server information.

Constant Summary collapse

DEFAULT_APP_NAME =
'rails'
DEFAULT_APP_PATH =
'/'
DEFAULT_SERVER_NAME =
'localhost'
DEFAULT_SERVER_PATH =
'/'

Instance Method Summary collapse

Methods included from Interface

included

Methods included from ComponentBase

included

Constructor Details

#initializeInterface

Returns a new instance of Interface.



26
27
28
# File 'lib/contrast/components/app_context.rb', line 26

def initialize
  original_pid
end

Instance Method Details

#build_agent_startup_messageObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/contrast/components/app_context.rb', line 93

def build_agent_startup_message
  msg = Contrast::Api::Dtm::AgentStartup.new
  msg.server_name      = Contrast::Utils::StringUtils.protobuf_format server_name
  msg.server_path      = Contrast::Utils::StringUtils.protobuf_format server_path
  msg.server_type      = Contrast::Utils::StringUtils.protobuf_format server_type
  msg.server_version   = Contrast::Agent::VERSION
  msg.version          = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.version
  msg.environment      = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.environment
  msg.server_tags      = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.tags
  msg.application_tags = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.tags
  msg.library_tags     = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.inventory.tags
  msg.finding_tags     = Contrast::Utils::StringUtils.protobuf_format ASSESS.tags
  logger.info('Application context',
              server_name: msg.server_name,
              server_path: msg.server_path,
              server_type: msg.server_type,
              application_name: name,
              application_path: path,
              application_language: Contrast::Utils::ObjectShare::RUBY)

  msg
end

#build_app_startup_messageObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/contrast/components/app_context.rb', line 79

def build_app_startup_message
  msg = Contrast::Api::Dtm::ApplicationCreate.new

  msg.group            = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.group
  msg.app_version      = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.version.to_s
  msg.code             = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.code
  msg.         = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.
  # Other fields have limits in TeamServer, the rest don't.
  msg.session_id       = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.session_id,        truncate: false
  msg. = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.,  truncate: false

  msg
end

#client_idObject



128
129
130
# File 'lib/contrast/components/app_context.rb', line 128

def client_id
  @_client_id ||= [name, pgid].join('-')
end

#disabled_agent_rake_tasksObject



136
137
138
# File 'lib/contrast/components/app_context.rb', line 136

def disabled_agent_rake_tasks
  CONFIG.root.agent.ruby.disabled_agent_rake_tasks
end

#in_new_process?Boolean

Determines if the Process we’re currently in matches that of the Process in which the App Context instance was created. If it doesn’t, that indicates the running context is in a new Process.

Returns:

  • (Boolean)

    if we’re in the original Process in which the App Context instance was initialized.



146
147
148
149
150
# File 'lib/contrast/components/app_context.rb', line 146

def in_new_process?
  current_pid = Process.pid
  original_pid = pid
  current_pid != original_pid
end

#instrument_middleware_stack?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/contrast/components/app_context.rb', line 132

def instrument_middleware_stack?
  !Contrast::Utils::JobServersRunning.job_servers_running?
end

#nameObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/contrast/components/app_context.rb', line 38

def name
  @_name ||= begin
    tmp = CONFIG.root.application.name
    tmp = Contrast::Agent.framework_manager.app_name unless Contrast::Utils::StringUtils.present?(tmp)
    tmp = File.basename(Dir.pwd) unless Contrast::Utils::StringUtils.present?(tmp)
    Contrast::Utils::StringUtils.truncate(tmp, DEFAULT_APP_NAME)
  rescue StandardError
    DEFAULT_APP_NAME
  end
end

#pathObject



49
50
51
52
53
54
55
56
# File 'lib/contrast/components/app_context.rb', line 49

def path
  @_path ||= begin
    tmp = CONFIG.root.application.path
    Contrast::Utils::StringUtils.truncate(tmp, DEFAULT_APP_PATH)
  rescue StandardError
    DEFAULT_APP_PATH
  end
end

#pgidObject



124
125
126
# File 'lib/contrast/components/app_context.rb', line 124

def pgid
  Process.getpgid(pid)
end

#pidObject



116
117
118
# File 'lib/contrast/components/app_context.rb', line 116

def pid
  Process.pid
end

#ppidObject



120
121
122
# File 'lib/contrast/components/app_context.rb', line 120

def ppid
  Process.ppid
end

#server_nameObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/contrast/components/app_context.rb', line 58

def server_name
  @_server_name ||= begin
    tmp = CONFIG.root.server.name
    tmp = Socket.gethostname unless Contrast::Utils::StringUtils.present?(tmp)
    tmp = Contrast::Utils::StringUtils.force_utf8(tmp)
    Contrast::Utils::StringUtils.truncate(tmp, DEFAULT_SERVER_NAME)
  rescue StandardError
    DEFAULT_SERVER_NAME
  end
end

#server_pathObject



69
70
71
72
73
74
75
76
77
# File 'lib/contrast/components/app_context.rb', line 69

def server_path
  @_server_path ||= begin
    tmp = CONFIG.root.server.path
    tmp = Dir.pwd unless Contrast::Utils::StringUtils.present?(tmp)
    Contrast::Utils::StringUtils.truncate(tmp, DEFAULT_SERVER_PATH)
  rescue StandardError
    DEFAULT_SERVER_PATH
  end
end

#server_typeObject



30
31
32
33
34
35
36
# File 'lib/contrast/components/app_context.rb', line 30

def server_type
  @_server_type ||= begin
    tmp = CONFIG.root.server.type
    tmp = Contrast::Agent.framework_manager.server_type unless Contrast::Utils::StringUtils.present?(tmp)
    tmp
  end
end