-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c616df
commit 24fc158
Showing
17 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
# Build directories | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Tests", | ||
"type": "cppvsdbg", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/build/test/Debug/tests.exe", | ||
"args": [], | ||
"stopAtEntry": false, | ||
"cwd": "${fileDirname}", | ||
"environment": [], | ||
"console": "externalTerminal" | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"cmake.configureOnOpen": true, | ||
"files.associations": { | ||
"atomic": "cpp", | ||
"memory": "cpp", | ||
"xutility": "cpp", | ||
"bit": "cpp", | ||
"compare": "cpp", | ||
"concepts": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"exception": "cpp", | ||
"initializer_list": "cpp", | ||
"iosfwd": "cpp", | ||
"limits": "cpp", | ||
"new": "cpp", | ||
"tuple": "cpp", | ||
"type_traits": "cpp", | ||
"typeinfo": "cpp", | ||
"utility": "cpp", | ||
"xmemory": "cpp", | ||
"xstddef": "cpp", | ||
"xtr1common": "cpp", | ||
"xtree": "cpp", | ||
"algorithm": "cpp", | ||
"cctype": "cpp", | ||
"cmath": "cpp", | ||
"functional": "cpp", | ||
"iterator": "cpp", | ||
"list": "cpp", | ||
"map": "cpp", | ||
"mutex": "cpp", | ||
"ratio": "cpp", | ||
"stdexcept": "cpp", | ||
"stop_token": "cpp", | ||
"string": "cpp", | ||
"system_error": "cpp", | ||
"thread": "cpp", | ||
"unordered_map": "cpp", | ||
"vector": "cpp", | ||
"xhash": "cpp", | ||
"xstring": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "cmake", | ||
"label": "CMake: build", | ||
"command": "build", | ||
"targets": [ | ||
"ALL_BUILD" | ||
], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
}, | ||
"problemMatcher": [], | ||
"detail": "CMake template build task" | ||
}, | ||
{ | ||
"type": "cmake", | ||
"label": "CMake: test", | ||
"command": "test", | ||
"problemMatcher": [], | ||
"detail": "CMake template test task", | ||
"group": { | ||
"kind": "test", | ||
"isDefault": true | ||
}, | ||
"dependsOn": [ | ||
"CMake: build" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.19) | ||
project(metrics-cpp VERSION 1.0) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
add_subdirectory(src) | ||
add_subdirectory(test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <metrics_export.h> | ||
|
||
#include <metric.h> | ||
|
||
#include <memory> | ||
|
||
namespace Metrics | ||
{ | ||
class ICounterValue : public IMetric | ||
{ | ||
public: | ||
virtual ICounterValue& operator++(int) = 0; | ||
virtual ICounterValue& operator+=(uint32_t value) = 0; | ||
virtual uint64_t value() const = 0; | ||
virtual void reset() = 0; | ||
operator uint64_t() { return value(); }; | ||
|
||
protected: | ||
METRICS_EXPORT virtual ~ICounterValue() =0; | ||
}; | ||
|
||
// Stack-based container for actual metric | ||
class Counter : public ICounterValue | ||
{ | ||
private: | ||
std::shared_ptr<ICounterValue> m_value; | ||
|
||
public: | ||
ICounterValue& operator++(int) override { return (*m_value)++; }; | ||
ICounterValue& operator+=(uint32_t value) override { return (*m_value+=value); }; | ||
void reset() override { m_value.reset(); }; | ||
uint64_t value() const override { return m_value->value(); }; | ||
|
||
Counter(std::shared_ptr<ICounterValue> value) : m_value(value) {}; | ||
Counter(const Counter &other) = default; | ||
~Counter() = default; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <metrics_export.h> | ||
|
||
#include <metric.h> | ||
|
||
#include <memory> | ||
|
||
namespace Metrics | ||
{ | ||
class IGaugeValue : public IMetric | ||
{ | ||
public: | ||
virtual IGaugeValue& operator=(double value) = 0; | ||
virtual IGaugeValue& operator+=(double value) = 0; | ||
virtual IGaugeValue& operator-=(double value) = 0; | ||
virtual double value() = 0; | ||
operator double() { return value(); }; | ||
|
||
protected: | ||
METRICS_EXPORT virtual ~IGaugeValue() = 0; | ||
}; | ||
|
||
// Stack-based container for actual metric | ||
class Gauge : public IGaugeValue | ||
{ | ||
private: | ||
std::shared_ptr<IGaugeValue> m_value; | ||
|
||
public: | ||
IGaugeValue& operator=(double value) override | ||
{ | ||
*m_value = value; | ||
return *this; | ||
}; | ||
IGaugeValue& operator+=(double value) override | ||
{ | ||
*m_value += value; | ||
return *this; | ||
}; | ||
IGaugeValue& operator-=(double value) override | ||
{ | ||
*m_value -= value; | ||
return *this; | ||
}; | ||
double value() override { return m_value->value(); }; | ||
|
||
Gauge(std::shared_ptr<IGaugeValue> value) : m_value(value){}; | ||
Gauge(const Gauge &other) = default; | ||
~Gauge() = default; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace Metrics { | ||
typedef std::pair<std::string,std::string> Label; | ||
// TODO: use vector-based container | ||
typedef std::map<std::string,std::string> Labels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include <labels.h> | ||
|
||
#include <metrics_export.h> | ||
|
||
#include <string> | ||
|
||
namespace Metrics | ||
{ | ||
class IMetric { | ||
public: | ||
IMetric() = default; | ||
IMetric(const IMetric&) = default; | ||
IMetric(IMetric&&) = default; | ||
METRICS_EXPORT virtual ~IMetric() = default; | ||
}; | ||
|
||
struct Key | ||
{ | ||
std::string name; | ||
Labels labels; | ||
|
||
bool operator<(const Key& other) { | ||
return name < other.name && labels < other.labels; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <metrics_export.h> | ||
|
||
#include <counter.h> | ||
#include <gauge.h> | ||
|
||
namespace Metrics | ||
{ | ||
class IRegistry { | ||
public: | ||
virtual Gauge getGauge() = 0; | ||
virtual Counter getCounter() = 0; | ||
}; | ||
|
||
METRICS_EXPORT IRegistry& defaultRegistry(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
include(GenerateExportHeader) | ||
|
||
add_library(metrics SHARED | ||
counter.cpp | ||
gauge.cpp | ||
registry.cpp | ||
) | ||
generate_export_header(metrics) | ||
|
||
target_include_directories(metrics PUBLIC "${PROJECT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <counter.h> | ||
|
||
#include <atomic> | ||
|
||
namespace Metrics | ||
{ | ||
ICounterValue::~ICounterValue() | ||
{ | ||
} | ||
|
||
class CounterImpl : public ICounterValue | ||
{ | ||
private: | ||
std::atomic<uint64_t> m_value; | ||
|
||
public: | ||
CounterImpl() = default; | ||
CounterImpl(const CounterImpl &) = delete; | ||
~CounterImpl() = default; | ||
ICounterValue &operator++(int) override | ||
{ | ||
m_value++; | ||
return *this; | ||
}; | ||
ICounterValue &operator+=(uint32_t value) override | ||
{ | ||
m_value += value; | ||
return *this; | ||
}; | ||
uint64_t value() const override | ||
{ | ||
return m_value.load(); | ||
} | ||
void reset() override | ||
{ | ||
m_value.store(0); | ||
} | ||
}; | ||
|
||
std::shared_ptr<IMetric> createCounter() | ||
{ | ||
return std::make_shared<CounterImpl>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
|
||
namespace Metrics { | ||
class IMetric; | ||
|
||
std::shared_ptr<IMetric> createGauge(); | ||
std::shared_ptr<IMetric> createCounter(); | ||
} |
Oops, something went wrong.