37#include <seastar/core/future.hh>
38#include <seastar/net/byteorder.hh>
39#include <seastar/core/shared_ptr.hh>
40#include <seastar/core/sstring.hh>
41#include <seastar/util/log.hh>
42#include <seastar/util/program-options.hh>
43#include <seastar/util/modules.hh>
88extern seastar::logger logger;
90using data_type = seastar::metrics::impl::data_type;
92enum class known_type {
109 changes_since_last_save,
178 http_request_methods,
204 memory_throttle_count,
280 typed(data_type t, T && v)
281 : type(t),
value(std::forward<T>(v)) {
288inline typed<T> make_typed(data_type type, T&& t) {
289 return typed<T>(type, std::forward<T>(t));
297type_id type_id_for(known_type);
301static constexpr unsigned max_collectd_field_text_len = 63;
304 static thread_local unsigned _next_truncated_idx;
307 void truncate(sstring& field,
const char* field_desc);
311 scollectd::type_instance ti = std::string())
312 : _plugin(std::move(p)), _plugin_instance(std::move(pi)), _type(
313 std::move(t)), _type_instance(std::move(ti)) {
315 truncate(_plugin,
"plugin");
316 truncate(_plugin_instance,
"plugin_instance");
317 truncate(_type,
"type");
318 truncate(_type_instance,
"type_instance");
321 _plugin_instance(
id.instance_id()), _type(inherit_type),
322 _type_instance(
id.name()) {
330 const plugin_id & plugin()
const {
333 const plugin_instance_id & plugin_instance()
const {
334 return _plugin_instance;
336 const type_id & type()
const {
339 const scollectd::type_instance & type_instance()
const {
340 return _type_instance;
346 plugin_instance_id _plugin_instance;
348 scollectd::type_instance _type_instance;
351extern const plugin_instance_id per_cpu_plugin_instance;
404 remove_polled_metric(_id);
420 :
public std::vector<registration>
423 typedef std::vector<registration> vector_type;
429 registrations(
const std::initializer_list<type_instance_id>& l)
430 : vector_type(l.begin(),l.end())
433 vector_type::operator=(std::move(v));
436 registrations& operator=(
const std::initializer_list<type_instance_id>& l) {
448 template<
typename... Args>
451 template<
typename... Args>
452 typed_value(
const type_id& tid,
const scollectd::type_instance& ti, Args&&... args)
456 const scollectd::type_instance& type_instance()
const {
457 return _type_instance;
462 const type_id & type()
const {
467 scollectd::type_instance _type_instance;
473 template<
typename... TypedValues>
476 , _plugin_instance(pi)
477 , _registrations({ add_impl(values)... })
479 std::vector<type_instance_id> bound_ids()
const;
484 plugin_id _plugin_id;
485 plugin_instance_id _plugin_instance;
495 template<
typename... TypedValues>
505template<known_type Type>
507 template<
typename ... Args>
509 :
typed_value(type_id_for(Type), ti, std::forward<Args>(args)...)
512 template<
typename ... Args>
514 :
typed_value(type_id_for(Type), std::move(ti), std::move(d), std::forward<Args>(args)...)
516 template<
typename ... Args>
518 :
typed_value(type_id_for(Type), scollectd::type_instance(), std::move(d), std::forward<Args>(args)...)
543template<
typename T,
typename En =
void>
546template<
typename T,
typename En =
void>
552!std::is_void_v<std::invoke_result_t<T>>,
553void>> :
public std::true_type {
558std::enable_if_t<std::is_fundamental_v<T>, void>> :
public std::false_type {
564std::is_integral_v<T> && std::is_unsigned_v<T>,
565void>> :
public std::integral_constant<data_type,
570std::enable_if_t<std::is_floating_point_v<T>, void>> :
public std::integral_constant<
571data_type, data_type::GAUGE> {
576std::invoke_result_t<T>> {
590 const W & operator()()
const {
596 typedef std::remove_reference_t<T> value_type;
597 typedef std::conditional_t<
601 value(
const value_type & t)
604 value(data_type type,
const value_type & t)
605 : _type(type), _t(t) {
607 uint64_t operator()()
const {
609 if (_type == data_type::GAUGE) {
610 return convert(
double(v));
616 operator uint64_t()
const {
619 operator data_type()
const {
622 data_type type()
const {
627 template<
typename _Iter>
628 void bpack(_Iter s, _Iter e, uint64_t v)
const {
635 std::enable_if_t<std::is_integral_v<V>, uint64_t> convert(
642 std::enable_if_t<std::is_floating_point_v<V>, uint64_t> convert(
656 bpack(std::begin(u.b), std::end(u.b), v.i);
661 const data_type _type;
662 const stored_type _t;
674 bool _enabled =
true;
681 virtual size_t size()
const = 0;
683 virtual void types(data_type *)
const = 0;
694 bool is_enabled()
const {
698 void set_enabled(
bool b) {
705template<
typename ... Args>
708 static const size_t num_values =
sizeof...(Args);
712 , _values(std::forward<Args>(args)...)
718 size_t size()
const override {
721 void types(data_type * p)
const override {
722 unpack(_values, [p](Args... args) {
723 std::initializer_list<data_type> tmp = { args... };
724 std::copy(tmp.begin(), tmp.end(), p);
728 unpack(_values, [p](Args... args) {
729 std::initializer_list<uint64_t> tmp = { args... };
730 std::copy(tmp.begin(), tmp.end(), p);
734 template<
typename _Op>
735 void unpack(
const std::tuple<Args...>& t, _Op&& op)
const {
736 do_unpack(t, std::index_sequence_for<Args...> {}, std::forward<_Op>(op));
739 template<
size_t ...S,
typename _Op>
740 void do_unpack(
const std::tuple<Args...>& t,
const std::index_sequence<S...> &, _Op&& op)
const {
741 op(std::get<S>(t)...);
744 std::tuple < Args... > _values;
747void add_polled(
const type_instance_id &,
const shared_ptr<value_list> &,
bool enabled =
true);
749typedef std::function<void()> notify_function;
750template<
typename... _Args>
751static auto make_type_instance(description d, _Args && ... args) -> values_impl < decltype(value<_Args>(std::forward<_Args>(args)))... >
753 return values_impl<decltype(value<_Args>(std::forward<_Args>(args)))...>(
754 std::move(d), value<_Args>(std::forward<_Args>(args))...);
760template<
typename ... _Args>
762 const plugin_instance_id & plugin_instance,
const type_id & type,
763 const scollectd::type_instance & type_instance, _Args&& ... args) {
765 std::forward<_Args>(args)...);
771template<
typename ... _Args>
773 const plugin_instance_id & plugin_instance,
const type_id & type,
774 const scollectd::type_instance & type_instance,
description d, _Args&& ... args) {
776 type_instance_id(plugin, plugin_instance, type, type_instance), std::move(d),
777 std::forward<_Args>(args)...);
779template<
typename ... _Args>
780static future<> send_explicit_metric(
const plugin_id & plugin,
781 const plugin_instance_id & plugin_instance,
const type_id & type,
782 const scollectd::type_instance & type_instance, _Args&& ... args) {
783 return send_explicit_metric(
784 type_instance_id(plugin, plugin_instance, type, type_instance),
785 std::forward<_Args>(args)...);
787template<
typename ... _Args>
788static notify_function create_explicit_metric(
const plugin_id & plugin,
789 const plugin_instance_id & plugin_instance,
const type_id & type,
790 const scollectd::type_instance & type_instance, _Args&& ... args) {
791 return create_explicit_metric(
792 type_instance_id(plugin, plugin_instance, type, type_instance),
793 std::forward<_Args>(args)...);
801template<
typename Arg>
803 Arg&& arg,
bool enabled =
true) {
804 seastar::metrics::impl::get_local_impl()->add_registration(to_metrics_id(
id), arg.type, seastar::metrics::impl::make_function(arg.value, arg.type), d, enabled);
811template<
typename Arg>
821template<
typename Args>
827template<
typename Args>
828static type_instance_id add_disabled_polled_metric(
const type_instance_id &
id,
830 return add_disabled_polled_metric(
id, description(), std::forward<Args>(args));
833template<
typename ... Args>
834static type_instance_id add_disabled_polled_metric(
const type_instance_id &
id,
836 return add_disabled_polled_metric(
id, description(), std::forward<Args>(args)...);
841template<
typename ... _Args>
842static future<> send_explicit_metric(
const type_instance_id &
id,
844 return send_metric(
id, make_type_instance(std::forward<_Args>(args)...));
846template<
typename ... _Args>
847static notify_function create_explicit_metric(
const type_instance_id &
id,
849 auto list = make_type_instance(std::forward<_Args>(args)...);
850 return [id, list=std::move(list)]() {
851 send_metric(
id, list);
855template<
typename... Args>
856typed_value::typed_value(
const type_id& tid,
const scollectd::type_instance& ti,
description d, Args&&... args)
859 , _values(::
seastar::make_shared<decltype(make_type_instance(
std::move(d),
std::forward<Args>(args)...))>(make_type_instance(
std::move(d),
std::forward<Args>(args)...)))
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Human-readable description of a metric/group.
Definition: metrics.hh:133
Definition: metrics_api.hh:105
Definition: program-options.hh:293
Definition: scollectd.hh:493
Definition: scollectd.hh:471
Definition: scollectd.hh:421
Definition: scollectd.hh:303
Definition: scollectd.hh:673
Definition: scollectd.hh:583
Definition: scollectd.hh:706
future< uint64_t > file_size(std::string_view name) noexcept
sstring metric_type_def
Definition: metrics.hh:113
sstring metric_name_type
Definition: metrics.hh:114
sstring instance_id_type
Definition: metrics.hh:115
header file for metric API layer (like prometheus or collectd)
sstring group_name_type
Definition: metrics_registration.hh:64
static type_instance_id add_polled_metric(const plugin_id &plugin, const plugin_instance_id &plugin_instance, const type_id &type, const scollectd::type_instance &type_instance, _Args &&... args)
Definition: scollectd.hh:761
typed_value_impl< known_type::total_bytes > total_bytes
Definition: scollectd.hh:527
static type_instance_id add_disabled_polled_metric(const type_instance_id &id, description d, Args &&arg)
Definition: scollectd.hh:822
Definition: scollectd.hh:547
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
Definition: scollectd.hh:544
Definition: scollectd.hh:354
program_options::value< std::string > collectd_hostname
Definition: scollectd.hh:369
program_options::value< unsigned > collectd_poll_period
Poll period (ms).
Definition: scollectd.hh:367
program_options::value< std::string > collectd_address
Address to send/broadcast metrics to.
Definition: scollectd.hh:362
program_options::value< bool > collectd
Enable collectd daemon.
Definition: scollectd.hh:358
Definition: scollectd.hh:393
Definition: scollectd.hh:506
Definition: scollectd.hh:443
typed_value(const type_id &tid, const scollectd::type_instance &ti, description, Args &&... args)
Definition: scollectd.hh:856
Definition: scollectd.hh:279
Definition: scollectd.hh:586
Definition: unaligned.hh:58