Class: Datadog::Core::Remote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/remote/client.rb,
lib/datadog/core/remote/client/capabilities.rb

Overview

Client communicates with the agent and sync remote configuration

Defined Under Namespace

Classes: Capabilities, SyncError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, capabilities, repository: Configuration::Repository.new) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/core/remote/client.rb', line 17

def initialize(transport, capabilities, repository: Configuration::Repository.new)
  @transport = transport

  @repository = repository
  @id = SecureRandom.uuid
  @dispatcher = Dispatcher.new
  @capabilities = capabilities

  @capabilities.receivers.each do |receiver|
    dispatcher.receivers << receiver
  end
end

Instance Attribute Details

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



15
16
17
# File 'lib/datadog/core/remote/client.rb', line 15

def dispatcher
  @dispatcher
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/datadog/core/remote/client.rb', line 15

def id
  @id
end

#repositoryObject (readonly)

Returns the value of attribute repository.



15
16
17
# File 'lib/datadog/core/remote/client.rb', line 15

def repository
  @repository
end

#transportObject (readonly)

Returns the value of attribute transport.



15
16
17
# File 'lib/datadog/core/remote/client.rb', line 15

def transport
  @transport
end

Instance Method Details

#syncObject

rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/datadog/core/remote/client.rb', line 31

def sync
  # TODO: Skip sync if no capabilities are registered
  response = transport.send_config(payload)

  if response.ok?
    # when response is completely empty, do nothing as in: leave as is
    if response.empty?
      Datadog.logger.debug { 'remote: empty response => NOOP' }

      return
    end

    paths = response.client_configs.map do |path|
      Configuration::Path.parse(path)
    end

    targets = Configuration::TargetMap.parse(response.targets)

    contents = Configuration::ContentList.parse(response.target_files)

    # TODO: sometimes it can strangely be so that paths.empty?
    # TODO: sometimes it can strangely be so that targets.empty?

    changes = repository.transaction do |current, transaction|
      # paths to be removed: previously applied paths minus ingress paths
      (current.paths - paths).each { |p| transaction.delete(p) }

      # go through each ingress path
      paths.each do |path|
        # match target with path
        target = targets[path]

        # abort entirely if matching target not found
        raise SyncError, "no target for path '#{path}'" if target.nil?

        # new paths are not in previously applied paths
        new = !current.paths.include?(path)

        # updated paths are in previously applied paths
        # but the content hash changed
        changed = current.paths.include?(path) && !current.contents.find_content(path, target)

        # skip if unchanged
        same = !new && !changed

        next if same

        # match content with path and target
        content = contents.find_content(path, target)

        # abort entirely if matching content not found
        raise SyncError, "no valid content for target at path '#{path}'" if content.nil?

        # to be added or updated << config
        # TODO: metadata (hash, version, etc...)
        transaction.insert(path, target, content) if new
        transaction.update(path, target, content) if changed
      end

      # save backend opaque backend state
      transaction.set(opaque_backend_state: targets.opaque_backend_state)
      transaction.set(targets_version: targets.version)

      # upon transaction end, new list of applied config + metadata (add, change, remove) will be saved
      # TODO: also remove stale config (matching removed) from cache (client configs is exhaustive list of paths)
    end

    if changes.empty?
      Datadog.logger.debug { 'remote: no changes' }
    else
      dispatcher.dispatch(changes, repository)
    end
  else
    raise SyncError, "unexpected transport response: #{response.inspect}"
  end
end