0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MetricsHandler.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
29 #include "MetricsHandler.h"
30 
31 #include <AsyncComm/Comm.h>
32 
33 #include <Common/Error.h>
34 #include <Common/Logger.h>
35 #include <Common/Properties.h>
36 
37 #include <mutex>
38 
39 using namespace Hyperspace;
40 using namespace Hypertable;
41 using namespace std;
42 
43 
45  m_ganglia_collector = std::make_shared<MetricsCollectorGanglia>("hyperspace", props);
46  m_collection_interval = props->get_i32("Hypertable.Monitoring.Interval");
47  m_last_timestamp = Hypertable::get_ts64();
48  m_comm = Comm::instance();
49 }
50 
52 }
53 
55  int error;
56  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
57  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
58 }
59 
61  DispatchHandlerPtr handler = shared_from_this();
62  m_comm->cancel_timer(handler);
63 }
64 
65 
67  int error;
68 
69  if (event->type == Hypertable::Event::TIMER) {
70 
71  int64_t timestamp = Hypertable::get_ts64();
72 
73  m_metrics_process.collect(timestamp, m_ganglia_collector.get());
74 
75  int64_t elapsed_secs = (timestamp - m_last_timestamp) / 1000000000LL;
76 
77  m_ganglia_collector->update("requests", m_requests.rate(elapsed_secs));
78  m_requests.reset();
79 
80  try {
81  m_ganglia_collector->publish();
82  }
83  catch (Exception &e) {
84  HT_INFOF("Problem publishing Ganglia metrics - %s", e.what());
85  }
86 
87  m_last_timestamp = timestamp;
88 
89  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
90  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
91 
92  }
93  else
94  HT_FATALF("Unrecognized event - %s", event->to_str().c_str());
95 
96 }
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
MetricsHandler(PropertiesPtr &props)
Constructor.
Program options handling.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
STL namespace.
void start_collecting()
Starts metrics collection.
Hyperspace definitions
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
std::shared_ptr< Properties > PropertiesPtr
Definition: Properties.h:447
Logging routines and macros.
Compatibility Macros for C/C++.
void stop_collecting()
Stops metrics collection.
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
virtual ~MetricsHandler()
Destructor.
virtual void handle(EventPtr &event)
Collects and publishes metrics.
Declarations for Comm.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
Timer event
Definition: Event.h:65
This is a generic exception class for Hypertable.
Definition: Error.h:314
std::shared_ptr< DispatchHandler > DispatchHandlerPtr
Smart pointer to DispatchHandler.
Declarations for MetricsHandler.
Error codes, Exception handling, error logging.
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40