From eee66882ef6714cd25364c34e4024744b72c8d1a Mon Sep 17 00:00:00 2001 From: Joseph Perez <45749060+josmperez@users.noreply.github.com> Date: Mon, 29 Jul 2024 06:06:50 -0700 Subject: [PATCH] Docs: Edit of files in the backend contributor guide (part 7 of doc quality improvement project) (#89974) * Docs: Edit of multiple files in the backend contributor guide (part 7 of doc quality improvement project) * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/package-hierarchy.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/package-hierarchy.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/services.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/services.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/services.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/package-hierarchy.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/services.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Fix for active voice * Update contribute/backend/instrumentation.md * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Update contribute/backend/instrumentation.md Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> * Fix from review * Update contribute/backend/services.md * Update contribute/backend/services.md * Spelling of wire * Prettier fixes * Minor fix --------- Co-authored-by: Eve Meelan <81647476+Eve832@users.noreply.github.com> --- contribute/backend/instrumentation.md | 138 ++++++++++++------------ contribute/backend/package-hierarchy.md | 81 +++++++------- contribute/backend/services.md | 36 +++---- 3 files changed, 126 insertions(+), 129 deletions(-) diff --git a/contribute/backend/instrumentation.md b/contribute/backend/instrumentation.md index cebd0357837..5960137c372 100644 --- a/contribute/backend/instrumentation.md +++ b/contribute/backend/instrumentation.md @@ -1,6 +1,6 @@ # Instrumenting Grafana -Guidance, conventions and best practices for instrumenting Grafana using logs, metrics and traces. +This guide provides conventions and best practices for instrumenting Grafana using logs, metrics, and traces. ## Logs @@ -8,7 +8,7 @@ Logs are files that record events, warnings and errors as they occur within a so ### Usage -Use the [pkg/infra/log](/pkg/infra/log/) package to create a named structured logger. Example: +Use the [pkg/infra/log](/pkg/infra/log/) package to create a named, structured logger. Example: ```go import ( @@ -26,38 +26,38 @@ logger.Error("Error msg", "error", fmt.Errorf("BOOM")) ### Naming conventions -Name the logger using lowercase characters, e.g. `log.New("my-logger")` using snake_case or kebab-case styling. +Name the logger using lowercase characters, for example, `log.New("my-logger")` using snake_case or kebab-case styling. -Prefix the logger name with an area name when using different loggers across a feature or related packages, e.g. `log.New("plugin.loader")` and `log.New("plugin.client")`. +Prefix the logger name with an area name when using different loggers across a feature or related packages; for example, `log.New("plugin.loader")` and `log.New("plugin.client")`. -Start the log message with a capital letter, e.g. `logger.Info("Hello world")` instead of `logger.Info("hello world")`. The log message should be an identifier for the log entry, avoid parameterization in favor of key-value pairs for additional data. +Start the log message with a capital letter, for example, `logger.Info("Hello world")` instead of `logger.Info("hello world")`. The log message should be an identifier for the log entry. Avoid parameterization in favor of key-value pairs for additional data. -Prefer using camelCase style when naming log keys, e.g. _remoteAddr_, to be consistent with Go identifiers. +To be consistent with Go identifiers, prefer using camelCase style when naming log keys; for example, `remoteAddr`. -Use the key _error_ when logging Go errors, e.g. `logger.Error("Something failed", "error", fmt.Errorf("BOOM"))`. +Use the key `Error` when logging Go errors; for example, `logger.Error("Something failed", "error", fmt.Errorf("BOOM"))`. ### Validate and sanitize input coming from user input -If log messages or key/value pairs originates from user input they **should** be validated and sanitized. +If log messages or key/value pairs originate from user input they should be validated and sanitized. -Be **careful** to not expose any sensitive information in log messages e.g. secrets, credentials etc. It's especially easy to do by mistake when including a struct as value. +Be careful not to expose any sensitive information in log messages; for example, secrets and credentials. It's easy to do this by mistake if you include a struct as a value. ### Log levels -When to use which log level? +When should you use each log level? -- **Debug:** Informational messages of high frequency and/or less-important messages during normal operations. -- **Info:** Informational messages of low frequency and/or important messages. -- **Warning:** Should in normal cases not be used/needed. If used should be actionable. -- **Error:** Error messages indicating some operation failed (with an error) and the program didn't have a way of handle the error. +- **Debug:** Informational messages of high frequency, less-important messages during normal operations, or both. +- **Info:** Informational messages of low frequency, important messages, or both. +- **Warning:** Use warning messages sparingly. If used, messages should be actionable. +- **Error:** Error messages indicating some operation failed (with an error) and the program didn't have a way to handle the error. ### Contextual logging -Use a contextual logger to include additional key/value pairs attached to `context.Context`, e.g. `traceID`, to allow correlating logs with traces and/or correlate logs with a common identifier. +Use a contextual logger to include additional key/value pairs attached to `context.Context`. For example, a `traceID`, used to allow correlating logs with traces, correlate logs with a common identifier, either or both. -You must [Enable tracing in Grafana](#2-enable-tracing-in-grafana) to get a traceID +You must [Enable tracing in Grafana](#2-enable-tracing-in-grafana) to get a `traceID`. -Example: +For example: ```go import ( @@ -80,9 +80,9 @@ func doSomething(ctx context.Context) { ### Enable certain log levels for certain loggers -During development, it's convenient to enable certain log level, e.g. debug, for certain loggers to minimize the generated log output and make it easier to find things. See [[log.filters]](https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#filters) for information how to configure this. +You can enable certain log levels during development to make logging easier. For example, you can enable `debug` to allow certain loggers to minimize the generated log output and makes it easier to find things. Refer to [[log.filters]](https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/#filters) for information on how to to set different levels for specific loggers. -It's also possible to configure multiple loggers: +You can also configure multiple loggers. For example: ```ini [log] @@ -114,41 +114,41 @@ There are many possible types of metrics that can be tracked. One popular method ### Naming conventions -Use the namespace _grafana_ as that would prefix any defined metric names with `grafana_`. This will make it clear for operators that any metric named `grafana_*` belongs to Grafana. +Use the namespace `grafana` to prefix any defined metric names with `grafana_`. This prefix makes it clear for operators that any metric named `grafana_*` belongs to Grafana. -Use snake*case style when naming metrics, e.g. \_http_request_duration_seconds* instead of _httpRequestDurationSeconds_. +Use snake_case style when naming metrics; for example, `http_request_duration_seconds` instead of `httpRequestDurationSeconds`. -Use snake*case style when naming labels, e.g. \_status_code* instead of _statusCode_. +Use snake_case style when naming labels; for example, `status_code` instead of `statusCode`. -If metric type is a _counter_, name it with a `_total` suffix, e.g. _http_requests_total_. +If a metric type is a counter, name it with a `_total` suffix; for example, `http_requests_total`. -If metric type is a _histogram_ and you're measuring duration, name it with a `_` suffix, e.g. _http_request_duration_seconds_. +If a metric type is a histogram and you're measuring duration, name it with a `_` suffix; for example, `http_request_duration_seconds`. -If metric type is a _gauge_, name it to denote it's a value that can increase and decrease , e.g. _http_request_in_flight_. +If a metric type is a gauge, name it to denote that it's a value that can increase and decrease; for example, `http_request_in_flight`. ### Label values and high cardinality -Be careful with what label values you add/accept. Using/allowing too many label values could result in [high cardinality problems](https://grafana.com/blog/2022/02/15/what-are-cardinality-spikes-and-why-do-they-matter/). +Be careful with what label values you accept or add. Using or allowing too many label values could result in [high cardinality problems](https://grafana.com/blog/2022/02/15/what-are-cardinality-spikes-and-why-do-they-matter/). -If label values originates from user input they **should** be validated. Use `metricutil.SanitizeLabelName(