GithubHelp home page GithubHelp logo

dmitry-saprykin / cppmetrics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ultradns/cppmetrics

0.0 1.0 0.0 85 KB

C++ port of the codahale/dropwizard metrics library

License: Apache License 2.0

CMake 3.77% Shell 1.66% C++ 93.61% C 0.96%

cppmetrics's Introduction

##Background

cppmetrics is a C++ port of the DropWizard metrics!. The library implements the standard metrics primitives like Gauge, Counter, Histogram, Meter and Timer and the provides the reporter implementations like the ConsoleReporter, GraphiteRepoter out of the box. Its written in C++98 to make the integration into existing pre-C++11 codebases easier and should be portable across different platforms but being used only in linux environment.

Build Status

Build dependencies

  • cmake (>= 2.6.5)
  • boost libraries (>= 1.53.0)
  • google logging framework (>= 0.3.1)
  • gtest (>= 1.6.0, dependency for the unit tests only.)

How to build

# It is recommended to create the build directory in the parent directory of cppmetrics source as opposed to creating in the cppmetrics directory.
mkdir build
cd build
cmake -DCMAKE_CXX_COMPILER=g++ -DCMAKE_INSTALL_PREFIX=dist -DBOOST_DIR=<BOOST_BINARY_DISTRO> -DGLOG_DIR=<GLOG_BINARY_DISTRO> -DGTEST_DIR=<GTEST_BINARY_DISTRO> ../cppmetrics/
make gtest
make package

The above process produces a tar file of include files and a static library and should be used to statically link in your existing application. The shared library option is not turned off by default but can be turned on easily if required.

##Sample code snippet

####Using a Histogram or a timer or a meter..

#include <cppmetrics/cppmetrics.h>
...

bool QueryHandler::doProcess(const Query& query) {
    cppmetrics::core::MetricRegistryPtr registry(
            cppmetrics::core::MetricRegistry::DEFAULT_REGISTRY());

    // More initialization.

    cppmetrics::core::CounterPtr query_counter(registry->counter("get_requests"));
    query_counter->increment();
    // More  processing
    {
       cppmetrics::core::TimerContextPtr timer(
                metrics->timer("query_process")->timerContextPtr());
       // Do some computation or IO.
       // timer stats will be updated in the registry at the end of the scope.                
    }
}

####Creating the default metrics registry and a graphite reporter that pushes the data to graphite server.

#include <boost/noncopyable.hpp>
#include <cppmetrics/cppmetrics.h>
#include <glog/logging.h>

namespace sample {

class GraphiteReporterOptions {
public:
    std::string host_;                  ///<  The graphite server.
    boost::uint32_t port_;              ///<  The graphite port.
    std::string prefix_;                ///<  The prefix to the graphite.
    boost::uint32_t interval_in_secs_;  ///<  The reporting period in secs.
};

/*
 *  Helper class that sets up the default registry and the graphite reporter.
 */
class Controller : boost::noncopyable
{
public:
    cppmetrics::core::MetricRegistryPtr getRegistry() {
        return core::MetricRegistry::DEFAULT_REGISTRY();
    }
    
    void configureAndStartGraphiteReporter(const GraphiteReporterOptions& graphite_options) {
        if (!graphite_reporter_) {
            const std::string& graphite_host(graphite_options.host_);

            boost::uint32_t graphite_port(graphite_options.port_);
            graphite::GraphiteSenderPtr graphite_sender(
                new graphite::GraphiteSenderTCP(graphite_host, graphite_port));

            graphite_reporter_.reset(
                new graphite::GraphiteReporter(getRegistry(), graphite_sender,
                        graphite_options.prefix_));
            graphite_reporter_->start(boost::chrono::seconds(graphite_options.interval_in_secs_));
        } else {
            LOG(ERROR) << "Graphite reporter already configured.";
        }
    }
private:
    boost::scoped_ptr<cppmetrics::graphite::GraphiteReporter> graphite_reporter_;
};

}

###TODO

  • Currently the Timer and Meter resolutions are in millis and per-minute respectively, make this configurable.
  • Provide more reporters out of the box.

cppmetrics's People

Contributors

harelba avatar roeckelein avatar vpoliboy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.