From 91da38f7534f7587fcc83594e3da20f935037cd4 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 17 Aug 2022 13:39:49 +1000 Subject: [PATCH] feat: only print metrics warning once per thread --- lib/pact/utils/metrics.rb | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/pact/utils/metrics.rb b/lib/pact/utils/metrics.rb index 1d86d032..c7fef930 100644 --- a/lib/pact/utils/metrics.rb +++ b/lib/pact/utils/metrics.rb @@ -9,13 +9,17 @@ module Utils class Metrics 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? - 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'." - 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)) @@ -31,6 +35,7 @@ def self.report_metric(event, category, action, value = 1) end private + def self.handle_error e if ENV['PACT_METRICS_DEBUG'] == 'true' Pact.configuration.output_stream.puts("DEBUG: #{e.inspect}\n" + e.backtrace.join("\n")) @@ -43,6 +48,16 @@ def self.in_thread end end + # not super safe to use the thread, but it's good enough for this usecase + def self.do_once_per_thread(key) + result = nil + if !Thread.current[key] + result = yield + end + Thread.current[key] = true + result + end + def self.create_tracking_event(event, category, action, value) { "v" => 1,