gitlab-labkit 1.21.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43d394fdecdc0275e1ddab3a28ae7824f907e21637c4d06b3cc183a801df9377
4
- data.tar.gz: 18632e75c51ec22c6b78079c59476f736b3b3b298c7988fbf4a49c9505c4d05e
3
+ metadata.gz: 88d5f5a7cd756153b8c69edbf0a887b0d7fbd98c0eab2828c3150cc2fafa1fac
4
+ data.tar.gz: 0bf07e6f00b14c3a31ba3dab3b3bbd7cd72af81b61dd66992e9d9ef6bd73d6da
5
5
  SHA512:
6
- metadata.gz: d56453b6af992b51da987bec68fd990fefc808b68be96702c3b4274ae609a773139b39bb5609032ed9148a7427976bd64ea0566fa0c8354582e4d4d4135d481e
7
- data.tar.gz: f8d71107b36351983b8a1da70b9a43624b2eb73e000b37fd45cad03606749c84a2da0282d1e02cd7f5a1b687e2e030717fc1770f2671d05175a7cd4d9eb9bad1
6
+ metadata.gz: 737e6ee6b0fc00f716cf3aba4f94bd02c65c01dc5436ca8a4b99ce64ce092efe8bd39af8fe7d6d68ce5e8c2dcd1ba7c991d49b4c0c742c17a4e4ea1d48e50f1b
7
+ data.tar.gz: 17db3fe540d65f81193adfc8f86d9e984761b76b17f639bad7cd73df46206035690b1c3466bc8d54c26b7c7a3f043c6158edb657137c67b9318098b0eae61f48
@@ -18,7 +18,8 @@ For the architectural decision and rationale, see
18
18
 
19
19
  Offense
20
20
  : A unique combination of file path, deprecated field, and logger class.
21
- Multiple log calls in the same file using the same deprecated field count as
21
+ Multiple log calls in the same file using the same deprecated field or multiple
22
+ log calls originating from the application context count as
22
23
  one offense.
23
24
  An offense exists until the deprecated field is entirely removed from the
24
25
  file.
@@ -15,6 +15,15 @@ module Labkit
15
15
  %r{/.*logger\.rb$}
16
16
  ].freeze
17
17
 
18
+ DEFAULT_CONTEXT_CALLSITE = "Labkit::Context"
19
+
20
+ # Placeholder logger_class used when an offense originates from the Labkit
21
+ # context. The same context fields appear in every logger that runs within
22
+ # the context, so collapsing across loggers prevents one offense per
23
+ # logger x deprecated_field combination, and prevents new offenses being
24
+ # raised whenever a developer adds a new logger class.
25
+ ANY_LOGGER = "*"
26
+
18
27
  class << self
19
28
  def register_wrapper_pattern(pattern)
20
29
  wrapper_patterns << pattern
@@ -33,6 +42,31 @@ module Labkit
33
42
  def combined_ignore_pattern
34
43
  @combined_ignore_pattern ||= Regexp.union(IGNORE_PATHS + wrapper_patterns)
35
44
  end
45
+
46
+ # The callsite name used for offenses originating from the Labkit context
47
+ # (e.g. ApplicationContext) rather than from the log caller directly.
48
+ # Override this in your application to point to the actual context provider file:
49
+ # Labkit::Logging::FieldValidator::LogInterceptor.context_callsite =
50
+ # "lib/gitlab/application_context.rb"
51
+ def context_callsite
52
+ @context_callsite || DEFAULT_CONTEXT_CALLSITE
53
+ end
54
+
55
+ attr_writer :context_callsite
56
+
57
+ def reset_context_callsite!
58
+ @context_callsite = nil
59
+ end
60
+
61
+ # The context prefix that Labkit::Context applies to every field it
62
+ # stores (see Labkit::Context::LOG_KEY). Any deprecated field with
63
+ # this prefix is by convention a context field, owned by whichever
64
+ # provider populates the context (e.g. ApplicationContext), even
65
+ # when a particular log call happens to pass it directly. Lazily
66
+ # built so the constant is not referenced at load time.
67
+ def context_field_prefix
68
+ @context_field_prefix ||= "#{::Labkit::Context::LOG_KEY}."
69
+ end
36
70
  end
37
71
 
38
72
  def format_data(severity, timestamp, progname, message)
@@ -46,6 +80,13 @@ module Labkit
46
80
 
47
81
  logger_class = self.class.name || 'AnonymousLogger'
48
82
 
83
+ # Keys the caller explicitly passed in the log message. Any deprecated field
84
+ # present in `data` but absent here arrived via Labkit::Context (e.g.
85
+ # ApplicationContext) and should be attributed to the context callsite rather
86
+ # than the individual log call site.
87
+ direct_keys = message.is_a?(Hash) ? message.transform_keys(&:to_s).keys.to_set : Set.new
88
+ context_prefix = LogInterceptor.context_field_prefix
89
+
49
90
  deprecated_lookup = Labkit::Fields::Deprecated.all
50
91
  if data.is_a?(Hash)
51
92
  data.each_key do |key|
@@ -53,11 +94,19 @@ module Labkit
53
94
  standard_field = deprecated_lookup[key_str]
54
95
  next unless standard_field
55
96
 
56
- Registry.instance.record_offense(callsite_path, location.lineno, key_str, standard_field, logger_class)
97
+ # context_prefix is the Labkit::Context namespace. The field is conceptually
98
+ # context-owned even if the caller happens to pass it directly. Otherwise
99
+ # use direct-vs-context based on whether the caller actually included it.
100
+ if direct_keys.include?(key_str) && !key_str.start_with?(context_prefix)
101
+ Registry.instance.record_offense(callsite_path, location.lineno, key_str, standard_field, logger_class)
102
+ else
103
+ Registry.instance.record_offense(LogInterceptor.context_callsite, 0, key_str, standard_field, ANY_LOGGER)
104
+ end
57
105
  end
58
106
  end
59
107
 
60
108
  Registry.instance.check_for_removed_offenses(callsite_path, data, logger_class)
109
+ Registry.instance.check_for_removed_offenses(LogInterceptor.context_callsite, data, ANY_LOGGER)
61
110
 
62
111
  data
63
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab-labkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.21.0
4
+ version: 1.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Newdigate