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, TransportError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

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.



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

def dispatcher
  @dispatcher
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#repositoryObject (readonly)

Returns the value of attribute repository.



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

def repository
  @repository
end

#transportObject (readonly)

Returns the value of attribute transport.



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

def transport
  @transport
end

Instance Method Details

#syncObject

rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/MethodLength,Metrics/CyclomaticComplexity



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
107
108
109
110
111
112
113
114
# File 'lib/datadog/core/remote/client.rb', line 32

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

    begin
      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)
    rescue Remote::Configuration::Path::ParseError => e
      raise SyncError, e.message
    end

    # To make sure steep does not complain
    return unless paths && targets && contents

    # 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
  elsif response.internal_error?
    raise TransportError, response.to_s
  end
end