Class: Pact::Utils::Metrics

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

Class Method Summary collapse

Class Method Details

.calculate_cidObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pact/utils/metrics.rb', line 87

def self.calculate_cid
  if RUBY_PLATFORM.include? "windows"
    hostname = ENV['COMPUTERNAME']
  else
    hostname = ENV['HOSTNAME']
  end
  if !hostname
    hostname = Socket.gethostname
  end
  Digest::MD5.hexdigest hostname || SecureRandom.urlsafe_base64(5)
end

.create_tracking_event(event, category, action, value) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pact/utils/metrics.rb', line 61

def self.create_tracking_event(event, category, action, value)
  {
    "v" => 1,
    "t" => "event",
    "tid" => "UA-117778936-1",
    "cid" => calculate_cid,
    "an" => "Pact Ruby",
    "av" => Pact::VERSION,
    "aid" => "pact-ruby",
    "aip" => 1,
    "ds" => ENV['PACT_EXECUTING_LANGUAGE'] ? "client" : "cli",
    "cd2" => ENV['CI'] == "true" ? "CI" : "unknown",
    "cd3" => RUBY_PLATFORM,
    "cd6" => ENV['PACT_EXECUTING_LANGUAGE'] || "unknown",
    "cd7" => ENV['PACT_EXECUTING_LANGUAGE_VERSION'],
    "el" => event,
    "ec" => category,
    "ea" => action,
    "ev" => value
  }
end

.do_once_per_thread(key) ⇒ Object

not super safe to use the thread, but it's good enough for this usecase



52
53
54
55
56
57
58
59
# File 'lib/pact/utils/metrics.rb', line 52

def self.do_once_per_thread(key)
  result = nil
  if !Thread.current[key]
    result = yield
  end
  Thread.current[key] = true
  result
end

.handle_error(e) ⇒ Object



39
40
41
42
43
# File 'lib/pact/utils/metrics.rb', line 39

def self.handle_error e
  if ENV['PACT_METRICS_DEBUG'] == 'true'
    Pact.configuration.output_stream.puts("DEBUG: #{e.inspect}\n" + e.backtrace.join("\n"))
  end
end

.in_threadObject



45
46
47
48
49
# File 'lib/pact/utils/metrics.rb', line 45

def self.in_thread
  Thread.new do
    yield
  end
end

.report_metric(event, category, action, value = 1) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pact/utils/metrics.rb', line 11

def self.report_metric(event, category, action, value = 1)
  do_once_per_thread(:pact_metrics_message_shown) do
    if track_events?
      Pact.configuration.output_stream.puts "pact WARN: Please note: we are tracking events anonymously to gather important usage statistics like Pact-Ruby version
        and operating system. To disable tracking, set the 'PACT_DO_NOT_TRACK' environment
        variable to 'true'."
    end
  end

  in_thread do
    begin
      if track_events?
        uri = URI('https://www.google-analytics.com/collect')
        req = Net::HTTP::Post.new(uri)
        req.set_form_data(create_tracking_event(event, category, action, value))

        Net::HTTP.start(uri.hostname, uri.port, read_timeout:2, open_timeout:2, :use_ssl => true  ) do |http|
          http.request(req)
        end
      end
    rescue StandardError => e
      handle_error(e)
    end
  end
end

.track_events?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/pact/utils/metrics.rb', line 83

def self.track_events?
  ENV['PACT_DO_NOT_TRACK'] != 'true'
end