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
}.freeze

Class Method Summary collapse

Class Method Details

.build_clientObject



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

def self.build_client
  logger = Logging.logger[self]
  begin
    config_file = config_path(logger)
    config = load_config(config_file, logger)
  rescue ArgumentError
    config = { 'disabled' => true }
  end

  if config['disabled'] || ENV['BOLT_DISABLE_ANALYTICS']
    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
  logger.debug "Failed to initialize analytics client, analytics will be disabled: #{e}"
  NoopClient.new
end

.config_path(logger) ⇒ Object



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

def self.config_path(logger)
  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}'."
      logger.warn(message)
    end

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

.load_config(filename, logger) ⇒ Object



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

def self.load_config(filename, logger)
  if File.exist?(filename)
    YAML.load_file(filename)
  else
    unless ENV['BOLT_DISABLE_ANALYTICS']
      logger.warn <<~ANALYTICS
        Bolt collects data about how you use it. You can opt out of providing this data.

        To disable analytics data collection, add this line to ~/.puppetlabs/etc/bolt/analytics.yaml :
          disabled: true

        Read more about what data Bolt collects and why here:
          https://puppet.com/docs/bolt/latest/bolt_installing.html#analytics-data-collection
        ANALYTICS
    end

    {}
  end
end

.write_config(filename, config) ⇒ Object



93
94
95
96
# File 'lib/bolt/analytics.rb', line 93

def self.write_config(filename, config)
  FileUtils.mkdir_p(File.dirname(filename))
  File.write(filename, config.to_yaml)
end