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

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

Overview

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) ⇒ Component

Returns a new instance of Component.



18
19
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
# File 'lib/datadog/core/remote/component.rb', line 18

def initialize(settings, capabilities, agent_settings)
  transport_options = {}
  transport_options[:agent_settings] = agent_settings if agent_settings

  negotiation = Negotiation.new(settings, agent_settings)
  transport_v7 = Datadog::Core::Remote::Transport::HTTP.v7(**transport_options.dup)

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

  @client = Client.new(transport_v7, capabilities)
  @healthy = false
  Datadog.logger.debug { "new remote configuration client: #{@client.id}" }

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

      next
    end

    begin
      @client.sync
      @healthy ||= true
    rescue Client::SyncError => e
      Datadog.logger.error do
        "remote worker client sync error: #{e.message} location: #{Array(e.backtrace).first}. skipping sync"
      end
    rescue StandardError => 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)

      Datadog.logger.error do
        "remote worker error: #{e.class.name} #{e.message} location: #{Array(e.backtrace).first}. "\
        'reseting client state'
      end

      # client state is unknown, state might be corrupted
      @client = Client.new(transport_v7, capabilities)
      @healthy = false
      Datadog.logger.debug { "new remote configuration client: #{@client.id}" }

      # TODO: bail out if too many errors?
    end

    @barrier.lift
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/datadog/core/remote/component.rb', line 16

def client
  @client
end

#healthyObject (readonly)

Returns the value of attribute healthy.



16
17
18
# File 'lib/datadog/core/remote/component.rb', line 16

def healthy
  @healthy
end

Class Method Details

.build(settings, agent_settings) ⇒ Object

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.



153
154
155
156
157
# File 'lib/datadog/core/remote/component.rb', line 153

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

  new(settings, Client::Capabilities.new(settings), agent_settings)
end

Instance Method Details

#barrier(_kind) ⇒ Object

If the worker is not initialized, initialize it.

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



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

def barrier(_kind)
  start
  @barrier.wait_once
end

#shutdown!Object



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

def shutdown!
  @worker.stop
end

#startObject

Starts the Remote Configuration worker without waiting for first run



69
70
71
# File 'lib/datadog/core/remote/component.rb', line 69

def start
  @worker.start
end

#started?Boolean

Is the Remote Configuration worker running?

Returns:

  • (Boolean)


74
75
76
# File 'lib/datadog/core/remote/component.rb', line 74

def started?
  @worker.started?
end