|
| 1 | +# Logging |
| 2 | + |
| 3 | +This document outlines the logging standards and best practices for Ramen. |
| 4 | + |
| 5 | +## General Principles |
| 6 | + |
| 7 | +1. **Consistency**: All controllers should follow the same logging conventions. |
| 8 | +1. **Performance Awareness**: Logging should not significantly impact the |
| 9 | + performance. Be mindful of the logging levels and the amount of data logged. |
| 10 | + |
| 11 | +## Logger Configuration |
| 12 | + |
| 13 | +- Each controller should create its logger instance with a name specific to the |
| 14 | + controller for easy identification. For example, if the controller is named |
| 15 | + `ProtectedVolumeReplicationGroupList`, the logger name could be a shortened |
| 16 | + form of the controller like `pvrgl`. |
| 17 | + |
| 18 | + ```go |
| 19 | + logger := ctrl.Log.WithName("pvrgl") |
| 20 | + ``` |
| 21 | + |
| 22 | +- Loggers should be enriched with key contextual information to make the logs |
| 23 | + more informative. This includes: |
| 24 | + - The name of the custom resource. |
| 25 | + - Then namespace of the custom resource, if applicable. |
| 26 | + - The UID of the custom resource. |
| 27 | + - The version of the custom resource. |
| 28 | + |
| 29 | + ```go |
| 30 | + logger = logger.WithValues("name", req.NamespacedName.Name, "rid", instance.ObjectMeta.UID, "version", instance.ObjectMeta.ResourceVersion) |
| 31 | + ``` |
| 32 | + |
| 33 | +- The context passed to functions should be augmented with the logger instance: |
| 34 | + |
| 35 | + ```go |
| 36 | + ctx = ctrl.LoggerInto(ctx, logger) |
| 37 | + ``` |
| 38 | + |
| 39 | + This will be useful when we call functions that are outside of the controller |
| 40 | + scope and the instance.log object is not available to them. |
| 41 | + |
| 42 | +## Log Messages |
| 43 | + |
| 44 | +- Log messages should be concise yet informative. Wherever possible, use |
| 45 | + structured logging to provide context. |
| 46 | + |
| 47 | +- Start and end of significant operations, like reconciliation, should be logged: |
| 48 | + |
| 49 | + ```go |
| 50 | + logger.Info("reconcile start") |
| 51 | + defer logger.Info("reconcile end", "time spent", time.Since(start)) |
| 52 | + ``` |
| 53 | + |
| 54 | +- When logging errors, include the context of the error: |
| 55 | + |
| 56 | + ```go |
| 57 | + logger.Error(err, "Error description", "key", "value") |
| 58 | + ``` |
| 59 | + |
| 60 | +- Use key-value pairs instead of concatenating the message and data. |
| 61 | + |
| 62 | + For example, avoid logging like this: |
| 63 | + |
| 64 | + ```go |
| 65 | + log.Info(fmt.Sprintf("Requeuing after %v", duration)) |
| 66 | + ``` |
| 67 | + |
| 68 | + This approach logs everything as one string, making it harder to parse and filter. |
| 69 | + |
| 70 | + Log additional information as key-value pairs. This method ensures that |
| 71 | + each piece of information is logged as a separate field, making it easier |
| 72 | + to search and analyze. |
| 73 | + |
| 74 | + Example of a well-structured log message: |
| 75 | + |
| 76 | + ```go |
| 77 | + s.log.Info("reconcile end", "time spent", time.Since(start)) |
| 78 | + ``` |
| 79 | + |
| 80 | + This will log in the following format, providing clear and structured context: |
| 81 | + |
| 82 | + ``` |
| 83 | + 2024-01-31T14:27:12.726-0500 INFO pvrgl reconcile end {"name": "protectedvrglist-0", "rid": "1e09b0fb-687b-4100-9ab1-a52ba899b37b", "rv": "322", "time spent": 6ms} |
| 84 | + ``` |
| 85 | + |
| 86 | +## Debugging with Logs |
| 87 | + |
| 88 | +The structured logs help debugging. Here's how you can effectively use the logs |
| 89 | +for debugging purposes: |
| 90 | +
|
| 91 | +### Filtering Logs for a Specific Resource |
| 92 | +
|
| 93 | +To focus on logs related to a specific resource, you can use `grep` with a |
| 94 | +pattern that matches the resource's unique identifiers. For example: |
| 95 | + |
| 96 | +``` |
| 97 | +grep -e 'pvrgl.*4db288b5-3f03-441c-bc44-00e356e77f62' |
| 98 | +``` |
| 99 | +
|
| 100 | +This command filters logs to show entries for the resource with UID |
| 101 | +`4db288b5-3f03-441c-bc44-00e356e77f62`. |
0 commit comments