Class: Datadog::Core::Cloudwise::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/cloudwise/component.rb

Overview

Main component that manages Cloudwise workers with proper initialization order

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings:, logger:) ⇒ Component



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/datadog/core/cloudwise/component.rb', line 66

def initialize(settings:, logger:)
  @logger = logger
  @enabled = settings.cloudwise.enabled

  return unless @enabled

  # 使用工厂方法创建 Client,自动处理 SSL 配置
  # AgentSettingsResolver 会根据以下优先级解析配置:
  # 1. c.agent.use_ssl(代码配置)
  # 2. DD_TRACE_AGENT_URL 环境变量的 scheme(https:// 或 http://)
  # 3. 默认值(http)
  @client = Client.from_settings(settings, logger: logger)

  # Probe state manager (shared across workers)
  @probe_state = ProbeState.new(logger: logger)

  # 判断是否使用融合模式(DOCC)
  if @client.use_integrated_mode?
    Cloudwise.log_info { 'Cloudwise: Initializing in DOCC integrated mode' }
    @probe_state.enable_docc_mode!
    initialize_docc_workers(settings)
  else
    Cloudwise.log_info { 'Cloudwise: Initializing in traditional mode' }
    initialize_traditional_workers(settings)
  end
end

Class Attribute Details

.initialization_mutexObject

Returns the value of attribute initialization_mutex.



63
64
65
# File 'lib/datadog/core/cloudwise/component.rb', line 63

def initialization_mutex
  @initialization_mutex
end

.initialization_startedObject

Returns the value of attribute initialization_started.



63
64
65
# File 'lib/datadog/core/cloudwise/component.rb', line 63

def initialization_started
  @initialization_started
end

.singleton_instanceObject

Returns the value of attribute singleton_instance.



63
64
65
# File 'lib/datadog/core/cloudwise/component.rb', line 63

def singleton_instance
  @singleton_instance
end

Instance Attribute Details

#app_registration_workerObject (readonly)

Returns the value of attribute app_registration_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def app_registration_worker
  @app_registration_worker
end

#clientObject (readonly)

Returns the value of attribute client.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def client
  @client
end

#docc_app_registration_workerObject (readonly)

Returns the value of attribute docc_app_registration_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def docc_app_registration_worker
  @docc_app_registration_worker
end

#docc_heartbeat_workerObject (readonly)

Returns the value of attribute docc_heartbeat_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def docc_heartbeat_worker
  @docc_heartbeat_worker
end

#docc_operation_workerObject (readonly)

Returns the value of attribute docc_operation_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def docc_operation_worker
  @docc_operation_worker
end

#docc_registration_workerObject (readonly)

Returns the value of attribute docc_registration_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def docc_registration_worker
  @docc_registration_worker
end

#heartbeat_workerObject (readonly)

Returns the value of attribute heartbeat_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def heartbeat_worker
  @heartbeat_worker
end

#host_id_workerObject (readonly)

Returns the value of attribute host_id_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def host_id_worker
  @host_id_worker
end

#license_workerObject (readonly)

Returns the value of attribute license_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def license_worker
  @license_worker
end

#loggerObject (readonly)

Returns the value of attribute logger.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def logger
  @logger
end

#probe_stateObject (readonly)

Returns the value of attribute probe_state.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def probe_state
  @probe_state
end

#time_sync_workerObject (readonly)

Returns the value of attribute time_sync_worker.



51
52
53
# File 'lib/datadog/core/cloudwise/component.rb', line 51

def time_sync_worker
  @time_sync_worker
end

Instance Method Details

#enabled?Boolean



209
210
211
# File 'lib/datadog/core/cloudwise/component.rb', line 209

def enabled?
  @enabled
end

#initialize_async(&block) ⇒ Object

Initialize Cloudwise asynchronously This will NOT block application startup Host ID generation runs in background and retries until success Cloudwise and Datadog functionality will NOT work until Host ID is ready Application can still serve requests normally



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/datadog/core/cloudwise/component.rb', line 101

def initialize_async(&block)
  return true unless @enabled

  # 使用类级别的锁确保全局只初始化一次
  cloudwise_already_started = false
  self.class.initialization_mutex.synchronize do
    if self.class.initialization_started
      cloudwise_already_started = true
      Cloudwise.log_debug { 'Cloudwise Component already initialized globally' }
    elsif self.class.singleton_instance && instance_already_started?
      # 检查是否有已存在的单例实例
      cloudwise_already_started = true
      Cloudwise.log_debug { 'Initializing Datadog components for this Components instance...' }
    else
      self.class.initialization_started = true
      self.class.singleton_instance = self
    end
  end

  # 如果 Cloudwise 已经启动,只需要为这个新的 Components 实例初始化 Datadog 组件
  if cloudwise_already_started
    block&.call  # 立即初始化这个实例的 Datadog 组件
    return true
  end

  # 立即初始化 Datadog 组件(不等待 Cloudwise 验证)
  # NOTE: 这里不能使用 block&.call 因为需要在调用前后执行日志
  if block # rubocop:disable Style/SafeNavigation
    Cloudwise.log_debug { 'Initializing Datadog components immediately...' }
    block.call
    Cloudwise.log_debug { 'Datadog components initialized (data collection pending ProbeState)' }
  end

  # 根据模式启动不同的 workers
  if @client.use_integrated_mode?
    start_docc_workers_when_ready
  else
    # Start Host ID Worker in background (will retry until success)
    Cloudwise.log_debug { 'Starting Host ID generation worker (async, infinite retry)...' }
    @host_id_worker.perform

    # Start other Cloudwise workers in background
    start_cloudwise_workers_when_ready
  end

  Cloudwise.log_debug { 'Cloudwise Component initialization started (async)' }

  true
end

#probe_active?Boolean

Check if probe is active (not suspended)



214
215
216
217
218
# File 'lib/datadog/core/cloudwise/component.rb', line 214

def probe_active?
  return true unless @enabled

  probe_state.active?
end

#startObject

Legacy async start method (for backward compatibility)



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/datadog/core/cloudwise/component.rb', line 169

def start
  return unless @enabled

  Cloudwise.log_debug { 'Starting Cloudwise Component (Async)' }

  # Step 1: Start Host ID Worker (will retry every 30s until success)
  Cloudwise.log_debug { ' Step 1: Starting Host ID generation worker (30s retry)...' }
  @host_id_worker.start

  # Wait a bit to see if Host ID is generated immediately
  sleep(1)

  # Step 2: Start other workers
  start_background_workers

  Cloudwise.log_debug { 'Cloudwise Component started successfully' }
end

#start_background_workersObject

Start background workers (async) - called after Host ID is ready



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/datadog/core/cloudwise/component.rb', line 152

def start_background_workers
  return unless @enabled

  # Start Heartbeat Worker
  Cloudwise.log_debug { 'Starting Heartbeat worker (60s interval)...' }
  @heartbeat_worker.start

  # Start Application Registration Worker
  Cloudwise.log_debug { 'Starting Application registration worker (3 min interval)...' }
  @app_registration_worker.start

  # Start License Worker (only in traditional mode)
  Cloudwise.log_debug { 'Starting License verification worker (5 min interval)...' }
  @license_worker.start
end

#statusObject

Get component status



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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/datadog/core/cloudwise/component.rb', line 221

def status
  return {enabled: false} unless @enabled

  probe_status = probe_state.status
  # 获取时间同步状态
  time_sync_status = if defined?(Datadog::Core::Cloudwise::TimeSyncWorker)
    Datadog::Core::Cloudwise::TimeSyncWorker.status
  else
    {enabled: false}
  end

  base_status = {
    enabled: true,
    integrated_mode: @client.use_integrated_mode?,
    can_collect_data: probe_status[:can_collect_data],
    probe_active: probe_state.active?,
    probe_suspended: probe_state.suspended?,
    license_valid: @client.use_integrated_mode? ? true : probe_status[:license_valid],
    license_running: @client.use_integrated_mode? ? false : license_worker&.running?,
    time_sync_enabled: time_sync_status[:enabled],
    time_sync_offset_ms: time_sync_status[:offset_ms],
    time_sync_running: time_sync_worker&.running?
  }

  if @client.use_integrated_mode?
    base_status.merge(
      docc_registered: probe_status[:docc_registered],
      docc_heartbeat_active: probe_status[:docc_heartbeat_active],
      docc_operation_active: probe_status[:docc_operation_active],
      docc_heartbeat_running: docc_heartbeat_worker&.running?,
      docc_registration_running: docc_registration_worker&.running?,
      docc_app_registered: probe_status[:app_registered],
      docc_app_registration_running: docc_app_registration_worker&.running?,
      docc_operation_running: docc_operation_worker&.running?
    )
  else
    base_status.merge(
      account_id: client.,
      host_id_generated: host_id_worker&.host_id_generated?,
      host_id_ready: probe_status[:host_id_ready],
      heartbeat_active: probe_status[:heartbeat_active],
      app_registered: probe_status[:app_registered],
      heartbeat_running: heartbeat_worker&.running?,
      app_registration_running: app_registration_worker&.running?
    )
  end
end

#stopObject

Stop all workers



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/datadog/core/cloudwise/component.rb', line 188

def stop
  return unless @enabled

  # 停止时间同步 worker(两种模式都需要)
  @time_sync_worker&.stop(true)

  if @client.use_integrated_mode?
    @docc_heartbeat_worker&.stop(true)
    @docc_registration_worker&.stop(true)
    @docc_app_registration_worker&.stop(true)
    @docc_operation_worker&.stop(true)
    Cloudwise.log_debug { 'Cloudwise DOCC component stopped' }
  else
    @host_id_worker&.stop(true)
    @heartbeat_worker&.stop(true)
    @license_worker&.stop(true)
    @app_registration_worker&.stop(true)
    Cloudwise.log_debug { 'Cloudwise component stopped' }
  end
end