Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions builds/gnu/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ src_libbitcoin_node_la_SOURCES = \
${srcdir}/../../src/sessions/session_manual.cpp \
${srcdir}/../../src/sessions/session_outbound.cpp \
${srcdir}/../../src/types/meters.cpp \
${srcdir}/../../src/types/metric.cpp \
${srcdir}/../../src/types/rolling_bloom_filter.cpp \
${srcdir}/../../src/types/solo_spans.cpp

Expand Down Expand Up @@ -227,6 +228,7 @@ include_bitcoin_node_types_HEADERS = \
${srcdir}/../../include/bitcoin/node/types/constants.hpp \
${srcdir}/../../include/bitcoin/node/types/estimate_mode.hpp \
${srcdir}/../../include/bitcoin/node/types/meters.hpp \
${srcdir}/../../include/bitcoin/node/types/metric.hpp \
${srcdir}/../../include/bitcoin/node/types/pool_event.hpp \
${srcdir}/../../include/bitcoin/node/types/pool_scan.hpp \
${srcdir}/../../include/bitcoin/node/types/relay_tier.hpp \
Expand Down Expand Up @@ -281,6 +283,7 @@ test_libbitcoin_node_test_SOURCES = \
${srcdir}/../../test/protocols/protocol_transaction.cpp \
${srcdir}/../../test/sessions/session.cpp \
${srcdir}/../../test/types/meters.cpp \
${srcdir}/../../test/types/metric.cpp \
${srcdir}/../../test/types/rolling_bloom_filter.cpp \
${srcdir}/../../test/types/solo_spans.cpp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<ClCompile Include="..\..\..\..\test\settings.cpp" />
<ClCompile Include="..\..\..\..\test\test.cpp" />
<ClCompile Include="..\..\..\..\test\types\meters.cpp" />
<ClCompile Include="..\..\..\..\test\types\metric.cpp" />
<ClCompile Include="..\..\..\..\test\types\rolling_bloom_filter.cpp" />
<ClCompile Include="..\..\..\..\test\types\solo_spans.cpp" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
<ClCompile Include="..\..\..\..\test\types\meters.cpp">
<Filter>src\types</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\types\metric.cpp">
<Filter>src\types</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\types\rolling_bloom_filter.cpp">
<Filter>src\types</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
<ClCompile Include="..\..\..\..\src\sessions\session_outbound.cpp" />
<ClCompile Include="..\..\..\..\src\settings.cpp" />
<ClCompile Include="..\..\..\..\src\types\meters.cpp" />
<ClCompile Include="..\..\..\..\src\types\metric.cpp" />
<ClCompile Include="..\..\..\..\src\types\rolling_bloom_filter.cpp" />
<ClCompile Include="..\..\..\..\src\types\solo_spans.cpp" />
</ItemGroup>
Expand Down Expand Up @@ -231,6 +232,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\constants.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\estimate_mode.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\meters.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\metric.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\pool_event.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\pool_scan.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\relay_tier.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@
<ClCompile Include="..\..\..\..\src\types\meters.cpp">
<Filter>src\types</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\types\metric.cpp">
<Filter>src\types</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\types\rolling_bloom_filter.cpp">
<Filter>src\types</Filter>
</ClCompile>
Expand Down Expand Up @@ -407,6 +410,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\meters.hpp">
<Filter>include\bitcoin\node\types</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\metric.hpp">
<Filter>include\bitcoin\node\types</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\types\pool_event.hpp">
<Filter>include\bitcoin\node\types</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <bitcoin/node/types/constants.hpp>
#include <bitcoin/node/types/estimate_mode.hpp>
#include <bitcoin/node/types/meters.hpp>
#include <bitcoin/node/types/metric.hpp>
#include <bitcoin/node/types/pool_event.hpp>
#include <bitcoin/node/types/pool_scan.hpp>
#include <bitcoin/node/types/relay_tier.hpp>
Expand Down
18 changes: 12 additions & 6 deletions include/bitcoin/node/types/meters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <atomic>
#include <bitcoin/node/events.hpp>
#include <bitcoin/node/types/metric.hpp>

namespace libbitcoin {
namespace node {
Expand All @@ -39,6 +40,9 @@ class BCN_API meters
/// Accumulate an event of the value.
void accumulate(uint8_t event, uint64_t value) NOEXCEPT;

/// Append the measures as metric families.
void collect(metric_families& out) const NOEXCEPT;

/// The measures in text exposition format.
std::string render() const NOEXCEPT;

Expand All @@ -48,12 +52,14 @@ class BCN_API meters
using counter = std::atomic<uint64_t>;
using histogram = std::array<counter, buckets>;

void render_height(std::string& out, size_t event) const NOEXCEPT;
void render_count(std::string& out, size_t event) const NOEXCEPT;
void render_gauge(std::string& out, size_t event) const NOEXCEPT;
void render_reasons(std::string& out, size_t event) const NOEXCEPT;
void render_directions(std::string& out, size_t event) const NOEXCEPT;
void render_histogram(std::string& out, size_t event) const NOEXCEPT;
void collect_height(metric_families& out, size_t event) const NOEXCEPT;
void collect_count(metric_families& out, size_t event) const NOEXCEPT;
void collect_gauge(metric_families& out, size_t event) const NOEXCEPT;
void collect_reasons(metric_families& out, size_t event) const NOEXCEPT;
void collect_directions(metric_families& out,
size_t event) const NOEXCEPT;
void collect_histogram(metric_families& out,
size_t event) const NOEXCEPT;

// These are thread safe.
std::atomic_bool enabled_{};
Expand Down
93 changes: 93 additions & 0 deletions include/bitcoin/node/types/metric.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_TYPES_METRIC_HPP
#define LIBBITCOIN_NODE_TYPES_METRIC_HPP

#include <type_traits>
#include <utility>
#include <variant>
#include <bitcoin/node/error.hpp>

namespace libbitcoin {
namespace node {

/// A metric family, in the terms of the text exposition format.
struct BCN_API metric
{
enum class type
{
counter,
gauge,
summary,
histogram
};

using label = std::pair<std::string, std::string>;
using labels = std::vector<label>;
using number = std::variant<uint64_t, int64_t, double>;

/// A sample, named by the family name and its suffix.
struct sample
{
std::string suffix{};
labels tags{};
number value{};
};

/// The number of an unsigned, signed or floating point value.
template <typename Value>
static constexpr number to_number(Value value) NOEXCEPT
{
if constexpr (std::is_floating_point_v<Value>)
return double{ value };
else if constexpr (std::is_signed_v<Value>)
return int64_t{ value };
else
return uint64_t{ value };
}

/// Append a sample of the value.
template <typename Value>
void add(Value value, labels tags={}, std::string suffix={}) NOEXCEPT
{
samples.push_back({ std::move(suffix), std::move(tags),
to_number(value) });
}

std::string name{};
type kind{};
std::vector<sample> samples{};
};

using metric_families = std::vector<metric>;

/// Append a family of no samples, returning it.
BCN_API metric& add_metric(metric_families& out, std::string name,
metric::type kind) NOEXCEPT;

/// The name of the family type in the text exposition format.
BCN_API std::string_view to_string(metric::type kind) NOEXCEPT;

/// The families in the text exposition format (0.0.4).
BCN_API std::string to_prometheus(const metric_families& families) NOEXCEPT;

} // namespace node
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/node/types/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <bitcoin/node/types/constants.hpp>
#include <bitcoin/node/types/estimate_mode.hpp>
#include <bitcoin/node/types/meters.hpp>
#include <bitcoin/node/types/metric.hpp>
#include <bitcoin/node/types/pool_event.hpp>
#include <bitcoin/node/types/pool_scan.hpp>
#include <bitcoin/node/types/relay_tier.hpp>
Expand Down
Loading
Loading