![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* graphite_connector.h -*- C++ -*- 00002 Jeremy Barnes, 3 August 2011 00003 Copyright (c) 2011 Datacratic. All rights reserved. 00004 00005 Class to accumulate operational stats and connect to graphite (directly). 00006 */ 00007 00008 #pragma once 00009 00010 #include "ace/SOCK_Dgram.h" 00011 #include "jml/stats/distribution.h" 00012 #include <boost/thread.hpp> 00013 #include "soa/types/date.h" 00014 #include <unordered_map> 00015 #include <map> 00016 #include <deque> 00017 #include <boost/scoped_ptr.hpp> 00018 00019 00020 namespace Datacratic { 00021 00022 00023 struct StatReading { 00024 StatReading(const std::string & name = "", 00025 float value = 0.0, 00026 Date timestamp = Date()) 00027 : name(name), value(value), timestamp(timestamp) 00028 { 00029 } 00030 00031 std::string name; 00032 float value; 00033 Date timestamp; 00034 }; 00035 00036 /*****************************************************************************/ 00037 /* STAT AGGREGATOR */ 00038 /*****************************************************************************/ 00039 00042 struct StatAggregator { 00043 00044 virtual ~StatAggregator() 00045 { 00046 } 00047 00049 virtual void record(float value) = 0; 00050 00053 virtual std::vector<StatReading> read(const std::string & prefix) = 0; 00054 }; 00055 00056 00057 /*****************************************************************************/ 00058 /* COUNTER AGGREGATOR */ 00059 /*****************************************************************************/ 00060 00063 struct CounterAggregator : public StatAggregator { 00064 CounterAggregator(); 00065 00066 virtual ~CounterAggregator(); 00067 00068 virtual void record(float value); 00069 00070 std::pair<double, Date> reset(); 00071 00074 virtual std::vector<StatReading> read(const std::string & prefix); 00075 00076 private: 00077 Date start; //< Date at which we last cleared the counter 00078 double total; //< total since we last added it up 00079 00080 std::deque<double> totalsBuffer; //< Totals for the last n reads. 00081 00082 }; 00083 00084 00085 /*****************************************************************************/ 00086 /* GAUGE AGGREGATOR */ 00087 /*****************************************************************************/ 00088 00091 struct GaugeAggregator : public StatAggregator { 00092 GaugeAggregator(); 00093 00094 virtual ~GaugeAggregator(); 00095 00097 virtual void record(float value); 00098 00100 std::pair<ML::distribution<float> *, Date> reset(); 00101 00105 virtual std::vector<StatReading> read(const std::string & prefix); 00106 00107 private: 00108 Date start; //< Date at which we last cleared the counter 00109 ML::distribution<float> * volatile values; //< List of added values 00110 }; 00111 00112 00113 } // namespace Datacratic