Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWanderer committed Jan 16, 2023
1 parent 4c616df commit 24fc158
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app

# Build directories
build/
19 changes: 19 additions & 0 deletions .vscode/launch.json
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"
},
]
}
49 changes: 49 additions & 0 deletions .vscode/settings.json
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"
}
}
33 changes: 33 additions & 0 deletions .vscode/tasks.json
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"
]
}
]
}
11 changes: 11 additions & 0 deletions CMakeLists.txt
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)
40 changes: 40 additions & 0 deletions include/counter.h
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;
};
}
52 changes: 52 additions & 0 deletions include/gauge.h
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;
};
}
10 changes: 10 additions & 0 deletions include/labels.h
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;
}
28 changes: 28 additions & 0 deletions include/metric.h
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;
}
};
}
17 changes: 17 additions & 0 deletions include/registry.h
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();
}
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
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}")
44 changes: 44 additions & 0 deletions src/counter.cpp
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>();
}
}
10 changes: 10 additions & 0 deletions src/factory.h
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();
}
Loading

0 comments on commit 24fc158

Please sign in to comment.