.. code-block:: lua
- myMetric=getMetric("myspecialmetric")
+ myMetric = getMetric("myspecialmetric")
.. function:: getMetric(name [, prometheusName]) -> Metric
:param string prometheusName: The optional Prometheus specific name.
+.. versionadded:: 5.4.0
+
.. function:: initMetric(name [, prometheusName]) -> Metric
initMetric(name [, prometheusTable]) -> Metric
:param string prometheusName: The optional Prometheus specific name
:param table prometheusTable: The optional table of Prometheus specific options
- Creates a new :class:`Metric` object with the name ``name``, and initializes it with optional Prometheus specific details. Calling this function with a string is identical to calling ``getMetric``. Calling this function with a table gives the metric an optional Prometheus name, type, and description.
+ Creates a new :class:`Metric` object with the name ``name`` and initializes it with optional Prometheus specific details.
+ Calling this function with a string is identical to calling :func:`getMetric`.
+ Calling this function with a table allows passing the desired Prometheus name, type, and description.
The elements of the table can be:
std::string prometheusDescr;
if (opts) {
- auto* optPrometheusName = boost::get<std::string>(&opts.get());
- if (optPrometheusName != nullptr) {
+ if (const auto* optPrometheusName = boost::get<std::string>(&*opts); optPrometheusName != nullptr) {
prometheusName = *optPrometheusName;
- } else {
- boost::optional<std::unordered_map<std::string, std::string>> vars = {boost::get<std::unordered_map<std::string, std::string>>(opts.get())};
+ } else if (auto* vars = boost::get<std::unordered_map<std::string, std::string>>(&*opts); vars != nullptr) {
prometheusName = (*vars)["prometheusName"];
prometheusTypeName = (*vars)["type"];
prometheusDescr = (*vars)["description"];
return iter->second.d_ptr;
}
- std::string name(str);
- if (!prometheusName.empty()) {
- name = prometheusName;
- }
- else {
- name = getPrometheusName(name);
- }
+ std::string name = prometheusName.empty() ? getPrometheusName(str) : prometheusName;
std::optional<PrometheusMetricType> metricType;
- if (!prometheusTypeName.empty()) {
- static const std::map<std::string, PrometheusMetricType> namesToTypes = {
- {"counter", PrometheusMetricType::counter},
- {"gauge", PrometheusMetricType::gauge},
- };
- auto realtype = namesToTypes.find(prometheusTypeName);
- if (realtype != namesToTypes.end()) {
- metricType = std::optional(realtype->second);
- }
+ if (prometheusTypeName == "counter") {
+ metricType = PrometheusMetricType::counter;
+ }
+ else if (prometheusTypeName == "gauge") {
+ metricType = PrometheusMetricType::gauge;
}
+
std::optional<std::string> descr;
if (!prometheusDescr.empty()) {
- descr = std::optional(std::move(prometheusDescr));
+ descr = prometheusDescr;
}
- auto ret = dynmetrics{new std::atomic<unsigned long>(), std::move(name), metricType, descr};
+ auto ret = dynmetrics{new std::atomic<unsigned long>(), std::move(name), metricType, std::move(descr)};
(*locked)[str] = ret;
return ret.d_ptr;
}