Class: Dropsonde::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde/metrics.rb

Overview

metrics class

Defined Under Namespace

Classes: Dependencies, Environments, Modules, Platforms, Puppetfiles

Instance Method Summary collapse

Constructor Details

#initializeMetrics

Returns a new instance of Metrics.



10
11
12
13
14
15
16
17
18
19
# File 'lib/dropsonde/metrics.rb', line 10

def initialize
  if Dropsonde.settings[:enable]
    Dropsonde.settings[:disable] ||= []
    disable = Dropsonde::Metrics.plugins.keys - Dropsonde.settings[:enable].map(&:to_sym)
    Dropsonde.settings[:disable].concat disable
  end

  Dropsonde::Metrics.disregard_plugins(*Dropsonde.settings[:disable])
  Dropsonde::Metrics.initialize_plugins
end

Instance Method Details

#check_for_duplicates(schema) ⇒ Object



162
163
164
165
166
167
# File 'lib/dropsonde/metrics.rb', line 162

def check_for_duplicates(schema)
  keys  = schema.map { |col| col[:name] }
  dupes = keys.select { |e| keys.count(e) > 1 }.uniq

  raise "The schema defines duplicate keys: #{dupes}" unless dupes.empty?
end

#exampleObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dropsonde/metrics.rb', line 110

def example
  require 'ipaddr'
  results = skeleton_report
  results[:message_id] = generate_guid
  results[:timestamp]  = rand((Time.now - 60 * 60 * 24 * 365)..Time.now).utc
  results[:ip]         = IPAddr.new(rand(2**32), Socket::AF_INET)
  results.delete(:'self-service-analytics')

  Dropsonde::Metrics.plugins.each do |_name, plugin|
    sanity_check_data(plugin, plugin.example).each do |row|
      results.merge!(row)
    end
  end

  results
end

#generate_guidObject



221
222
223
# File 'lib/dropsonde/metrics.rb', line 221

def generate_guid
  SecureRandom.uuid
end

#listObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dropsonde/metrics.rb', line 37

def list
  str = "                    Loaded telemetry plugins\n"
  str += "                 ===============================\n\n"
  Dropsonde::Metrics.plugins.each do |name, plugin|
    str += name.to_s
    str += "\n--------\n"
    str += plugin.description.strip
    str += "\n\n"
  end
  if Dropsonde.settings[:disable]
    str += "Disabled plugins:\n"
    str += "  #{Dropsonde.settings[:disable].join(', ')}"
  end
  str
end

#preview(puppetdb_session = nil) ⇒ Object



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
# File 'lib/dropsonde/metrics.rb', line 62

def preview(puppetdb_session = nil)
  str = "                      Puppet Telemetry Report Preview\n"
  str += "                      ===============================\n\n"
  Dropsonde::Metrics.plugins.each do |_name, plugin|
    schema = plugin.schema

    plugin.setup if plugin.respond_to? :setup
    data = sanity_check_data(plugin, plugin.run(puppetdb_session))
    plugin.cleanup if plugin.respond_to? :cleanup

    str += "#{plugin.name}\n"
    str += "-------------------------------\n"
    str += plugin.description
    data.each do |row|
      key    = row.keys.first
      values = row.values.flatten

      desc = schema.find { |item| item[:name].to_sym == key.to_sym }[:description]
      str += "- #{key}: #{desc}\n"
      values.each do |item|
        str += "    #{item}\n"
      end
    end
    str += "\n\n"
  end
  str += "Site ID:\n"
  str += siteid
  str
end

#report(puppetdb_session = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dropsonde/metrics.rb', line 92

def report(puppetdb_session = nil)
  snapshots = {}
  Dropsonde::Metrics.plugins.each do |_name, plugin|
    plugin.setup
    sanity_check_data(plugin, plugin.run(puppetdb_session)).each do |row|
      snapshots[row.keys.first] = {
        'value' => row.values.first,
        'timestamp' => Time.now.iso8601,
      }
    end
    plugin.cleanup
  end

  results = skeleton_report
  results[:'self-service-analytics'][:snapshots] = snapshots
  results
end

#sanity_check_data(plugin, data) ⇒ Object

We accept both the plugin and data gathered from the plugin so that we can sanitize both data and example data



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dropsonde/metrics.rb', line 129

def sanity_check_data(plugin, data)
  # This allows plugin authors to easily skip metrics with no results
  return [] if data.nil?

  keys_data   = data.map { |item| item.keys }.flatten.map(&:to_s)
  keys_schema = plugin.schema.map { |item| item[:name] }

  disallowed = (keys_data - keys_schema)

  raise "ERROR: The #{plugin.name} plugin exported the following keys not documented in the schema: #{disallowed}" unless disallowed.empty?

  data
end

#sanity_check_schema(plugin) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dropsonde/metrics.rb', line 143

def sanity_check_schema(plugin)
  schema = plugin.schema

  if (schema.class != Array) || schema.find { |item| item.class != Hash }
    raise "The #{plugin.name} plugin schema is not an array of hashes"
  end

  error = ''
  [:name, :type, :description].each do |field|
    count = schema.reject { |item| item[field] }.count
    next if count.zero?

    error += "The #{plugin.name} plugin schema has #{count} missing #{field}s\n"
  end
  raise error unless error.empty?

  schema
end

#schemaObject



53
54
55
56
57
58
59
60
# File 'lib/dropsonde/metrics.rb', line 53

def schema
  schema = skeleton_schema
  Dropsonde::Metrics.plugins.each do |_name, plugin|
    schema.concat(sanity_check_schema(plugin))
  end
  check_for_duplicates(schema)
  schema
end

#siteidObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dropsonde/metrics.rb', line 21

def siteid
  return @siteid if @siteid

  @siteid = Dropsonde.settings[:siteid]

  unless @siteid
    sha2 = Digest::SHA512.new
    sha2.update Puppet.settings[:certname]
    sha2.update Puppet.settings[:cacert]
    sha2.update Dropsonde.settings[:seed] if Dropsonde.settings[:seed]
    @siteid = sha2.hexdigest
  end

  @siteid
end

#skeleton_reportObject



210
211
212
213
214
215
216
217
218
219
# File 'lib/dropsonde/metrics.rb', line 210

def skeleton_report
  {
    "product": 'popularity-module',
    "version": '1.0.0',
    "site_id": siteid,
    "self-service-analytics": {
      "snapshots": {},
    },
  }
end

#skeleton_schemaObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dropsonde/metrics.rb', line 169

def skeleton_schema
  [
    {
      "description": "An ID that's unique for each checkin to Dujour.",
      "mode": 'NULLABLE',
      "name": 'message_id',
      "type": 'STRING',
    },
    {
      "description": 'A unique identifier for a site, derived as a hash of the CA certificate and optional seed.',
      "mode": 'NULLABLE',
      "name": 'site_id',
      "type": 'BYTES',
    },
    {
      "description": 'The name of the product.',
      "mode": 'NULLABLE',
      "name": 'product',
      "type": 'STRING',
    },
    {
      "description": 'Version of the project.',
      "mode": 'NULLABLE',
      "name": 'version',
      "type": 'STRING',
    },
    {
      "description": 'Time the checkin to Dujour occurred.',
      "mode": 'NULLABLE',
      "name": 'timestamp',
      "type": 'TIMESTAMP',
    },
    {
      "description": 'IP Address of node checking in to Dujour.',
      "mode": 'NULLABLE',
      "name": 'ip',
      "type": 'STRING',
    },
  ]
end