summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrajaktaPurohit <prajakta@opscode.com>2014-01-19 18:07:17 -0800
committerPrajaktaPurohit <prajakta@opscode.com>2014-01-23 23:38:37 -0800
commitbc5a91236c69a091eff373a2e129da7b681806a9 (patch)
tree43134b3a0024f156e0d60da3796960c881ade6a8
parent4bffa7d9682170cd940d544ddadc485a8c90c032 (diff)
downloadchef-bc5a91236c69a091eff373a2e129da7b681806a9.tar.gz
Adding request_id to the set of headers for every request that will be
sent to erchef
-rw-r--r--lib/chef/client.rb5
-rw-r--r--lib/chef/http/request_id.rb20
-rw-r--r--lib/chef/resource_reporter.rb2
-rw-r--r--lib/chef/rest.rb6
-rw-r--r--lib/chef/run_id.rb19
-rw-r--r--lib/chef/run_status.rb5
6 files changed, 54 insertions, 3 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 390dc247ab..c8e6460585 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/run_id'
require 'ohai'
require 'rbconfig'
@@ -362,10 +363,13 @@ class Chef
# don't add code that may fail before entering this section to be sure to release lock
begin
runlock.save_pid
+ Chef::RunID.instance.reset_run_id
+ @run_id = Chef::RunID.instance.run_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 run_id: #{@run_id}")
enforce_path_sanity
run_ohai
@events.ohai_completed(node)
@@ -375,6 +379,7 @@ class Chef
build_node
+ run_status.run_id = @run_id
run_status.start_clock
Chef::Log.info("Starting Chef Run for #{node.name}")
run_started
diff --git a/lib/chef/http/request_id.rb b/lib/chef/http/request_id.rb
new file mode 100644
index 0000000000..02a57c7541
--- /dev/null
+++ b/lib/chef/http/request_id.rb
@@ -0,0 +1,20 @@
+class Chef
+ class HTTP
+ class RequestID
+
+ def handle_request(method, url, headers={}, data=false)
+ headers.merge!({'X-Ops-RequestId' => Chef::RunID.instance.run_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
+
+ end
+ end
+end
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb
index d29949086e..6f25ae25e8 100644
--- a/lib/chef/resource_reporter.rb
+++ b/lib/chef/resource_reporter.rb
@@ -107,13 +107,13 @@ class Chef
@pending_update = nil
@status = "success"
@exception = nil
- @run_id = SecureRandom.uuid
@rest_client = rest_client
@error_descriptions = {}
end
def run_started(run_status)
@run_status = run_status
+ @run_id = @run_status.run_id
if reporting_enabled?
begin
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index 04ee0b0cb2..39e338963c 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -35,6 +35,7 @@ require 'chef/http/cookie_manager'
require 'chef/config'
require 'chef/exceptions'
require 'chef/platform/query_helpers'
+require 'chef/http/request_id'
class Chef
# == Chef::REST
@@ -61,12 +62,15 @@ class Chef
@decompressor = Decompressor.new(options)
@authenticator = Authenticator.new(options)
+ @request_id = RequestID.new
@middlewares << JSONInput.new(options)
@middlewares << JSONToModelOutput.new(options)
@middlewares << CookieManager.new(options)
@middlewares << @decompressor
@middlewares << @authenticator
+ @middlewares << @request_id
+
end
def signing_key_filename
@@ -130,7 +134,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_id.rb b/lib/chef/run_id.rb
new file mode 100644
index 0000000000..9756d81b71
--- /dev/null
+++ b/lib/chef/run_id.rb
@@ -0,0 +1,19 @@
+require 'singleton'
+
+class Chef
+ class RunID
+ include Singleton
+
+ def reset_run_id
+ @run_id = nil
+ end
+
+ def run_id
+ @run_id ||= generate_run_id
+ end
+
+ def generate_run_id
+ SecureRandom.uuid
+ end
+ end
+end
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