Class: FastlaneCore::ToolCollector

Inherits:
Object
  • Object
show all
Defined in:
fastlane_core/lib/fastlane_core/tool_collector.rb

Direct Known Subclasses

Fastlane::ActionCollector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeToolCollector

Returns a new instance of ToolCollector.



25
26
27
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 25

def initialize
  @crash = false
end

Instance Attribute Details

#crashObject (readonly)

This is the newer field for tracking only uncontrolled exceptions.

This is written to only when ‘did_crash` is called, and therefore excludes controlled exceptions.

This value is a boolean, which is true if the error was an uncontrolled exception



23
24
25
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 23

def crash
  @crash
end

#errorObject (readonly)

This is the original error reporting mechanism, which has always represented either controlled (UI.user_error!), or uncontrolled (UI.crash!, anything else) exceptions.

Thus, if you call ‘did_crash`, it will record the failure both here, and in the newer, more specific `crash` field.

This value is a String, which is the name of the tool that caused the error



15
16
17
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 15

def error
  @error
end

Class Method Details

.determine_version(name) ⇒ Object



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
209
210
211
212
213
214
215
216
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 180

def self.determine_version(name)
  unless name.to_s.start_with?("fastlane-plugin")
    # In the early days before the mono gem this was more complicated
    # Now we have a mono version number, which makes this method easy
    # for all built-in actions and tools
    require 'fastlane/version'
    return Fastlane::VERSION
  end

  # For plugins we still need to load the specific version
  begin
    name = name.to_s.downcase

    # We need to pre-load the version file because tools that are invoked through their actions
    # will not yet have run their action, and thus will not yet have loaded the file which defines
    # the module and constant we need.
    require File.join(name, "version")

    # Go from :foo_bar to 'FooBar'
    module_name = name.fastlane_module

    # Look up the VERSION constant defined for the given tool name,
    # or return 'unknown' if we can't find it where we'd expect
    if Kernel.const_defined?(module_name)
      tool_module = Kernel.const_get(module_name)

      if tool_module.const_defined?('VERSION')
        return tool_module.const_get('VERSION')
      end
    end
  rescue LoadError
    # If there is no version file to load, this is not a tool for which
    # we can report a particular version
  end

  return nil
end

Instance Method Details

#create_analytic_event_bodyObject



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
115
116
117
118
119
120
121
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 89

def create_analytic_event_body
  analytics = []
  timestamp_seconds = Time.now.to_i

  # `fastfile_id` helps us track success/failure metrics for Fastfiles we
  # generate as part of an automated process.
  fastfile_id = ENV["GENERATED_FASTFILE_ID"]

  if fastfile_id && launches.size == 1 && launches['fastlane']
    if crash
      completion_status = 'crash'
    elsif error
      completion_status = 'error'
    else
      completion_status = 'success'
    end
    analytics << event_for_web_onboarding(fastfile_id, completion_status, timestamp_seconds)
  end

  launches.each do |action, count|
    action_version = versions[action] || 'unknown'
    if crash && error == action
      action_completion_status = 'crash'
    elsif action == error
      action_completion_status = 'error'
    else
      action_completion_status = 'success'
    end
    analytics << event_for_completion(action, action_completion_status, action_version, timestamp_seconds)
    analytics << event_for_count(action, count, action_version, timestamp_seconds)
  end
  { analytics: analytics }.to_json
end

#determine_version(name) ⇒ Object



176
177
178
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 176

def determine_version(name)
  self.class.determine_version(name)
end

#did_crash(name) ⇒ Object

Call when the problem is an uncaught/uncontrolled exception (e.g. via UI.crash!)



48
49
50
51
52
53
54
55
56
57
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 48

def did_crash(name)
  name = name_to_track(name.to_sym)
  return unless name

  # Write to the @error field to maintain the historical behavior of the field, so
  # that the server gets the same data in that field from old and new clients
  @error = name
  # Also specifically note that this exception was uncontrolled in the @crash field
  @crash = true
end

#did_finishObject



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
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 59

def did_finish
  return false if FastlaneCore::Env.truthy?("FASTLANE_OPT_OUT_USAGE")

  if !did_show_message? && !Helper.ci?
    show_message
  end

  require 'excon'
  url = ENV["FASTLANE_METRICS_URL"] || "https://fastlane-metrics.fabric.io/public"

  analytic_event_body = create_analytic_event_body

  # Never generate web requests during tests
  unless Helper.test?
    Thread.new do
      begin
        Excon.post(url,
                   body: analytic_event_body,
                   headers: { "Content-Type" => 'application/json' })
      rescue
        # we don't want to show a stack trace if something goes wrong
      end
    end
  end

  return true
rescue
  # We don't care about connection errors
end

#did_launch_action(name) ⇒ Object



29
30
31
32
33
34
35
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 29

def did_launch_action(name)
  name = name_to_track(name.to_sym)
  return unless name

  launches[name] += 1
  versions[name] ||= determine_version(name)
end

#did_raise_error(name) ⇒ Object

Call when the problem is a caught/controlled exception (e.g. via UI.user_error!)



38
39
40
41
42
43
44
45
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 38

def did_raise_error(name)
  name = name_to_track(name.to_sym)
  return unless name

  @error = name
  # Don't write to the @crash field so that we can distinguish this exception later
  # as being controlled
end

#did_show_message?Boolean

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 163

def did_show_message?
  file_name = ".did_show_opt_info"

  legacy_path = File.join(File.expand_path('~'), file_name)
  new_path = File.join(FastlaneCore.fastlane_user_dir, file_name)
  did_show = File.exist?(new_path) || File.exist?(legacy_path)

  return did_show if did_show

  File.write(new_path, '1')
  false
end

#event_for_completion(action, completion_status, version, timestamp_seconds) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 244

def event_for_completion(action, completion_status, version, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'action',
      detail: action
    },
    action: {
      name: 'execution_completed'
    },
    primary_target: {
      name: 'completion_status',
      detail: completion_status
    },
    secondary_target: {
      name: 'version',
      detail: secondary_target_string(version)
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

#event_for_count(action, count, version, timestamp_seconds) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 270

def event_for_count(action, count, version, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane'
    },
    actor: {
      name: 'action',
      detail: action
    },
    action: {
      name: 'execution_counted'
    },
    primary_target: {
      name: 'count',
      detail: count.to_s || "1"
    },
    secondary_target: {
      name: 'version',
      detail: secondary_target_string(version)
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

#event_for_web_onboarding(fastfile_id, completion_status, timestamp_seconds) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 218

def event_for_web_onboarding(fastfile_id, completion_status, timestamp_seconds)
  {
    event_source: {
      oauth_app_name: oauth_app_name,
      product: 'fastlane_web_onboarding'
    },
    actor: {
      name: 'customer',
      detail: fastfile_id
    },
    action: {
      name: 'fastfile_executed'
    },
    primary_target: {
      name: 'fastlane_completion_status',
      detail: completion_status
    },
    secondary_target: {
      name: 'executed',
      detail: secondary_target_string('')
    },
    millis_since_epoch: timestamp_seconds * 1000,
    version: 1
  }
end

#is_official?(name) ⇒ Boolean

Override this in subclasses

Returns:



151
152
153
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 151

def is_official?(name)
  return true
end

#launchesObject



133
134
135
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 133

def launches
  @launches ||= Hash.new(0)
end

#name_to_track(name) ⇒ Object

Returns nil if we shouldn’t track this action Returns a (maybe modified) name that should be sent to the analytic ingester Modificiation is used to prefix the action name with the name of the plugin



158
159
160
161
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 158

def name_to_track(name)
  return nil unless is_official?(name)
  name
end

#oauth_app_nameObject



296
297
298
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 296

def oauth_app_name
  return 'fastlane-enhancer'
end

#secondary_target_string(string) ⇒ Object



300
301
302
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 300

def secondary_target_string(string)
  return string
end

#show_messageObject



123
124
125
126
127
128
129
130
131
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 123

def show_message
  UI.message("Sending Crash/Success information")
  UI.message("Learn more at https://docs.fastlane.tools/#metrics")
  UI.message("No personal/sensitive data is sent. Only sharing the following:")
  UI.message(launches)
  UI.message(@error) if @error
  UI.message("This information is used to fix failing tools and improve those that are most often used.")
  UI.message("You can disable this by adding `opt_out_usage` at the top of your Fastfile")
end

#versionsObject

Maintains a hash of tool names to their detected versions.

This data is sent in the same manner as launches, as an inline form-encoded JSON value in the POST. For example:

match: '0.5.0',
fastlane: '1.86.1'



146
147
148
# File 'fastlane_core/lib/fastlane_core/tool_collector.rb', line 146

def versions
  @versions ||= {}
end