Module: Bolt::Analytics

Defined in:
lib/bolt/analytics.rb

Defined Under Namespace

Classes: Client, NoopClient

Constant Summary collapse

PROTOCOL_VERSION =
1
APPLICATION_NAME =
'bolt'
TRACKING_ID =
'UA-120367942-1'
TRACKING_URL =
'https://google-analytics.com/collect'
CUSTOM_DIMENSIONS =
{
  operating_system: :cd1,
  inventory_nodes: :cd2,
  inventory_groups: :cd3,
  target_nodes: :cd4,
  output_format: :cd5,
  statement_count: :cd6,
  resource_mean: :cd7,
  plan_steps: :cd8,
  return_type: :cd9,
  inventory_version: :cd10,
  boltdir_type: :cd11,
  puppet_plan_count: :cd12,
  yaml_plan_count: :cd13
}.freeze

Class Method Summary collapse

Class Method Details

.build_client(enabled = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bolt/analytics.rb', line 32

def self.build_client(enabled = true)
  begin
    config_file = config_path
    config = enabled ? load_config(config_file) : {}
  rescue ArgumentError
    config = { 'disabled' => true }
  end

  if !enabled || config['disabled'] || ENV['BOLT_DISABLE_ANALYTICS']
    Bolt::Logger.debug "Analytics opt-out is set, analytics will be disabled"
    NoopClient.new
  else
    unless config.key?('user-id')
      config['user-id'] = SecureRandom.uuid
      write_config(config_file, config)
    end

    Client.new(config['user-id'])
  end
rescue StandardError => e
  Bolt::Logger.debug "Failed to initialize analytics client, analytics will be disabled: #{e}"
  NoopClient.new
end

.config_pathObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bolt/analytics.rb', line 56

def self.config_path
  path     = File.expand_path(File.join('~', '.puppetlabs', 'etc', 'bolt', 'analytics.yaml'))
  old_path = File.expand_path(File.join('~', '.puppetlabs', 'bolt', 'analytics.yaml'))

  if File.exist?(path)
    if File.exist?(old_path)
      message = "Detected analytics configuration files at '#{old_path}' and '#{path}'. Loading "\
                "analytics configuration from '#{path}'."
      Bolt::Logger.warn_once('duplicate_analytics', message)
    end

    path
  elsif File.exist?(old_path)
    old_path
  else
    path
  end
end

.load_config(filename) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bolt/analytics.rb', line 75

def self.load_config(filename)
  if File.exist?(filename)
    Bolt::Util.read_optional_yaml_hash(filename, 'analytics')
  else
    unless ENV['BOLT_DISABLE_ANALYTICS']
      msg = <<~ANALYTICS
        Bolt collects data about how you use it. You can opt out of providing this data.
        To learn how to disable data collection, or see what data Bolt collects and why,
        see http://pup.pt/bolt-analytics
      ANALYTICS
      Bolt::Logger.warn_once('analytics_opt_out', msg)
    end

    {}
  end
end

.write_config(filename, config) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/bolt/analytics.rb', line 92

def self.write_config(filename, config)
  FileUtils.mkdir_p(File.dirname(filename))
  File.write(filename, config.to_yaml)
rescue StandardError => e
  Bolt::Logger.warn_once('unwriteable_file', "Could not write analytics configuration to #{filename}.")
  # This will get caught by build_client and create a NoopClient
  raise e
end