diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2018-12-05 23:54:40 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2018-12-06 22:46:14 +0300 |
commit | 39c1731a53d1014eab7c876d70632b1abf738712 (patch) | |
tree | 5d51a48a32a92fdedf7b907a32caa172d1c0d923 /lib/gitlab/correlation_id.rb | |
parent | cfe484795d4ddb8c7b3247802547e3da74c64cf2 (diff) |
Log and pass correlation-id between Unicorn, Sidekiq and Gitaly
The Correlation ID is taken or generated from received X-Request-ID.
Then it is being passed to all executed services (sidekiq workers
or gitaly calls).
The Correlation ID is logged in all structured logs as `correlation_id`.
Diffstat (limited to 'lib/gitlab/correlation_id.rb')
-rw-r--r-- | lib/gitlab/correlation_id.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/gitlab/correlation_id.rb b/lib/gitlab/correlation_id.rb new file mode 100644 index 00000000000..0f9bde4390e --- /dev/null +++ b/lib/gitlab/correlation_id.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module Gitlab + module CorrelationId + LOG_KEY = 'correlation_id'.freeze + + class << self + def use_id(correlation_id, &blk) + # always generate a id if null is passed + correlation_id ||= new_id + + ids.push(correlation_id || new_id) + + begin + yield(current_id) + ensure + ids.pop + end + end + + def current_id + ids.last + end + + def current_or_new_id + current_id || new_id + end + + private + + def ids + Thread.current[:correlation_id] ||= [] + end + + def new_id + SecureRandom.uuid + end + end + end +end |