Class: Datadog::Core::Cloudwise::Client
- Inherits:
-
Object
- Object
- Datadog::Core::Cloudwise::Client
- Defined in:
- lib/datadog/core/cloudwise/client.rb
Overview
HTTP client for Cloudwise API calls rubocop:disable CustomCops/EnvUsageCop
Constant Summary collapse
- CODE_SUCCESS =
Response codes
1000- CODE_PROBE_SUSPENDED =
探针熔断
1001- CODE_PROBE_ACTIVE =
探针活跃/恢复
1002- CODE_LICENSE_INVALID =
License 校验失败
2001- DOCC_CODE_SUCCESS =
DOCC response codes
100000
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#agent_instance_id ⇒ Object
readonly
Returns the value of attribute agent_instance_id.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#integrated_mode ⇒ Object
readonly
Returns the value of attribute integrated_mode.
-
#license_key ⇒ Object
readonly
Returns the value of attribute license_key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#server_name ⇒ Object
readonly
Returns the value of attribute server_name.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_account_id ⇒ Object
readonly
Returns the value of attribute token_account_id.
-
#token_user_id ⇒ Object
readonly
Returns the value of attribute token_user_id.
Class Method Summary collapse
-
.from_settings(settings, logger: Datadog.logger) ⇒ Client
工厂方法:从 Datadog settings 创建 Client 使用 AgentSettingsResolver 来正确处理 SSL 配置.
Instance Method Summary collapse
-
#docc_fetch_operation ⇒ Object
DOCC 操作拉取接口 接口路径: api/ext/gaia/daemon/fetchExt/$agentInstanceId 调用频率: 每30秒一次.
-
#docc_heartbeat_ext(fusing: false, fusing_condition: nil, fusing_start_time: nil, fusing_end_time: nil) ⇒ Object
DOCC 心跳纳管接口 接口路径: api/ext/gaia/daemon/heartbeatExt 调用频率: 每30秒一次.
-
#docc_register_application ⇒ Object
DOCC 模式下的应用注册接口(使用 doop-agent-api 前缀) 在 DOCC 模式下也需要调用 DOOP 模式的应用注册逻辑 接口路径: /doop-agent-api/v2/app/create 调用频率: 每3分钟一次.
-
#docc_register_ext ⇒ Object
DOCC 注册纳管接口 接口路径: api/ext/gaia/daemon/registExt 调用频率: 每10分钟一次.
-
#docc_report_operation(results) ⇒ Object
DOCC 操作结果上报接口 接口路径: api/ext/gaia/daemon/report/$agentInstanceId 上报操作结果(支持批量上报).
-
#fetch_server_timestamp ⇒ Hash
获取服务器时间戳接口.
-
#generate_host_id ⇒ Object
生成 Host ID 接口(第一步).
-
#heartbeat ⇒ Object
心跳接口.
-
#initialize(base_url:, server_name:, license_key:, logger:, integrated_mode: true, token: nil, api_prefix: nil, api_prefix_mode: nil) ⇒ Client
constructor
A new instance of Client.
-
#register_application ⇒ Object
应用注册接口.
-
#register_host(host_ip) ⇒ Object
注册 Host 接口(第二步).
-
#report_fuse_state(fuse_state:, fuse_describe:) ⇒ Object
熔断状态上报接口.
-
#use_integrated_mode? ⇒ Boolean
Check if should use integrated mode (DOCC interfaces) 判断是否需要使用融合模式 token不为空且integrated_mode=true时启用.
-
#verify_license ⇒ Object
License 校验接口.
Constructor Details
#initialize(base_url:, server_name:, license_key:, logger:, integrated_mode: true, token: nil, api_prefix: nil, api_prefix_mode: nil) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/datadog/core/cloudwise/client.rb', line 26 def initialize(base_url:, server_name:, license_key:, logger:, integrated_mode: true, token: nil, api_prefix: nil, api_prefix_mode: nil) @base_url = normalize_url(base_url) @server_name = server_name @license_key = license_key @logger = logger @integrated_mode = integrated_mode @token = token @api_prefix = api_prefix # 自定义 API 路径前缀 @api_prefix_mode = api_prefix_mode # API 前缀模式(简化配置) @host_id = nil @account_id = nil @is_first_heartbeat = true # Parse token if provided parse_token if @token && !@token.empty? # Generate agent instance ID for DOCC @agent_instance_id = generate_agent_id(get_local_ip) if use_integrated_mode? end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def account_id @account_id end |
#agent_instance_id ⇒ Object (readonly)
Returns the value of attribute agent_instance_id.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def agent_instance_id @agent_instance_id end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def base_url @base_url end |
#integrated_mode ⇒ Object (readonly)
Returns the value of attribute integrated_mode.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def integrated_mode @integrated_mode end |
#license_key ⇒ Object (readonly)
Returns the value of attribute license_key.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def license_key @license_key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def logger @logger end |
#server_name ⇒ Object (readonly)
Returns the value of attribute server_name.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def server_name @server_name end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def token @token end |
#token_account_id ⇒ Object (readonly)
Returns the value of attribute token_account_id.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def token_account_id @token_account_id end |
#token_user_id ⇒ Object (readonly)
Returns the value of attribute token_user_id.
14 15 16 |
# File 'lib/datadog/core/cloudwise/client.rb', line 14 def token_user_id @token_user_id end |
Class Method Details
.from_settings(settings, logger: Datadog.logger) ⇒ Client
工厂方法:从 Datadog settings 创建 Client 使用 AgentSettingsResolver 来正确处理 SSL 配置
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/datadog/core/cloudwise/client.rb', line 52 def self.from_settings(settings, logger: Datadog.logger) require_relative '../configuration/agent_settings_resolver' # 使用 AgentSettingsResolver 解析 agent 配置(包括 SSL) agent_settings = Configuration::AgentSettingsResolver.call(settings, logger: logger) # 构建 base_url(自动处理 http/https) base_url = build_url_from_agent_settings(agent_settings) new( base_url: base_url, server_name: settings.service || 'unknown-service', license_key: settings.cloudwise.license_key, logger: logger, integrated_mode: settings.cloudwise.integrated_mode, token: settings.cloudwise.token, api_prefix: settings.cloudwise.api_prefix, api_prefix_mode: settings.cloudwise.api_prefix_mode ) end |
Instance Method Details
#docc_fetch_operation ⇒ Object
DOCC 操作拉取接口 接口路径: api/ext/gaia/daemon/fetchExt/$agentInstanceId 调用频率: 每30秒一次
346 347 348 |
# File 'lib/datadog/core/cloudwise/client.rb', line 346 def docc_fetch_operation post_docc("/api/ext/gaia/daemon/fetchExt/#{@agent_instance_id}", {}) end |
#docc_heartbeat_ext(fusing: false, fusing_condition: nil, fusing_start_time: nil, fusing_end_time: nil) ⇒ Object
DOCC 心跳纳管接口 接口路径: api/ext/gaia/daemon/heartbeatExt 调用频率: 每30秒一次
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/datadog/core/cloudwise/client.rb', line 326 def docc_heartbeat_ext(fusing: false, fusing_condition: nil, fusing_start_time: nil, fusing_end_time: nil) host_ip = get_local_ip data = { agent_id: 'rubyagent', agent_instance_id: generate_agent_id(host_ip), agent_pid: Process.pid, agent_run_user: get_run_user, fusing: fusing, fusing_condition: fusing_condition, fusing_start_time: fusing_start_time, fusing_end_time: fusing_end_time } post_docc('/api/ext/gaia/daemon/heartbeatExt', data) end |
#docc_register_application ⇒ Object
DOCC 模式下的应用注册接口(使用 doop-agent-api 前缀) 在 DOCC 模式下也需要调用 DOOP 模式的应用注册逻辑 接口路径: /doop-agent-api/v2/app/create 调用频率: 每3分钟一次
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/datadog/core/cloudwise/client.rb', line 252 def docc_register_application host_ip = get_local_ip post('/doop-agent-api/v2/app/create', { serviceType: 'RUBY', accountId: @token_account_id.to_i, hostId: 0, agentId: generate_agent_id(host_ip).to_i, appId: generate_app_id(server_name).to_i, sn: host_ip, sport: get_server_port, appName: server_name, userDirRaw: server_name, codeType: 1012, app_type: 1, vpc: '', env_tag: 'default', service_tag: 'default', version_tag: 'default', business_tag: '', sys: '', host_tag: '{}', tags: {}, service_instance_id: generate_service_instance_id(host_ip).to_i, system_uuid: '' }) end |
#docc_register_ext ⇒ Object
DOCC 注册纳管接口 接口路径: api/ext/gaia/daemon/registExt 调用频率: 每10分钟一次
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/datadog/core/cloudwise/client.rb', line 294 def docc_register_ext host_ip = get_local_ip data = { agent_instance_id: generate_agent_id(host_ip), agent_id: 'rubyagent', version: 'v' + Datadog::VERSION::STRING, agent_pid: Process.pid, agent_run_user: get_run_user, log_path: get_log_path, ip_address: host_ip, host_name: Socket.gethostname, system_uuid: get_system_uuid, os: get_os_name, arch: get_architecture, meta_info: { app_name: @server_name, service_path: get_process_path, sample: calculate_sample_rate, physical_ip: detect_container? ? get_host_real_ip : '', container_id: detect_container? ? get_container_id : '', sys: get_cloudwise_sys, env_tag: Datadog.configuration.env || 'default' } } post_docc('/api/ext/gaia/daemon/registExt', data) end |
#docc_report_operation(results) ⇒ Object
DOCC 操作结果上报接口 接口路径: api/ext/gaia/daemon/report/$agentInstanceId 上报操作结果(支持批量上报)
354 355 356 357 358 359 |
# File 'lib/datadog/core/cloudwise/client.rb', line 354 def docc_report_operation(results) # 如果传入的是数组,直接使用;否则包装成数组 data = results.is_a?(Array) ? results : [results] post_docc("/api/ext/gaia/daemon/report/#{@agent_instance_id}", data) end |
#fetch_server_timestamp ⇒ Hash
获取服务器时间戳接口
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/datadog/core/cloudwise/client.rb', line 363 def path = apply_api_prefix('/api/v70/timestamp') uri = URI.join(base_url, path) Cloudwise.log_debug { "Cloudwise: Fetching server timestamp from #{uri}" } http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') http.open_timeout = 5 http.read_timeout = 5 request = Net::HTTP::Get.new(uri.path) request['User-Agent'] = "Datadog-Ruby-Agent/#{Datadog::VERSION::STRING}" request['DD-Internal-Untraced-Request'] = '1' # 记录请求前的本地时间 local_before = (Time.now.to_f * 1000).to_i response = http.request(request) # 记录请求后的本地时间 local_after = (Time.now.to_f * 1000).to_i if response.code.to_i == 200 = response.body.to_s.strip.to_i # 使用请求前后的中间时间作为本地参考时间 = (local_before + local_after) / 2 # 计算时间偏移(服务器时间 - 本地时间) # 正值表示服务器时间比本地快,负值表示服务器时间比本地慢 offset_ms = - Cloudwise.log_debug do "Cloudwise: Server timestamp sync - server=#{server_timestamp}, local=#{local_timestamp}, offset=#{offset_ms}ms" end { success: true, server_timestamp: , local_timestamp: , offset_ms: offset_ms } else Cloudwise.log_warn { "Cloudwise: path: #{path} Failed to fetch server timestamp: #{response.code}" } {success: false, error: "HTTP #{response.code}"} end rescue => e Cloudwise.log_error { "Cloudwise: Error fetching server timestamp: #{e.message}" } {success: false, error: e.} end |
#generate_host_id ⇒ Object
生成 Host ID 接口(第一步)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/datadog/core/cloudwise/client.rb', line 84 def generate_host_id host_ip = get_local_ip response = post('/v2/app/generateHostId', { host_ip: host_ip, app_type: 1, license_key: license_key }) if response[:success] && response[:data] && response[:data]['hostId'] @host_id = response[:data]['hostId'] # 第二步:注册 Host 获取 account_id register_response = register_host(host_ip) if register_response[:success] && register_response[:data] && register_response[:data]['account_id'] @account_id = register_response[:data]['account_id'].to_i # 设置到环境变量 (转为字符串存储) # NOTE: 设置环境变量必须使用 ENV,DATADOG_ENV 只支持读取 ENV['CLOUDWISE_ACCOUNT_ID'] = @account_id.to_s ENV['CLOUDWISE_HOST_ID'] = @host_id.to_s ENV['CLOUDWISE_AGENT_ID'] = generate_agent_id(host_ip).to_s ENV['CLOUDWISE_HOST_NAME'] = safe_gethostname ENV['CLOUDWISE_INSTANCE_ID'] = generate_service_instance_id(host_ip).to_s Cloudwise.log_debug { "Cloudwise: Registered Host, account_id: #{@account_id}, host_id: #{@host_id}" } else error_msg = register_response[:error] || "code=#{register_response[:code]}" Cloudwise.log_error { "Cloudwise: Failed to register host: #{error_msg}" } end # 返回 register_host 的响应(包含 account_id 或错误信息) return register_response else error_msg = response[:error] || "No hostId in response" Cloudwise.log_error { "Cloudwise: Failed to generate host ID: #{error_msg}" } end response end |
#heartbeat ⇒ Object
心跳接口
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/datadog/core/cloudwise/client.rb', line 148 def heartbeat host_ip = get_local_ip host_name = safe_gethostname post('/api/v1/agent/heartbeat', { version: Datadog::VERSION::STRING, accountId: @account_id, hostId: @host_id, plugId: '1012', omniAgent: 0, imagePath: get_agent_path, ip: host_ip, agentId: generate_agent_id(host_ip), timestamp: , routingKey: 'HeartBeat', classPath: '', isFirst: @is_first_heartbeat ? 1 : 0, app_name: server_name, appId: generate_app_id(server_name), physical_sip: host_ip, process_path: get_process_path, hostName: host_name, isContainer: detect_container? ? 1 : 0, hostRealIp: detect_container? ? get_host_real_ip : host_ip, hostNameAlias: detect_container? ? get_host_alias : host_name, containerId: detect_container? ? get_container_id : '', sample: calculate_sample_rate, pid: Process.pid, identification: generate_identification, env: 'env', service: 'service', business: '', sys: '', hostTag: '{}', tags: {}, systemUuid: '', serviceInstanceId: generate_service_instance_id(host_ip) }).tap do @is_first_heartbeat = false if @is_first_heartbeat end end |
#register_application ⇒ Object
应用注册接口
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/datadog/core/cloudwise/client.rb', line 220 def register_application host_ip = get_local_ip post('/v2/app/create', { serviceType: 'RUBY', accountId: @account_id, hostId: @host_id.to_i, agentId: generate_agent_id(host_ip).to_i, appId: generate_app_id(server_name).to_i, sn: host_ip, sport: get_server_port, appName: server_name, userDirRaw: server_name, codeType: 1012, app_type: 1, vpc: '', env_tag: 'default', service_tag: 'default', version_tag: 'default', business_tag: '', sys: '', host_tag: '{}', tags: {}, service_instance_id: generate_service_instance_id(host_ip).to_i, system_uuid: '' }) end |
#register_host(host_ip) ⇒ Object
注册 Host 接口(第二步)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/datadog/core/cloudwise/client.rb', line 126 def register_host(host_ip) unless @host_id Cloudwise.log_error { 'Cloudwise: Cannot register host without host_id' } return {success: false, error: 'host_id not generated', code: nil} end # 获取系统信息 host_name = safe_gethostname sys_version = RUBY_VERSION post('/v2/app/registerHost', { host_ip: host_ip, license_key: license_key, host_id: @host_id, host_name: host_name, host_type: 1, cluster_name: '', sys_version: sys_version }) end |
#report_fuse_state(fuse_state:, fuse_describe:) ⇒ Object
熔断状态上报接口
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/datadog/core/cloudwise/client.rb', line 206 def report_fuse_state(fuse_state:, fuse_describe:) host_ip = get_local_ip post('/api/v1/agent/fuseHeart', { version: Datadog::VERSION::STRING, accountId: @account_id, agentId: generate_agent_id(host_ip), fuseState: fuse_state, fuseDescribe: fuse_describe, appId: generate_app_id(server_name) }) end |
#use_integrated_mode? ⇒ Boolean
Check if should use integrated mode (DOCC interfaces) 判断是否需要使用融合模式 token不为空且integrated_mode=true时启用
283 284 285 |
# File 'lib/datadog/core/cloudwise/client.rb', line 283 def use_integrated_mode? @integrated_mode && @token && !@token.empty? && @token_account_id && @token_user_id end |
#verify_license ⇒ Object
License 校验接口
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/datadog/core/cloudwise/client.rb', line 191 def verify_license host_ip = get_local_ip post('/v2/licence/verification', { account_id: @account_id.to_s, agent_id: generate_agent_id(host_ip), service_type: 'RUBY', version: Datadog::VERSION::STRING, env_tag: Datadog.configuration.env }) end |