lib/labkit/context.rb in gitlab-labkit-0.13.3 vs lib/labkit/context.rb in gitlab-labkit-0.13.4
- old
+ new
@@ -19,10 +19,11 @@
#
class Context
LOG_KEY = "meta"
CORRELATION_ID_KEY = "correlation_id"
RAW_KEYS = [CORRELATION_ID_KEY].freeze
+ HEADER_PREFIX = "X-Gitlab-"
KNOWN_KEYS = %w[user project root_namespace subscription_plan caller_id
related_class feature_category].freeze
class << self
def with_context(attributes = {})
@@ -65,10 +66,14 @@
def known_log_keys
@known_log_keys ||= (KNOWN_KEYS.map(&method(:log_key)) + RAW_KEYS).freeze
end
+ def header_name(name)
+ HEADER_PREFIX + log_key(name).titlecase(keep_id_suffix: true).gsub(/\W/, "-")
+ end
+
private
def contexts
Thread.current[:labkit_contexts] ||= []
end
@@ -91,9 +96,15 @@
expand_data
end
def correlation_id
data[CORRELATION_ID_KEY]
+ end
+
+ def to_headers
+ to_h.except(CORRELATION_ID_KEY).transform_keys do |key|
+ self.class.header_name(key)
+ end
end
protected
def assign_attributes(attributes)