Debian Codegraph @

docs/rfcs/logging-before-config-resolution.md

raw
1 # How To Log Before Config Resolution
2
3 ## Overview
4
5 The OpenTelemetry Collector supports configuring a primary logger that the collector and its components use to write logs.
6 This logger cannot be created until the user's configuration has been completely resolved.
7 There is a need to write logs during the collector start-up, before the primary logger is instantiated, such as during
8 configuration resolution or config validation. This document describes
9
10 - why providing logging capabilities during startup is important
11 - the current (as of v0.99.0) behavior of the Collector
12 - different solutions to the problem
13 - the accepted solution
14
15 ## Why Logging During Startup is Important
16
17 When the collector is starting it tries to resolve user configuration as quickly as possible.
18 But the Collector's configuration resolution strategy is not trivial - it allows for complex interactions between
19 multiple, different config sources that must all resolve without error. During this process important information could
20 be shared with users such as:
21 - [Warnings about deprecated syntax](https://github.com/open-telemetry/opentelemetry-collector/issues/9162)
22 - [Warnings about undesired, but handled, situations](https://github.com/open-telemetry/opentelemetry-collector/issues/5615)
23 - Debug information
24
25 ## Requirements for any solution
26
27 1. Once the primary logger is instantiated, it should be usable anywhere in the Collector that does logging.
28 2. Log timestamps must be accurate to when the log was written, regardless of when the log is written to the user-specified location(s).
29 3. The EntryCaller (what line number the log originates from) must be accurate to where the log was written.
30 4. If an error causes the collector to gracefully terminate before the primary logger is created any previously written logs MUST be written to the either stout or stderr if they have not already been written there.
31
32 ## Current behavior
33
34 As of v0.99.0, the collector does not provide a way to log before the primary logger is instantiated.
35
36 ## Solutions
37
38 ### Buffer Logs in Memory and Write Them Once the Primary Logger Exists
39
40 The Collector could provide a temporary logger that, when written to, keeps the logs in memory. These logs could then
41 be passed to the primary logger to be written out in the properly configured format/level.
42
43 Benefits:
44 - Logs are written in the user-specified format/level
45
46 Downsides:
47 - If the primary logger is used to write any logs before the buffered logs are passed, logs may be out of order. There are no guarantees that logs will be written in order, so the log timestamps should be taken as the source of truth for ordering.
48
49 ### Create a Logger Using the Primary Logger's Defaults
50
51 When the user provides no primary logger configuration the Collector creates a Logger using a set of default values.
52 The Collector could, very early in startup, create a logger using these exact defaults and use it until the primary
53 logger is instantiated.
54
55 Benefits:
56 - Logs order is preserved
57 - Logs are still written when an error occurs before the primary logger can be instantiated
58
59 Downsides:
60 - Logs may be written in a format/level that differs from the format/level of the primary logger
61
62 ## Accepted Solution
63
64 [Buffer Logs in Memory and Write Them Once the Primary Logger Exists](#buffer-logs-in-memory-and-write-them-once-the-primary-logger-exists)
65
66 This solution, while more complex, allows the collector to write out the logs in the user-specified format whenever possible. A fallback logger must be used in situations where the primary logger could not be created and the collector is shutting down, such as when encountering an error during configuration resolution, but otherwise the primary logger will be used to write logs that occurred before the primary logger existed.