From 9f4338bd9dab812689a34d7b4f37ddd57fea2761 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 | 40 ++++++++++++++++++++- 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, 78 insertions(+), 3 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 554044510a..421343e2ab 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 390acbde42..9916d4b390 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; @@ -1084,7 +1088,10 @@ srs_error_t SrsGoApiMetrics::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa } /* + * node_uname gauge * build_info gauge + * cpu gauge + * memory gauge * send_bytes_total counter * receive_bytes_total counter * streams gauge @@ -1094,9 +1101,22 @@ srs_error_t SrsGoApiMetrics::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa */ 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" @@ -1109,6 +1129,24 @@ srs_error_t SrsGoApiMetrics::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa 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); diff --git a/trunk/src/core/srs_core_version5.hpp b/trunk/src/core/srs_core_version5.hpp index 0e523d1ec8..7fd6e0185f 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 c99f06e9d8..46c561e249 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -930,3 +930,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 6324789c7a..d506888d05 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 26323d44ed..1c4e042498 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -3644,6 +3644,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)