Skip to content

Commit

Permalink
For ossrs#2899: Exporter: Add metrics cpu, memory and uname. (ossrs#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
2 people authored and johzzy committed Apr 18, 2023
1 parent 7670af4 commit 9e04cd3
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 2 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
133 changes: 133 additions & 0 deletions 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 @@ -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());
}
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 @@ -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
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 @@ -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)
Expand Down

0 comments on commit 9e04cd3

Please sign in to comment.