Skip to content

Commit aae1600

Browse files
Merge branch 'master' into bjacobs/remove_unused_ansicolor_dependency
2 parents 14058c2 + 3a4f98d commit aae1600

File tree

8 files changed

+157
-2
lines changed

8 files changed

+157
-2
lines changed

.github/workflows/release_gem.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
with:
3333
fetch-depth: 0
3434
- id: release-gem
35-
uses: pact-foundation/release-gem@v0.0.11
35+
uses: pact-foundation/release-gem@v0.0.12
3636
env:
3737
GEM_HOST_API_KEY: '${{ secrets.RUBYGEMS_API_KEY }}'
3838
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<a name="v3.10.0"></a>
2+
### v3.10.0 (2022-02-21)
3+
4+
#### Features
5+
6+
* add telemetry ([db5dfa5](/../../commit/db5dfa5))
7+
18
<a name="v3.9.1"></a>
29
### v3.9.1 (2021-06-03)
310

lib/pact/mock_service/app_manager.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'pact/consumer/server'
55
require 'singleton'
66
require 'pact/mock_service/app'
7+
require 'pact/support/metrics'
78

89
module Pact
910
module MockService
@@ -29,6 +30,8 @@ def register_mock_service_for(name, url, options = {})
2930
pact_dir: pact_dir,
3031
pact_specification_version: options.fetch(:pact_specification_version)
3132
)
33+
34+
Pact::Support::Metrics.report_metric("Pact mock server started", "ConsumerTest", "MockServerStarted")
3235
register(app, uri.host, uri.port)
3336
end
3437

lib/pact/mock_service/run.rb

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'pact/mock_service/run'
55
require 'pact/mock_service/server/webrick_request_monkeypatch'
66
require 'pact/specification_version'
7+
require 'pact/support/metrics'
78

89
module Pact
910
module MockService
@@ -35,6 +36,7 @@ def call
3536
def mock_service
3637
@mock_service ||= begin
3738
mock_service = Pact::MockService.new(service_options)
39+
Pact::Support::Metrics.report_metric("Pact mock server started", "ConsumerTest", "MockServerStarted")
3840
Pact::Consumer::SetLocation.new(mock_service, base_url)
3941
end
4042
end

lib/pact/mock_service/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Pact
22
module MockService
3-
VERSION = "3.9.2"
3+
VERSION = "3.10.1"
44
end
55
end

lib/pact/support/metrics.rb

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'securerandom'
2+
require 'digest'
3+
require 'socket'
4+
require 'pact/support/metrics'
5+
require 'pact/mock_service/version'
6+
require 'net/http'
7+
8+
module Pact
9+
module Support
10+
class Metrics
11+
12+
def self.report_metric(event, category, action, value = 1)
13+
in_thread do
14+
begin
15+
if track_events?
16+
Pact.configuration.output_stream.puts "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'."
17+
18+
uri = URI('https://www.google-analytics.com/collect')
19+
req = Net::HTTP::Post.new(uri)
20+
req.set_form_data(create_tracking_event(event, category, action, value))
21+
22+
Net::HTTP.start(uri.hostname, uri.port, read_timeout:2, open_timeout:2, :use_ssl => true ) do |http|
23+
http.request(req)
24+
end
25+
end
26+
rescue StandardError => e
27+
handle_error(e)
28+
end
29+
end
30+
end
31+
32+
private
33+
34+
def self.handle_error e
35+
if ENV['PACT_METRICS_DEBUG'] == 'true'
36+
Pact.configuration.output_stream.puts("DEBUG: #{e.inspect}\n" + e.backtrace.join("\n"))
37+
end
38+
end
39+
40+
def self.in_thread
41+
Thread.new do
42+
yield
43+
end
44+
end
45+
46+
def self.create_tracking_event(event, category, action, value)
47+
{
48+
"v" => 1,
49+
"t" => "event",
50+
"tid" => "UA-117778936-1",
51+
"cid" => calculate_cid,
52+
"an" => "Pact Mock Service",
53+
"av" => Pact::MockService::VERSION,
54+
"aid" => "pact-mock_service",
55+
"aip" => 1,
56+
"ds" => ENV['PACT_EXECUTING_LANGUAGE'] ? "client" : "cli",
57+
"cd2" => ENV['CI'] == "true" ? "CI" : "unknown",
58+
"cd3" => RUBY_PLATFORM,
59+
"cd6" => ENV['PACT_EXECUTING_LANGUAGE'] || "unknown",
60+
"cd7" => ENV['PACT_EXECUTING_LANGUAGE_VERSION'],
61+
"el" => event,
62+
"ec" => category,
63+
"ea" => action,
64+
"ev" => value
65+
}
66+
end
67+
68+
def self.track_events?
69+
ENV['PACT_DO_NOT_TRACK'] != 'true'
70+
end
71+
72+
def self.calculate_cid
73+
if RUBY_PLATFORM.include? "windows"
74+
hostname = ENV['COMPUTERNAME']
75+
else
76+
hostname = ENV['HOSTNAME']
77+
end
78+
if !hostname
79+
hostname = Socket.gethostname
80+
end
81+
Digest::MD5.hexdigest hostname || SecureRandom.urlsafe_base64(5)
82+
end
83+
end
84+
end
85+
end

spec/lib/pact/mock_service/app_manager_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'pact/mock_service/app_manager'
2+
require 'pact/support/metrics'
23

34
module Pact::MockService
45
describe AppManager do
@@ -9,6 +10,7 @@ module Pact::MockService
910
describe "register_mock_service_for" do
1011
before do
1112
allow_any_instance_of(AppRegistration).to receive(:spawn) # Don't want process actually spawning during the tests
13+
allow(Pact::Support::Metrics).to receive(:report_metric)
1214
end
1315

1416
let(:name) { 'some_service'}
@@ -28,6 +30,11 @@ module Pact::MockService
2830
expect(AppManager.instance.app_registered_on?(1234)).to eq true
2931
end
3032

33+
it "reports the metric for pact mock service started" do
34+
expect(Pact::Support::Metrics).to receive(:report_metric).with("Pact mock server started", "ConsumerTest", "MockServerStarted")
35+
AppManager.instance.register_mock_service_for name, url, options
36+
end
37+
3138
it "creates a mock service with the configured pact_dir" do
3239
allow(Pact.configuration).to receive(:pact_dir).and_return('pact_dir')
3340
expect(Pact::MockService).to receive(:new) do | options |

spec/pact/support/metrics_spec.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'rspec'
2+
require 'pact/support/metrics'
3+
4+
describe Pact::Support::Metrics do
5+
6+
before do
7+
stub_request(:post, "https://www.google-analytics.com/collect").to_return(status: 200, body: "", headers: {})
8+
ENV['COMPUTERNAME'] = 'test'
9+
ENV['HOSTNAME'] = 'test'
10+
allow(Pact::Support::Metrics).to receive(:in_thread) { |&block| block.call }
11+
end
12+
13+
describe ".report_metric" do
14+
subject { Pact::Support::Metrics.report_metric("Event", "Category", "Action", "Value") }
15+
context 'when do not track is not set' do
16+
let(:expected_event) { {
17+
"v" => 1,
18+
"t" => "event",
19+
"tid" => "UA-117778936-1",
20+
"cid" => "098f6bcd4621d373cade4e832627b4f6",
21+
"an" => "Pact Mock Service",
22+
"av" => Pact::MockService::VERSION,
23+
"aid" => "pact-mock_service",
24+
"aip" => 1,
25+
"ds" => ENV['PACT_EXECUTING_LANGUAGE'] ? "client" : "cli",
26+
"cd2" => ENV['CI'] == "true" ? "CI" : "unknown",
27+
"cd3" => RUBY_PLATFORM,
28+
"cd6" => ENV['PACT_EXECUTING_LANGUAGE'] || "unknown",
29+
"cd7" => ENV['PACT_EXECUTING_LANGUAGE_VERSION'],
30+
"el" => "Event",
31+
"ec" => "Category",
32+
"ea" => "Action",
33+
"ev" => "Value"
34+
} }
35+
it 'sends metrics' do
36+
expect_any_instance_of(Net::HTTP::Post).to receive(:set_form_data).with(expected_event)
37+
expect(Net::HTTP).to receive(:start)
38+
subject
39+
end
40+
end
41+
context 'when do not track is set to true' do
42+
before do
43+
ENV['PACT_DO_NOT_TRACK'] = "true"
44+
end
45+
it 'does not send metrics' do
46+
expect(Net::HTTP).to_not receive(:start)
47+
subject
48+
end
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)