diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index d67ce904d..d1f439812 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -635,10 +635,16 @@ tencentcloud_apm { # Overwrite by env SRS_TENCENTCLOUD_APM_ENABLED # default: off enabled on; - # The APM token for authentication. See https://console.cloud.tencent.com/apm/monitor/access + # The APM team or business system ID, to which spans belongs to. For example, the team is apm-FsOsPOIMl (just an + # example, not available), please get your team from https://console.cloud.tencent.com/apm/monitor/team + # Overwrite by env SRS_TENCENTCLOUD_APM_TEAM + team apm-xxxxxxxxx; + # The APM token for authentication. For example, the token is xzddEaegsxGadEpGEDFx (just an example, not available), + # please get your token from https://console.cloud.tencent.com/apm/monitor/access # Overwrite by env SRS_TENCENTCLOUD_APM_TOKEN token xxxxxxxx; # The APM endpoint. See https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace + # Please note that 4317 is for GRPC/HTTP2, while SRS only support HTTP and the port shoule be 55681. # Overwrite by env SRS_TENCENTCLOUD_APM_ENDPOINT endpoint ap-guangzhou.apm.tencentcs.com:55681; # The service.name of resource. diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 6c6b3eba2..fece95242 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -3468,6 +3468,25 @@ bool SrsConfig::get_tencentcloud_apm_enabled() return SRS_CONF_PERFER_FALSE(conf->arg0()); } +string SrsConfig::get_tencentcloud_apm_team() +{ + SRS_OVERWRITE_BY_ENV_STRING("SRS_TENCENTCLOUD_APM_TEAM"); + + static string DEFAULT = ""; + + SrsConfDirective* conf = root->get("tencentcloud_apm"); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("team"); + if (!conf) { + return DEFAULT; + } + + return conf->arg0(); +} + string SrsConfig::get_tencentcloud_apm_token() { SRS_OVERWRITE_BY_ENV_STRING("SRS_TENCENTCLOUD_APM_TOKEN"); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 1967500cf..378e90658 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -470,6 +470,7 @@ public: virtual std::string get_tencentcloud_cls_endpoint(); virtual std::string get_tencentcloud_cls_topic_id(); virtual bool get_tencentcloud_apm_enabled(); + virtual std::string get_tencentcloud_apm_team(); virtual std::string get_tencentcloud_apm_token(); virtual std::string get_tencentcloud_apm_endpoint(); virtual std::string get_tencentcloud_apm_service_name(); diff --git a/trunk/src/app/srs_app_tencentcloud.cpp b/trunk/src/app/srs_app_tencentcloud.cpp index d6b06bf23..52053089e 100644 --- a/trunk/src/app/srs_app_tencentcloud.cpp +++ b/trunk/src/app/srs_app_tencentcloud.cpp @@ -1962,7 +1962,7 @@ ISrsApmSpan* SrsApmSpan::set_kind(SrsApmKind kind) ISrsApmSpan* SrsApmSpan::as_child(ISrsApmSpan* parent) { - // Should be child of different parents. + // Should not be child of multiple parent spans. if (child_) return this; // For child, always load parent from context. @@ -2107,16 +2107,40 @@ srs_error_t SrsApmClient::initialize() return err; } + team_ = _srs_config->get_tencentcloud_apm_team(); token_ = _srs_config->get_tencentcloud_apm_token(); endpoint_ = _srs_config->get_tencentcloud_apm_endpoint(); service_name_ = _srs_config->get_tencentcloud_apm_service_name(); debug_logging_ = _srs_config->get_tencentcloud_apm_debug_logging(); - srs_trace("Initialize TencentCloud APM, token=%dB, endpoint=%s, service_name=%s, debug_logging=%d", token_.length(), endpoint_.c_str(), service_name_.c_str(), debug_logging_); + srs_trace("Initialize TencentCloud APM, team=%s, token=%dB, endpoint=%s, service_name=%s, debug_logging=%d", team_.c_str(), token_.length(), endpoint_.c_str(), service_name_.c_str(), debug_logging_); + + // Check authentication, the team or token. + if (team_.empty()) { + return srs_error_new(ERROR_APM_AUTH, "No authentication team for APM"); + } + if (token_.empty()) { + return srs_error_new(ERROR_APM_AUTH, "No authentication token for APM"); + } + + // Please note that 4317 is for GRPC/HTTP2, while SRS only support HTTP and the port shoule be 55681. + if (srs_string_contains(endpoint_, ":4317")) { + return srs_error_new(ERROR_APM_ENDPOINT, "Port 4317 is for GRPC over HTTP2 for APM"); + } return err; } srs_error_t SrsApmClient::report() +{ + srs_error_t err = do_report(); + if (err != srs_success) { + return srs_error_wrap(err, "team=%s, token=%dB", team_.c_str(), token_.length()); + } + + return err; +} + +srs_error_t SrsApmClient::do_report() { srs_error_t err = srs_success; @@ -2133,6 +2157,8 @@ srs_error_t SrsApmClient::report() rs->resource()->add_addr(SrsOtelAttribute::kv("service.name", service_name_)); // For Tencent Cloud APM authentication, see https://console.cloud.tencent.com/apm/monitor/access rs->resource()->add_addr(SrsOtelAttribute::kv("token", token_)); + // For Tencent Cloud APM debugging, see https://console.cloud.tencent.com/apm/monitor/team + rs->resource()->add_addr(SrsOtelAttribute::kv("tapm.team", team_)); SrsOtelScopeSpans* spans = rs->append(); spans->scope()->name_ = "srs"; @@ -2198,7 +2224,7 @@ srs_error_t SrsApmClient::report() if (debug_logging_) { string server_id = SrsStatistic::instance()->server_id(); - srs_trace("APM write logs=%d, size=%dB, server_id=%s", spans->size(), body.length(), server_id.c_str()); + srs_trace("APM write team=%s, token=%dB, logs=%d, size=%dB, server_id=%s", team_.c_str(), token_.length(), spans->size(), body.length(), server_id.c_str()); } return err; diff --git a/trunk/src/app/srs_app_tencentcloud.hpp b/trunk/src/app/srs_app_tencentcloud.hpp index 839060cd7..6f9f4c6e6 100644 --- a/trunk/src/app/srs_app_tencentcloud.hpp +++ b/trunk/src/app/srs_app_tencentcloud.hpp @@ -493,6 +493,7 @@ class SrsApmClient private: bool enabled_; uint64_t nn_spans_; + std::string team_; std::string token_; std::string endpoint_; std::string service_name_; @@ -504,6 +505,9 @@ public: public: srs_error_t initialize(); srs_error_t report(); +private: + srs_error_t do_report(); +public: bool enabled(); uint64_t nn_spans(); public: diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index 0491e12a7..43cec1645 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -97,7 +97,9 @@ 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") \ /**************************************************/ /* RTMP protocol error. */