mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-07-03 23:47:12 +02:00
Update POCO library.
This commit is contained in:
37
vendor/POCO/Prometheus/src/Collector.cpp
vendored
Normal file
37
vendor/POCO/Prometheus/src/Collector.cpp
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// Collector.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: Collector
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/Collector.h"
|
||||
#include "Poco/RegularExpression.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
const std::string& Collector::validateName(const std::string& name)
|
||||
{
|
||||
static Poco::RegularExpression nameExpr{"^[a-zA-Z_:][a-zA-Z0-9_:]*$"s};
|
||||
|
||||
if (!nameExpr.match(name)) throw Poco::SyntaxException("Not a valid collector or metric name"s, name);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
82
vendor/POCO/Prometheus/src/Counter.cpp
vendored
Normal file
82
vendor/POCO/Prometheus/src/Counter.cpp
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
//
|
||||
// Counter.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: Counter
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/Counter.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
Counter::Counter(const std::string& name):
|
||||
LabeledMetricImpl<CounterSample>(Metric::Type::COUNTER, name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Counter::Counter(const std::string& name, const Params& params):
|
||||
LabeledMetricImpl<CounterSample>(Metric::Type::COUNTER, name)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
Counter::Counter(const std::string& name, Registry* pRegistry):
|
||||
LabeledMetricImpl<CounterSample>(Metric::Type::COUNTER, name, pRegistry)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Counter::Counter(const std::string& name, const Params& params, Registry* pRegistry):
|
||||
LabeledMetricImpl<CounterSample>(Metric::Type::COUNTER, name, pRegistry)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
double Counter::value() const
|
||||
{
|
||||
return _sample.value();
|
||||
}
|
||||
|
||||
|
||||
void Counter::exportTo(Exporter& exporter) const
|
||||
{
|
||||
if (labelNames().empty())
|
||||
{
|
||||
exporter.writeHeader(*this);
|
||||
exporter.writeSample(*this, {}, {}, _sample.value(), _sample.timestamp());
|
||||
}
|
||||
else
|
||||
{
|
||||
LabeledMetricImpl<CounterSample>::exportTo(exporter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<CounterSample> Counter::createSample() const
|
||||
{
|
||||
return std::make_unique<CounterSample>();
|
||||
}
|
||||
|
||||
|
||||
void Counter::writeSample(Exporter& exporter, const std::vector<std::string>& labelValues, const CounterSample& sample) const
|
||||
{
|
||||
exporter.writeSample(*this, labelNames(), labelValues, sample.value(), sample.timestamp());
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
82
vendor/POCO/Prometheus/src/Gauge.cpp
vendored
Normal file
82
vendor/POCO/Prometheus/src/Gauge.cpp
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
//
|
||||
// Gauge.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: Gauge
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/Gauge.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
Gauge::Gauge(const std::string& name):
|
||||
LabeledMetricImpl<GaugeSample>(Metric::Type::GAUGE, name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Gauge::Gauge(const std::string& name, const Params& params):
|
||||
LabeledMetricImpl<GaugeSample>(Metric::Type::GAUGE, name)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
Gauge::Gauge(const std::string& name, Registry* pRegistry):
|
||||
LabeledMetricImpl<GaugeSample>(Metric::Type::GAUGE, name, pRegistry)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Gauge::Gauge(const std::string& name, const Params& params, Registry* pRegistry):
|
||||
LabeledMetricImpl<GaugeSample>(Metric::Type::GAUGE, name, pRegistry)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
double Gauge::value() const
|
||||
{
|
||||
return _sample.value();
|
||||
}
|
||||
|
||||
|
||||
void Gauge::exportTo(Exporter& exporter) const
|
||||
{
|
||||
if (labelNames().empty())
|
||||
{
|
||||
exporter.writeHeader(*this);
|
||||
exporter.writeSample(*this, {}, {}, _sample.value(), _sample.timestamp());
|
||||
}
|
||||
else
|
||||
{
|
||||
LabeledMetricImpl<GaugeSample>::exportTo(exporter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<GaugeSample> Gauge::createSample() const
|
||||
{
|
||||
return std::make_unique<GaugeSample>();
|
||||
}
|
||||
|
||||
|
||||
void Gauge::writeSample(Exporter& exporter, const std::vector<std::string>& labelValues, const GaugeSample& sample) const
|
||||
{
|
||||
exporter.writeSample(*this, labelNames(), labelValues, sample.value(), sample.timestamp());
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
151
vendor/POCO/Prometheus/src/Histogram.cpp
vendored
Normal file
151
vendor/POCO/Prometheus/src/Histogram.cpp
vendored
Normal file
@ -0,0 +1,151 @@
|
||||
//
|
||||
// Histogram.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: Histogram
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/Histogram.h"
|
||||
#include "Poco/Prometheus/Gauge.h"
|
||||
#include "Poco/Prometheus/Exporter.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
HistogramSample::HistogramSample(const std::vector<double>& bucketBounds):
|
||||
_bucketBounds(bucketBounds),
|
||||
_bucketCounts(bucketBounds.size(), 0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void HistogramSample::observe(double value)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
const std::size_t n = _bucketBounds.size();
|
||||
for (std::size_t i = 0; i < n; i++)
|
||||
{
|
||||
if (value <= _bucketBounds[i])
|
||||
{
|
||||
_bucketCounts[i]++;
|
||||
}
|
||||
}
|
||||
_sum += value;
|
||||
_count++;
|
||||
}
|
||||
|
||||
|
||||
void HistogramSample::observe(Poco::Clock::ClockVal v)
|
||||
{
|
||||
observe(double(v)/Poco::Clock::resolution());
|
||||
}
|
||||
|
||||
|
||||
Histogram::Histogram(const std::string& name):
|
||||
LabeledMetricImpl(Metric::Type::HISTOGRAM, name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Histogram::Histogram(const std::string& name, const Params& params):
|
||||
LabeledMetricImpl(Metric::Type::HISTOGRAM, name),
|
||||
_bucketBounds(params.buckets)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
Histogram::Histogram(const std::string& name, Registry* pRegistry):
|
||||
LabeledMetricImpl(Metric::Type::HISTOGRAM, name, pRegistry)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Histogram::Histogram(const std::string& name, const Params& params, Registry* pRegistry):
|
||||
LabeledMetricImpl(Metric::Type::HISTOGRAM, name, pRegistry),
|
||||
_bucketBounds(params.buckets)
|
||||
{
|
||||
setHelp(params.help);
|
||||
setLabelNames(params.labelNames);
|
||||
}
|
||||
|
||||
|
||||
Histogram& Histogram::buckets(const std::vector<double>& bucketBounds)
|
||||
{
|
||||
_bucketBounds = bucketBounds;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void Histogram::observe(double value)
|
||||
{
|
||||
labels(EMPTY_LABEL).observe(value);
|
||||
}
|
||||
|
||||
|
||||
void Histogram::observe(Poco::Clock::ClockVal v)
|
||||
{
|
||||
labels(EMPTY_LABEL).observe(double(v)/Poco::Clock::resolution());
|
||||
}
|
||||
|
||||
|
||||
HistogramData Histogram::data() const
|
||||
{
|
||||
return labels(EMPTY_LABEL).data();
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<HistogramSample> Histogram::createSample() const
|
||||
{
|
||||
return std::make_unique<HistogramSample>(_bucketBounds);
|
||||
}
|
||||
|
||||
|
||||
void Histogram::exportTo(Exporter& exporter) const
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
Gauge bucket(name() + "_bucket"s, nullptr);
|
||||
Gauge sum(name() + "_sum"s, nullptr);
|
||||
Gauge count(name() + "_count"s, nullptr);
|
||||
|
||||
exporter.writeHeader(*this);
|
||||
std::vector<std::string> bucketLabels = labelNames();
|
||||
bucketLabels.push_back("le"s);
|
||||
const std::size_t n = _bucketBounds.size();
|
||||
forEach<HistogramSample>(
|
||||
[&](const std::vector<std::string>& labelValues, const HistogramSample& sample)
|
||||
{
|
||||
std::vector<std::string> bucketLabelValues = labelValues;
|
||||
bucketLabelValues.push_back(""s);
|
||||
const HistogramData data = sample.data();
|
||||
for (std::size_t i = 0; i < n; i++)
|
||||
{
|
||||
bucketLabelValues.back() = Poco::NumberFormatter::format(_bucketBounds[i]);
|
||||
|
||||
exporter.writeSample(bucket, bucketLabels, bucketLabelValues, data.bucketCounts[i]);
|
||||
}
|
||||
bucketLabelValues.back() = "+Inf"s;
|
||||
exporter.writeSample(bucket, bucketLabels, bucketLabelValues, data.count);
|
||||
exporter.writeSample(sum, labelNames(), labelValues, data.sum);
|
||||
exporter.writeSample(count, labelNames(), labelValues, data.count);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
33
vendor/POCO/Prometheus/src/IntCounter.cpp
vendored
Normal file
33
vendor/POCO/Prometheus/src/IntCounter.cpp
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
//
|
||||
// IntCounter.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: IntCounter
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/IntCounter.h"
|
||||
#include "Poco/Prometheus/Exporter.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
void IntCounter::exportTo(Exporter& exporter) const
|
||||
{
|
||||
const std::vector<std::string> EMPTY_VEC;
|
||||
|
||||
exporter.writeHeader(*this);
|
||||
exporter.writeSample(*this, EMPTY_VEC, EMPTY_VEC, _value, 0);
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
33
vendor/POCO/Prometheus/src/IntGauge.cpp
vendored
Normal file
33
vendor/POCO/Prometheus/src/IntGauge.cpp
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
//
|
||||
// IntGauge.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: IntGauge
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/IntGauge.h"
|
||||
#include "Poco/Prometheus/Exporter.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
void IntGauge::exportTo(Exporter& exporter) const
|
||||
{
|
||||
const std::vector<std::string> EMPTY_VEC;
|
||||
|
||||
exporter.writeHeader(*this);
|
||||
exporter.writeSample(*this, EMPTY_VEC, EMPTY_VEC, _value, 0);
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
52
vendor/POCO/Prometheus/src/LabeledMetric.cpp
vendored
Normal file
52
vendor/POCO/Prometheus/src/LabeledMetric.cpp
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// LabeledMetric.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: LabeledMetric
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/LabeledMetric.h"
|
||||
#include "Poco/RegularExpression.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
const std::vector<std::string> LabeledMetric::EMPTY_LABEL;
|
||||
|
||||
|
||||
const std::string& LabeledMetric::validateLabelName(const std::string& name)
|
||||
{
|
||||
static Poco::RegularExpression nameExpr{"^[a-zA-Z0-9_]*$"s};
|
||||
|
||||
if (!nameExpr.match(name)) throw Poco::SyntaxException("Not a valid label name"s, name);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
void LabeledMetric::setLabelNames(const std::vector<std::string>& labelNames)
|
||||
{
|
||||
if (!_labelNames.empty()) throw Poco::IllegalStateException("Label names have already been set for metric"s, name());
|
||||
|
||||
_labelNames.reserve(labelNames.size());
|
||||
for (const auto& labelName: labelNames)
|
||||
{
|
||||
_labelNames.push_back(validateLabelName(labelName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
69
vendor/POCO/Prometheus/src/MetricsRequestHandler.cpp
vendored
Normal file
69
vendor/POCO/Prometheus/src/MetricsRequestHandler.cpp
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
//
|
||||
// MetricsRequestHandler.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: HTTP
|
||||
// Module: MetricsRequestHandler
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/MetricsRequestHandler.h"
|
||||
#include "Poco/Prometheus/Registry.h"
|
||||
#include "Poco/Prometheus/TextExporter.h"
|
||||
#include "Poco/Net/HTTPServerRequest.h"
|
||||
#include "Poco/Net/HTTPServerResponse.h"
|
||||
#include "Poco/DeflatingStream.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
MetricsRequestHandler::MetricsRequestHandler():
|
||||
_registry(Registry::defaultRegistry())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MetricsRequestHandler::MetricsRequestHandler(const Registry& registry):
|
||||
_registry(registry)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void MetricsRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
|
||||
{
|
||||
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET || request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD)
|
||||
{
|
||||
response.setChunkedTransferEncoding(true);
|
||||
response.setContentType("text/plain; version=0.0.4"s);
|
||||
bool compressResponse(request.hasToken("Accept-Encoding"s, "gzip"s));
|
||||
if (compressResponse) response.set("Content-Encoding"s, "gzip"s);
|
||||
response.set("Cache-Control"s, "no-cache, no-store"s);
|
||||
std::ostream& plainResponseStream = response.send();
|
||||
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET)
|
||||
{
|
||||
Poco::DeflatingOutputStream gzipStream(plainResponseStream, Poco::DeflatingStreamBuf::STREAM_GZIP, 1);
|
||||
std::ostream& responseStream = compressResponse ? gzipStream : plainResponseStream;
|
||||
TextExporter exporter(responseStream);
|
||||
_registry.exportTo(exporter);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_METHOD_NOT_ALLOWED);
|
||||
response.setContentLength(0);
|
||||
response.send();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
118
vendor/POCO/Prometheus/src/MetricsServer.cpp
vendored
Normal file
118
vendor/POCO/Prometheus/src/MetricsServer.cpp
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
//
|
||||
// MetricsServer.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: HTTP
|
||||
// Module: MetricsServer
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/MetricsServer.h"
|
||||
#include "Poco/Prometheus/MetricsRequestHandler.h"
|
||||
#include "Poco/Prometheus/Registry.h"
|
||||
#include "Poco/Net/HTTPRequestHandlerFactory.h"
|
||||
#include "Poco/Net/HTTPServerRequest.h"
|
||||
#include "Poco/Net/HTTPServerResponse.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
class NotFoundRequestHandler: public Poco::Net::HTTPRequestHandler
|
||||
{
|
||||
public:
|
||||
void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
|
||||
{
|
||||
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
|
||||
response.setChunkedTransferEncoding(true);
|
||||
response.setContentType("text/html"s);
|
||||
response.send()
|
||||
<< "<html>"
|
||||
<< "<head><title>404 - Not Found</title></head>"
|
||||
<< "<body><h1>Not Found</h1><p>The requested resource was not found.</p></body>"
|
||||
<< "</html>";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class MetricsRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
|
||||
{
|
||||
public:
|
||||
MetricsRequestHandlerFactory(const Registry& registry, const std::string& path):
|
||||
_registry(registry),
|
||||
_path(path)
|
||||
{
|
||||
}
|
||||
|
||||
Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request)
|
||||
{
|
||||
if (request.getURI() == _path)
|
||||
{
|
||||
return new MetricsRequestHandler(_registry);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new NotFoundRequestHandler;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const Registry& _registry;
|
||||
const std::string _path;
|
||||
};
|
||||
|
||||
|
||||
const Poco::UInt16 MetricsServer::DEFAULT_PORT{9100};
|
||||
const std::string MetricsServer::DEFAULT_PATH{"/metrics"s};
|
||||
|
||||
|
||||
MetricsServer::MetricsServer(Poco::UInt16 port, const std::string& path):
|
||||
_httpServer(new MetricsRequestHandlerFactory(Registry::defaultRegistry(), path), port, defaultParams())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MetricsServer::MetricsServer(const Registry& registry, Poco::UInt16 port, const std::string& path):
|
||||
_httpServer(new MetricsRequestHandlerFactory(registry, path), port, defaultParams())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MetricsServer::MetricsServer(const Registry& registry, Poco::Net::ServerSocket& socket, Poco::Net::HTTPServerParams::Ptr pServerParams, const std::string& path):
|
||||
_httpServer(new MetricsRequestHandlerFactory(registry, path), socket, pServerParams)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void MetricsServer::start()
|
||||
{
|
||||
_httpServer.start();
|
||||
}
|
||||
|
||||
|
||||
void MetricsServer::stop()
|
||||
{
|
||||
_httpServer.stopAll();
|
||||
}
|
||||
|
||||
|
||||
Poco::Net::HTTPServerParams::Ptr MetricsServer::defaultParams()
|
||||
{
|
||||
Poco::Net::HTTPServerParams::Ptr pParams = new Poco::Net::HTTPServerParams;
|
||||
pParams->setMaxQueued(8);
|
||||
pParams->setMaxThreads(2);
|
||||
pParams->setKeepAlive(false);
|
||||
return pParams;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
102
vendor/POCO/Prometheus/src/ProcessCollector.cpp
vendored
Normal file
102
vendor/POCO/Prometheus/src/ProcessCollector.cpp
vendored
Normal file
@ -0,0 +1,102 @@
|
||||
//
|
||||
// ProcessCollector.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Collectors
|
||||
// Module: ProcessCollector
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/ProcessCollector.h"
|
||||
#include "Poco/Process.h"
|
||||
#ifdef POCO_OS_FAMILY_UNIX
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#endif
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
Poco::Timestamp ProcessCollector::_startTime;
|
||||
|
||||
|
||||
ProcessCollector::ProcessCollector():
|
||||
Collector("process"s)
|
||||
{
|
||||
buildMetrics();
|
||||
}
|
||||
|
||||
|
||||
ProcessCollector::ProcessCollector(const std::string& name, Registry* pRegistry):
|
||||
Collector(name, pRegistry)
|
||||
{
|
||||
buildMetrics();
|
||||
}
|
||||
|
||||
|
||||
void ProcessCollector::exportTo(Exporter& exporter) const
|
||||
{
|
||||
for (const auto& p: _metrics)
|
||||
{
|
||||
p->exportTo(exporter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ProcessCollector::buildMetrics()
|
||||
{
|
||||
_metrics.push_back(std::make_unique<CallbackIntGauge>(
|
||||
name() + "_cpu_seconds_total"s,
|
||||
"Total user and system CPU time spent in seconds"s,
|
||||
nullptr,
|
||||
[]()
|
||||
{
|
||||
long user;
|
||||
long system;
|
||||
Poco::Process::times(user, system);
|
||||
return static_cast<Poco::Int64>(user) + static_cast<Poco::Int64>(system);
|
||||
}));
|
||||
|
||||
#ifdef POCO_OS_FAMILY_UNIX
|
||||
_metrics.push_back(std::make_unique<CallbackIntGauge>(
|
||||
name() + "_max_fds"s,
|
||||
"Maximum number of open file descriptors"s,
|
||||
nullptr,
|
||||
[]()
|
||||
{
|
||||
return sysconf(_SC_OPEN_MAX);
|
||||
}));
|
||||
#endif
|
||||
|
||||
_metrics.push_back(std::make_unique<CallbackGauge>(
|
||||
name() + "_start_time_seconds"s,
|
||||
"Start time of the process since unix epoch in seconds"s,
|
||||
nullptr,
|
||||
[]()
|
||||
{
|
||||
return static_cast<double>(ProcessCollector::startTime().epochMicroseconds()/1000)/1000.0;
|
||||
}));
|
||||
|
||||
_metrics.push_back(std::make_unique<CallbackGauge>(
|
||||
name() + "_up_time_seconds"s,
|
||||
"Time in seconds the process has been up and running"s,
|
||||
nullptr,
|
||||
[]()
|
||||
{
|
||||
return static_cast<double>(ProcessCollector::startTime().elapsed()/1000)/1000.0;
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
105
vendor/POCO/Prometheus/src/Registry.cpp
vendored
Normal file
105
vendor/POCO/Prometheus/src/Registry.cpp
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
//
|
||||
// Registry.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: Registry
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/Registry.h"
|
||||
#include "Poco/Prometheus/Collector.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/SingletonHolder.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
void Registry::registerCollector(Collector* pCollector)
|
||||
{
|
||||
poco_check_ptr (pCollector);
|
||||
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
const auto it = _collectors.find(pCollector->name());
|
||||
if (it == _collectors.end())
|
||||
{
|
||||
_collectors[pCollector->name()] = pCollector;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw Poco::ExistsException("collector"s, pCollector->name());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Registry::unregisterCollector(Collector* pCollector)
|
||||
{
|
||||
poco_check_ptr (pCollector);
|
||||
|
||||
unregisterCollector(pCollector->name());
|
||||
}
|
||||
|
||||
|
||||
void Registry::unregisterCollector(const std::string& collectorName)
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
_collectors.erase(collectorName);
|
||||
}
|
||||
|
||||
|
||||
Collector* Registry::findCollector(const std::string& collectorName) const
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
const auto it = _collectors.find(collectorName);
|
||||
if (it != _collectors.end())
|
||||
return it->second;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
void Registry::clear()
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
_collectors.clear();
|
||||
}
|
||||
|
||||
|
||||
void Registry::exportTo(Exporter& exporter) const
|
||||
{
|
||||
Poco::FastMutex::ScopedLock lock(_mutex);
|
||||
|
||||
for (const auto& p: _collectors)
|
||||
{
|
||||
p.second->exportTo(exporter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
static SingletonHolder<Registry> sh;
|
||||
}
|
||||
|
||||
|
||||
Registry& Registry::defaultRegistry()
|
||||
{
|
||||
return *sh.get();
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
185
vendor/POCO/Prometheus/src/TextExporter.cpp
vendored
Normal file
185
vendor/POCO/Prometheus/src/TextExporter.cpp
vendored
Normal file
@ -0,0 +1,185 @@
|
||||
//
|
||||
// TextExporter.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Core
|
||||
// Module: TextExporter
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/TextExporter.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include <vector>
|
||||
#include <ostream>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
const std::string TextExporter::COUNTER{"counter"s};
|
||||
const std::string TextExporter::GAUGE{"gauge"s};
|
||||
const std::string TextExporter::HISTOGRAM{"histogram"s};
|
||||
const std::string TextExporter::SUMMARY{"summary"s};
|
||||
const std::string TextExporter::UNTYPED{"untyped"s};
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string escape(const std::string& str)
|
||||
{
|
||||
std::string result;
|
||||
for (char c: str)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\\':
|
||||
result += "\\\\";
|
||||
break;
|
||||
case '\n':
|
||||
result += "\\n";
|
||||
break;
|
||||
case '"':
|
||||
result += "\\\"";
|
||||
break;
|
||||
default:
|
||||
result += c;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void writeSampleImpl(std::ostream& stream, const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, T&& value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
poco_assert_dbg (labelNames.size() == labelValues.size());
|
||||
|
||||
stream << metric.name();
|
||||
if (!labelNames.empty())
|
||||
{
|
||||
stream << '{';
|
||||
for (std::size_t i = 0; i < labelNames.size(); i++)
|
||||
{
|
||||
if (i > 0) stream << ',';
|
||||
stream << labelNames[i] << "=\"" << escape(labelValues[i]) << '"';
|
||||
}
|
||||
stream << '}';
|
||||
}
|
||||
stream << ' ' << value;
|
||||
if (timestamp != 0)
|
||||
{
|
||||
stream << ' ' << timestamp.epochMicroseconds()/1000;
|
||||
}
|
||||
stream << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TextExporter::TextExporter(std::ostream& ostr):
|
||||
_stream(ostr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeHeader(const Metric& metric)
|
||||
{
|
||||
const std::string& help = metric.help();
|
||||
const std::string& type = typeToString(metric.type());
|
||||
|
||||
if (!help.empty())
|
||||
{
|
||||
_stream << "# HELP " << metric.name() << ' ' << help << '\n';
|
||||
}
|
||||
_stream << "# TYPE " << metric.name() << ' ' << type << '\n';
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, float value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSample(metric, labelNames, labelValues, static_cast<double>(value), timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, double value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
std::string valueString;
|
||||
if (std::isinf(value))
|
||||
{
|
||||
if (value > 0)
|
||||
valueString = "+Inf";
|
||||
else
|
||||
valueString = "-Inf";
|
||||
}
|
||||
else if (std::isnan(value))
|
||||
{
|
||||
valueString = "NaN";
|
||||
}
|
||||
else
|
||||
{
|
||||
valueString = Poco::NumberFormatter::format(value);
|
||||
}
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, valueString, timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, Poco::UInt32 value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, value, timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, Poco::Int32 value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, value, timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, Poco::UInt64 value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, value, timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, Poco::Int64 value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, value, timestamp);
|
||||
}
|
||||
|
||||
|
||||
void TextExporter::writeSample(const Metric& metric, const std::vector<std::string>& labelNames, const std::vector<std::string>& labelValues, const std::string& value, const Poco::Timestamp& timestamp)
|
||||
{
|
||||
writeSampleImpl(_stream, metric, labelNames, labelValues, value, timestamp);
|
||||
}
|
||||
|
||||
|
||||
const std::string& TextExporter::typeToString(Metric::Type type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case Metric::Type::COUNTER:
|
||||
return COUNTER;
|
||||
case Metric::Type::GAUGE:
|
||||
return GAUGE;
|
||||
case Metric::Type::HISTOGRAM:
|
||||
return HISTOGRAM;
|
||||
case Metric::Type::SUMMARY:
|
||||
return SUMMARY;
|
||||
case Metric::Type::UNTYPED:
|
||||
return UNTYPED;
|
||||
default:
|
||||
poco_bugcheck();
|
||||
return UNTYPED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
120
vendor/POCO/Prometheus/src/ThreadPoolCollector.cpp
vendored
Normal file
120
vendor/POCO/Prometheus/src/ThreadPoolCollector.cpp
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
//
|
||||
// ThreadPoolCollector.cpp
|
||||
//
|
||||
// Library: Prometheus
|
||||
// Package: Collectors
|
||||
// Module: ThreadPoolCollector
|
||||
//
|
||||
// Copyright (c) 2022, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Prometheus/ThreadPoolCollector.h"
|
||||
#include "Poco/Ascii.h"
|
||||
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Prometheus {
|
||||
|
||||
|
||||
const std::string ThreadPoolCollector::NAME_PREFIX{"poco_threadpool"s};
|
||||
|
||||
|
||||
ThreadPoolCollector::ThreadPoolCollector():
|
||||
Collector(NAME_PREFIX),
|
||||
_threadPoolName("default"s),
|
||||
_threadPool(Poco::ThreadPool::defaultPool())
|
||||
{
|
||||
buildMetrics();
|
||||
}
|
||||
|
||||
|
||||
ThreadPoolCollector::ThreadPoolCollector(const Poco::ThreadPool& threadPool):
|
||||
Collector(collectorName(threadPool.name())),
|
||||
_threadPoolName(threadPool.name()),
|
||||
_threadPool(threadPool)
|
||||
{
|
||||
buildMetrics();
|
||||
}
|
||||
|
||||
|
||||
ThreadPoolCollector::ThreadPoolCollector(const std::string& name, const Poco::ThreadPool& threadPool, Registry* pRegistry):
|
||||
Collector(collectorName(name), pRegistry),
|
||||
_threadPoolName(name),
|
||||
_threadPool(threadPool)
|
||||
{
|
||||
buildMetrics();
|
||||
}
|
||||
|
||||
|
||||
void ThreadPoolCollector::exportTo(Exporter& exporter) const
|
||||
{
|
||||
const std::vector<std::string> labelNames{"name"s};
|
||||
const std::vector<std::string> labelValues{_threadPoolName};
|
||||
|
||||
for (const auto& p: _metrics)
|
||||
{
|
||||
exporter.writeHeader(*p);
|
||||
exporter.writeSample(*p, labelNames, labelValues, p->value(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ThreadPoolCollector::buildMetrics()
|
||||
{
|
||||
const Poco::ThreadPool& threadPool = _threadPool;
|
||||
|
||||
_metrics.push_back(std::make_unique<CallbackIntGauge>(
|
||||
NAME_PREFIX + "_max_threads"s,
|
||||
"Maximum number of threads available in the thread pool (capacity)"s,
|
||||
nullptr,
|
||||
[&threadPool]()
|
||||
{
|
||||
return threadPool.capacity();
|
||||
}));
|
||||
|
||||
_metrics.push_back(std::make_unique<CallbackIntGauge>(
|
||||
NAME_PREFIX + "_used_threads"s,
|
||||
"Number of currently used threads"s,
|
||||
nullptr,
|
||||
[&threadPool]()
|
||||
{
|
||||
return threadPool.used();
|
||||
}));
|
||||
|
||||
_metrics.push_back(std::make_unique<CallbackIntGauge>(
|
||||
NAME_PREFIX + "_allocated_threads"s,
|
||||
"Number of currently allocated threads"s,
|
||||
nullptr,
|
||||
[&threadPool]()
|
||||
{
|
||||
return threadPool.allocated();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
std::string ThreadPoolCollector::collectorName(const std::string& threadPoolName)
|
||||
{
|
||||
std::string result(NAME_PREFIX);
|
||||
if (!threadPoolName.empty())
|
||||
{
|
||||
result += '_';
|
||||
for (const char c: threadPoolName)
|
||||
{
|
||||
if (Poco::Ascii::isAlphaNumeric(c) || c == '_')
|
||||
result += c;
|
||||
else
|
||||
result += '_';
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Prometheus
|
Reference in New Issue
Block a user