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 
43  m_ganglia_collector = std::make_shared<MetricsCollectorGanglia>("master", props);
44  m_collection_interval = props->get_i32("Hypertable.Monitoring.Interval");
45  m_last_timestamp = Hypertable::get_ts64();
46  m_comm = Comm::instance();
47 }
48 
50  if (m_started)
51  return;
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  m_started = true;
56 }
57 
59  if (!m_started)
60  return;
61  m_comm->cancel_timer(shared_from_this());
62  m_started = false;
63 }
64 
66  int error;
67 
68  if (!m_started)
69  return;
70 
71  if (event->type == Hypertable::Event::TIMER) {
72 
73  int64_t timestamp = Hypertable::get_ts64();
74 
75  m_metrics_process.collect(timestamp, m_ganglia_collector.get());
76 
77  int64_t elapsed_secs = (timestamp - m_last_timestamp) / 1000000000LL;
78 
79  m_ganglia_collector->update("operations", m_operations.rate(elapsed_secs));
80  m_operations.reset();
81 
82  try {
83  m_ganglia_collector->publish();
84  }
85  catch (Exception &e) {
86  HT_INFOF("Problem publishing Ganglia metrics - %s", e.what());
87  }
88 
89  m_last_timestamp = timestamp;
90 
91  if ((error = m_comm->set_timer(m_collection_interval, shared_from_this())) != Error::OK)
92  HT_FATALF("Problem setting timer - %s", Error::get_text(error));
93 
94  }
95  else
96  HT_FATALF("Unrecognized event - %s", event->to_str().c_str());
97 
98 }
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.
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 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
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