RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/logger/stats_output.h
00001 /* stats_output.h                                                -*- C++ -*-
00002    Jeremy Barnes, 8 February 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Output that logs stats to the console.
00006 */
00007 
00008 #ifndef __logger__stats_output_h__
00009 #define __logger__stats_output_h__
00010 
00011 #include "logger.h"
00012 #include "soa/service/carbon_connector.h"
00013 #include "soa/service/service_base.h"
00014 #include "soa/types/date.h"
00015 #include <thread>
00016 
00017 
00018 namespace Datacratic {
00019 
00020 
00021 /*****************************************************************************/
00022 /* CONSOLE STATS OUTPUT                                                      */
00023 /*****************************************************************************/
00024 
00026 struct ConsoleStatsOutput : public LogOutput {
00027     ConsoleStatsOutput (bool debug = false);
00028 
00029     virtual ~ConsoleStatsOutput ()  {}
00030 
00031 private:
00032 
00033         struct StatItem {
00034             StatItem(const std::string& channel) :
00035                 channel(channel),
00036                 messages(0),
00037                 bytes(0)
00038             {}
00039 
00040             std::string channel;
00041             uint64_t messages;
00042             uint64_t bytes;
00043         };
00044 
00045 public:
00046 
00047     virtual void logMessage(const std::string & channel, const std::string & message);
00048 
00049     void dumpStats ();
00050 
00051     virtual void close() {}
00052     virtual Json::Value stats() const { return Json::Value (); }
00053     virtual void clearStats() {}
00054 
00055 private :
00056     bool debug;
00057     std::map<std::string, StatItem> logStats;
00058 
00059     Date lastSeconds;
00060     std::mutex lock;
00061 };
00062 
00063 
00064 /*****************************************************************************/
00065 /* CARBON STATS OUTPUT                                                       */
00066 /*****************************************************************************/
00067 
00068 struct CarbonStatsOutput : public LogOutput, public EventRecorder {
00069 
00070     CarbonStatsOutput (const std::string& carbonConnection,
00071                        const std::string& carbonPrefix);
00072 
00073     CarbonStatsOutput(std::shared_ptr<EventService> eventService,
00074                       std::string prefix);
00075 
00076     virtual ~CarbonStatsOutput ()  {}
00077 
00078     virtual void logMessage(const std::string & channel,
00079                             const std::string & message);
00080 
00081     void recordBytesWrittenToFile (const std::string& file, size_t bytes);
00082 
00083     void recordLevel (const std::string& name, double val);
00084 
00085     virtual void close() {}
00086     virtual Json::Value stats() const { return Json::Value (); }
00087     virtual void clearStats() {}
00088 
00089 private:
00090     std::shared_ptr<EventService> eventService;
00091 };
00092 
00093 
00094 
00095 
00096 } // namespace Datacratic
00097 
00098 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator