summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2020-02-05 09:13:01 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-02-06 17:39:47 +0000
commit432a6e7fbcd8694fd8ecbf143bcb226efe4ce084 (patch)
treeca02eea85fefb3b34b6ff1f47b70f4603d588cbe
parent98159f8798a3d73878beab634e9ce81784848e8f (diff)
downloadmongo-432a6e7fbcd8694fd8ecbf143bcb226efe4ce084.tar.gz
SERVER-45985 Add truncation support to the text formatter
create mode 100644 src/mongo/logv2/log_truncation.h
-rw-r--r--src/mongo/logger/logv2_appender.h4
-rw-r--r--src/mongo/logv2/attributes.cpp5
-rw-r--r--src/mongo/logv2/attributes.h1
-rw-r--r--src/mongo/logv2/constants.h5
-rw-r--r--src/mongo/logv2/log_detail.cpp3
-rw-r--r--src/mongo/logv2/log_options.h11
-rw-r--r--src/mongo/logv2/log_source.h10
-rw-r--r--src/mongo/logv2/log_truncation.h38
-rw-r--r--src/mongo/logv2/plain_formatter.cpp7
9 files changed, 80 insertions, 4 deletions
diff --git a/src/mongo/logger/logv2_appender.h b/src/mongo/logger/logv2_appender.h
index 75c7bb8f684..4e8735f677f 100644
--- a/src/mongo/logger/logv2_appender.h
+++ b/src/mongo/logger/logv2_appender.h
@@ -111,7 +111,9 @@ public:
_domain,
logv2::LogTag{static_cast<logv2::LogTag::Value>(
static_cast<std::underlying_type_t<logv2::LogTag::Value>>(logTagValue) |
- static_cast<std::underlying_type_t<logv2::LogTag::Value>>(_tag))}},
+ static_cast<std::underlying_type_t<logv2::LogTag::Value>>(_tag))},
+ event.isTruncatable() ? logv2::LogTruncation::Enabled
+ : logv2::LogTruncation::Disabled},
"{}",
"message"_attr = message);
diff --git a/src/mongo/logv2/attributes.cpp b/src/mongo/logv2/attributes.cpp
index dc76a88fb8f..f767f3ae94c 100644
--- a/src/mongo/logv2/attributes.cpp
+++ b/src/mongo/logv2/attributes.cpp
@@ -78,6 +78,11 @@ const boost::log::attribute_name& attributes() {
return attr;
}
+const boost::log::attribute_name& truncation() {
+ static const boost::log::attribute_name attr("truncation");
+ return attr;
+}
+
} // namespace attributes
} // namespace logv2
} // namespace mongo
diff --git a/src/mongo/logv2/attributes.h b/src/mongo/logv2/attributes.h
index a69e097aaf6..f39f24a95cd 100644
--- a/src/mongo/logv2/attributes.h
+++ b/src/mongo/logv2/attributes.h
@@ -45,6 +45,7 @@ const boost::log::attribute_name& tags();
const boost::log::attribute_name& id();
const boost::log::attribute_name& message();
const boost::log::attribute_name& attributes();
+const boost::log::attribute_name& truncation();
} // namespace attributes
} // namespace logv2
diff --git a/src/mongo/logv2/constants.h b/src/mongo/logv2/constants.h
index 5c702e723e8..6b16cccbc8a 100644
--- a/src/mongo/logv2/constants.h
+++ b/src/mongo/logv2/constants.h
@@ -29,6 +29,8 @@
#pragma once
+#include "mongo/logv2/log_truncation.h"
+
namespace mongo::logv2::constants {
// Used in data structures to indicate number of attributes to store without having to allocate
@@ -48,4 +50,7 @@ constexpr StringData kTagsFieldName = "tags"_sd;
// String to be used when logging empty boost::optional with the text formatter
constexpr StringData kNullOptionalString = "(nothing)"_sd;
+constexpr LogTruncation kDefaultTruncation = LogTruncation::Enabled;
+constexpr size_t kDefaultMaxAttributeOutputSize = 10 * 1024;
+
} // namespace mongo::logv2::constants
diff --git a/src/mongo/logv2/log_detail.cpp b/src/mongo/logv2/log_detail.cpp
index 048fa423d7f..4668ff2339f 100644
--- a/src/mongo/logv2/log_detail.cpp
+++ b/src/mongo/logv2/log_detail.cpp
@@ -49,7 +49,8 @@ void doLogImpl(int32_t id,
TypeErasedAttributeStorage const& attrs) {
dassert(options.component() != LogComponent::kNumLogComponents);
auto& source = options.domain().internal().source();
- auto record = source.open_record(id, severity, options.component(), options.tags());
+ auto record =
+ source.open_record(id, severity, options.component(), options.tags(), options.truncation());
if (record) {
record.attribute_values().insert(
attributes::message(),
diff --git a/src/mongo/logv2/log_options.h b/src/mongo/logv2/log_options.h
index baf3faafd5c..cc7601baddb 100644
--- a/src/mongo/logv2/log_options.h
+++ b/src/mongo/logv2/log_options.h
@@ -32,6 +32,7 @@
#include "mongo/logv2/log_component.h"
#include "mongo/logv2/log_manager.h"
#include "mongo/logv2/log_tag.h"
+#include "mongo/logv2/log_truncation.h"
namespace mongo {
namespace logv2 {
@@ -51,9 +52,14 @@ public:
LogOptions(LogTag tags) : _tags(tags) {}
+ LogOptions(LogTruncation truncation) : _truncation(truncation) {}
+
LogOptions(LogComponent component, LogDomain* domain, LogTag tags)
: _domain(domain), _tags(tags), _component(component) {}
+ LogOptions(LogComponent component, LogDomain* domain, LogTag tags, LogTruncation truncation)
+ : _domain(domain), _tags(tags), _component(component), _truncation(truncation) {}
+
LogComponent component() const {
return _component;
}
@@ -66,10 +72,15 @@ public:
return _tags;
}
+ LogTruncation truncation() const {
+ return _truncation;
+ }
+
private:
LogDomain* _domain = &LogManager::global().getGlobalDomain();
LogTag _tags;
LogComponent _component = LogComponent::kAutomaticDetermination;
+ LogTruncation _truncation = constants::kDefaultTruncation;
};
} // namespace logv2
diff --git a/src/mongo/logv2/log_source.h b/src/mongo/logv2/log_source.h
index 108f623ad99..25eea5c36b4 100644
--- a/src/mongo/logv2/log_source.h
+++ b/src/mongo/logv2/log_source.h
@@ -38,10 +38,12 @@
#include <boost/log/sources/threading_models.hpp>
#include "mongo/logv2/attributes.h"
+#include "mongo/logv2/constants.h"
#include "mongo/logv2/log_component.h"
#include "mongo/logv2/log_domain.h"
#include "mongo/logv2/log_severity.h"
#include "mongo/logv2/log_tag.h"
+#include "mongo/logv2/log_truncation.h"
#include "mongo/util/time_support.h"
namespace mongo {
@@ -60,11 +62,13 @@ public:
_severity(LogSeverity::Log()),
_component(LogComponent::kDefault),
_tags(LogTag::kNone),
+ _truncation(constants::kDefaultTruncation),
_id(-1) {
add_attribute_unlocked(attributes::domain(), _domain);
add_attribute_unlocked(attributes::severity(), _severity);
add_attribute_unlocked(attributes::component(), _component);
add_attribute_unlocked(attributes::tags(), _tags);
+ add_attribute_unlocked(attributes::truncation(), _truncation);
add_attribute_unlocked(attributes::id(), _id);
add_attribute_unlocked(attributes::timeStamp(), boost::log::attributes::make_function([]() {
return Date_t::now();
@@ -77,12 +81,14 @@ public:
boost::log::record open_record(int32_t id,
LogSeverity severity,
LogComponent component,
- LogTag tags) {
+ LogTag tags,
+ LogTruncation truncation) {
// Perform a quick check first
if (this->core()->get_logging_enabled()) {
_severity.set(severity);
_component.set(component);
_tags.set(tags);
+ _truncation.set(truncation);
_id.set(id);
return Base::open_record_unlocked();
} else
@@ -94,6 +100,7 @@ public:
_severity.set(LogSeverity::Log());
_component.set(LogComponent::kDefault);
_tags.set(LogTag::kNone);
+ _truncation.set(constants::kDefaultTruncation);
_id.set(-1);
}
@@ -102,6 +109,7 @@ private:
boost::log::attributes::mutable_constant<LogSeverity> _severity;
boost::log::attributes::mutable_constant<LogComponent> _component;
boost::log::attributes::mutable_constant<LogTag> _tags;
+ boost::log::attributes::mutable_constant<LogTruncation> _truncation;
boost::log::attributes::mutable_constant<int32_t> _id;
};
diff --git a/src/mongo/logv2/log_truncation.h b/src/mongo/logv2/log_truncation.h
new file mode 100644
index 00000000000..5336a2ff0c0
--- /dev/null
+++ b/src/mongo/logv2/log_truncation.h
@@ -0,0 +1,38 @@
+/**
+ * Copyright (C) 2020-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+namespace mongo {
+namespace logv2 {
+
+enum class LogTruncation { Enabled, Disabled };
+
+} // namespace logv2
+} // namespace mongo
diff --git a/src/mongo/logv2/plain_formatter.cpp b/src/mongo/logv2/plain_formatter.cpp
index d6d11ffb523..e156a1148ac 100644
--- a/src/mongo/logv2/plain_formatter.cpp
+++ b/src/mongo/logv2/plain_formatter.cpp
@@ -149,7 +149,12 @@ void PlainFormatter::operator()(boost::log::record_view const& rec,
buffer,
to_string_view(message),
fmt::basic_format_args<fmt::format_context>(extractor.args.data(), extractor.args.size()));
- strm.write(buffer.data(), buffer.size());
+
+ LogTruncation truncation = extract<LogTruncation>(attributes::truncation(), rec).get();
+ strm.write(buffer.data(),
+ truncation == LogTruncation::Enabled
+ ? std::min(constants::kDefaultMaxAttributeOutputSize, buffer.size())
+ : buffer.size());
}
} // namespace mongo::logv2