feat add spdlog
Some checks failed
sm-rpc / build (Debug, aarch64-linux-gnu) (push) Failing after 29s
sm-rpc / build (Debug, arm-linux-gnueabihf) (push) Failing after 16s
sm-rpc / build (Debug, host.gcc) (push) Failing after 11s
sm-rpc / build (Debug, mipsel-linux-gnu) (push) Failing after 12s
sm-rpc / build (Release, aarch64-linux-gnu) (push) Failing after 11s
sm-rpc / build (Release, arm-linux-gnueabihf) (push) Failing after 11s
sm-rpc / build (Release, host.gcc) (push) Failing after 12s
sm-rpc / build (Release, mipsel-linux-gnu) (push) Failing after 16s

This commit is contained in:
tqcq
2025-08-22 18:22:57 +08:00
parent 3d395b45f0
commit c68893bace
2331 changed files with 1306786 additions and 103 deletions

View File

@@ -0,0 +1,50 @@
cc_binary(
name = "sample-server",
srcs = ["sample_server.cc"],
deps = ["//pull"],
)
cc_binary(
name = "sample-server_multi",
srcs = ["sample_server_multi.cc"],
deps = ["//pull"],
)
cc_binary(
name = "sample-server_auth",
srcs = ["sample_server_auth.cc"],
deps = ["//pull"],
)
sh_test(
name = "scrape-test",
size = "small",
srcs = ["scrape.sh"],
data = [
"sample-server",
"scrape.conf",
],
tags = ["manual"],
)
sh_test(
name = "lint-test",
size = "small",
srcs = ["lint.sh"],
data = [
"sample-server",
],
tags = ["manual"],
)
cc_test(
name = "integration_test",
srcs = ["integration_test.cc"],
copts = ["-Iexternal/googletest/include"],
linkstatic = True,
deps = [
"//pull",
"@com_github_curl//:curl",
"@com_google_googletest//:gtest_main",
],
)

View File

@@ -0,0 +1,47 @@
add_executable(sample_server
sample_server.cc
)
target_link_libraries(sample_server
PRIVATE
${PROJECT_NAME}::pull
)
add_executable(sample_server_multi
sample_server_multi.cc
)
target_link_libraries(sample_server_multi
PRIVATE
${PROJECT_NAME}::pull
)
add_executable(sample_server_auth
sample_server_auth.cc
)
target_link_libraries(sample_server_auth
PRIVATE
${PROJECT_NAME}::pull
)
find_package(CURL)
if(CURL_FOUND)
add_executable(prometheus_pull_integration_test
integration_test.cc
)
target_link_libraries(prometheus_pull_integration_test
PRIVATE
${PROJECT_NAME}::pull
CURL::libcurl
GTest::gmock_main
)
add_test(
NAME prometheus_pull_integration_test
COMMAND prometheus_pull_integration_test
)
endif()

View File

@@ -0,0 +1,304 @@
#include <curl/curl.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <cstddef>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
#include "prometheus/counter.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/exposer.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
namespace prometheus {
namespace {
using namespace testing;
class IntegrationTest : public testing::Test {
public:
void SetUp() override {
exposer_ = detail::make_unique<Exposer>("127.0.0.1:0");
auto ports = exposer_->GetListeningPorts();
base_url_ = std::string("http://127.0.0.1:") + std::to_string(ports.at(0));
}
struct Response {
long code = 0;
std::string body;
std::string contentType;
};
std::function<void(CURL*)> fetchPrePerform_;
Response FetchMetrics(const std::string& metrics_path) const {
auto curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
if (!curl) {
throw std::runtime_error("failed to initialize libcurl");
}
const auto url = base_url_ + metrics_path;
Response response;
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response.body);
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteCallback);
if (fetchPrePerform_) {
fetchPrePerform_(curl.get());
}
CURLcode curl_error = curl_easy_perform(curl.get());
if (curl_error != CURLE_OK) {
throw std::runtime_error("failed to perform HTTP request");
}
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &response.code);
char* ct = nullptr;
curl_easy_getinfo(curl.get(), CURLINFO_CONTENT_TYPE, &ct);
if (ct) {
response.contentType = ct;
}
return response;
}
std::shared_ptr<Registry> RegisterSomeCounter(const std::string& name,
const std::string& path) const {
auto registry = std::make_shared<Registry>();
BuildCounter().Name(name).Register(*registry).Add({}).Increment();
exposer_->RegisterCollectable(registry, path);
return registry;
};
std::unique_ptr<Exposer> exposer_;
std::string base_url_;
std::string default_metrics_path_ = "/metrics";
private:
static std::size_t WriteCallback(void* contents, std::size_t size,
std::size_t nmemb, void* userp) {
auto response = reinterpret_cast<std::string*>(userp);
std::size_t realsize = size * nmemb;
response->append(reinterpret_cast<const char*>(contents), realsize);
return realsize;
}
};
TEST_F(IntegrationTest, doesNotExposeAnythingOnDefaultPath) {
const auto metrics = FetchMetrics(default_metrics_path_);
EXPECT_GE(metrics.code, 400);
}
TEST_F(IntegrationTest, exposeSingleCounter) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(counter_name));
}
TEST_F(IntegrationTest, exposesCountersOnDifferentUrls) {
const std::string first_metrics_path = "/first";
const std::string second_metrics_path = "/second";
const std::string first_counter_name = "first_total";
const std::string second_counter_name = "second_total";
const auto first_registry =
RegisterSomeCounter(first_counter_name, first_metrics_path);
const auto second_registry =
RegisterSomeCounter(second_counter_name, second_metrics_path);
// all set-up
const auto first_metrics = FetchMetrics(first_metrics_path);
const auto second_metrics = FetchMetrics(second_metrics_path);
// check results
ASSERT_EQ(first_metrics.code, 200);
ASSERT_EQ(second_metrics.code, 200);
EXPECT_THAT(first_metrics.body, HasSubstr(first_counter_name));
EXPECT_THAT(second_metrics.body, HasSubstr(second_counter_name));
EXPECT_THAT(first_metrics.body, Not(HasSubstr(second_counter_name)));
EXPECT_THAT(second_metrics.body, Not(HasSubstr(first_counter_name)));
}
TEST_F(IntegrationTest, unexposeRegistry) {
const std::string counter_name = "some_counter_total";
const auto registry =
RegisterSomeCounter(counter_name, default_metrics_path_);
exposer_->RemoveCollectable(registry, default_metrics_path_);
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, Not(HasSubstr(counter_name)));
}
TEST_F(IntegrationTest, acceptOptionalCompression) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);
fetchPrePerform_ = [](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(counter_name));
}
TEST_F(IntegrationTest, shouldDealWithExpiredCollectables) {
const std::string first_counter_name = "first_total";
const std::string second_counter_name = "second_total";
const auto registry =
RegisterSomeCounter(first_counter_name, default_metrics_path_);
auto disappearing_registry =
RegisterSomeCounter(second_counter_name, default_metrics_path_);
disappearing_registry.reset();
// all set-up
const auto metrics = FetchMetrics(default_metrics_path_);
// check results
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(first_counter_name));
EXPECT_THAT(metrics.body, Not(HasSubstr(second_counter_name)));
}
TEST_F(IntegrationTest, shouldSendBodyAsUtf8) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);
const auto metrics = FetchMetrics(default_metrics_path_);
// check content type
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.contentType, HasSubstr("utf-8"));
}
class BasicAuthIntegrationTest : public IntegrationTest {
public:
void SetUp() override {
IntegrationTest::SetUp();
registry_ = RegisterSomeCounter(counter_name_, default_metrics_path_);
exposer_->RegisterAuth(
[&](const std::string& user, const std::string& password) {
return user == username_ && password == password_;
},
"Some Auth Realm", default_metrics_path_);
}
protected:
const std::string counter_name_ = "example_total";
const std::string username_ = "test_user";
const std::string password_ = "test_password";
std::shared_ptr<Registry> registry_;
};
TEST_F(BasicAuthIntegrationTest, shouldPerformProperAuthentication) {
fetchPrePerform_ = [&](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, username_.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, password_.c_str());
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(counter_name_));
}
TEST_F(BasicAuthIntegrationTest, shouldRejectRequestWithoutAuthorization) {
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
TEST_F(BasicAuthIntegrationTest, shouldRejectRequestWithWrongUsername) {
fetchPrePerform_ = [&](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, "dummy");
curl_easy_setopt(curl, CURLOPT_PASSWORD, password_.c_str());
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
TEST_F(BasicAuthIntegrationTest, shouldRejectRequestWithWrongPassword) {
fetchPrePerform_ = [&](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, username_.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, "dummy");
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
#if defined(CURLAUTH_BEARER) && defined(CURLOPT_XOAUTH2_BEARER)
TEST_F(BasicAuthIntegrationTest, shouldRejectWrongAuthorizationMethod) {
fetchPrePerform_ = [](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, "test_token");
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
#endif
TEST_F(BasicAuthIntegrationTest, shouldRejectMalformedBase64) {
std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)> header(
curl_slist_append(nullptr, "Authorization: Basic $"),
curl_slist_free_all);
fetchPrePerform_ = [&header](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header.get());
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
TEST_F(BasicAuthIntegrationTest, shouldRejectMalformedBasicAuth) {
std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)> header(
curl_slist_append(nullptr, "Authorization: Basic YWJj"),
curl_slist_free_all);
fetchPrePerform_ = [&header](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header.get());
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
} // namespace
} // namespace prometheus

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
curl=$(which curl)
if [ ! -x "$curl" ] ; then
echo "curl must be in path for this test to run"
exit 1
fi
promtool=$(which promtool)
if [ ! -x "$promtool" ] ; then
echo "promtool must be in path for this test to run"
exit 1
fi
pull/tests/integration/sample-server&
sample_server_pid=$!
function stop_server {
echo "Stopping sample-server"
kill -9 $sample_server_pid
}
trap stop_server EXIT
sleep 1
"$curl" -s http://localhost:8080/metrics | "$promtool" check metrics

View File

@@ -0,0 +1,77 @@
#include <array>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>
#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
#include "prometheus/exposer.h"
#include "prometheus/family.h"
#include "prometheus/info.h"
#include "prometheus/registry.h"
int main() {
using namespace prometheus;
// create a http server running on port 8080
Exposer exposer{"127.0.0.1:8080"};
// create a metrics registry
// @note it's the users responsibility to keep the object alive
auto registry = std::make_shared<Registry>();
// add a new counter family to the registry (families combine values with the
// same name, but distinct label dimensions)
//
// @note please follow the metric-naming best-practices:
// https://prometheus.io/docs/practices/naming/
auto& packet_counter = BuildCounter()
.Name("observed_packets_total")
.Help("Number of observed packets")
.Register(*registry);
// add and remember dimensional data, incrementing those is very cheap
auto& tcp_rx_counter =
packet_counter.Add({{"protocol", "tcp"}, {"direction", "rx"}});
auto& tcp_tx_counter =
packet_counter.Add({{"protocol", "tcp"}, {"direction", "tx"}});
auto& udp_rx_counter =
packet_counter.Add({{"protocol", "udp"}, {"direction", "rx"}});
auto& udp_tx_counter =
packet_counter.Add({{"protocol", "udp"}, {"direction", "tx"}});
// add a counter whose dimensional data is not known at compile time
// nevertheless dimensional values should only occur in low cardinality:
// https://prometheus.io/docs/practices/naming/#labels
auto& http_requests_counter = BuildCounter()
.Name("http_requests_total")
.Help("Number of HTTP requests")
.Register(*registry);
auto& version_info = BuildInfo()
.Name("versions")
.Help("Static info about the library")
.Register(*registry);
version_info.Add({{"prometheus", "1.0"}});
// ask the exposer to scrape the registry on incoming HTTP requests
exposer.RegisterCollectable(registry);
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
const auto random_value = std::rand();
if (random_value & 1) tcp_rx_counter.Increment();
if (random_value & 2) tcp_tx_counter.Increment();
if (random_value & 4) udp_rx_counter.Increment();
if (random_value & 8) udp_tx_counter.Increment();
const std::array<std::string, 4> methods = {"GET", "PUT", "POST", "HEAD"};
auto method = methods.at(random_value % methods.size());
// dynamically calling Family<T>.Add() works but is slow and should be
// avoided
http_requests_counter.Add({{"method", method}}).Increment();
}
return 0;
}

View File

@@ -0,0 +1,45 @@
#include <chrono>
#include <memory>
#include <string>
#include <thread>
#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
#include "prometheus/exposer.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
int main() {
using namespace prometheus;
// create an http server running on port 8080
Exposer exposer{"127.0.0.1:8080", 1};
auto registry = std::make_shared<Registry>();
// add a new counter family to the registry (families combine values with the
// same name, but distinct label dimensions)
auto& counter_family = BuildCounter()
.Name("time_running_seconds_total")
.Help("How many seconds is this server running?")
.Register(*registry);
// add a counter to the metric family
auto& seconds_counter = counter_family.Add(
{{"another_label", "bar"}, {"yet_another_label", "baz"}});
// ask the exposer to scrape registry on incoming scrapes for "/metrics"
exposer.RegisterCollectable(registry, "/metrics");
exposer.RegisterAuth(
[](const std::string& user, const std::string& password) {
return user == "test_user" && password == "test_password";
},
"Some Auth Realm");
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
// increment the counters by one (second)
seconds_counter.Increment(1.0);
}
return 0;
}

View File

@@ -0,0 +1,54 @@
#include <chrono>
#include <memory>
#include <thread>
#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
#include "prometheus/exposer.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
int main() {
using namespace prometheus;
// create an http server running on port 8080
Exposer exposer{"127.0.0.1:8080", 1};
auto registryA = std::make_shared<Registry>();
// add a new counter family to the registry (families combine values with the
// same name, but distinct label dimensions)
auto& counter_familyA = BuildCounter()
.Name("time_running_seconds_total")
.Help("How many seconds is this server running?")
.Register(*registryA);
// add a counter to the metric family
auto& seconds_counterA = counter_familyA.Add(
{{"another_label", "bar"}, {"yet_another_label", "baz"}});
// ask the exposer to scrape registryA on incoming scrapes for "/metricsA"
exposer.RegisterCollectable(registryA, "/metricsA");
auto registryB = std::make_shared<Registry>();
auto& counter_familyB =
BuildCounter()
.Name("other_time_running_seconds_total")
.Help("How many seconds has something else been running?")
.Register(*registryB);
auto& seconds_counterB = counter_familyB.Add(
{{"another_label", "not_bar"}, {"yet_another_label", "not_baz"}});
// This endpoint exposes registryB.
exposer.RegisterCollectable(registryB, "/metricsB");
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
// increment the counters by one (second)
seconds_counterA.Increment(1.0);
seconds_counterB.Increment(1.5);
}
return 0;
}

View File

@@ -0,0 +1,7 @@
[[inputs.prometheus]]
# An array of urls to scrape metrics from.
urls = ["http://localhost:8080/metrics"]
interval = '1s'
[[outputs.file]]
files = ["stdout"]
flush_interval = "1s"

View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
telegraf=$(which telegraf)
if [ ! -x "$telegraf" ] ; then
echo "telegraf must be in path for this test to run"
exit 1
fi
function timeout_after
{
( echo failing after $1 seconds for execution && sleep $1 && kill $$ ) &
}
trap 'kill $(jobs -p)' EXIT
timeout_after 10
pull/tests/integration/sample-server &
telegraf --config pull/tests/integration/scrape.conf --quiet | grep -m1 http_requests_total