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 Hypertable::FsBroker::Lib;
41 using namespace std;
42 
43 
44 MetricsHandler::MetricsHandler(PropertiesPtr &props, const std::string &type)
45  : m_type(type) {
46  m_ganglia_collector = std::make_shared<MetricsCollectorGanglia>("fsbroker", props);
47  m_collection_interval = props->get_i32("Hypertable.Monitoring.Interval");
50 }
51 
53  int error;
54  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
55  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
56 }
57 
59  m_comm->cancel_timer(shared_from_this());
60 }
61 
63  int error;
64 
65  if (event->type == Hypertable::Event::TIMER) {
66 
67  int64_t now = Hypertable::get_ts64();
69  int64_t elapsed_millis = (now - m_last_timestamp) / 1000000LL;
70  int64_t elapsed_seconds = elapsed_millis / 1000;
71  m_ganglia_collector->update("type", m_type);
72 
73  {
74  lock_guard<mutex> lock(m_mutex);
75 
76  m_ganglia_collector->update("errors", m_errors);
77 
78  int32_t avgSyncLatency = (m_syncs > 0) ? m_sync_latency/m_syncs : 0;
79  m_ganglia_collector->update("syncLatency", avgSyncLatency);
80 
81  if (elapsed_millis > 0) {
82  double sps = (double)m_syncs / (double)elapsed_seconds;
83  m_ganglia_collector->update("syncs", sps);
84  int64_t mbps = (m_bytes_read / 1000000) / elapsed_seconds;
85  m_ganglia_collector->update("readThroughput", (int)mbps);
86  mbps = (m_bytes_written / 1000000) / elapsed_seconds;
87  m_ganglia_collector->update("writeThroughput", (int)mbps);
88  }
89 
90  m_last_timestamp = now;
91  m_errors = 0;
92  m_syncs = 0;
93  m_sync_latency = 0;
94  m_bytes_read = 0;
95  m_bytes_written = 0;
96  }
97 
98  try {
99  m_ganglia_collector->publish();
100  }
101  catch (Exception &e) {
102  HT_INFOF("Problem publishing Ganglia metrics - %s", e.what());
103  }
104 
105  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
106  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
107 
108  }
109  else
110  HT_FATALF("Unrecognized event - %s", event->to_str().c_str());
111 
112 }
static Comm * instance()
Creates/returns singleton instance of the Comm class.
Definition: Comm.h:72
int32_t m_syncs
Syncs since last metrics collection.
int32_t m_collection_interval
Metrics collection interval
Program options handling.
int64_t m_bytes_read
Bytes read since last metrics collection.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
void start_collecting()
Starts metrics collection.
STL namespace.
virtual void handle(EventPtr &event)
Collects and publishes metrics.
MetricsProcess m_metrics_process
General process metrics tracker.
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++.
int64_t m_last_timestamp
Timestamp of last metrics collection
int32_t m_errors
Error count since last metrics collection.
Hypertable definitions
#define HT_FATALF(msg,...)
Definition: Logger.h:343
Declarations for MetricsHandler.
int64_t m_bytes_written
Bytes written since last metrics collection.
MetricsCollectorGangliaPtr m_ganglia_collector
Ganglia metrics collector.
std::string m_type
FsBroker type (e.g. "local", "qfs", etc.)
Declarations for Comm.
#define HT_INFOF(msg,...)
Definition: Logger.h:272
MetricsHandler(PropertiesPtr &props, const std::string &type)
Constructor.
int32_t m_sync_latency
Cumulative sync latency since last metrics collection.
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.
std::mutex m_mutex
Mutex for serializing access to members
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
File system broker framework and client library.
Definition: Broker.h:44
Error codes, Exception handling, error logging.
void stop_collecting()
Stops metrics collection.
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40