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_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, #deprecation, #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, #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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/chef/data_collector.rb', line 59

def initialize
  validate_data_collector_server_url!

  @all_resource_reports    = []
  @current_resource_loaded = nil
  @error_descriptions      = {}
  @expanded_run_list       = {}
  @http                    = Chef::HTTP.new(data_collector_server_url)
  @enabled                 = true
end

Instance Attribute Details

#all_resource_reportsObject (readonly)

Returns the value of attribute all_resource_reports.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def all_resource_reports
  @all_resource_reports
end

#current_resource_reportObject (readonly)

Returns the value of attribute current_resource_report.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def current_resource_report
  @current_resource_report
end

#enabledObject (readonly)

Returns the value of attribute enabled.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def enabled
  @enabled
end

#error_descriptionsObject (readonly)

Returns the value of attribute error_descriptions.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def error_descriptions
  @error_descriptions
end

#exceptionObject (readonly)

Returns the value of attribute exception.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def exception
  @exception
end

#expanded_run_listObject (readonly)

Returns the value of attribute expanded_run_list.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def expanded_run_list
  @expanded_run_list
end

#httpObject (readonly)

Returns the value of attribute http.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def http
  @http
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def run_context
  @run_context
end

#run_statusObject (readonly)

Returns the value of attribute run_status.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

def run_status
  @run_status
end

#statusObject (readonly)

Returns the value of attribute status.



55
56
57
# File 'lib/chef/data_collector.rb', line 55

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.



111
112
113
# File 'lib/chef/data_collector.rb', line 111

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



118
119
120
# File 'lib/chef/data_collector.rb', line 118

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.



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

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.



205
206
207
208
209
210
211
212
# File 'lib/chef/data_collector.rb', line 205

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.



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

def cookbook_sync_failed(cookbooks, exception)
  update_error_description(
    Formatters::ErrorMapper.cookbook_sync_failed(
      cookbooks,
      exception
    ).for_json
  )
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.



175
176
177
178
179
180
181
# File 'lib/chef/data_collector.rb', line 175

def resource_completed(new_resource)
  if current_resource_report && !nested_resource?(new_resource)
    current_resource_report.finish
    add_resource_report(current_resource_report)
    update_current_resource_report(nil)
  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.



127
128
129
130
# File 'lib/chef/data_collector.rb', line 127

def resource_current_state_loaded(new_resource, action, current_resource)
  return if nested_resource?(new_resource)
  update_current_resource_report(create_resource_report(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.



161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/data_collector.rb', line 161

def resource_failed(new_resource, action, exception)
  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.



142
143
144
145
146
147
148
# File 'lib/chef/data_collector.rb', line 142

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

  resource_report = create_resource_report(new_resource, action)
  resource_report.skipped(conditional)
  update_current_resource_report(resource_report)
end

#resource_up_to_date(new_resource, action) ⇒ Object

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



134
135
136
# File 'lib/chef/data_collector.rb', line 134

def resource_up_to_date(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).



153
154
155
# File 'lib/chef/data_collector.rb', line 153

def resource_updated(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.



90
91
92
# File 'lib/chef/data_collector.rb', line 90

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

#run_failed(exception) ⇒ Object

see EventDispatch::Base#run_failed



95
96
97
# File 'lib/chef/data_collector.rb', line 95

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.



193
194
195
196
197
198
199
200
# File 'lib/chef/data_collector.rb', line 193

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.



186
187
188
# File 'lib/chef/data_collector.rb', line 186

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.



77
78
79
80
81
82
83
84
85
# File 'lib/chef/data_collector.rb', line 77

def run_started(current_run_status)
  update_run_status(current_run_status)

  disable_reporter_on_error do
    send_to_data_collector(
      Chef::DataCollector::Messages.run_start_message(current_run_status).to_json
    )
  end
end