Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ envoy_package()

envoy_cc_library(
name = "backtrace_lib",
srcs = ["backtrace.cc"],
hdrs = ["backtrace.h"],
external_deps = [
"abseil_stacktrace",
Expand Down
11 changes: 11 additions & 0 deletions source/server/backtrace.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "server/backtrace.h"

#include <iostream>

namespace Envoy {

bool BackwardsTrace::log_to_stderr_ = false;

void BackwardsTrace::setLogToStderr(bool log_to_stderr) { log_to_stderr_ = log_to_stderr; }

} // namespace Envoy
23 changes: 23 additions & 0 deletions source/server/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ class BackwardsTrace : Logger::Loggable<Logger::Id::backtrace> {
public:
BackwardsTrace() = default;

/**
* Directs the output of logTrace() to directly stderr rather than the
* logging infrastructure.
*
* This is intended for coverage tests, where we enable trace logs, but send
* them to /dev/null to avoid accumulating too much data in CI.
*
* @param log_to_stderr Whether to log to stderr or the logging system.
*/
static void setLogToStderr(bool log_to_stderr);

/**
* @return whether the system directing backtraces directly to stderr.
*/
static bool logToStderr() { return log_to_stderr_; }

/**
* Capture a stack trace.
*
Expand Down Expand Up @@ -67,6 +83,11 @@ class BackwardsTrace : Logger::Loggable<Logger::Id::backtrace> {
* Log the stack trace.
*/
void logTrace() {
if (log_to_stderr_) {
printTrace(std::cerr);
return;
}

ENVOY_LOG(critical, "Backtrace (use tools/stack_decode.py to get line numbers):");
ENVOY_LOG(critical, "Envoy version: {}", VersionInfo::version());

Expand Down Expand Up @@ -94,6 +115,8 @@ class BackwardsTrace : Logger::Loggable<Logger::Id::backtrace> {
}

private:
static bool log_to_stderr_;

/**
* Visit the previously captured stack trace.
*
Expand Down
1 change: 1 addition & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ envoy_cc_test_library(
"//source/common/common:thread_lib",
"//source/common/event:libevent_lib",
"//source/exe:process_wide_lib",
"//source/server:backtrace_lib",
"//test/common/runtime:utility_lib",
"//test/mocks/access_log:access_log_mocks",
"//test/test_common:environment_lib",
Expand Down
3 changes: 3 additions & 0 deletions test/server/backtrace_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ TEST(Backward, Basic) {
// There isn't much to test here and this feature is really just useful for
// debugging. This test simply verifies that we do not cause a crash when
// logging a backtrace, and covers the added lines.
const bool save_log_to_stderr = BackwardsTrace::logToStderr();
BackwardsTrace::setLogToStderr(false);
BackwardsTrace tracer;
tracer.capture();
EXPECT_LOG_CONTAINS("critical", "Envoy version:", tracer.logTrace());
BackwardsTrace::setLogToStderr(save_log_to_stderr);
}

TEST(Backward, InvalidUsageTest) {
Expand Down
18 changes: 16 additions & 2 deletions test/test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "exe/process_wide.h"

#include "server/backtrace.h"

#include "test/common/runtime/utility.h"
#include "test/mocks/access_log/mocks.h"
#include "test/test_common/environment.h"
Expand Down Expand Up @@ -103,10 +105,22 @@ int TestRunner::RunTests(int argc, char** argv) {
listeners.Append(new RuntimeManagingListener(runtime_override));
}

#ifdef ENVOY_CONFIG_COVERAGE
// Coverage tests are run with -l trace --log-path /dev/null, in order to
// ensure that all of the code-paths from the maximum level of tracing are
// covered in tests, but we don't wind up filling up CI with useless detailed
// artifacts.
//
// The downside of this is that if there's a crash, the backtrace is lost, as
// the backtracing mechanism uses logging, so force the backtraces to stderr.
BackwardsTrace::setLogToStderr(true);
#endif

TestEnvironment::initializeOptions(argc, argv);
Thread::MutexBasicLockable lock;
Logger::Context logging_state(TestEnvironment::getOptions().logLevel(),
TestEnvironment::getOptions().logFormat(), lock, false);

Server::Options& options = TestEnvironment::getOptions();
Logger::Context logging_state(options.logLevel(), options.logFormat(), lock, false);

// Allocate fake log access manager.
testing::NiceMock<AccessLog::MockAccessLogManager> access_log_manager;
Expand Down
2 changes: 2 additions & 0 deletions tools/spelling/spelling_dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ backgrounded
backoff
backpressure
backticks
backtraces
backtracing
balancer
balancers
barbaz
Expand Down