|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Code0 |
| 4 | + module ZeroTrack |
| 5 | + class Context |
| 6 | + LOG_KEY = 'meta' |
| 7 | + CORRELATION_ID_KEY = 'correlation_id' |
| 8 | + RAW_KEYS = [CORRELATION_ID_KEY].freeze |
| 9 | + |
| 10 | + class << self |
| 11 | + def with_context(attributes = {}) |
| 12 | + context = push(attributes) |
| 13 | + |
| 14 | + begin |
| 15 | + yield(context) |
| 16 | + ensure |
| 17 | + pop(context) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + def push(new_attributes = {}) |
| 22 | + new_context = current&.merge(new_attributes) || new(new_attributes) |
| 23 | + |
| 24 | + contexts.push(new_context) |
| 25 | + |
| 26 | + new_context |
| 27 | + end |
| 28 | + |
| 29 | + def pop(context) |
| 30 | + contexts.pop while contexts.include?(context) |
| 31 | + end |
| 32 | + |
| 33 | + def correlation_id |
| 34 | + current&.correlation_id |
| 35 | + end |
| 36 | + |
| 37 | + def current |
| 38 | + contexts.last |
| 39 | + end |
| 40 | + |
| 41 | + def log_key(key) |
| 42 | + key = key.to_s |
| 43 | + return key if RAW_KEYS.include?(key) |
| 44 | + return key if key.start_with?("#{LOG_KEY}.") |
| 45 | + |
| 46 | + "#{LOG_KEY}.#{key}" |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + def contexts |
| 52 | + Thread.current[:labkit_contexts] ||= [] |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def initialize(values = {}) |
| 57 | + @data = {} |
| 58 | + |
| 59 | + assign_attributes(values) |
| 60 | + end |
| 61 | + |
| 62 | + def merge(new_attributes) |
| 63 | + new_context = self.class.new(data.dup) |
| 64 | + new_context.assign_attributes(new_attributes) |
| 65 | + |
| 66 | + new_context |
| 67 | + end |
| 68 | + |
| 69 | + def to_h |
| 70 | + expand_data |
| 71 | + end |
| 72 | + |
| 73 | + def [](key) |
| 74 | + to_h[log_key(key)] |
| 75 | + end |
| 76 | + |
| 77 | + def correlation_id |
| 78 | + data[CORRELATION_ID_KEY] |
| 79 | + end |
| 80 | + |
| 81 | + def get_attribute(attribute) |
| 82 | + raw = call_or_value(data[log_key(attribute)]) |
| 83 | + |
| 84 | + call_or_value(raw) |
| 85 | + end |
| 86 | + |
| 87 | + protected |
| 88 | + |
| 89 | + def assign_attributes(attributes) |
| 90 | + attributes = attributes.transform_keys(&method(:log_key)) |
| 91 | + |
| 92 | + data.merge!(attributes) |
| 93 | + |
| 94 | + # Remove keys that had their values set to `nil` in the new attributes |
| 95 | + data.keep_if { |_, value| valid_data?(value) } |
| 96 | + |
| 97 | + # Assign a correlation if it was missing in the first context or when |
| 98 | + # explicitly removed |
| 99 | + data[CORRELATION_ID_KEY] ||= new_id |
| 100 | + |
| 101 | + data |
| 102 | + end |
| 103 | + |
| 104 | + private |
| 105 | + |
| 106 | + attr_reader :data |
| 107 | + |
| 108 | + def log_key(key) |
| 109 | + self.class.log_key(key) |
| 110 | + end |
| 111 | + |
| 112 | + def call_or_value(value) |
| 113 | + value.respond_to?(:call) ? value.call : value |
| 114 | + end |
| 115 | + |
| 116 | + def expand_data |
| 117 | + data.transform_values do |value| |
| 118 | + value = call_or_value(value) |
| 119 | + |
| 120 | + value if valid_data?(value) |
| 121 | + end.compact |
| 122 | + end |
| 123 | + |
| 124 | + def new_id |
| 125 | + SecureRandom.hex |
| 126 | + end |
| 127 | + |
| 128 | + def valid_data?(value) |
| 129 | + value == false || value.present? |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments