Class: Chef::DataCollector::Reporter

Inherits:
EventDispatch::Base show all
Defined in:
lib/chef/data_collector.rb

Overview

Chef::DataCollector::Reporter

Provides an event handler that can be registered to report on Chef run data. Unlike the existing Chef::ResourceReporter event handler, the DataCollector handler is not tied to a Chef Server / Chef Reporting and exports its data through a webhook-like mechanism to a configured endpoint.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from EventDispatch::Base

#attribute_changed, #attribute_file_load_failed, #attribute_file_loaded, #attribute_load_complete, #attribute_load_start, #audit_phase_complete, #audit_phase_failed, #audit_phase_start, #control_example_failure, #control_example_success, #control_group_started, #cookbook_clean_complete, #cookbook_clean_start, #cookbook_gem_failed, #cookbook_gem_finished, #cookbook_gem_installing, #cookbook_gem_start, #cookbook_gem_using, #cookbook_resolution_complete, #cookbook_resolution_start, #cookbook_sync_complete, #cookbook_sync_start, #definition_file_load_failed, #definition_file_loaded, #definition_load_complete, #definition_load_start, #handler_executed, #handlers_completed, #handlers_start, #library_file_load_failed, #library_file_loaded, #library_load_complete, #library_load_start, #lwrp_file_load_failed, #lwrp_file_loaded, #lwrp_load_complete, #lwrp_load_start, #msg, #node_load_completed, #node_load_failed, #node_load_start, #ohai_completed, #ohai_plugin_file_load_failed, #ohai_plugin_file_loaded, #ohai_plugin_load_complete, #ohai_plugin_load_start, #policyfile_loaded, #provider_requirement_failed, #recipe_file_load_failed, #recipe_file_loaded, #recipe_load_complete, #recipe_load_start, #recipe_not_found, #registration_completed, #registration_failed, #registration_start, #removed_cookbook_file, #resource_action_start, #resource_bypassed, #resource_current_state_load_bypassed, #resource_failed_retriable, #resource_update_applied, #resource_update_progress, #run_start, #skipping_registration, #stream_closed, #stream_opened, #stream_output, #synchronized_cookbook, #updated_cookbook_file, #whyrun_assumption

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/data_collector.rb', line 109

def initialize
  validate_data_collector_server_url!
  validate_data_collector_output_locations! if data_collector_output_locations
  @all_resource_reports    = []
  @current_resource_loaded = nil
  @error_descriptions      = {}
  @expanded_run_list       = {}
  @deprecations            = Set.new
  @enabled                 = true

  @http = setup_http_client(data_collector_server_url)
  if data_collector_output_locations
    @http_output_locations = setup_http_output_locations if data_collector_output_locations[:urls]
  end
end

Instance Attribute Details

#all_resource_reportsObject (readonly)

Returns the value of attribute all_resource_reports.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def all_resource_reports
  @all_resource_reports
end

#current_resource_reportObject (readonly)

Returns the value of attribute current_resource_report.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def current_resource_report
  @current_resource_report
end

#deprecationsObject (readonly)

Returns the value of attribute deprecations.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def deprecations
  @deprecations
end

#enabledObject (readonly)

Returns the value of attribute enabled.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def enabled
  @enabled
end

#error_descriptionsObject (readonly)

Returns the value of attribute error_descriptions.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def error_descriptions
  @error_descriptions
end

#exceptionObject (readonly)

Returns the value of attribute exception.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def exception
  @exception
end

#expanded_run_listObject (readonly)

Returns the value of attribute expanded_run_list.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def expanded_run_list
  @expanded_run_list
end

#httpObject (readonly)

Returns the value of attribute http.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def http
  @http
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def run_context
  @run_context
end

#run_statusObject (readonly)

Returns the value of attribute run_status.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def run_status
  @run_status
end

#statusObject (readonly)

Returns the value of attribute status.



105
106
107
# File 'lib/chef/data_collector.rb', line 105

def status
  @status
end

Instance Method Details

#converge_completeObject

see EventDispatch::Base#converge_complete At the end of the converge, we add any unprocessed resources to our report list.



166
167
168
# File 'lib/chef/data_collector.rb', line 166

def converge_complete
  detect_unprocessed_resources
end

#converge_failed(exception) ⇒ Object

see EventDispatch::Base#converge_failed At the end of the converge, we add any unprocessed resources to our report list



173
174
175
# File 'lib/chef/data_collector.rb', line 173

def converge_failed(exception)
  detect_unprocessed_resources
end

#converge_start(run_context) ⇒ Object

see EventDispatch::Base#converge_start Upon receipt, we stash the run_context for use at the end of the run in order to determine what resource+action combinations have not yet fired so we can report on unprocessed resources.



159
160
161
# File 'lib/chef/data_collector.rb', line 159

def converge_start(run_context)
  @run_context = run_context
end

#cookbook_resolution_failed(expanded_run_list, exception) ⇒ Object

see EventDispatch::Base#cookbook_resolution_failed The run error text is updated with the output of the appropriate formatter.



262
263
264
265
266
267
268
269
# File 'lib/chef/data_collector.rb', line 262

def cookbook_resolution_failed(expanded_run_list, exception)
  update_error_description(
    Formatters::ErrorMapper.cookbook_resolution_failed(
      expanded_run_list,
      exception
    ).for_json
  )
end

#cookbook_sync_failed(cookbooks, exception) ⇒ Object

see EventDispatch::Base#cookbook_sync_failed The run error text is updated with the output of the appropriate formatter.



274
275
276
277
278
279
280
281
# File 'lib/chef/data_collector.rb', line 274

def cookbook_sync_failed(cookbooks, exception)
  update_error_description(
    Formatters::ErrorMapper.cookbook_sync_failed(
      cookbooks,
      exception
    ).for_json
  )
end

#deprecation(message, location = ) ⇒ Object

see EventDispatch::Base#deprecation Append a received deprecation to the list of deprecations



285
286
287
# File 'lib/chef/data_collector.rb', line 285

def deprecation(message, location = caller(2..2)[0])
  add_deprecation(message.message, message.url, location)
end

#resource_completed(new_resource) ⇒ Object

see EventDispatch::Base#resource_completed Mark the ResourceReport instance as finished (for timing details). This marks the end of this resource during this run.



232
233
234
235
236
237
238
# File 'lib/chef/data_collector.rb', line 232

def resource_completed(new_resource)
  if current_resource_report && !nested_resource?(new_resource)
    current_resource_report.finish
    add_resource_report(current_resource_report)
    clear_current_resource_report
  end
end

#resource_current_state_loaded(new_resource, action, current_resource) ⇒ Object

see EventDispatch::Base#resource_current_state_loaded Create a new ResourceReport instance that we’ll use to track the state of this resource during the run. Nested resources are ignored as they are assumed to be an inline resource of a custom resource, and we only care about tracking top-level resources.



182
183
184
185
# File 'lib/chef/data_collector.rb', line 182

def resource_current_state_loaded(new_resource, action, current_resource)
  return if nested_resource?(new_resource)
  initialize_resource_report_if_needed(new_resource, action, current_resource)
end

#resource_failed(new_resource, action, exception) ⇒ Object

see EventDispatch::Base#resource_failed Flag the current ResourceReport as failed and supply the exception as long as it’s a top-level resource, and update the run error text with the proper Formatter.



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/chef/data_collector.rb', line 217

def resource_failed(new_resource, action, exception)
  initialize_resource_report_if_needed(new_resource, action)
  current_resource_report.failed(exception) unless nested_resource?(new_resource)
  update_error_description(
    Formatters::ErrorMapper.resource_failed(
      new_resource,
      action,
      exception
    ).for_json
  )
end

#resource_skipped(new_resource, action, conditional) ⇒ Object

see EventDispatch::Base#resource_skipped If this is a top-level resource, we create a ResourceReport instance (because a skipped resource does not trigger the resource_current_state_loaded event), and flag it as skipped.



198
199
200
201
202
203
# File 'lib/chef/data_collector.rb', line 198

def resource_skipped(new_resource, action, conditional)
  return if nested_resource?(new_resource)

  initialize_resource_report_if_needed(new_resource, action)
  current_resource_report.skipped(conditional)
end

#resource_up_to_date(new_resource, action) ⇒ Object

see EventDispatch::Base#resource_up_to_date Mark our ResourceReport status accordingly



189
190
191
192
# File 'lib/chef/data_collector.rb', line 189

def resource_up_to_date(new_resource, action)
  initialize_resource_report_if_needed(new_resource, action)
  current_resource_report.up_to_date unless nested_resource?(new_resource)
end

#resource_updated(new_resource, action) ⇒ Object

see EventDispatch::Base#resource_updated Flag the current ResourceReport instance as updated (as long as it’s a top-level resource).



208
209
210
211
# File 'lib/chef/data_collector.rb', line 208

def resource_updated(new_resource, action)
  initialize_resource_report_if_needed(new_resource, action)
  current_resource_report.updated unless nested_resource?(new_resource)
end

#run_completed(node) ⇒ Object

see EventDispatch::Base#run_completed Upon receipt, we will send our run completion message to the configured DataCollector endpoint.



145
146
147
# File 'lib/chef/data_collector.rb', line 145

def run_completed(node)
  send_run_completion(status: "success")
end

#run_failed(exception) ⇒ Object

see EventDispatch::Base#run_failed



150
151
152
# File 'lib/chef/data_collector.rb', line 150

def run_failed(exception)
  send_run_completion(status: "failure")
end

#run_list_expand_failed(node, exception) ⇒ Object

see EventDispatch::Base#run_list_expand_failed The run error text is updated with the output of the appropriate formatter.



250
251
252
253
254
255
256
257
# File 'lib/chef/data_collector.rb', line 250

def run_list_expand_failed(node, exception)
  update_error_description(
    Formatters::ErrorMapper.run_list_expand_failed(
      node,
      exception
    ).for_json
  )
end

#run_list_expanded(run_list_expansion) ⇒ Object

see EventDispatch::Base#run_list_expanded The expanded run list is stored for later use by the run_completed event and message.



243
244
245
# File 'lib/chef/data_collector.rb', line 243

def run_list_expanded(run_list_expansion)
  @expanded_run_list = run_list_expansion
end

#run_started(current_run_status) ⇒ Object

see EventDispatch::Base#run_started Upon receipt, we will send our run start message to the configured DataCollector endpoint. Depending on whether the user has configured raise_on_failure, if we cannot send the message, we will either disable the DataCollector Reporter for the duration of this run, or we’ll raise an exception.



132
133
134
135
136
137
138
139
140
# File 'lib/chef/data_collector.rb', line 132

def run_started(current_run_status)
  update_run_status(current_run_status)

  message = Chef::DataCollector::Messages.run_start_message(current_run_status)
  disable_reporter_on_error do
    send_to_data_collector(message)
  end
  send_to_output_locations(message) if data_collector_output_locations
end