Remove zeek::Span and use std::span instead

This commit is contained in:
Dominik Charousset 2025-07-24 20:25:51 +02:00
parent 941ea4282b
commit 690a2a1122
33 changed files with 125 additions and 238 deletions

View file

@ -2,138 +2,11 @@
#pragma once #pragma once
#include <cstddef> #include <span>
#include <iterator>
#include <type_traits>
namespace zeek { namespace zeek {
/**
* Drop-in replacement for C++20's @c std::span with dynamic extent only:
* https://en.cppreference.com/w/cpp/container/span. After upgrading to C++20,
* this class may get replaced with a type alias instead and/or deprecated.
*/
template<class T> template<class T>
class Span { using Span [[deprecated("Remove in v8.1: Use std::span instead")]] = std::span<T>;
public:
// -- member types ---------------------------------------------------------
using element_type = T;
using value_type = std::remove_cv_t<T>;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
// -- constructors, destructors, and assignment operators ------------------
constexpr Span() noexcept : memory_block(nullptr), num_elements(0) {}
constexpr Span(pointer ptr, size_t size) : memory_block(ptr), num_elements(size) {}
constexpr Span(pointer first, pointer last)
: memory_block(first), num_elements(static_cast<size_t>(last - first)) {}
template<size_t Size>
constexpr Span(element_type (&arr)[Size]) noexcept : memory_block(arr), num_elements(Size) {}
template<class Container, class Data = typename Container::value_type>
requires std::is_convertible_v<Data*, T*>
Span(Container& xs) noexcept : memory_block(xs.data()), num_elements(xs.size()) {}
template<class Container, class Data = typename Container::value_type>
requires std::is_convertible_v<const Data*, T*>
Span(const Container& xs) noexcept : memory_block(xs.data()), num_elements(xs.size()) {}
constexpr Span(const Span&) noexcept = default;
Span& operator=(const Span&) noexcept = default;
// -- iterators ------------------------------------------------------------
constexpr iterator begin() const noexcept { return memory_block; }
constexpr const_iterator cbegin() const noexcept { return memory_block; }
constexpr iterator end() const noexcept { return begin() + num_elements; }
constexpr const_iterator cend() const noexcept { return cbegin() + num_elements; }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{end()}; }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator{begin()}; }
// -- element access -------------------------------------------------------
constexpr reference operator[](size_t index) const noexcept { return memory_block[index]; }
constexpr reference front() const noexcept { return *memory_block; }
constexpr reference back() const noexcept { return (*this)[num_elements - 1]; }
// -- properties -----------------------------------------------------------
constexpr size_t size() const noexcept { return num_elements; }
constexpr size_t size_bytes() const noexcept { return num_elements * sizeof(element_type); }
constexpr bool empty() const noexcept { return num_elements == 0; }
constexpr pointer data() const noexcept { return memory_block; }
// -- subviews -------------------------------------------------------------
constexpr Span subspan(size_t offset, size_t count) const { return {memory_block + offset, count}; }
constexpr Span subspan(size_t offset) const { return {memory_block + offset, num_elements - offset}; }
constexpr Span first(size_t count) const { return {memory_block, count}; }
constexpr Span last(size_t count) const { return subspan(num_elements - count, num_elements); }
private:
// -- member variables -----------------------------------------------------
/// Points to the first element in the contiguous memory block.
pointer memory_block;
/// Stores the number of elements in the contiguous memory block.
size_t num_elements;
};
// -- deduction guides ---------------------------------------------------------
template<class T>
Span(T*, size_t) -> Span<T>;
template<class T, size_t N>
Span(T (&)[N]) -> Span<T>;
template<class Container>
Span(Container&) -> Span<typename Container::value_type>;
template<class Container>
Span(const Container&) -> Span<const typename Container::value_type>;
} // namespace zeek } // namespace zeek

View file

@ -11,12 +11,12 @@
#include <broker/store.hh> #include <broker/store.hh>
#include <broker/zeek.hh> #include <broker/zeek.hh>
#include <memory> #include <memory>
#include <span>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/Span.h"
#include "zeek/broker/Data.h" #include "zeek/broker/Data.h"
#include "zeek/cluster/Backend.h" #include "zeek/cluster/Backend.h"
#include "zeek/iosource/IOSource.h" #include "zeek/iosource/IOSource.h"
@ -284,7 +284,7 @@ public:
[[deprecated("Remove in v8.1: Use the ArgsSpan version instead")]] RecordVal* MakeEvent(ValPList* args, [[deprecated("Remove in v8.1: Use the ArgsSpan version instead")]] RecordVal* MakeEvent(ValPList* args,
zeek::detail::Frame* frame); zeek::detail::Frame* frame);
using ArgsSpan = Span<const ValPtr>; using ArgsSpan = std::span<const ValPtr>;
/** /**
* Create an `Event` record value from an event and its arguments. * Create an `Event` record value from an event and its arguments.
@ -433,7 +433,7 @@ private:
// //
// TODO: Move log buffering out of broker and implement. // TODO: Move log buffering out of broker and implement.
bool DoPublishLogWrites(const logging::detail::LogWriteHeader& header, bool DoPublishLogWrites(const logging::detail::LogWriteHeader& header,
zeek::Span<logging::detail::LogRecord> records) override { std::span<logging::detail::LogRecord> records) override {
// Not implemented by broker. // Not implemented by broker.
throw std::logic_error("not implemented"); throw std::logic_error("not implemented");
} }

View file

@ -3,15 +3,15 @@
%%{ %%{
#include <set> #include <set>
#include <span>
#include <string> #include <string>
#include "zeek/Span.h"
#include "zeek/broker/Manager.h" #include "zeek/broker/Manager.h"
#include "zeek/logging/Manager.h" #include "zeek/logging/Manager.h"
namespace { namespace {
using ArgsSpan = zeek::Span<const zeek::ValPtr>; using ArgsSpan = std::span<const zeek::ValPtr>;
} }

View file

@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <span>
#include "zeek/Desc.h" #include "zeek/Desc.h"
#include "zeek/Event.h" #include "zeek/Event.h"
@ -196,7 +197,7 @@ bool Backend::DoPublishEvent(const std::string& topic, cluster::detail::Event& e
// Default implementation doing log record serialization. // Default implementation doing log record serialization.
bool Backend::DoPublishLogWrites(const zeek::logging::detail::LogWriteHeader& header, bool Backend::DoPublishLogWrites(const zeek::logging::detail::LogWriteHeader& header,
zeek::Span<zeek::logging::detail::LogRecord> records) { std::span<zeek::logging::detail::LogRecord> records) {
byte_buffer buf; byte_buffer buf;
if ( ! log_serializer->SerializeLogWrite(buf, header, records) ) if ( ! log_serializer->SerializeLogWrite(buf, header, records) )

View file

@ -6,12 +6,12 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <span>
#include <string_view> #include <string_view>
#include <variant> #include <variant>
#include "zeek/Event.h" #include "zeek/Event.h"
#include "zeek/EventHandler.h" #include "zeek/EventHandler.h"
#include "zeek/Span.h"
#include "zeek/Tag.h" #include "zeek/Tag.h"
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/ZeekArgs.h" #include "zeek/ZeekArgs.h"
@ -323,7 +323,7 @@ public:
* @param records A span of logging::detail::LogRecords to be published. * @param records A span of logging::detail::LogRecords to be published.
*/ */
bool PublishLogWrites(const zeek::logging::detail::LogWriteHeader& header, bool PublishLogWrites(const zeek::logging::detail::LogWriteHeader& header,
zeek::Span<zeek::logging::detail::LogRecord> records) { std::span<zeek::logging::detail::LogRecord> records) {
return DoPublishLogWrites(header, records); return DoPublishLogWrites(header, records);
} }
@ -531,7 +531,7 @@ private:
* @return true if the message has been published successfully. * @return true if the message has been published successfully.
*/ */
virtual bool DoPublishLogWrites(const zeek::logging::detail::LogWriteHeader& header, virtual bool DoPublishLogWrites(const zeek::logging::detail::LogWriteHeader& header,
zeek::Span<zeek::logging::detail::LogRecord> records); std::span<zeek::logging::detail::LogRecord> records);
/** /**
* Send out a serialized log batch. * Send out a serialized log batch.
@ -589,7 +589,7 @@ struct EventMessage {
std::string format; std::string format;
byte_buffer payload; byte_buffer payload;
auto payload_span() const { return Span(payload.data(), payload.size()); }; auto payload_span() const { return std::span(payload.data(), payload.size()); };
}; };
/** /**
@ -599,7 +599,7 @@ struct LogMessage {
std::string format; std::string format;
byte_buffer payload; byte_buffer payload;
auto payload_span() const { return Span(payload.data(), payload.size()); }; auto payload_span() const { return std::span(payload.data(), payload.size()); };
}; };
/** /**
@ -612,7 +612,7 @@ struct BackendMessage {
int tag; int tag;
byte_buffer payload; byte_buffer payload;
auto payload_span() const { return Span(payload.data(), payload.size()); }; auto payload_span() const { return std::span(payload.data(), payload.size()); };
}; };
using QueueMessage = std::variant<EventMessage, LogMessage, BackendMessage>; using QueueMessage = std::variant<EventMessage, LogMessage, BackendMessage>;

View file

@ -2,6 +2,8 @@
#include "zeek/cluster/BifSupport.h" #include "zeek/cluster/BifSupport.h"
#include <span>
#include "zeek/Desc.h" #include "zeek/Desc.h"
#include "zeek/Event.h" #include "zeek/Event.h"
#include "zeek/EventRegistry.h" #include "zeek/EventRegistry.h"
@ -30,7 +32,7 @@ std::optional<zeek::cluster::detail::Event> to_cluster_event(const zeek::cluster
for ( size_t i = 0; i < vargs->Size(); i++ ) for ( size_t i = 0; i < vargs->Size(); i++ )
args[i] = vargs->ValAt(i); args[i] = vargs->ValAt(i);
return backend->MakeClusterEvent(func, zeek::Span{args}); return backend->MakeClusterEvent(func, std::span{args});
} }
} // namespace } // namespace
@ -155,7 +157,7 @@ zeek::RecordValPtr make_endpoint_info(const std::string& id, const std::string&
return ep_rec; return ep_rec;
} }
zeek::VectorValPtr make_string_vec(zeek::Span<const std::string> strings) { zeek::VectorValPtr make_string_vec(std::span<const std::string> strings) {
static const auto string_vec_type = zeek::id::find_type<zeek::VectorType>("string_vec"); static const auto string_vec_type = zeek::id::find_type<zeek::VectorType>("string_vec");
auto vec = zeek::make_intrusive<zeek::VectorVal>(string_vec_type); auto vec = zeek::make_intrusive<zeek::VectorVal>(string_vec_type);
vec->Reserve(strings.size()); vec->Reserve(strings.size());

View file

@ -3,10 +3,10 @@
#pragma once #pragma once
#include <optional> #include <optional>
#include <span>
#include <string> #include <string>
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/Span.h"
#include "zeek/net_util.h" #include "zeek/net_util.h"
// Helpers for cluster.bif // Helpers for cluster.bif
@ -24,7 +24,7 @@ using VectorValPtr = IntrusivePtr<VectorVal>;
class Val; class Val;
using ValPtr = IntrusivePtr<Val>; using ValPtr = IntrusivePtr<Val>;
using ArgsSpan = Span<const ValPtr>; using ArgsSpan = std::span<const ValPtr>;
namespace cluster::detail::bif { namespace cluster::detail::bif {
@ -72,7 +72,7 @@ zeek::RecordValPtr make_endpoint_info(const std::string& id, const std::string&
* *
* @return a VectorVal instance of type string_vec filled with strings. * @return a VectorVal instance of type string_vec filled with strings.
*/ */
zeek::VectorValPtr make_string_vec(zeek::Span<const std::string> strings); zeek::VectorValPtr make_string_vec(std::span<const std::string> strings);
} // namespace cluster::detail::bif } // namespace cluster::detail::bif

View file

@ -5,9 +5,9 @@
#pragma once #pragma once
#include <optional> #include <optional>
#include <span>
#include <string> #include <string>
#include "zeek/Span.h"
#include "zeek/logging/Types.h" #include "zeek/logging/Types.h"
namespace zeek::cluster { namespace zeek::cluster {
@ -80,7 +80,7 @@ public:
* @param records The actual log writes. * @param records The actual log writes.
*/ */
virtual bool SerializeLogWrite(byte_buffer& buf, const logging::detail::LogWriteHeader& header, virtual bool SerializeLogWrite(byte_buffer& buf, const logging::detail::LogWriteHeader& header,
zeek::Span<logging::detail::LogRecord> records) = 0; std::span<logging::detail::LogRecord> records) = 0;
/** /**
* Unserialize log writes from a given byte buffer. * Unserialize log writes from a given byte buffer.

View file

@ -3,6 +3,7 @@
#include "zeek/cluster/Telemetry.h" #include "zeek/cluster/Telemetry.h"
#include <cinttypes> #include <cinttypes>
#include <span>
#include "zeek/Desc.h" #include "zeek/Desc.h"
#include "zeek/Expr.h" #include "zeek/Expr.h"
@ -187,7 +188,7 @@ DebugTelemetry::DebugTelemetry(TopicNormalizer topic_normalizer, std::string_vie
labels.emplace_back("script_location", ""); labels.emplace_back("script_location", "");
labels_view = to_label_view_vec(labels); labels_view = to_label_view_vec(labels);
labels_view_no_location = zeek::Span{labels_view.data(), labels_view.size() - 1}; labels_view_no_location = std::span{labels_view.data(), labels_view.size() - 1};
auto label_names = to_label_names_vec(labels); auto label_names = to_label_names_vec(labels);

View file

@ -6,12 +6,12 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <span>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/Span.h"
namespace zeek { namespace zeek {
@ -175,7 +175,7 @@ private:
std::vector<double> size_bounds; std::vector<double> size_bounds;
LabelList labels; LabelList labels;
LabelViewList labels_view; LabelViewList labels_view;
zeek::Span<telemetry::LabelView> labels_view_no_location; std::span<telemetry::LabelView> labels_view_no_location;
size_t topic_idx, handler_idx, size_t topic_idx, handler_idx,
script_location_idx; // Index of topic, handler and script_location labels in labels_view script_location_idx; // Index of topic, handler and script_location labels in labels_view
telemetry::HistogramFamilyPtr in, out; telemetry::HistogramFamilyPtr in, out;

View file

@ -3,6 +3,7 @@
#include "zeek/cluster/serializer/binary-serialization-format/Serializer.h" #include "zeek/cluster/serializer/binary-serialization-format/Serializer.h"
#include <cstdint> #include <cstdint>
#include <span>
#include "zeek/DebugLogger.h" #include "zeek/DebugLogger.h"
#include "zeek/ID.h" #include "zeek/ID.h"
@ -27,7 +28,7 @@ extern Plugin plugin;
bool detail::BinarySerializationFormatLogSerializer::SerializeLogWrite(byte_buffer& buf, bool detail::BinarySerializationFormatLogSerializer::SerializeLogWrite(byte_buffer& buf,
const logging::detail::LogWriteHeader& header, const logging::detail::LogWriteHeader& header,
zeek::Span<logging::detail::LogRecord> records) { std::span<logging::detail::LogRecord> records) {
zeek::detail::BinarySerializationFormat fmt; zeek::detail::BinarySerializationFormat fmt;
SERIALIZER_DEBUG("Serializing stream=%s writer=%s filter=%s path=%s num_fields=%zu num_records=%zu", SERIALIZER_DEBUG("Serializing stream=%s writer=%s filter=%s path=%s num_fields=%zu num_records=%zu",

View file

@ -3,6 +3,7 @@
#pragma once #pragma once
#include <optional> #include <optional>
#include <span>
#include "zeek/cluster/Serializer.h" #include "zeek/cluster/Serializer.h"
#include "zeek/logging/Types.h" #include "zeek/logging/Types.h"
@ -14,7 +15,7 @@ public:
BinarySerializationFormatLogSerializer() : LogSerializer("zeek-bin-serializer") {} BinarySerializationFormatLogSerializer() : LogSerializer("zeek-bin-serializer") {}
bool SerializeLogWrite(byte_buffer& buf, const logging::detail::LogWriteHeader& header, bool SerializeLogWrite(byte_buffer& buf, const logging::detail::LogWriteHeader& header,
zeek::Span<logging::detail::LogRecord> records) override; std::span<logging::detail::LogRecord> records) override;
std::optional<logging::detail::LogWriteBatch> UnserializeLogWrite(byte_buffer_span buf) override; std::optional<logging::detail::LogWriteBatch> UnserializeLogWrite(byte_buffer_span buf) override;
}; };

View file

@ -3,6 +3,7 @@
#include "zeek/logging/WriterBackend.h" #include "zeek/logging/WriterBackend.h"
#include <broker/data.hh> #include <broker/data.hh>
#include <span>
#include "zeek/logging/Manager.h" #include "zeek/logging/Manager.h"
#include "zeek/logging/WriterFrontend.h" #include "zeek/logging/WriterFrontend.h"
@ -170,7 +171,7 @@ bool WriterBackend::Init(int arg_num_fields, const Field* const* arg_fields) {
return true; return true;
} }
bool WriterBackend::Write(int arg_num_fields, zeek::Span<detail::LogRecord> records) { bool WriterBackend::Write(int arg_num_fields, std::span<detail::LogRecord> records) {
// Double-check that the arguments match. If we get this from remote, // Double-check that the arguments match. If we get this from remote,
// something might be mixed up. // something might be mixed up.
if ( num_fields != arg_num_fields ) { if ( num_fields != arg_num_fields ) {

View file

@ -4,7 +4,8 @@
#pragma once #pragma once
#include "zeek/Span.h" #include <span>
#include "zeek/logging/Component.h" #include "zeek/logging/Component.h"
#include "zeek/logging/Types.h" #include "zeek/logging/Types.h"
#include "zeek/threading/MsgThread.h" #include "zeek/threading/MsgThread.h"
@ -154,7 +155,7 @@ public:
* *
* @return False if an error occurred. * @return False if an error occurred.
*/ */
bool Write(int arg_num_fields, zeek::Span<detail::LogRecord> records); bool Write(int arg_num_fields, std::span<detail::LogRecord> records);
/** /**
* Sets the buffering status for the writer, assuming the writer * Sets the buffering status for the writer, assuming the writer

View file

@ -2,8 +2,9 @@
#include "zeek/logging/WriterFrontend.h" #include "zeek/logging/WriterFrontend.h"
#include <span>
#include "zeek/RunState.h" #include "zeek/RunState.h"
#include "zeek/Span.h"
#include "zeek/broker/Manager.h" #include "zeek/broker/Manager.h"
#include "zeek/cluster/Backend.h" #include "zeek/cluster/Backend.h"
#include "zeek/logging/Manager.h" #include "zeek/logging/Manager.h"
@ -59,7 +60,7 @@ public:
num_fields(num_fields), num_fields(num_fields),
records(std::move(records)) {} records(std::move(records)) {}
bool Process() override { return Object()->Write(num_fields, zeek::Span{records}); } bool Process() override { return Object()->Write(num_fields, std::span{records}); }
private: private:
int num_fields; int num_fields;
@ -234,7 +235,7 @@ void WriterFrontend::FlushWriteBuffer() {
// is used, push all the buffered log records to it now. // is used, push all the buffered log records to it now.
const bool broker_is_cluster_backend = zeek::cluster::backend == zeek::broker_mgr; const bool broker_is_cluster_backend = zeek::cluster::backend == zeek::broker_mgr;
if ( remote && ! broker_is_cluster_backend ) if ( remote && ! broker_is_cluster_backend )
zeek::cluster::backend->PublishLogWrites(header, Span{records}); zeek::cluster::backend->PublishLogWrites(header, std::span{records});
if ( backend ) if ( backend )
backend->SendIn(new WriteMessage(backend, num_fields, std::move(records))); backend->SendIn(new WriteMessage(backend, num_fields, std::move(records)));

View file

@ -2,6 +2,8 @@
#include "zeek/packet_analysis/Manager.h" #include "zeek/packet_analysis/Manager.h"
#include <span>
#include "zeek/Event.h" #include "zeek/Event.h"
#include "zeek/RunState.h" #include "zeek/RunState.h"
#include "zeek/Stats.h" #include "zeek/Stats.h"
@ -251,8 +253,8 @@ void Manager::ReportUnknownProtocol(const std::string& analyzer, uint32_t protoc
} }
} }
std::vector<zeek::Span<const uint8_t>> Manager::GetAnalyzerData(const AnalyzerPtr& analyzer) { std::vector<std::span<const uint8_t>> Manager::GetAnalyzerData(const AnalyzerPtr& analyzer) {
std::vector<zeek::Span<const uint8_t>> result; std::vector<std::span<const uint8_t>> result;
for ( const auto [sa, span] : analyzer_stack ) { for ( const auto [sa, span] : analyzer_stack ) {
if ( sa == analyzer.get() ) if ( sa == analyzer.get() )
result.push_back(span); result.push_back(span);

View file

@ -2,6 +2,8 @@
#pragma once #pragma once
#include <span>
#include "zeek/PacketFilter.h" #include "zeek/PacketFilter.h"
#include "zeek/Tag.h" #include "zeek/Tag.h"
#include "zeek/iosource/Packet.h" #include "zeek/iosource/Packet.h"
@ -195,7 +197,7 @@ public:
* *
* @returns An array of data spans. * @returns An array of data spans.
*/ */
std::vector<zeek::Span<const uint8_t>> GetAnalyzerData(const AnalyzerPtr& analyzer); std::vector<std::span<const uint8_t>> GetAnalyzerData(const AnalyzerPtr& analyzer);
private: private:
/** /**
@ -248,7 +250,7 @@ private:
struct StackEntry { struct StackEntry {
const Analyzer* analyzer; const Analyzer* analyzer;
zeek::Span<const uint8_t> data; // Start of this layer, limited by span's size. std::span<const uint8_t> data; // Start of this layer, limited by span's size.
}; };
std::vector<StackEntry> analyzer_stack; std::vector<StackEntry> analyzer_stack;

View file

@ -2,13 +2,14 @@
#include "zeek/packet_analysis/protocol/geneve/Geneve.h" #include "zeek/packet_analysis/protocol/geneve/Geneve.h"
#include "zeek/Span.h" #include <span>
#include "zeek/packet_analysis/protocol/geneve/events.bif.h" #include "zeek/packet_analysis/protocol/geneve/events.bif.h"
#include "zeek/packet_analysis/protocol/iptunnel/IPTunnel.h" #include "zeek/packet_analysis/protocol/iptunnel/IPTunnel.h"
using namespace zeek::packet_analysis::Geneve; using namespace zeek::packet_analysis::Geneve;
void zeek::packet_analysis::Geneve::detail::parse_options(zeek::Span<const uint8_t> data, detail::Callback cb) { void zeek::packet_analysis::Geneve::detail::parse_options(std::span<const uint8_t> data, detail::Callback cb) {
size_t remaining = data.size(); size_t remaining = data.size();
if ( remaining < 8 ) if ( remaining < 8 )
@ -40,7 +41,7 @@ void zeek::packet_analysis::Geneve::detail::parse_options(zeek::Span<const uint8
if ( remaining < opt_len ) if ( remaining < opt_len )
break; break;
cb(opt_class, opt_critical, opt_type, zeek::Span{p, opt_len}); cb(opt_class, opt_critical, opt_type, std::span{p, opt_len});
p += opt_len; p += opt_len;
} }

View file

@ -4,8 +4,8 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <span>
#include "zeek/Span.h"
#include "zeek/iosource/Packet.h" #include "zeek/iosource/Packet.h"
#include "zeek/packet_analysis/Analyzer.h" #include "zeek/packet_analysis/Analyzer.h"
@ -17,7 +17,7 @@ namespace detail {
* Callback for parse_options(), passing the individual option pieces. * Callback for parse_options(), passing the individual option pieces.
*/ */
using Callback = using Callback =
std::function<void(uint16_t opt_class, bool opt_critical, uint8_t opt_type, zeek::Span<const uint8_t> opt_data)>; std::function<void(uint16_t opt_class, bool opt_critical, uint8_t opt_type, std::span<const uint8_t> opt_data)>;
/** /**
* Parse Geneve options from the header data. * Parse Geneve options from the header data.
@ -27,7 +27,7 @@ using Callback =
* @param data The data span to treat as a Geneve header. * @param data The data span to treat as a Geneve header.
* @param cb The callback to invoke with each parsed option. * @param cb The callback to invoke with each parsed option.
*/ */
void parse_options(zeek::Span<const uint8_t> data, Callback cb); void parse_options(std::span<const uint8_t> data, Callback cb);
} // namespace detail } // namespace detail

View file

@ -1,6 +1,7 @@
module PacketAnalyzer::Geneve; module PacketAnalyzer::Geneve;
%%{ %%{
#include <span>
#include "zeek/packet_analysis/Manager.h" #include "zeek/packet_analysis/Manager.h"
#include "zeek/packet_analysis/protocol/geneve/Geneve.h" #include "zeek/packet_analysis/protocol/geneve/Geneve.h"
%%} %%}
@ -24,7 +25,7 @@ function get_options%(%): geneve_options_vec_vec
for ( const auto& span : spans ) { for ( const auto& span : spans ) {
auto v = zeek::make_intrusive<zeek::VectorVal>(vtype); auto v = zeek::make_intrusive<zeek::VectorVal>(vtype);
auto cb = [&v](uint16_t opt_class, bool opt_critical, uint8_t opt_type, zeek::Span<const uint8_t> opt_data) -> void { auto cb = [&v](uint16_t opt_class, bool opt_critical, uint8_t opt_type, std::span<const uint8_t> opt_data) -> void {
auto rv = zeek::make_intrusive<zeek::RecordVal>(rtype); auto rv = zeek::make_intrusive<zeek::RecordVal>(rtype);
rv->Assign(0, zeek::val_mgr->Count(opt_class)); rv->Assign(0, zeek::val_mgr->Count(opt_class));
rv->Assign(1, zeek::val_mgr->Bool(opt_critical)); rv->Assign(1, zeek::val_mgr->Bool(opt_critical));

View file

@ -3,6 +3,7 @@
#include "zeek/telemetry/Counter.h" #include "zeek/telemetry/Counter.h"
#include <algorithm> #include <algorithm>
#include <span>
using namespace zeek::telemetry; using namespace zeek::telemetry;
@ -16,7 +17,8 @@ double Counter::Value() const noexcept {
return handle.Value(); return handle.Value();
} }
std::shared_ptr<Counter> CounterFamily::GetOrAdd(Span<const LabelView> labels, detail::CollectCallbackPtr callback) { std::shared_ptr<Counter> CounterFamily::GetOrAdd(std::span<const LabelView> labels,
detail::CollectCallbackPtr callback) {
prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels); prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels);
auto check = [&](const std::shared_ptr<Counter>& counter) { return counter->CompareLabels(p_labels); }; auto check = [&](const std::shared_ptr<Counter>& counter) { return counter->CompareLabels(p_labels); };
@ -31,7 +33,7 @@ std::shared_ptr<Counter> CounterFamily::GetOrAdd(Span<const LabelView> labels, d
std::shared_ptr<Counter> CounterFamily::GetOrAdd(std::initializer_list<LabelView> labels, std::shared_ptr<Counter> CounterFamily::GetOrAdd(std::initializer_list<LabelView> labels,
detail::CollectCallbackPtr callback) { detail::CollectCallbackPtr callback) {
return GetOrAdd(Span{labels.begin(), labels.size()}, std::move(callback)); return GetOrAdd(std::span{labels.begin(), labels.size()}, std::move(callback));
} }
void CounterFamily::RunCallbacks() { void CounterFamily::RunCallbacks() {

View file

@ -6,9 +6,9 @@
#include <prometheus/family.h> #include <prometheus/family.h>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
#include <span>
#include "zeek/NetVar.h" // For BifEnum::Telemetry value #include "zeek/NetVar.h" // For BifEnum::Telemetry value
#include "zeek/Span.h"
#include "zeek/telemetry/MetricFamily.h" #include "zeek/telemetry/MetricFamily.h"
#include "zeek/telemetry/Utils.h" #include "zeek/telemetry/Utils.h"
@ -84,14 +84,14 @@ class CounterFamily : public MetricFamily {
public: public:
static inline const char* OpaqueName = "CounterMetricFamilyVal"; static inline const char* OpaqueName = "CounterMetricFamilyVal";
CounterFamily(prometheus::Family<prometheus::Counter>* family, Span<const std::string_view> labels) CounterFamily(prometheus::Family<prometheus::Counter>* family, std::span<const std::string_view> labels)
: MetricFamily(labels), family(family) {} : MetricFamily(labels), family(family) {}
/** /**
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
CounterPtr GetOrAdd(Span<const LabelView> labels, detail::CollectCallbackPtr callback = nullptr); CounterPtr GetOrAdd(std::span<const LabelView> labels, detail::CollectCallbackPtr callback = nullptr);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd

View file

@ -3,6 +3,7 @@
#include "zeek/telemetry/Gauge.h" #include "zeek/telemetry/Gauge.h"
#include <algorithm> #include <algorithm>
#include <span>
using namespace zeek::telemetry; using namespace zeek::telemetry;
@ -17,7 +18,7 @@ double Gauge::Value() const noexcept {
Gauge::Gauge(FamilyType* family, const prometheus::Labels& labels, detail::CollectCallbackPtr callback) noexcept Gauge::Gauge(FamilyType* family, const prometheus::Labels& labels, detail::CollectCallbackPtr callback) noexcept
: family(family), handle(family->Add(labels)), labels(labels), callback(std::move(callback)) {} : family(family), handle(family->Add(labels)), labels(labels), callback(std::move(callback)) {}
std::shared_ptr<Gauge> GaugeFamily::GetOrAdd(Span<const LabelView> labels, detail::CollectCallbackPtr callback) { std::shared_ptr<Gauge> GaugeFamily::GetOrAdd(std::span<const LabelView> labels, detail::CollectCallbackPtr callback) {
prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels); prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels);
auto check = [&](const std::shared_ptr<Gauge>& gauge) { return gauge->CompareLabels(p_labels); }; auto check = [&](const std::shared_ptr<Gauge>& gauge) { return gauge->CompareLabels(p_labels); };
@ -32,7 +33,7 @@ std::shared_ptr<Gauge> GaugeFamily::GetOrAdd(Span<const LabelView> labels, detai
std::shared_ptr<Gauge> GaugeFamily::GetOrAdd(std::initializer_list<LabelView> labels, std::shared_ptr<Gauge> GaugeFamily::GetOrAdd(std::initializer_list<LabelView> labels,
detail::CollectCallbackPtr callback) { detail::CollectCallbackPtr callback) {
return GetOrAdd(Span{labels.begin(), labels.size()}, std::move(callback)); return GetOrAdd(std::span{labels.begin(), labels.size()}, std::move(callback));
} }
void GaugeFamily::RunCallbacks() { void GaugeFamily::RunCallbacks() {

View file

@ -7,9 +7,9 @@
#include <unistd.h> #include <unistd.h>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
#include <span>
#include "zeek/NetVar.h" // For BifEnum::Telemetry value #include "zeek/NetVar.h" // For BifEnum::Telemetry value
#include "zeek/Span.h"
#include "zeek/telemetry/MetricFamily.h" #include "zeek/telemetry/MetricFamily.h"
#include "zeek/telemetry/Utils.h" #include "zeek/telemetry/Utils.h"
@ -103,7 +103,7 @@ public:
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
GaugePtr GetOrAdd(Span<const LabelView> labels, detail::CollectCallbackPtr callback = nullptr); GaugePtr GetOrAdd(std::span<const LabelView> labels, detail::CollectCallbackPtr callback = nullptr);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd
@ -112,7 +112,7 @@ public:
zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::GAUGE; } zeek_int_t MetricType() const noexcept override { return BifEnum::Telemetry::MetricType::GAUGE; }
GaugeFamily(prometheus::Family<prometheus::Gauge>* family, Span<const std::string_view> labels) GaugeFamily(prometheus::Family<prometheus::Gauge>* family, std::span<const std::string_view> labels)
: MetricFamily(labels), family(family) {} : MetricFamily(labels), family(family) {}
void RunCallbacks() override; void RunCallbacks() override;

View file

@ -3,6 +3,7 @@
#include "zeek/telemetry/Histogram.h" #include "zeek/telemetry/Histogram.h"
#include <algorithm> #include <algorithm>
#include <span>
using namespace zeek::telemetry; using namespace zeek::telemetry;
@ -15,7 +16,7 @@ Histogram::Histogram(FamilyType* family, const prometheus::Labels& labels,
prometheus::Histogram::BucketBoundaries bounds) noexcept prometheus::Histogram::BucketBoundaries bounds) noexcept
: handle(family->Add(labels, std::move(bounds))), labels(labels) {} : handle(family->Add(labels, std::move(bounds))), labels(labels) {}
std::shared_ptr<Histogram> HistogramFamily::GetOrAdd(Span<const LabelView> labels) { std::shared_ptr<Histogram> HistogramFamily::GetOrAdd(std::span<const LabelView> labels) {
prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels); prometheus::Labels p_labels = detail::BuildPrometheusLabels(labels);
auto check = [&](const std::shared_ptr<Histogram>& histo) { return histo->CompareLabels(p_labels); }; auto check = [&](const std::shared_ptr<Histogram>& histo) { return histo->CompareLabels(p_labels); };
@ -32,11 +33,11 @@ std::shared_ptr<Histogram> HistogramFamily::GetOrAdd(Span<const LabelView> label
* @copydoc GetOrAdd * @copydoc GetOrAdd
*/ */
std::shared_ptr<Histogram> HistogramFamily::GetOrAdd(std::initializer_list<LabelView> labels) { std::shared_ptr<Histogram> HistogramFamily::GetOrAdd(std::initializer_list<LabelView> labels) {
return GetOrAdd(Span{labels.begin(), labels.size()}); return GetOrAdd(std::span{labels.begin(), labels.size()});
} }
HistogramFamily::HistogramFamily(prometheus::Family<prometheus::Histogram>* family, Span<const double> bounds, HistogramFamily::HistogramFamily(prometheus::Family<prometheus::Histogram>* family, std::span<const double> bounds,
Span<const std::string_view> labels) std::span<const std::string_view> labels)
: MetricFamily(labels), family(family) { : MetricFamily(labels), family(family) {
std::ranges::copy(bounds, std::back_inserter(boundaries)); std::ranges::copy(bounds, std::back_inserter(boundaries));
} }

View file

@ -6,9 +6,9 @@
#include <prometheus/histogram.h> #include <prometheus/histogram.h>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
#include <span>
#include "zeek/NetVar.h" // For BifEnum::Telemetry values #include "zeek/NetVar.h" // For BifEnum::Telemetry values
#include "zeek/Span.h"
#include "zeek/telemetry/MetricFamily.h" #include "zeek/telemetry/MetricFamily.h"
#include "zeek/telemetry/Utils.h" #include "zeek/telemetry/Utils.h"
@ -49,14 +49,14 @@ class HistogramFamily : public MetricFamily {
public: public:
static inline const char* OpaqueName = "HistogramMetricFamilyVal"; static inline const char* OpaqueName = "HistogramMetricFamilyVal";
HistogramFamily(prometheus::Family<prometheus::Histogram>* family, Span<const double> bounds, HistogramFamily(prometheus::Family<prometheus::Histogram>* family, std::span<const double> bounds,
Span<const std::string_view> labels); std::span<const std::string_view> labels);
/** /**
* Returns the metrics handle for given labels, creating a new instance * Returns the metrics handle for given labels, creating a new instance
* lazily if necessary. * lazily if necessary.
*/ */
HistogramPtr GetOrAdd(Span<const LabelView> labels); HistogramPtr GetOrAdd(std::span<const LabelView> labels);
/** /**
* @copydoc GetOrAdd * @copydoc GetOrAdd

View file

@ -14,6 +14,7 @@
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include <algorithm> #include <algorithm>
#include <span>
#include <thread> #include <thread>
#include "zeek/Func.h" #include "zeek/Func.h"
@ -502,7 +503,7 @@ void Manager::BuildClusterJson() {
} }
CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, std::string_view helptext, std::span<const std::string_view> labels, std::string_view helptext,
std::string_view unit) { std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, true); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, true);
@ -520,11 +521,11 @@ CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_vie
CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::string_view helptext, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view unit) { std::string_view unit) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = std::span{labels.begin(), labels.size()};
return CounterFamily(prefix, name, lbl_span, helptext, unit); return CounterFamily(prefix, name, lbl_span, helptext, unit);
} }
CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name, std::span<const LabelView> labels,
std::string_view helptext, std::string_view unit, std::string_view helptext, std::string_view unit,
detail::CollectCallbackPtr callback) { detail::CollectCallbackPtr callback) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
@ -536,12 +537,12 @@ CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view na
CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name, CounterPtr Manager::CounterInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::string_view helptext, std::initializer_list<LabelView> labels, std::string_view helptext,
std::string_view unit, detail::CollectCallbackPtr callback) { std::string_view unit, detail::CollectCallbackPtr callback) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = std::span{labels.begin(), labels.size()};
return CounterInstance(prefix, name, lbl_span, helptext, unit, std::move(callback)); return CounterInstance(prefix, name, lbl_span, helptext, unit, std::move(callback));
} }
std::shared_ptr<GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name, std::shared_ptr<GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, std::string_view helptext, std::span<const std::string_view> labels, std::string_view helptext,
std::string_view unit) { std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, false); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit, false);
@ -559,11 +560,11 @@ std::shared_ptr<GaugeFamily> Manager::GaugeFamily(std::string_view prefix, std::
GaugeFamilyPtr Manager::GaugeFamily(std::string_view prefix, std::string_view name, GaugeFamilyPtr Manager::GaugeFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, std::string_view helptext, std::initializer_list<std::string_view> labels, std::string_view helptext,
std::string_view unit) { std::string_view unit) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = std::span{labels.begin(), labels.size()};
return GaugeFamily(prefix, name, lbl_span, helptext, unit); return GaugeFamily(prefix, name, lbl_span, helptext, unit);
} }
GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, std::span<const LabelView> labels,
std::string_view helptext, std::string_view unit, detail::CollectCallbackPtr callback) { std::string_view helptext, std::string_view unit, detail::CollectCallbackPtr callback) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = GaugeFamily(prefix, name, labelNames, helptext, unit); auto family = GaugeFamily(prefix, name, labelNames, helptext, unit);
@ -573,12 +574,12 @@ GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name,
GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels, GaugePtr Manager::GaugeInstance(std::string_view prefix, std::string_view name, std::initializer_list<LabelView> labels,
std::string_view helptext, std::string_view unit, detail::CollectCallbackPtr callback) { std::string_view helptext, std::string_view unit, detail::CollectCallbackPtr callback) {
auto lbl_span = Span{labels.begin(), labels.size()}; auto lbl_span = std::span{labels.begin(), labels.size()};
return GaugeInstance(prefix, name, lbl_span, helptext, unit, std::move(callback)); return GaugeInstance(prefix, name, lbl_span, helptext, unit, std::move(callback));
} }
HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, ConstSpan<double> bounds, std::span<const std::string_view> labels, std::span<const double> bounds,
std::string_view helptext, std::string_view unit) { std::string_view helptext, std::string_view unit) {
auto full_name = detail::BuildFullPrometheusName(prefix, name, unit); auto full_name = detail::BuildFullPrometheusName(prefix, name, unit);
@ -594,14 +595,16 @@ HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string
} }
HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr Manager::HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, ConstSpan<double> bounds, std::initializer_list<std::string_view> labels,
std::string_view helptext, std::string_view unit) { std::span<const double> bounds, std::string_view helptext,
auto lbl_span = Span{labels.begin(), labels.size()}; std::string_view unit) {
auto lbl_span = std::span{labels.begin(), labels.size()};
return HistogramFamily(prefix, name, lbl_span, bounds, helptext, unit); return HistogramFamily(prefix, name, lbl_span, bounds, helptext, unit);
} }
HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name,
ConstSpan<double> bounds, std::string_view helptext, std::string_view unit) { std::span<const LabelView> labels, std::span<const double> bounds,
std::string_view helptext, std::string_view unit) {
return WithLabelNames(labels, [&, this](auto labelNames) { return WithLabelNames(labels, [&, this](auto labelNames) {
auto family = HistogramFamily(prefix, name, labelNames, bounds, helptext, unit); auto family = HistogramFamily(prefix, name, labelNames, bounds, helptext, unit);
return family->GetOrAdd(labels); return family->GetOrAdd(labels);
@ -611,8 +614,8 @@ HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_vie
HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr Manager::HistogramInstance(std::string_view prefix, std::string_view name,
std::initializer_list<LabelView> labels, std::initializer_list<double> bounds, std::initializer_list<LabelView> labels, std::initializer_list<double> bounds,
std::string_view helptext, std::string_view unit) { std::string_view helptext, std::string_view unit) {
auto lbls = Span{labels.begin(), labels.size()}; auto lbls = std::span{labels.begin(), labels.size()};
auto bounds_span = Span{bounds.begin(), bounds.size()}; auto bounds_span = std::span{bounds.begin(), bounds.size()};
return HistogramInstance(prefix, name, lbls, bounds_span, helptext, unit); return HistogramInstance(prefix, name, lbls, bounds_span, helptext, unit);
} }

View file

@ -6,12 +6,12 @@
#include <cstdint> #include <cstdint>
#include <initializer_list> #include <initializer_list>
#include <memory> #include <memory>
#include <span>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#include "zeek/Flare.h" #include "zeek/Flare.h"
#include "zeek/IntrusivePtr.h" #include "zeek/IntrusivePtr.h"
#include "zeek/Span.h"
#include "zeek/iosource/IOSource.h" #include "zeek/iosource/IOSource.h"
#include "zeek/telemetry/Counter.h" #include "zeek/telemetry/Counter.h"
#include "zeek/telemetry/Gauge.h" #include "zeek/telemetry/Gauge.h"
@ -81,8 +81,9 @@ public:
* @param helptext Short explanation of the metric. * @param helptext Short explanation of the metric.
* @param unit Unit of measurement. * @param unit Unit of measurement.
*/ */
CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels, CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name,
std::string_view helptext, std::string_view unit = ""); std::span<const std::string_view> labels, std::string_view helptext,
std::string_view unit = "");
/// @copydoc CounterFamily /// @copydoc CounterFamily
CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr CounterFamily(std::string_view prefix, std::string_view name,
@ -100,7 +101,7 @@ public:
* @param callback Passing a callback method will enable asynchronous mode. The callback method will be called * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called
* by the metrics subsystem whenever data is requested. * by the metrics subsystem whenever data is requested.
*/ */
CounterPtr CounterInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, CounterPtr CounterInstance(std::string_view prefix, std::string_view name, std::span<const LabelView> labels,
std::string_view helptext, std::string_view unit = "", std::string_view helptext, std::string_view unit = "",
detail::CollectCallbackPtr callback = nullptr); detail::CollectCallbackPtr callback = nullptr);
@ -117,7 +118,7 @@ public:
* @param helptext Short explanation of the metric. * @param helptext Short explanation of the metric.
* @param unit Unit of measurement. * @param unit Unit of measurement.
*/ */
GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name, Span<const std::string_view> labels, GaugeFamilyPtr GaugeFamily(std::string_view prefix, std::string_view name, std::span<const std::string_view> labels,
std::string_view helptext, std::string_view unit = ""); std::string_view helptext, std::string_view unit = "");
/// @copydoc GaugeFamily /// @copydoc GaugeFamily
@ -136,7 +137,7 @@ public:
* @param callback Passing a callback method will enable asynchronous mode. The callback method will be called * @param callback Passing a callback method will enable asynchronous mode. The callback method will be called
* by the metrics subsystem whenever data is requested. * by the metrics subsystem whenever data is requested.
*/ */
GaugePtr GaugeInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, GaugePtr GaugeInstance(std::string_view prefix, std::string_view name, std::span<const LabelView> labels,
std::string_view helptext, std::string_view unit = "", std::string_view helptext, std::string_view unit = "",
detail::CollectCallbackPtr callback = nullptr); detail::CollectCallbackPtr callback = nullptr);
@ -145,17 +146,6 @@ public:
std::string_view helptext, std::string_view unit = "", std::string_view helptext, std::string_view unit = "",
detail::CollectCallbackPtr callback = nullptr); detail::CollectCallbackPtr callback = nullptr);
// Forces the compiler to use the type `Span<const T>` instead of trying to
// match parameters to a `span`.
template<class T>
struct ConstSpanOracle {
using Type = Span<const T>;
};
// Convenience alias to safe some typing.
template<class T>
using ConstSpan = typename ConstSpanOracle<T>::Type;
/** /**
* Returns a histogram metric family. Creates the family lazily if * Returns a histogram metric family. Creates the family lazily if
* necessary. * necessary.
@ -175,12 +165,12 @@ public:
* @p bounds via run-time configuration. * @p bounds via run-time configuration.
*/ */
HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
Span<const std::string_view> labels, ConstSpan<double> bounds, std::span<const std::string_view> labels, std::span<const double> bounds,
std::string_view helptext, std::string_view unit = ""); std::string_view helptext, std::string_view unit = "");
/// @copydoc HistogramFamily /// @copydoc HistogramFamily
HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name, HistogramFamilyPtr HistogramFamily(std::string_view prefix, std::string_view name,
std::initializer_list<std::string_view> labels, ConstSpan<double> bounds, std::initializer_list<std::string_view> labels, std::span<const double> bounds,
std::string_view helptext, std::string_view unit = ""); std::string_view helptext, std::string_view unit = "");
/** /**
@ -200,8 +190,9 @@ public:
* different bucket settings. Users may also override * different bucket settings. Users may also override
* @p bounds via run-time configuration. * @p bounds via run-time configuration.
*/ */
HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name, Span<const LabelView> labels, HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name, std::span<const LabelView> labels,
ConstSpan<double> bounds, std::string_view helptext, std::string_view unit = ""); std::span<const double> bounds, std::string_view helptext,
std::string_view unit = "");
/// @copdoc HistogramInstance /// @copdoc HistogramInstance
HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name, HistogramPtr HistogramInstance(std::string_view prefix, std::string_view name,
@ -229,20 +220,20 @@ public:
protected: protected:
template<class F> template<class F>
static auto WithLabelNames(Span<const LabelView> xs, F continuation) { static auto WithLabelNames(std::span<const LabelView> xs, F continuation) {
if ( xs.size() <= 10 ) { if ( xs.size() <= 10 ) {
std::string_view buf[10]; std::string_view buf[10];
for ( size_t index = 0; index < xs.size(); ++index ) for ( size_t index = 0; index < xs.size(); ++index )
buf[index] = xs[index].first; buf[index] = xs[index].first;
return continuation(Span{buf, xs.size()}); return continuation(std::span{buf, xs.size()});
} }
else { else {
std::vector<std::string_view> buf; std::vector<std::string_view> buf;
for ( auto x : xs ) for ( auto x : xs )
buf.emplace_back(x.first); buf.emplace_back(x.first);
return continuation(Span{buf}); return continuation(std::span{buf});
} }
} }

View file

@ -2,10 +2,10 @@
#pragma once #pragma once
#include <span>
#include <string> #include <string>
#include <vector> #include <vector>
#include "zeek/Span.h"
#include "zeek/util-types.h" #include "zeek/util-types.h"
namespace zeek::telemetry { namespace zeek::telemetry {
@ -25,7 +25,7 @@ public:
virtual void RunCallbacks() = 0; virtual void RunCallbacks() = 0;
protected: protected:
MetricFamily(Span<const std::string_view> labels) { MetricFamily(std::span<const std::string_view> labels) {
for ( const auto& lbl : labels ) for ( const auto& lbl : labels )
label_names.emplace_back(lbl); label_names.emplace_back(lbl);
} }

View file

@ -3,6 +3,7 @@
#include "zeek/telemetry/Utils.h" #include "zeek/telemetry/Utils.h"
#include <algorithm> #include <algorithm>
#include <span>
#include "zeek/ID.h" #include "zeek/ID.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
@ -42,7 +43,7 @@ std::string BuildFullPrometheusName(std::string_view prefix, std::string_view na
return fn; return fn;
} }
prometheus::Labels BuildPrometheusLabels(Span<const LabelView> labels) { prometheus::Labels BuildPrometheusLabels(std::span<const LabelView> labels) {
prometheus::Labels p_labels; prometheus::Labels p_labels;
static std::string metrics_endpoint_label = static std::string metrics_endpoint_label =

View file

@ -4,10 +4,9 @@
#include <prometheus/family.h> #include <prometheus/family.h>
#include <prometheus/labels.h> #include <prometheus/labels.h>
#include <span>
#include <string_view> #include <string_view>
#include "zeek/Span.h"
namespace zeek::telemetry { namespace zeek::telemetry {
using LabelView = std::pair<std::string_view, std::string_view>; using LabelView = std::pair<std::string_view, std::string_view>;
@ -18,7 +17,7 @@ namespace detail {
* Builds a set of labels for prometheus based on a set of labels from * Builds a set of labels for prometheus based on a set of labels from
* Zeek. This adds an 'endpoint' label if it's missing from the set. * Zeek. This adds an 'endpoint' label if it's missing from the set.
*/ */
prometheus::Labels BuildPrometheusLabels(Span<const LabelView> labels); prometheus::Labels BuildPrometheusLabels(std::span<const LabelView> labels);
/** /**
* Builds a full metric name for Prometheus from prefix, name, and unit values. * Builds a full metric name for Prometheus from prefix, name, and unit values.

View file

@ -4,6 +4,7 @@ module Telemetry;
%%{ %%{
#include <span>
#include "zeek/telemetry/Counter.h" #include "zeek/telemetry/Counter.h"
#include "zeek/telemetry/Gauge.h" #include "zeek/telemetry/Gauge.h"
#include "zeek/telemetry/Histogram.h" #include "zeek/telemetry/Histogram.h"
@ -64,8 +65,8 @@ std::vector<zeek::telemetry::LabelView> sv_tbl(zeek::TableVal* xs)
return result; return result;
} }
bool labels_valid(zeek::Span<const zeek::telemetry::LabelView> labels, bool labels_valid(std::span<const zeek::telemetry::LabelView> labels,
zeek::Span<const std::string> label_names) std::span<const std::string> label_names)
{ {
auto key_in_label_names = [keys{label_names}](auto x) auto key_in_label_names = [keys{label_names}](auto x)
{ {

View file

@ -3,10 +3,10 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <span>
#include <string> #include <string>
#include "zeek/3rdparty/nonstd/expected.hpp" #include "zeek/3rdparty/nonstd/expected.hpp"
#include "zeek/Span.h"
// These two types are not namespaced intentionally. // These two types are not namespaced intentionally.
using zeek_int_t = int64_t; using zeek_int_t = int64_t;
@ -24,7 +24,7 @@ using unexpected = nonstd::unexpected<E>;
// Byte buffer types used by serialization code in storage and cluster. // Byte buffer types used by serialization code in storage and cluster.
using byte_buffer = std::vector<std::byte>; using byte_buffer = std::vector<std::byte>;
using byte_buffer_span = Span<const std::byte>; using byte_buffer_span = std::span<const std::byte>;
namespace util { namespace util {
namespace detail { namespace detail {