Skip to content

Commit

Permalink
Logging of sessions
Browse files Browse the repository at this point in the history
This fix lets the session logs go to any or all of collector/
file/syslog. To accommodate this, 2 new logger object is
created for handling only SLO/Sample session destinations. The
destinations to this logger are provided by the agent. A snippet
of the generated_file is added to the bug. Here is a snippet of
the generated sandesh file:

 static void LogUnrolled(std::string category, SandeshLevel::type level, const std::vector<SessionEndpoint> & session_data);
static void adjust_session_end_point_objects(std::vector <SessionEndpoint> & session_data);

  static void Send(std::string category, SandeshLevel::type level, const std::vector<SessionEndpoint> & session_data) {
    if (HandleTest(level, category)) {
      return;
    }
    if (IsSendingAllMessagesDisabled() || IsSendingFlowsDisabled()) {
      UpdateTxMsgFailStats("SessionEndpointObject", 0, SandeshTxDropReason::SendingDisabled);
      if (IsLoggingDroppedAllowed(SandeshType::FLOW)) {
        Log(category, level, session_data);
      }
      return;
    }
    if (is_send_slo_to_logger_enabled() || is_send_sampled_to_logger_enabled()) {
      LogUnrolled(category, level, session_data);
    }
    if (level >= SendingLevel()) {
      UpdateTxMsgFailStats("SessionEndpointObject", 0, SandeshTxDropReason::QueueLevel);
      if (IsLoggingDroppedAllowed(SandeshType::FLOW)) {
        std::string drop_reason = "SANDESH: Queue Drop: ";
        DropLog(drop_reason, category, level, session_data);
      }
      return;
    }
    SessionEndpointObject * snh = new SessionEndpointObject(lseqnum_++, session_data);
    if (!is_send_sampled_to_collector_enabled() && !is_send_slo_to_collector_enabled()) {
      return;
    }
     if (is_send_sampled_to_collector_enabled() != is_send_slo_to_collector_enabled()) {
      adjust_session_end_point_objects(snh->session_data);    }

    snh->set_level(level);
    snh->set_category(category);
    snh->Dispatch();
  }

Depends-On: I763658b0775acc2eb479b3ae373266698ce710eb
Change-Id: I486656dc9d0866c395aabf9b9ed36cc129a12797
Partial-Bug: 1729812
  • Loading branch information
arvindvis committed Jun 22, 2018
1 parent 2473bc5 commit f4e9036
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 18 deletions.
49 changes: 31 additions & 18 deletions compiler/generate/t_cpp_generator.cc
Expand Up @@ -151,6 +151,8 @@ class t_cpp_generator : public t_oop_generator {
void generate_sandesh_async_create_fn(ofstream &out, t_sandesh *tsandesh);
void generate_sandesh_async_create_macro(ofstream &out, t_sandesh *tsandesh);
void generate_sandesh_session_log_unrolled_fn(ofstream &out, t_sandesh *tsandesh);
void generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out,
t_sandesh *tsandesh);
void generate_sandesh_flow_send_fn(ofstream &out, t_sandesh *tsandesh);
void generate_sandesh_systemlog_creators(ofstream &out, t_sandesh *tsandesh);
void generate_sandesh_objectlog_creators(ofstream &out, t_sandesh *tsandesh);
Expand Down Expand Up @@ -1569,10 +1571,18 @@ void t_cpp_generator::generate_sandesh_objectlog_creators(ofstream &out,

void t_cpp_generator::generate_sandesh_session_log_unrolled_fn(ofstream &out,
t_sandesh *tsandesh) {
//To be removed
out << indent() << "static void LogUnrolled";
out << generate_sandesh_async_creator(tsandesh, true, false, false, "", "",
true, false, false, false);
out << ";";
out << ";" << endl;
}

void t_cpp_generator::generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out,
t_sandesh *tsandesh) {
out << "static void adjust_session_end_point_objects(std::vector"
" <SessionEndpoint> & session_data);" << endl;
out << endl;
}

void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out,
Expand All @@ -1589,23 +1599,6 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out,
indent_up();
out << indent() << "return;" << endl;
scope_down(out);
out << indent() <<
"if (IsFlowLoggingEnabled() && LoggingUseSyslog()) {" << endl;
indent_up();
out << indent() << "UpdateTxMsgFailStats(\"" << tsandesh->get_name() <<
"\", 0, SandeshTxDropReason::SendingToSyslog);" << endl;
const t_type *t = tsandesh->get_type();
if (((t_base_type *)t)->is_sandesh_session()) {
out << indent() << "LogUnrolled" <<
generate_sandesh_async_creator(tsandesh, false,
false, false, "", "", false, false, false) << ";" << endl;
} else {
out << indent() << "Log" <<
generate_sandesh_async_creator(tsandesh, false,
false, false, "", "", false, false, false) << ";" << endl;
}
out << indent() << "return;" << endl;
scope_down(out);
out << indent() << "if (IsSendingAllMessagesDisabled() ||" <<
" IsSendingFlowsDisabled()) {" << endl;
indent_up();
Expand All @@ -1620,6 +1613,16 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out,
scope_down(out);
out << indent() << "return;" << endl;
scope_down(out);

out << indent() <<
"if (is_send_slo_to_logger_enabled() || is_send_sampled_to_logger_enabled()) { " <<
endl;
indent_up();
const t_type *t = tsandesh->get_type();
if (((t_base_type *)t)->is_sandesh_session()) {
out << indent() << "LogUnrolled(category, level, session_data);" << endl;
}
scope_down(out);
out << indent() << "if (level >= SendingLevel()) {" << endl;
indent_up();
out << indent() << "UpdateTxMsgFailStats(\"" << tsandesh->get_name() <<
Expand All @@ -1639,6 +1642,15 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out,
" * snh = new " << tsandesh->get_name() <<
generate_sandesh_no_static_const_string_function(tsandesh,
false, false, false, false) << ";" << endl;
out << indent() << "if (!is_send_sampled_to_collector_enabled() && !is_send_slo_to_collector_enabled()) {" << endl;
indent_up();
out << indent() << "return;" << endl;
scope_down(out);
out << indent() << " if (is_send_sampled_to_collector_enabled() != is_send_slo_to_collector_enabled()) {" << endl;
indent_up();
//out << indent() << "adjust_session_end_point_objects(snh->session_data);";
scope_down(out);
out << endl;
out << indent() << "snh->set_level(level);" << endl;
out << indent() << "snh->set_category(category);" << endl;
out << indent() << "snh->Dispatch();" << endl;
Expand All @@ -1653,6 +1665,7 @@ void t_cpp_generator::generate_sandesh_flow_creators(ofstream &out,
// Generate LogUnrolled declaration only for Session message
if (((t_base_type *)t)->is_sandesh_session()) {
generate_sandesh_session_log_unrolled_fn(out, tsandesh);
generate_sandesh_session_adjust_session_end_point_objects_fn(out, tsandesh);
}
// Generate send function and macros
generate_sandesh_flow_send_fn(out, tsandesh);
Expand Down
88 changes: 88 additions & 0 deletions library/cpp/sandesh.cc
Expand Up @@ -10,6 +10,8 @@

#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <base/logging.h>
#include <base/parse_object.h>
#include <base/queue_task.h>
Expand All @@ -33,11 +35,17 @@
#include "sandesh_connection.h"
#include "sandesh_state_machine.h"

#include <log4cplus/helpers/pointer.h>
#include <log4cplus/configurator.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/syslogappender.h>

using boost::asio::ip::tcp;
using boost::asio::ip::address;

using namespace contrail::sandesh::protocol;
using namespace contrail::sandesh::transport;
using namespace log4cplus;

// Statics
Sandesh::SandeshRole::type Sandesh::role_ = SandeshRole::Invalid;
Expand All @@ -51,6 +59,10 @@ bool Sandesh::disable_flow_collection_ = false;
bool Sandesh::disable_sending_all_ = false;
bool Sandesh::disable_sending_object_logs_ = false;
bool Sandesh::disable_sending_flows_ = false;
bool Sandesh::slo_to_collector_ = false;
bool Sandesh::sampled_to_collector_ = false;
bool Sandesh::slo_to_logger_ = false;
bool Sandesh::sampled_to_logger_ = false;
SandeshClient *Sandesh::client_ = NULL;
SandeshConfig Sandesh::config_;
std::auto_ptr<Sandesh::SandeshRxQueue> Sandesh::recv_queue_;
Expand All @@ -70,10 +82,17 @@ SandeshMessageStatistics Sandesh::msg_stats_;
tbb::mutex Sandesh::stats_mutex_;
log4cplus::Logger Sandesh::logger_ =
log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SANDESH"));
log4cplus::Logger Sandesh::slo_logger_ =
log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SLO_SESSION"));
log4cplus::Logger Sandesh::sampled_logger_ =
log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SAMPLED_SESSION"));

Sandesh::ModuleContextMap Sandesh::module_context_;
tbb::atomic<uint32_t> Sandesh::sandesh_send_ratelimit_;

const char *loggingPattern = "%D{%Y-%m-%d %a %H:%M:%S:%Q %Z} "
" %h [Thread %t, Pid %i]: %m%n";

const char * Sandesh::SandeshRoleToString(SandeshRole::type role) {
switch (role) {
case SandeshRole::Generator:
Expand Down Expand Up @@ -962,3 +981,72 @@ size_t Sandesh::SandeshQueue::AtomicDecrementQueueCount(
size_t sandesh_size = element->GetSize();
return count_.fetch_and_add((size_t)(0-sandesh_size)) - sandesh_size;
}

/*
* Add the configured appenders for the sample logger
*/
void Sandesh::set_logger_appender(const std::string &file_name, long max_file_size,
int max_backup_index,
const std::string &syslog_facility,
const std::vector<std::string> &destn,
const std::string &ident,
bool is_sampled_logger) {
log4cplus::Logger logger;
if (is_sampled_logger) {
logger = Sandesh::sampled_logger();
} else {
logger = Sandesh::slo_logger();
}
logger.setAdditivity(false);
// Session messages log level is
logger.setLogLevel(SandeshLevel::SYS_NOTICE);
// Local logging for SLO logger
if (std::find(destn.begin(), destn.end(), "file") !=
destn.end()) {
// Append file appender to SLO logger
SharedAppenderPtr fileappender(new RollingFileAppender(file_name,
max_file_size, max_backup_index));
logger.addAppender(fileappender);
if (is_sampled_logger) {
Sandesh::sampled_to_logger_ = true;
} else {
Sandesh::slo_to_logger_ = true;
}
}
// SYSLOG appender for SLO logger
if (std::find(destn.begin(), destn.end(), "syslog") !=
destn.end()) {
helpers::Properties props;
std::string syslogident = boost::str(
boost::format("%1%[%2%]") % ident % getpid());
props.setProperty(LOG4CPLUS_TEXT("facility"),
boost::starts_with(syslog_facility, "LOG_")
? syslog_facility.substr(4)
: syslog_facility);
props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident);
props.setProperty(LOG4CPLUS_TEXT("additivity"), "false");
SharedAppenderPtr syslogappender(new SysLogAppender(props));
std::auto_ptr<Layout> syslog_layout_ptr(new PatternLayout(
loggingPattern));
syslogappender->setLayout(syslog_layout_ptr);
logger.addAppender(syslogappender);
if (is_sampled_logger) {
Sandesh::sampled_to_logger_ = true;
} else {
Sandesh::slo_to_logger_ = true;
}
}
}

void Sandesh::set_send_to_collector_flags(
const std::vector<std::string> &sampled_destn,
const std::vector<std::string> &slo_destn) {
if (std::find(slo_destn.begin(), slo_destn.end(), "collector") !=
slo_destn.end()) {
Sandesh::slo_to_collector_ = true;
}
if (std::find(sampled_destn.begin(), sampled_destn.end(), "collector") !=
sampled_destn.end()) {
Sandesh::sampled_to_collector_ = true;
}
}
22 changes: 22 additions & 0 deletions library/cpp/sandesh.h
Expand Up @@ -319,6 +319,22 @@ class Sandesh {
static const char* LevelToString(SandeshLevel::type level);
static SandeshLevel::type StringToLevel(std::string level);
static log4cplus::Logger& logger() { return logger_; }
static log4cplus::Logger& slo_logger() { return slo_logger_; }
static log4cplus::Logger& sampled_logger() { return sampled_logger_; }
static void set_logger_appender(const std::string &file_name,
long max_file_size,
int max_backup_index,
const std::string &syslog_facility,
const std::vector<std::string> &destn,
const std::string &ident,
bool is_sampled_logger);
static void set_send_to_collector_flags(
const std::vector<std::string> &sampled_destination,
const std::vector<std::string> &slo_destination);
static bool is_send_slo_to_collector_enabled() { return slo_to_collector_; }
static bool is_send_sampled_to_collector_enabled() { return sampled_to_collector_; }
static bool is_send_slo_to_logger_enabled() { return slo_to_logger_; }
static bool is_send_sampled_to_logger_enabled() { return sampled_to_logger_; }

protected:
void set_timestamp(time_t timestamp) { timestamp_ = timestamp; }
Expand Down Expand Up @@ -407,6 +423,8 @@ class Sandesh {
static SandeshMessageStatistics msg_stats_;
static tbb::mutex stats_mutex_;
static log4cplus::Logger logger_;
static log4cplus::Logger slo_logger_;
static log4cplus::Logger sampled_logger_;
static bool disable_flow_collection_; // disable flow collection
static SandeshConfig config_;
static bool disable_sending_all_;
Expand All @@ -423,6 +441,10 @@ class Sandesh {
std::string category_;
std::string name_;
static tbb::atomic<uint32_t> sandesh_send_ratelimit_;
static bool slo_to_collector_;
static bool sampled_to_collector_;
static bool slo_to_logger_;
static bool sampled_to_logger_;
};

struct SandeshElement {
Expand Down

0 comments on commit f4e9036

Please sign in to comment.