Skip to content

Commit

Permalink
API: support metrics for prometheus.(ossrs#2899) (ossrs#3189)
Browse files Browse the repository at this point in the history
* 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 ossrs#2899: API: Support exporter for Prometheus. v5.0.67

Co-authored-by: winlin <winlin@vip.126.com>
  • Loading branch information
2 people authored and johzzy committed Apr 18, 2023
1 parent 9e04cd3 commit 5f903e7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
15 changes: 15 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,21 @@ tencentcloud_apm {
debug_logging off;
}

# Prometheus exporter config.
# See https://prometheus.io/docs/instrumenting/exporters
exporter {
# Whether exporter is enabled.
# Overwrite by env SRS_EXPORTER_ENABLED
# Default: off
enabled off;
# The logging label to category the cluster servers.
# Overwrite by env SRS_EXPORTER_LABEL
label cn-beijing;
# The logging tag to category the cluster servers.
# Overwrite by env SRS_EXPORTER_TAG
tag cn-edge;
}

#############################################################################################
# heartbeat/stats sections
#############################################################################################
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,7 @@ srs_error_t SrsConfig::check_normal_config()
&& n != "inotify_auto_reload" && n != "auto_reload_for_docker" && n != "tcmalloc_release_rate"
&& n != "query_latest_version" && n != "first_wait_for_qlv" && n != "threads"
&& n != "circuit_breaker" && n != "is_full" && n != "in_docker" && n != "tencentcloud_cls"
&& n != "exporter"
) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
}
Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,11 @@ class SrsConfig
// The device name configed in args of directive.
// @return the disk device name to stat. NULL if not configed.
virtual SrsConfDirective* get_stats_disk_device();
public:
// Get Prometheus exporter config.
virtual bool get_exporter_enabled();
virtual std::string get_exporter_label();
virtual std::string get_exporter_tag();
};

#endif
Expand Down
13 changes: 13 additions & 0 deletions trunk/src/app/srs_app_http_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,18 @@ class SrsGoApiTcmalloc : public ISrsHttpHandler
};
#endif

class SrsGoApiMetrics : public ISrsHttpHandler
{
private:
bool enabled_;
std::string label_;
std::string tag_;
public:
SrsGoApiMetrics();
virtual ~SrsGoApiMetrics();
public:
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
};

#endif

4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ srs_error_t SrsServer::http_handle()
return srs_error_wrap(err, "handle tests errors");
}
#endif
// metrics by prometheus
if ((err = http_api_mux->handle("/metrics", new SrsGoApiMetrics())) != srs_success) {
return srs_error_wrap(err, "handle tests errors");
}

// TODO: FIXME: for console.
// TODO: FIXME: support reload.
Expand Down
5 changes: 4 additions & 1 deletion trunk/src/kernel/srs_kernel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@
XX(ERROR_PB_NO_SPACE , 1084, "ProtobufNoSpace", "Failed to encode protobuf for no buffer space left") \
XX(ERROR_CLS_INVALID_CONFIG , 1085, "ClsConfig", "Invalid configuration for TencentCloud CLS") \
XX(ERROR_CLS_EXCEED_SIZE , 1086, "ClsExceedSize", "CLS logs exceed max size 5MB") \
XX(ERROR_APM_EXCEED_SIZE , 1087, "ApmExceedSize", "APM logs exceed max size 5MB")
XX(ERROR_APM_EXCEED_SIZE , 1087, "ApmExceedSize", "APM logs exceed max size 5MB") \
XX(ERROR_APM_ENDPOINT , 1088, "ApmEndpoint", "APM endpoint is invalid") \
XX(ERROR_APM_AUTH , 1089, "ApmAuth", "APM team or token is invalid") \
XX(ERROR_EXPORTER_DISABLED , 1090, "ExporterDisable", "Prometheus exporter is disabled")

/**************************************************/
/* RTMP protocol error. */
Expand Down

0 comments on commit 5f903e7

Please sign in to comment.