From a4cdce667a1bc1828eea875bdf3f49af6d96da70 Mon Sep 17 00:00:00 2001 From: PrajaktaPurohit Date: Sun, 19 Jan 2014 18:07:17 -0800 Subject: - Adding X-Remote-Request-Id to the set of headers for every request from CCR and knife that will be sent to erchef - Each knife request has a different X-Remote-Request-Id, where as it remains the same for all requests originating from the same chef-client run. - Adding and fixing tests --- lib/chef/client.rb | 6 +++++ lib/chef/http/remote_request_id.rb | 46 ++++++++++++++++++++++++++++++++++++++ lib/chef/knife/raw.rb | 1 + lib/chef/request_id.rb | 37 ++++++++++++++++++++++++++++++ lib/chef/resource_reporter.rb | 11 +++++---- lib/chef/rest.rb | 6 ++++- lib/chef/run_status.rb | 5 ++++- lib/chef/server_api.rb | 4 +++- 8 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 lib/chef/http/remote_request_id.rb create mode 100644 lib/chef/request_id.rb (limited to 'lib/chef') diff --git a/lib/chef/client.rb b/lib/chef/client.rb index 722c9915e9..638047331a 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -44,6 +44,7 @@ require 'chef/version' require 'chef/resource_reporter' require 'chef/run_lock' require 'chef/policy_builder' +require 'chef/request_id' require 'ohai' require 'rbconfig' @@ -391,10 +392,12 @@ class Chef # don't add code that may fail before entering this section to be sure to release lock begin runlock.save_pid + request_id = Chef::RequestID.instance.request_id run_context = nil @events.run_start(Chef::VERSION) Chef::Log.info("*** Chef #{Chef::VERSION} ***") Chef::Log.info "Chef-client pid: #{Process.pid}" + Chef::Log.debug("Chef-client request_id: #{request_id}") enforce_path_sanity run_ohai @events.ohai_completed(node) @@ -404,6 +407,7 @@ class Chef build_node + run_status.run_id = request_id run_status.start_clock Chef::Log.info("Starting Chef Run for #{node.name}") run_started @@ -434,6 +438,8 @@ class Chef @events.run_failed(e) raise ensure + Chef::RequestID.instance.reset_request_id + request_id = nil @run_status = nil run_context = nil runlock.release diff --git a/lib/chef/http/remote_request_id.rb b/lib/chef/http/remote_request_id.rb new file mode 100644 index 0000000000..6bec5dba4f --- /dev/null +++ b/lib/chef/http/remote_request_id.rb @@ -0,0 +1,46 @@ +# Author:: Prajakta Purohit () +# Copyright:: Copyright (c) 2009, 2010, 2013, 2014 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/request_id' + +class Chef + class HTTP + class RemoteRequestID + + def initialize(opts={}) + end + + def handle_request(method, url, headers={}, data=false) + headers.merge!({'X-REMOTE-REQUEST-ID' => Chef::RequestID.instance.request_id}) + [method, url, headers, data] + end + + def handle_response(http_response, rest_request, return_value) + [http_response, rest_request, return_value] + end + + def stream_response_handler(response) + nil + end + + def handle_stream_complete(http_response, rest_request, return_value) + [http_response, rest_request, return_value] + end + + end + end +end diff --git a/lib/chef/knife/raw.rb b/lib/chef/knife/raw.rb index 2756de1a5a..954d46beee 100644 --- a/lib/chef/knife/raw.rb +++ b/lib/chef/knife/raw.rb @@ -42,6 +42,7 @@ class Chef use Chef::HTTP::CookieManager use Chef::HTTP::Decompressor use Chef::HTTP::Authenticator + use Chef::HTTP::RemoteRequestID end def run diff --git a/lib/chef/request_id.rb b/lib/chef/request_id.rb new file mode 100644 index 0000000000..7fc177c633 --- /dev/null +++ b/lib/chef/request_id.rb @@ -0,0 +1,37 @@ +# Author:: Prajakta Purohit () +# Copyright:: Copyright (c) 2009, 2010, 2013, 2014 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/monkey_patches/securerandom' +require 'singleton' + +class Chef + class RequestID + include Singleton + + def reset_request_id + @request_id = nil + end + + def request_id + @request_id ||= generate_request_id + end + + def generate_request_id + SecureRandom.uuid + end + end +end diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 04f4ee26de..d191710cb4 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -107,7 +107,6 @@ class Chef @pending_update = nil @status = "success" @exception = nil - @run_id = SecureRandom.uuid @rest_client = rest_client @error_descriptions = {} end @@ -118,7 +117,7 @@ class Chef if reporting_enabled? begin resource_history_url = "reports/nodes/#{node_name}/runs" - server_response = @rest_client.post_rest(resource_history_url, {:action => :start, :run_id => @run_id, + server_response = @rest_client.post_rest(resource_history_url, {:action => :start, :run_id => run_id, :start_time => start_time.to_s}, headers) rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e handle_error_starting_run(e, resource_history_url) @@ -158,6 +157,10 @@ class Chef @reporting_enabled = false end + def run_id + @run_status.run_id + end + def resource_current_state_loaded(new_resource, action, current_resource) unless nested_resource?(new_resource) @pending_update = ResourceReport.new_with_current_state(new_resource, action, current_resource) @@ -214,8 +217,8 @@ class Chef def post_reporting_data if reporting_enabled? run_data = prepare_run_data - resource_history_url = "reports/nodes/#{node_name}/runs/#{@run_id}" - Chef::Log.info("Sending resource update report (run-id: #{@run_id})") + resource_history_url = "reports/nodes/#{node_name}/runs/#{run_id}" + Chef::Log.info("Sending resource update report (run-id: #{run_id})") Chef::Log.debug run_data.inspect compressed_data = encode_gzip(run_data.to_json) begin diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb index a1139d7fa2..73ca4b3293 100644 --- a/lib/chef/rest.rb +++ b/lib/chef/rest.rb @@ -36,6 +36,7 @@ require 'chef/http/validate_content_length' require 'chef/config' require 'chef/exceptions' require 'chef/platform/query_helpers' +require 'chef/http/remote_request_id' class Chef # == Chef::REST @@ -62,6 +63,7 @@ class Chef @decompressor = Decompressor.new(options) @authenticator = Authenticator.new(options) + @request_id = RemoteRequestID.new(options) @middlewares << ValidateContentLength.new(options) @middlewares << JSONInput.new(options) @@ -69,6 +71,8 @@ class Chef @middlewares << CookieManager.new(options) @middlewares << @decompressor @middlewares << @authenticator + @middlewares << @request_id + end def signing_key_filename @@ -132,7 +136,7 @@ class Chef def raw_http_request(method, path, headers, data) url = create_url(path) method, url, headers, data = @authenticator.handle_request(method, url, headers, data) - + method, url, headers, data = @request_id.handle_request(method, url, headers, data) response, rest_request, return_value = send_http_request(method, url, headers, data) response.error! unless success_response?(response) return_value diff --git a/lib/chef/run_status.rb b/lib/chef/run_status.rb index 9354f7872a..0f181426b0 100644 --- a/lib/chef/run_status.rb +++ b/lib/chef/run_status.rb @@ -37,6 +37,8 @@ class Chef::RunStatus attr_writer :exception + attr_accessor :run_id + def initialize(node, events) @node = node @events = events @@ -112,7 +114,8 @@ class Chef::RunStatus :all_resources => all_resources, :updated_resources => updated_resources, :exception => formatted_exception, - :backtrace => backtrace} + :backtrace => backtrace, + :run_id => run_id} end # Returns a string of the format "ExceptionClass: message" or +nil+ if no diff --git a/lib/chef/server_api.rb b/lib/chef/server_api.rb index e9e7593dd6..8cdcd7a09d 100644 --- a/lib/chef/server_api.rb +++ b/lib/chef/server_api.rb @@ -22,6 +22,7 @@ require 'chef/http/cookie_manager' require 'chef/http/decompressor' require 'chef/http/json_input' require 'chef/http/json_output' +require 'chef/http/remote_request_id' class Chef class ServerAPI < Chef::HTTP @@ -37,5 +38,6 @@ class Chef use Chef::HTTP::CookieManager use Chef::HTTP::Decompressor use Chef::HTTP::Authenticator + use Chef::HTTP::RemoteRequestID end -end \ No newline at end of file +end -- cgit v1.2.1