Class: Instana::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/instana/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/instana/collector.rb', line 6

def initialize
  @collectors = []

  # Snapshot data is collected once per process but resent
  # every 10 minutes along side process metrics.
  @snapshot = ::Instana::Util.take_snapshot

  # Set last snapshot to just under 10 minutes ago
  # so we send a snapshot sooner than later
  @last_snapshot = Time.now - 570

  # We track what we last sent as a metric payload so that
  # we can do delta reporting
  @last_values = {}
end

Instance Attribute Details

#collectorsObject

Returns the value of attribute collectors.



3
4
5
# File 'lib/instana/collector.rb', line 3

def collectors
  @collectors
end

#last_report_logObject

Returns the value of attribute last_report_log.



4
5
6
# File 'lib/instana/collector.rb', line 4

def last_report_log
  @last_report_log
end

Instance Method Details

#collect_and_reportObject

collect_and_report

Run through each collector, let them collect up data and then report what we have via the agent

Returns:

  • Boolean true on success



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
# File 'lib/instana/collector.rb', line 47

def collect_and_report
  return unless ::Instana.config[:metrics][:enabled]

  payload = {}
  with_snapshot = false

  # Run through the registered collectors and
  # get all the metrics
  #
  @collectors.each do |c|
    metrics = c.collect
    if metrics
      payload[c.payload_key] = metrics
    else
      payload.delete(c.payload_key)
    end
  end

  # Every 5 minutes, send snapshot data as well
  if (Time.now - @last_snapshot) > 600
    with_snapshot = true
    payload.merge!(@snapshot)

    # Add in process related that could have changed since
    # snapshot was taken.
    p = { :pid => ::Instana.agent.report_pid }
    p[:name] = ::Instana::Util.get_app_name
    p[:exec_args] = ::Instana.agent.process[:arguments]
    payload.merge!(p)
  else
    payload = enforce_deltas(payload, @last_values)
  end

  if ENV.key?('INSTANA_TEST')
    true
  else
    # Report all the collected goodies
    if ::Instana.agent.report_metrics(payload) && with_snapshot
      @last_snapshot = Time.now
    end
  end
end

#enforce_deltas(candidate, last) ⇒ Hash

Take two hashes and enforce delta reporting. We only report when values change (instead of reporting all of the time). This is a recursive method.

Parameters:

  • the (Hash)

    payload have delta reporting applied to

  • a (Hash)

    hash of the last values reported

Returns:

  • (Hash)

    the candidate hash with delta reporting applied



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/instana/collector.rb', line 99

def enforce_deltas(candidate, last)
  candidate.each do |k,v|
    if v.is_a?(Hash)
      last[k] ||= {}
      candidate[k] = enforce_deltas(candidate[k], last[k])
      candidate.delete(k) if candidate[k].empty?
    else
      if last[k] == v
        candidate.delete(k)
      else
        last[k] = candidate[k]
      end
    end
  end
  candidate
end

#register(klass) ⇒ Object

Register an individual collector.

Parameters:

  • klass (Object)

    of the collector to register



26
27
28
29
# File 'lib/instana/collector.rb', line 26

def register(klass)
  ::Instana.logger.debug "Adding #{klass} to collectors..."
  @collectors << klass.new
end

#reset_snapshot_timer!Object

Resets the timer on when to send process snapshot data.



33
34
35
36
37
# File 'lib/instana/collector.rb', line 33

def reset_snapshot_timer!
  # Set last snapshot to 10 minutes ago
  # so that we send a snapshot on first report
  @last_snapshot = Time.now - 601
end