From a58517497b5aec0cba14d6cdd291e84472f3ceb3 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 25 Aug 2025 12:01:16 +0800 Subject: [PATCH] feat add microprile examples --- CMakeLists.txt | 17 +++++---- cmake/generic.cmake | 3 ++ examples/microprofile_demo.cc | 31 ++++++++++++++++ src/metric_server.cc | 67 ----------------------------------- 4 files changed, 42 insertions(+), 76 deletions(-) create mode 100644 examples/microprofile_demo.cc delete mode 100644 src/metric_server.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index a760bc4..736b84e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,10 @@ check_cxx_source_compiles( HAVE_PTHREAD) add_subdirectory(third_party/breakpad) -add_subdirectory(third_party/microprofile) +if(WITH_MICROPROFILE) + add_subdirectory(third_party/microprofile) + set(COMM_LINK_MICROPROFILE ON) +endif() CPMAddPackage( NAME oatpp @@ -58,6 +61,10 @@ CPMAddPackage( SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog FORCE) +if(WITH_EXAMPLES) + cc_executable(microprofile_demo SRCS examples/microprofile_demo.cc) +endif() + enable_testing() cc_test( calc_test @@ -71,14 +78,6 @@ cc_executable( SRCS src/breakpad_test.cc DEPS PUBLIC breakpad) -cc_executable( - metric_server - SRCS src/metric_server.cc - DEPS PUBLIC - prometheus-cpp::core - prometheus-cpp::pull - prometheus-cpp::push) - cc_benchmark( calc_benchmark SRCS src/calc_benchmark.cc diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 40c3de9..759b137 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -5,6 +5,9 @@ function(comm_link TARGET_NAME) if(COMM_LINK_PTHREAD) target_link_libraries(${TARGET_NAME} PUBLIC pthread) endif() + if(COMM_LINK_MICROPROFILE) + target_link_libraries(${TARGET_NAME} PUBLIC microprofile) + endif() endfunction() # cc_library(name STATIC SRCS a.c b.c DEPS PUBLIC pub_lib PRIVATE pri_lib) diff --git a/examples/microprofile_demo.cc b/examples/microprofile_demo.cc new file mode 100644 index 0000000..64b6463 --- /dev/null +++ b/examples/microprofile_demo.cc @@ -0,0 +1,31 @@ +#include +#include + +#include +#include + +MICROPROFILE_DEFINE(MAIN, "MAIN", "Main", MP_AUTO); + +void Test() +{ + MICROPROFILE_TIMELINE_SCOPE(MP_AUTO, "one"); +} + +int main() +{ + MicroProfileOnThreadCreate("Main"); + MicroProfileSetEnableAllGroups(true); + MicroProfileSetForceMetaCounters(true); + printf("port: %d\n", MicroProfileWebServerPort()); + MicroProfileStartAutoFlip(30); + + while (true) + { + MICROPROFILE_SCOPE(MAIN); + Test(); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + MicroProfileStopAutoFlip(); + MicroProfileShutdown(); + return 0; +} diff --git a/src/metric_server.cc b/src/metric_server.cc deleted file mode 100644 index 85f36ff..0000000 --- a/src/metric_server.cc +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -int main() -{ - using namespace prometheus; - - // create an 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(); - - // 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); - - // 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 methods = {"GET", "PUT", "POST", "HEAD"}; - auto method = methods.at(random_value % methods.size()); - // dynamically calling Family.Add() works but is slow and should be - // avoided - http_requests_counter.Add({{"method", method}}).Increment(); - } - return 0; -}