From bc6a1c692273c6feb4490815661cdabd7283e497 Mon Sep 17 00:00:00 2001 From: Tanvi Jagtap <139093547+tanvi-jagtap@users.noreply.github.com> Date: Thu, 6 Jun 2024 19:05:18 -0700 Subject: [PATCH] [grpc][Gpr_To_Absl_Logging] Supporting verbosity flag (#36798) [grpc][Gpr_To_Absl_Logging] 1. Supporting the legacy GRPC_VERBOSITY environment variable in the new absl logging implementation. 2. Adding a new way to disable VLOG logging. 3. Documenting the recommendations clearly. 4. Editing the init code. Additional Context : Check function gpr_default_log() for more context https://github.com/search?q=repo%3Agrpc%2Fgrpc%20gpr_default_log&type=code Closes #36798 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36798 from tanvi-jagtap:grpc_verbosity_flag_support ab0d60084930f8f7abc9ca29ad9d7362ee054e28 PiperOrigin-RevId: 641092851 --- TROUBLESHOOTING.md | 17 +++++++++++--- doc/environment_variables.md | 22 ++++++++++++++---- src/core/lib/config/config_vars.cc | 2 +- src/core/lib/config/config_vars.h | 2 +- src/core/lib/config/config_vars.yaml | 2 +- src/core/util/log.cc | 34 ++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 11 deletions(-) diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index 035213b9e18..6a2cf4a6f09 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -4,16 +4,27 @@ This guide is for troubleshooting gRPC implementations based on C core library ( ## Enabling extra logging and tracing -Extra logging can be very useful for diagnosing problems. All gRPC implementations based on C core library support -the `GRPC_VERBOSITY` and `GRPC_TRACE` environment variables that can be used to increase the amount of information +Extra logging can be very useful for diagnosing problems. It can be used to increase the amount of information that gets printed to stderr. ## GRPC_VERBOSITY -`GRPC_VERBOSITY` is used to set the minimum level of log messages printed by gRPC (supported values are `DEBUG`, `INFO` and `ERROR`). If this environment variable is unset, only `ERROR` logs will be printed. + + + +`GRPC_VERBOSITY` is used to set the minimum level of log messages printed by gRPC (supported values are `DEBUG`, `INFO` and `ERROR`). If this environment variable is unset, only `ERROR` logs will be printed. `ERROR` is recomeded for production systems. + ## GRPC_TRACE + + `GRPC_TRACE` can be used to enable extra logging for some internal gRPC components. Enabling the right traces can be invaluable for diagnosing for what is going wrong when things aren't working as intended. Possible values for `GRPC_TRACE` are listed in [Environment Variables Overview](doc/environment_variables.md). Multiple traces can be enabled at once (use comma as separator). diff --git a/doc/environment_variables.md b/doc/environment_variables.md index 86db138e512..f5324b71653 100644 --- a/doc/environment_variables.md +++ b/doc/environment_variables.md @@ -140,11 +140,23 @@ some configuration as environment variables that can be set. export GRPC_TRACE=all,-pending_tags * GRPC_VERBOSITY - Default gRPC logging verbosity - one of: - - DEBUG - log all gRPC messages - - INFO - log INFO and ERROR message - - ERROR - log only errors (default) - - NONE - won't log any + + + +`GRPC_VERBOSITY` is used to set the minimum level of log messages printed by gRPC (supported values are `DEBUG`, `INFO` and `ERROR`). If this environment variable is unset, only `ERROR` logs will be printed. + gRPC logging verbosity - one of: + - DEBUG - log INFO, WARNING, ERROR and FATAL messages. Also sets absl VLOG(2) logs enabled. This is not recommended for production systems. This will be expensive for staging environments too, so it can be used when you want to debug a specific issue. + - INFO - log INFO, WARNING, ERROR and FATAL messages. This is not recommended for production systems. This may be slightly expensive for staging environments too. We recommend that you use your discretion for staging environments. + - ERROR - log ERROR and FATAL messages. This is recommended for production systems. + - NONE - won't log any. + GRPC_VERBOSITY will set verbosity of absl logging. + - If the external application sets some other verbosity, then whatever is set later will be honoured. + - If nothing is set as GRPC_VERBOSITY, then the setting of the exernal application will be honoured. + - If nothing is set by the external application also, the default set by absl will be honoured. + * GRPC_STACKTRACE_MINLOGLEVEL Minimum loglevel to print the stack-trace - one of DEBUG, INFO, ERROR, and NONE. diff --git a/src/core/lib/config/config_vars.cc b/src/core/lib/config/config_vars.cc index 7a4fed1a7c9..b3a919b1170 100644 --- a/src/core/lib/config/config_vars.cc +++ b/src/core/lib/config/config_vars.cc @@ -54,7 +54,7 @@ ABSL_FLAG(std::vector, grpc_trace, {}, "A comma separated list of tracers that provide additional insight " "into how gRPC C core is processing requests via debug logs."); ABSL_FLAG(absl::optional, grpc_verbosity, {}, - "Default gRPC logging verbosity"); + "Logging verbosity."); ABSL_FLAG(absl::optional, grpc_stacktrace_minloglevel, {}, "Messages logged at the same or higher level than this will print " "stacktrace"); diff --git a/src/core/lib/config/config_vars.h b/src/core/lib/config/config_vars.h index 8c534bf7c33..d368df239e4 100644 --- a/src/core/lib/config/config_vars.h +++ b/src/core/lib/config/config_vars.h @@ -79,7 +79,7 @@ class GPR_DLL ConfigVars { // A comma separated list of tracers that provide additional insight into how // gRPC C core is processing requests via debug logs. absl::string_view Trace() const { return trace_; } - // Default gRPC logging verbosity + // Logging verbosity. absl::string_view Verbosity() const { return verbosity_; } // Messages logged at the same or higher level than this will print stacktrace absl::string_view StacktraceMinloglevel() const { diff --git a/src/core/lib/config/config_vars.yaml b/src/core/lib/config/config_vars.yaml index 8ccd41c2d41..56e0e25d302 100644 --- a/src/core/lib/config/config_vars.yaml +++ b/src/core/lib/config/config_vars.yaml @@ -70,7 +70,7 @@ #endif // !GPR_DEFAULT_LOG_VERBOSITY_STRING default: $GPR_DEFAULT_LOG_VERBOSITY_STRING description: - Default gRPC logging verbosity + Logging verbosity. fuzz: true - name: stacktrace_minloglevel type: string diff --git a/src/core/util/log.cc b/src/core/util/log.cc index 9a6f68104e0..58ba0562c00 100644 --- a/src/core/util/log.cc +++ b/src/core/util/log.cc @@ -23,6 +23,7 @@ #include #include +#include "absl/log/globals.h" #include "absl/strings/match.h" #include "absl/strings/str_cat.h" @@ -123,6 +124,38 @@ static gpr_atm parse_log_severity(absl::string_view str, gpr_atm error_value) { return error_value; } +void gpr_to_absl_verbosity_setting_init(void) { +// This is enabled in Github only. +// This ifndef is converted to ifdef internally by copybara. +// Internally grpc verbosity is managed using absl settings. +// So internally we avoid setting it like this. +#ifndef GRPC_VERBOSITY_MACRO + absl::string_view verbosity = grpc_core::ConfigVars::Get().Verbosity(); + DVLOG(2) << "Log verbosity: " << verbosity; + if (absl::EqualsIgnoreCase(verbosity, "INFO")) { + LOG(WARNING) << "Not suitable for production. Prefer WARNING or ERROR. " + "However if you see this message in a debug environmenmt " + "or test environmenmt it is safe to ignore this message."; + absl::SetVLogLevel("*grpc*/*", -1); + absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo); + } else if (absl::EqualsIgnoreCase(verbosity, "DEBUG")) { + LOG(WARNING) << "Not suitable for production. Prefer WARNING or ERROR. " + "However if you see this message in a debug environmenmt " + "or test environmenmt it is safe to ignore this message."; + absl::SetVLogLevel("*grpc*/*", 2); + absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo); + } else if (absl::EqualsIgnoreCase(verbosity, "ERROR")) { + absl::SetVLogLevel("*grpc*/*", -1); + absl::SetMinLogLevel(absl::LogSeverityAtLeast::kError); + } else if (absl::EqualsIgnoreCase(verbosity, "NONE")) { + absl::SetVLogLevel("*grpc*/*", -1); + absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfinity); + } else { + LOG(ERROR) << "Unknown log verbosity: " << verbosity; + } +#endif // GRPC_VERBOSITY_MACRO +} + void gpr_log_verbosity_init() { // init verbosity when it hasn't been set if ((gpr_atm_no_barrier_load(&g_min_severity_to_print)) == @@ -148,6 +181,7 @@ void gpr_log_verbosity_init() { gpr_atm_no_barrier_store(&g_min_severity_to_print_stacktrace, min_severity_to_print_stacktrace); } + gpr_to_absl_verbosity_setting_init(); } void gpr_set_log_function(gpr_log_func f) {