1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

API: support metrics for prometheus.(#2899) (#3189)

* API: support metrics for prometheus.

* Metrics: optimize metrics statistics info.

* Refine: remove redundant code.

* Refine: fix metrics srs_streams param.

* Metrics: add major param.

* Metrics: refine params and metric comments.

* For #2899: API: Support exporter for Prometheus. v5.0.67

Co-authored-by: winlin <winlin@vip.126.com>
This commit is contained in:
chundonglinlin 2022-09-27 15:39:26 +08:00 committed by GitHub
parent e31f3b0e64
commit 981cab40d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 236 additions and 10 deletions

View file

@ -1062,3 +1062,96 @@ 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);
}
/*
* build_info 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;
// 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";
// 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";
return srs_api_response(w, r, ss.str());
}