Skip to content

Commit

Permalink
For #2899: Exporter: Add metrics cpu, memory and uname. (#3224)
Browse files Browse the repository at this point in the history
* Exporter: metrics support cpu gauge.
* Exporter: metrics support memory and uname..
* Exporter: Ignore error when uname fail.

Co-authored-by: winlin <winlin@vip.126.com>
  • Loading branch information
chundonglinlin and winlinvip authored Oct 31, 2022
1 parent 9673bfb commit 9f4338b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
3 changes: 2 additions & 1 deletion trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 39 additions & 1 deletion trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ using namespace std;
#include <srs_protocol_utility.hpp>
#include <srs_app_coworkers.hpp>

#if defined(__linux__) || defined(SRS_OSX)
#include <sys/utsname.h>
#endif

srs_error_t srs_api_response_jsonp(ISrsHttpResponseWriter* w, string callback, string data)
{
srs_error_t err = srs_success;
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 5
#define VERSION_MINOR 0
#define VERSION_REVISION 85
#define VERSION_REVISION 86

#endif
18 changes: 18 additions & 0 deletions trunk/src/protocol/srs_protocol_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions trunk/src/protocol/srs_protocol_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#include <srs_protocol_st.hpp>

#if defined(__linux__) || defined(SRS_OSX)
#include <sys/utsname.h>
#endif

class ISrsHttpMessage;

class SrsMessageHeader;
Expand Down Expand Up @@ -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

9 changes: 9 additions & 0 deletions trunk/src/utest/srs_utest_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9f4338b

Please sign in to comment.