Skip to content

Commit

Permalink
Removed (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpihl authored Dec 15, 2023
1 parent c002529 commit 4ca75b9
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 213 deletions.
3 changes: 2 additions & 1 deletion NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ every change, see the Git log.

Latest
------
* tbd
* Major: Removed `metrics::observe_metric()` as this functionality should be
handled elsewhere and differently.

5.2.2
-----
Expand Down
84 changes: 1 addition & 83 deletions src/abacus/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ metrics::metrics(const metric_info* info, std::size_t count) :
metrics::metrics(metrics&& other) noexcept :
m_info(std::move(other.m_info)),
m_name_to_index(std::move(other.m_name_to_index)),
m_meta_data(other.m_meta_data), m_value_data(other.m_value_data),
m_observer_map(std::move(other.m_observer_map))
m_meta_data(other.m_meta_data), m_value_data(other.m_value_data)
{
other.m_meta_data = nullptr;
other.m_value_data = nullptr;
Expand All @@ -208,7 +207,6 @@ auto metrics::operator=(metrics&& other) noexcept -> metrics&
{
m_info = std::move(other.m_info);
m_name_to_index = std::move(other.m_name_to_index);
m_observer_map = std::move(other.m_observer_map);
m_meta_data = other.m_meta_data;
m_value_data = other.m_value_data;
other.m_meta_data = nullptr;
Expand Down Expand Up @@ -240,10 +238,6 @@ auto metrics::meta_bytes() const -> std::size_t
auto metrics::value_data() const -> const uint8_t*
{
assert(m_meta_data != nullptr);
for (auto const& item : m_observer_map)
{
item.second();
}
return m_value_data;
}

Expand Down Expand Up @@ -327,74 +321,6 @@ auto metrics::kind(std::size_t index) const -> abacus::kind
return m_info[index].kind;
}

void metrics::observe_metric(const std::string& name,
delegate<uint64_t()> callback) const
{
assert(m_meta_data != nullptr);
assert(m_observer_map.find(name) == m_observer_map.end());
auto metric_index = index(name);
assert(is_uint64(metric_index));
auto value_ptr = static_cast<uint64_t*>(initialize(metric_index));
std::function<void()> mem_callback(
[callback, value_ptr]()
{
uint64_t value = callback();
*value_ptr = value;
});
m_observer_map.emplace(name, mem_callback);
}

void metrics::observe_metric(const std::string& name,
delegate<int64_t()> callback) const
{
assert(m_meta_data != nullptr);
assert(m_observer_map.find(name) == m_observer_map.end());
auto metric_index = index(name);
assert(is_int64(metric_index));
auto value_ptr = static_cast<int64_t*>(initialize(metric_index));
std::function<void()> mem_callback(
[callback, value_ptr]()
{
int64_t value = callback();
*value_ptr = value;
});
m_observer_map.emplace(name, mem_callback);
}

void metrics::observe_metric(const std::string& name,
delegate<double()> callback) const
{
assert(m_meta_data != nullptr);
assert(m_observer_map.find(name) == m_observer_map.end());
auto metric_index = index(name);
assert(is_float64(metric_index));
auto value_ptr = static_cast<double*>(initialize(metric_index));
std::function<void()> mem_callback(
[callback, value_ptr]()
{
double value = callback();
*value_ptr = value;
});
m_observer_map.emplace(name, mem_callback);
}

void metrics::observe_metric(const std::string& name,
delegate<bool()> callback) const
{
assert(m_meta_data != nullptr);
assert(m_observer_map.find(name) == m_observer_map.end());
auto metric_index = index(name);
assert(is_boolean(metric_index));
auto value_ptr = static_cast<bool*>(initialize(metric_index));
std::function<void()> mem_callback(
[callback, value_ptr]()
{
bool value = callback();
*value_ptr = value;
});
m_observer_map.emplace(name, mem_callback);
}

void metrics::initialize_constant(const std::string& name, uint64_t value) const
{
assert(m_meta_data != nullptr);
Expand Down Expand Up @@ -437,8 +363,6 @@ void metrics::value(std::size_t index, uint64_t& value) const
assert(index < count());
assert(is_initialized(index));
assert(is_uint64(index));
if (m_observer_map.find(m_info[index].name) != m_observer_map.end())
m_observer_map[m_info[index].name]();
value = *static_cast<uint64_t*>(
detail::value_ptr(m_meta_data, m_value_data, index));
}
Expand All @@ -449,8 +373,6 @@ void metrics::value(std::size_t index, int64_t& value) const
assert(index < count());
assert(is_initialized(index));
assert(is_int64(index));
if (m_observer_map.find(m_info[index].name) != m_observer_map.end())
m_observer_map[m_info[index].name]();
value = *static_cast<int64_t*>(
detail::value_ptr(m_meta_data, m_value_data, index));
}
Expand All @@ -461,8 +383,6 @@ void metrics::value(std::size_t index, double& value) const
assert(index < count());
assert(is_initialized(index));
assert(is_float64(index));
if (m_observer_map.find(m_info[index].name) != m_observer_map.end())
m_observer_map[m_info[index].name]();
value = *static_cast<double*>(
detail::value_ptr(m_meta_data, m_value_data, index));
}
Expand All @@ -473,8 +393,6 @@ void metrics::value(std::size_t index, bool& value) const
assert(index < count());
assert(is_initialized(index));
assert(is_boolean(index));
if (m_observer_map.find(m_info[index].name) != m_observer_map.end())
m_observer_map[m_info[index].name]();
value = *static_cast<bool*>(
detail::value_ptr(m_meta_data, m_value_data, index));
}
Expand Down
66 changes: 3 additions & 63 deletions src/abacus/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ class metrics
/// Destructor
~metrics();

/// @returns the pointer to the meta data of the metrics.
/// @returns the pointer to the metadata of the metrics.
auto meta_data() const -> const uint8_t*;

/// @return the size of the meta data of the metrics.
/// @return the size of the metadata of the metrics.
auto meta_bytes() const -> std::size_t;

/// @returns the pointer to the value data of the metrics.
Expand Down Expand Up @@ -186,62 +186,6 @@ class metrics
return metric<MetricType>{value_ptr};
}

/// This function registers a callback that will be called when the metric
/// data is sampled, i.e when value_data() or value() is called. The
/// callback must return the value of the metric.
///
/// The passed callback will give the value of the metric and this value
/// will then be written into memory behind the scenes.
///
/// @param name The name of the metric. This is used for a check to ensure
/// that the name matches the one at the given index.
/// @param callback The callback that will be called when the metric data
/// is sampled.
void observe_metric(const std::string& name,
delegate<uint64_t()> callback) const;

/// This function registers a callback that will be called when the metric
/// data is sampled, i.e when value_data() or value() is called. The
/// callback must return the value of the metric.
///
/// The passed callback will give the value of the metric and this value
/// will then be written into memory behind the scenes.
///
/// @param name The name of the metric. This is used for a check to ensure
/// that the name matches the one at the given index.
/// @param callback The callback that will be called when the metric data
/// is sampled.
void observe_metric(const std::string& name,
delegate<int64_t()> callback) const;

/// This function registers a callback that will be called when the metric
/// data is sampled, i.e when value_data() or value() is called. The
/// callback must return the value of the metric.
///
/// The passed callback will give the value of the metric and this value
/// will then be written into memory behind the scenes.
///
/// @param name The name of the metric. This is used for a check to ensure
/// that the name matches the one at the given index.
/// @param callback The callback that will be called when the metric data
/// is sampled.
void observe_metric(const std::string& name,
delegate<double()> callback) const;

/// This function registers a callback that will be called when the metric
/// data is sampled, i.e when value_data() or value() is called. The
/// callback must return the value of the metric.
///
/// The passed callback will give the value of the metric and this value
/// will then be written into memory behind the scenes.
///
/// @param name The name of the metric. This is used for a check to ensure
/// that the name matches the one at the given index.
/// @param callback The callback that will be called when the metric data
/// is sampled.
void observe_metric(const std::string& name,
delegate<bool()> callback) const;

/// Initialize a constant uint64_t metric at the given index.
///
/// A constant metric that is initialized with a value and never reset.
Expand Down Expand Up @@ -393,15 +337,11 @@ class metrics
/// Map to get index from names
std::map<std::string, std::size_t> m_name_to_index;

/// The raw memory for the meta data
/// The raw memory for the metadata
uint8_t* m_meta_data = nullptr;

/// The raw memory for the value data
uint8_t* m_value_data = nullptr;

/// Map of the observed metrics and the callbacks that are called on
/// value_data() and value()
mutable std::map<std::string, std::function<void()>> m_observer_map;
};
}
}
67 changes: 1 addition & 66 deletions test/src/test_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ TEST(test_metrics, protocol_version)
EXPECT_EQ(metrics.meta_bytes(), expected_meta_data.size());
EXPECT_EQ(metrics.value_bytes(), expected_value_data.size());
{
// Print the meta data as hex
// Print the metadata as hex
std::stringstream meta_stream;
meta_stream << std::hex;
for (std::size_t i = 0; i < metrics.meta_bytes(); ++i)
Expand Down Expand Up @@ -345,68 +345,3 @@ TEST(test_metrics, protocol_version)
metrics.value_data() + metrics.value_bytes()));
}
}

TEST(test_metrics, observe)
{
abacus::metric_info infos[5] = {
abacus::metric_info{"metric0", "An unsigned integer metric",
abacus::type::uint64, abacus::kind::counter,
abacus::unit{"bytes"}},
abacus::metric_info{"metric1", "An unsigned integer metric",
abacus::type::uint64, abacus::kind::counter,
abacus::unit{"bytes"}},
abacus::metric_info{"metric2", "A signed integer metric",
abacus::type::int64, abacus::kind::gauge,
abacus::unit{"USD"}},
abacus::metric_info{"metric3", "A floating point metric",
abacus::type::float64, abacus::kind::gauge,
abacus::unit{"ms"}},
abacus::metric_info{"metric4", "A boolean metric",
abacus::type::boolean, abacus::kind::gauge}};

abacus::metrics metrics(infos);

uint64_t observed_uint = 0;
int64_t observed_int = 0;
double observed_float = 0.0;
bool observed_bool = false;

uint64_t value_uint = 0;
uint64_t value_unobs = 0;
int64_t value_int = 0;
double value_float = 0.0;
bool value_bool = false;

metrics.observe_metric("metric0",
abacus::delegate<uint64_t()>(
[&observed_uint]() { return observed_uint; }));
auto metric1 = metrics.initialize_metric<abacus::type::uint64>("metric1");
metrics.observe_metric("metric2",
abacus::delegate<int64_t()>(
[&observed_int]() { return observed_int; }));
metrics.observe_metric("metric3",
abacus::delegate<double()>(
[&observed_float]() { return observed_float; }));
metrics.observe_metric(
"metric4",
abacus::delegate<bool()>([&observed_bool]() { return observed_bool; }));

observed_uint = 42U;
observed_int = -42;
observed_float = 142.0;
observed_bool = true;

metric1 = 142U;

metrics.value(0, value_uint);
metrics.value(1, value_unobs);
metrics.value(2, value_int);
metrics.value(3, value_float);
metrics.value(4, value_bool);

EXPECT_EQ(value_uint, observed_uint);
EXPECT_EQ(value_unobs, 142U);
EXPECT_EQ(value_int, observed_int);
EXPECT_EQ(value_float, observed_float);
EXPECT_EQ(value_bool, observed_bool);
}

0 comments on commit 4ca75b9

Please sign in to comment.