From 9e04cd3c7615553f065f8bac7ec15cccc26667a4 Mon Sep 17 00:00:00 2001 From: chundonglinlin Date: Mon, 31 Oct 2022 08:53:58 +0800 Subject: [PATCH] For #2899: Exporter: Add metrics cpu, memory and uname. (#3224) * Exporter: metrics support cpu gauge. * Exporter: metrics support memory and uname.. * Exporter: Ignore error when uname fail. Co-authored-by: winlin --- trunk/doc/CHANGELOG.md | 3 +- trunk/src/app/srs_app_http_api.cpp | 133 ++++++++++++++++++++ trunk/src/core/srs_core_version5.hpp | 2 +- trunk/src/protocol/srs_protocol_utility.cpp | 18 +++ trunk/src/protocol/srs_protocol_utility.hpp | 9 ++ trunk/src/utest/srs_utest_config.cpp | 9 ++ 6 files changed, 172 insertions(+), 2 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 554044510a6..421343e2abd 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for SRS. ## SRS 5.0 Changelog +* v5.0, 2022-10-31, For [#2899](https://github.com/ossrs/srs/issues/2899): Exporter: Add metrics cpu, memory and uname. v5.0.86 * v5.0, 2022-10-30, Config: Support startting with environment variable only. v5.0.85 * v5.0, 2022-10-26, Fix [#3218](https://github.com/ossrs/srs/issues/3218): Log: Follow Java/log4j log level specs. v5.0.83 * v5.0, 2022-10-25, Log: Refine the log interface. v5.0.82 @@ -23,7 +24,7 @@ The changelog for SRS. * v5.0, 2022-09-30, GB28181: Refine HTTP parser to support SIP. v5.0.70 * v5.0, 2022-09-30, Kernel: Support lazy sweeping simple GC. v5.0.69 * v5.0, 2022-09-30, HTTP: Support HTTP header in creating order. v5.0.68 -* v5.0, 2022-09-27, For [#2899](https://github.com/ossrs/srs/issues/2899): API: Support exporter for Prometheus. v5.0.67 +* v5.0, 2022-09-27, For [#2899](https://github.com/ossrs/srs/issues/2899): Exporter: Support exporter for Prometheus. v5.0.67 * v5.0, 2022-09-27, For [#3167](https://github.com/ossrs/srs/issues/3167): WebRTC: Refine sequence jitter algorithm. v5.0.66 * v5.0, 2022-09-22, Fix [#3164](https://github.com/ossrs/srs/issues/3164): SRT: Choppy when audio ts gap is too large. v5.0.65 * v5.0, 2022-09-16, APM: Support distributed tracing by Tencent Cloud APM. v5.0.64 diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index bd796567466..4064417b2e3 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -31,6 +31,10 @@ using namespace std; #include #include +#if defined(__linux__) || defined(SRS_OSX) +#include +#endif + srs_error_t srs_api_response_jsonp(ISrsHttpResponseWriter* w, string callback, string data) { srs_error_t err = srs_success; @@ -1164,3 +1168,132 @@ srs_error_t SrsGoApiTcmalloc::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess } #endif + +SrsGoApiMetrics::SrsGoApiMetrics() +{ + enabled_ = _srs_config->get_exporter_enabled(); + label_ = _srs_config->get_exporter_label(); + tag_ = _srs_config->get_exporter_tag(); +} + +SrsGoApiMetrics::~SrsGoApiMetrics() +{ +} + +srs_error_t SrsGoApiMetrics::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) +{ + // whether enabled the HTTP Metrics API. + if (!enabled_) { + return srs_api_response_code(w, r, ERROR_EXPORTER_DISABLED); + } + + /* + * node_uname gauge + * build_info gauge + * cpu gauge + * memory gauge + * send_bytes_total counter + * receive_bytes_total counter + * streams gauge + * clients gauge + * clients_total counter + * error counter + */ + + SrsStatistic* stat = SrsStatistic::instance(); + std::stringstream ss; + + #if defined(__linux__) || defined(SRS_OSX) + // Get system info + utsname* system_info = srs_get_system_uname_info(); + ss << "# HELP srs_node_uname_info Labeled system information as provided by the uname system call.\n" + << "# TYPE srs_node_uname_info gauge\n" + << "srs_node_uname_info{" + << "sysname=\"" << system_info->sysname << "\"," + << "nodename=\"" << system_info->nodename << "\"," + << "release=\"" << system_info->release << "\"," + << "version=\"" << system_info->version << "\"," + << "machine=\"" << system_info->machine << "\"" + << "} 1\n"; + #endif + + // Build info from Config. + ss << "# HELP srs_build_info A metric with a constant '1' value labeled by build_date, version from which SRS was built.\n" + << "# TYPE srs_build_info gauge\n" + << "srs_build_info{" + << "build_date=\"" << SRS_BUILD_DATE << "\"," + << "major=\"" << VERSION_MAJOR << "\"," + << "version=\"" << RTMP_SIG_SRS_VERSION << "\"," + << "code=\"" << RTMP_SIG_SRS_CODE<< "\""; + if (!label_.empty()) ss << ",label=\"" << label_ << "\""; + if (!tag_.empty()) ss << ",tag=\"" << tag_ << "\""; + ss << "} 1\n"; + + // Get ProcSelfStat + SrsProcSelfStat* u = srs_get_self_proc_stat(); + + // The cpu of proc used. + ss << "# HELP srs_cpu_percent SRS cpu used percent.\n" + << "# TYPE srs_cpu_percent gauge\n" + << "srs_cpu_percent " + << u->percent * 100 + << "\n"; + + // The memory of proc used.(MBytes) + int memory = (int)(u->rss * 4); + ss << "# HELP srs_memory SRS memory used.\n" + << "# TYPE srs_memory gauge\n" + << "srs_memory " + << memory + << "\n"; + + // Dump metrics by statistic. + int64_t send_bytes, recv_bytes, nstreams, nclients, total_nclients, nerrs; + stat->dumps_metrics(send_bytes, recv_bytes, nstreams, nclients, total_nclients, nerrs); + + // The total of bytes sent. + ss << "# HELP srs_send_bytes_total SRS total sent bytes.\n" + << "# TYPE srs_send_bytes_total counter\n" + << "srs_send_bytes_total " + << send_bytes + << "\n"; + + // The total of bytes received. + ss << "# HELP srs_receive_bytes_total SRS total received bytes.\n" + << "# TYPE srs_receive_bytes_total counter\n" + << "srs_receive_bytes_total " + << recv_bytes + << "\n"; + + // Current number of online streams. + ss << "# HELP srs_streams The number of SRS concurrent streams.\n" + << "# TYPE srs_streams gauge\n" + << "srs_streams " + << nstreams + << "\n"; + + // Current number of online clients. + ss << "# HELP srs_clients The number of SRS concurrent clients.\n" + << "# TYPE srs_clients gauge\n" + << "srs_clients " + << nclients + << "\n"; + + // The total of clients connections. + ss << "# HELP srs_clients_total The total counts of SRS clients.\n" + << "# TYPE srs_clients_total counter\n" + << "srs_clients_total " + << total_nclients + << "\n"; + + // The total of clients errors. + ss << "# HELP srs_clients_errs_total The total errors of SRS clients.\n" + << "# TYPE srs_clients_errs_total counter\n" + << "srs_clients_errs_total " + << nerrs + << "\n"; + + w->header()->set_content_type("text/plain; charset=utf-8"); + + return srs_api_response(w, r, ss.str()); +} diff --git a/trunk/src/core/srs_core_version5.hpp b/trunk/src/core/srs_core_version5.hpp index 0e523d1ec86..7fd6e0185f5 100644 --- a/trunk/src/core/srs_core_version5.hpp +++ b/trunk/src/core/srs_core_version5.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 5 #define VERSION_MINOR 0 -#define VERSION_REVISION 85 +#define VERSION_REVISION 86 #endif diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 6a45ce99d2b..c52d41b906d 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -937,3 +937,21 @@ srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content) return err; } +#if defined(__linux__) || defined(SRS_OSX) +utsname* srs_get_system_uname_info() +{ + static utsname* system_info = NULL; + + if (system_info != NULL) { + return system_info; + } + + system_info = new utsname(); + memset(system_info, 0, sizeof(utsname)); + if (uname(system_info) < 0) { + srs_warn("uname failed"); + } + + return system_info; +} +#endif diff --git a/trunk/src/protocol/srs_protocol_utility.hpp b/trunk/src/protocol/srs_protocol_utility.hpp index 6324789c7ad..d506888d050 100644 --- a/trunk/src/protocol/srs_protocol_utility.hpp +++ b/trunk/src/protocol/srs_protocol_utility.hpp @@ -26,6 +26,10 @@ #include +#if defined(__linux__) || defined(SRS_OSX) +#include +#endif + class ISrsHttpMessage; class SrsMessageHeader; @@ -187,5 +191,10 @@ extern std::string srs_get_system_hostname(void); // Read all content util EOF. extern srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content); +#if defined(__linux__) || defined(SRS_OSX) +// Get system uname info. +extern utsname* srs_get_system_uname_info(); +#endif + #endif diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index b5e50d67cc6..ce52d46bb11 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -3684,6 +3684,15 @@ VOID TEST(ConfigMainTest, CheckVhostConfig5) EXPECT_EQ(0, (int)conf.get_stats_network()); EXPECT_TRUE(conf.get_stats_disk_device() != NULL); } + + if (true) { + MockSrsConfig conf; + HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "exporter{enabled on;listen 9972;label cn-beijing;tag cn-edge;}")); + EXPECT_TRUE(conf.get_exporter_enabled()); + EXPECT_STREQ("9972", conf.get_exporter_listen().c_str()); + EXPECT_STREQ("cn-beijing", conf.get_exporter_label().c_str()); + EXPECT_STREQ("cn-edge", conf.get_exporter_tag().c_str()); + } } VOID TEST(ConfigMainTest, CheckIncludeConfig)