Class: Datadog::Core::Remote::Component Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Configures the HTTP transport to communicate with the agent to fetch and sync the remote configuration.

Defined Under Namespace

Classes: Barrier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, capabilities, agent_settings, logger:) ⇒ Component

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Component.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/datadog/core/remote/component.rb', line 20

def initialize(settings, capabilities, agent_settings, logger:)
  @logger = logger
  @settings = settings
  @capabilities = capabilities
  @agent_settings = agent_settings

  negotiation = Negotiation.new(settings, agent_settings, logger: logger)
  @transport = Datadog::Core::Remote::Transport::HTTP.v7(agent_settings: agent_settings, logger: logger)

  @barrier = Barrier.new(settings.remote.boot_timeout_seconds)

  @client_mutex = Mutex.new
  @client = Client.new(@transport, @capabilities, settings: settings, logger: logger)
  @healthy = false
  logger.debug { "new remote configuration client: #{@client.id} products: #{@capabilities.products.sort.join(", ")}" }

  @worker = Worker.new(interval: settings.remote.poll_interval_seconds, logger: logger) do
    unless @healthy || negotiation.endpoint?("/v0.7/config")
      @barrier.lift

      next
    end

    begin
      client.sync
      @healthy ||= true
    rescue Client::SyncError => e
      # Transient errors due to network or agent. Logged the error but not via telemetry
      logger.error do
        "remote worker client sync error: #{e.class}: #{e.message} location: #{Array(e.backtrace).first}. skipping sync"
      end
    rescue => e
      # In case of unexpected errors, reset the negotiation object
      # given external conditions have changed and the negotiation
      # negotiation object stores error logging state that should be reset.
      negotiation = Negotiation.new(settings, agent_settings, logger: logger)

      # Transient errors due to network or agent. Logged the error but not via telemetry
      logger.error do
        "remote worker error: #{e.class}: #{e.message} location: #{Array(e.backtrace).first}. " \
        "resetting client state"
      end

      # client state is unknown, state might be corrupted
      new_client = @client_mutex.synchronize do
        @client = Client.new(@transport, @capabilities, settings: settings, logger: logger)
      end
      @healthy = false
      logger.debug { "new remote configuration client: #{new_client.id} products: #{@capabilities.products.sort.join(", ")}" }

      # TODO: bail out if too many errors?
    end

    @barrier.lift
  end
end

Instance Attribute Details

#healthy ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
# File 'lib/datadog/core/remote/component.rb', line 18

def healthy
  @healthy
end

#logger ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
# File 'lib/datadog/core/remote/component.rb', line 18

def logger
  @logger
end

#worker ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
# File 'lib/datadog/core/remote/component.rb', line 18

def worker
  @worker
end

Class Method Details

.build(settings, agent_settings, logger:, telemetry:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Because the agent might not be available yet, we can't perform agent-specific checks yet, as they would prevent remote configuration from ever running.

Those checks are instead performed inside the worker loop. This allows users to upgrade their agent while keeping their application running.



212
213
214
215
216
217
# File 'lib/datadog/core/remote/component.rb', line 212

def build(settings, agent_settings, logger:, telemetry:)
  return unless settings.remote.enabled

  capabilities = Client::Capabilities.new(settings, telemetry: telemetry)
  new(settings, capabilities, agent_settings, logger: logger)
end

Instance Method Details

#add_products(*products) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
# File 'lib/datadog/core/remote/component.rb', line 115

def add_products(*products)
  @capabilities.add_products(*products)
end

#after_fork ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recreates the remote configuration client after a fork. This ensures each forked process has a unique client ID and fresh state.



105
106
107
108
109
110
111
112
113
# File 'lib/datadog/core/remote/component.rb', line 105

def after_fork
  @worker.after_fork
  @barrier = Barrier.new(@settings.remote.boot_timeout_seconds)
  new_client = @client_mutex.synchronize do
    @client = Client.new(@transport, @capabilities, settings: @settings, logger: @logger)
  end
  @healthy = false
  logger.debug { "remote configuration client recreated after fork: #{new_client.id} products: #{@capabilities.products.sort.join(", ")}" }
end

#barrier(_kind) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If the worker is not initialized, initialize it.

Then, waits for one client sync to be executed if kind is :once.



94
95
96
97
# File 'lib/datadog/core/remote/component.rb', line 94

def barrier(_kind)
  start
  @barrier.wait_once
end

#client ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



87
88
89
# File 'lib/datadog/core/remote/component.rb', line 87

def client
  @client_mutex.synchronize { @client }
end

#register(capabilities:, products:, receivers:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



123
124
125
126
127
128
129
130
131
132
# File 'lib/datadog/core/remote/component.rb', line 123

def register(capabilities:, products:, receivers:)
  @client_mutex.synchronize do
    @capabilities.register_runtime(
      capabilities: capabilities,
      products: products,
      receivers: receivers,
    )
    @client.dispatcher.add_receivers(*receivers)
  end
end

#remove_products(*products) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



119
120
121
# File 'lib/datadog/core/remote/component.rb', line 119

def remove_products(*products)
  @capabilities.remove_products(*products)
end

#shutdown! ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
# File 'lib/datadog/core/remote/component.rb', line 99

def shutdown!
  @worker.stop
end

#start ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Starts the Remote Configuration worker without waiting for first run



78
79
80
# File 'lib/datadog/core/remote/component.rb', line 78

def start
  @worker.start
end

#started? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the Remote Configuration worker running?

Returns:

  • (Boolean)


83
84
85
# File 'lib/datadog/core/remote/component.rb', line 83

def started?
  @worker.started?
end