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 Hypertable;
40 using namespace std;
41 
42 
44  : m_slow_query_log(slow_query_log) {
45  m_ganglia_collector = std::make_shared<MetricsCollectorGanglia>("thriftbroker", props);
46  m_collection_interval = props->get_i32("Hypertable.Monitoring.Interval");
49 }
50 
52  int error;
53  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
54  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
55 }
56 
58  m_comm->cancel_timer(shared_from_this());
59 }
60 
62  int error;
63 
64  if (event->type == Hypertable::Event::TIMER) {
65 
66  int64_t timestamp = Hypertable::get_ts64();
67 
69 
70  int64_t elapsed_secs = (timestamp - m_last_timestamp) / 1000000000LL;
71 
72  m_ganglia_collector->update("requests", m_requests.rate(elapsed_secs));
73  m_requests.reset();
74  m_ganglia_collector->update("errors", m_errors.rate(elapsed_secs));
75  m_errors.reset();
76  m_ganglia_collector->update("connections", m_active_connections);
77 
78  try {
79  m_ganglia_collector->publish();
80  }
81  catch (Exception &e) {
82  HT_INFOF("Problem publishing Ganglia metrics - %s", e.what());
83  }
84 
85  m_last_timestamp = timestamp;
86 
87  if (m_slow_query_log)
88  m_slow_query_log->sync();
89 
90  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
91  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
92 
93  }
94  else
95  HT_FATALF("Unrecognized event - %s", event->to_str().c_str());
96 
97 }
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.
interval_metric< int64_t > m_requests
Hyperspace requests
Declarations for MetricsHandler.
int64_t m_last_timestamp
Timestamp of last metrics collection
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++.
Time based rotating log.
Definition: Cronolog.h:59
void stop_collecting()
Stops metrics collection.
int32_t m_collection_interval
Metrics collection interval
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
virtual void handle(EventPtr &event)
Collects and publishes metrics.
Comm * m_comm
Comm layer.
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
void collect(int64_t now, MetricsCollector *collector) override
Collects process metrics.
int set_timer(uint32_t duration_millis, const DispatchHandlerPtr &handler)
Sets a timer for duration_millis milliseconds in the future.
Definition: Comm.cc:465
void cancel_timer(const DispatchHandlerPtr &handler)
Cancels all scheduled timers registered with the dispatch handler handler.
Definition: Comm.cc:483
MetricsCollectorGangliaPtr m_ganglia_collector
Ganglia metrics collector.
Error codes, Exception handling, error logging.
MetricsProcess m_metrics_process
General process metrics tracker.
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40