1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
88
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::cell::RefCell;
use std::env::consts::{ARCH, OS};
use std::env::var;
use std::process::Command;
use std::str;
use std::sync::Mutex;
use anyhow::anyhow;
use lazy_static::lazy_static;
use log::{debug, warn};
use maplit::hashmap;
use reqwest::Client;
use uuid::Uuid;
static CIS: &'static [&str] = &[
"CI",
"CONTINUOUS_INTEGRATION",
"BSTRUSE_BUILD_DIR",
"APPVEYOR",
"BUDDY_WORKSPACE_URL",
"BUILDKITE",
"CF_BUILD_URL",
"CIRCLECI",
"CODEBUILD_BUILD_ARN",
"CONCOURSE_URL",
"DRONE",
"GITLAB_CI",
"GO_SERVER_URL",
"JENKINS_URL",
"PROBO_ENVIRONMENT",
"SEMAPHORE",
"SHIPPABLE",
"TDDIUM",
"TEAMCITY_VERSION",
"TF_BUILD",
"TRAVIS",
"WERCKER_ROOT"
];
pub enum MetricEvent {
ConsumerTestRun {
interactions: usize,
test_framework: String,
app_name: String,
app_version: String
},
ProviderVerificationRan {
tests_run: usize,
test_framework: String,
app_name: String,
app_version: String
}
}
impl MetricEvent {
pub(crate) fn app_name(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { app_name, .. } => app_name.as_str(),
MetricEvent::ProviderVerificationRan { app_name, .. } => app_name.as_str()
}
}
pub(crate) fn app_version(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { app_version, .. } => app_version.as_str(),
MetricEvent::ProviderVerificationRan { app_version, .. } => app_version.as_str()
}
}
pub(crate) fn test_framework(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { test_framework, .. } => test_framework.as_str(),
MetricEvent::ProviderVerificationRan { test_framework, .. } => test_framework.as_str()
}
}
pub(crate) fn name(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { .. } => "Pact consumer tests ran",
MetricEvent::ProviderVerificationRan { .. } => "Pacts verified"
}
}
pub(crate) fn category(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { .. } => "ConsumerTest",
MetricEvent::ProviderVerificationRan { .. } => "ProviderTest"
}
}
pub(crate) fn action(&self) -> &str {
match self {
MetricEvent::ConsumerTestRun { .. } => "Completed",
MetricEvent::ProviderVerificationRan { .. } => "Completed"
}
}
pub(crate) fn value(&self) -> String {
match self {
MetricEvent::ConsumerTestRun { interactions, .. } => interactions.to_string(),
MetricEvent::ProviderVerificationRan { tests_run, .. } => tests_run.to_string()
}
}
}
const GA_ACCOUNT: &str = "UA-117778936-1";
const GA_URL: &str = "https://www.google-analytics.com/collect";
lazy_static! {
static ref WARNING_LOGGED: Mutex<RefCell<bool>> = Mutex::new(RefCell::new(false));
}
pub fn send_metrics(event: MetricEvent) {
let do_not_track = match var("pact_do_not_track") {
Ok(val) => val == "true",
Err(_) => false
};
if do_not_track {
debug!("'pact_do_not_track' environment variable is set, will not send metrics");
} else {
match tokio::runtime::Handle::try_current() {
Ok(handle) => {
let mut guard = WARNING_LOGGED.lock().unwrap();
let warning_logged = (*guard).get_mut();
if *warning_logged == false {
warn!(
"\n\nPlease note:\n\
Please note: we are tracking events anonymously to gather important usage statistics like Pact version \
and operating system. To disable tracking, set the 'pact_do_not_track' system property or environment \
variable to 'true'.\n\n"
);
*warning_logged = true;
}
handle.spawn(async move {
let ci_context = if CIS.iter()
.any(|n| var(n).map(|val| !val.is_empty()).unwrap_or(false)) {
"CI"
} else {
"unknown"
};
let osarch = format!("{}-{}", OS, ARCH);
let uid = hostname_hash();
let value = event.value();
let event_payload = hashmap!{
"v" => "1",
"t" => "event",
"tid" => GA_ACCOUNT,
"uid" => uid.as_str(),
"an" => event.app_name(),
"aid" => event.app_name(),
"av" => event.app_version(),
"aip" => "true",
"ds" => "client",
"cd2" => ci_context,
"cd3" => osarch.as_str(),
"cd6" => event.test_framework(),
"cd7" => env!("CARGO_PKG_VERSION"),
"el" => event.name(),
"ec" => event.category(),
"ea" => event.action(),
"ev" => value.as_str()
};
let result = Client::new().post(GA_URL)
.form(&event_payload)
.send()
.await;
if let Err(err) = result {
debug!("Failed to post event - {}", err);
}
});
},
Err(err) => {
debug!("Could not get the tokio runtime, will not send metrics - {}", err)
}
}
}
}
fn hostname_hash() -> String {
let host_name = if OS == "windows" {
var("COMPUTERNAME")
} else {
var("HOSTNAME")
}.or_else(|_| {
exec_hostname_command()
}).unwrap_or_else(|_| {
Uuid::new_v4().to_string()
});
let digest = md5::compute(host_name.as_bytes());
format!("{:x}", digest)
}
fn exec_hostname_command() -> anyhow::Result<String> {
match Command::new("hostname").output() {
Ok(output) => if output.status.success() {
Ok(str::from_utf8(&*output.stdout)?.to_string())
} else {
Err(anyhow!("Failed to invoke hostname command: status {}", output.status))
}
Err(err) => Err(anyhow!("Failed to invoke hostname command: {}", err))
}
}