Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consult applocal userconfig if metrics are enabled #160

Merged
merged 6 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ namespace vcpkg

void set_send_metrics(bool should_send_metrics);
void set_print_metrics(bool should_print_metrics);
void set_disabled(bool disabled);
void set_user_information(const std::string& user_id, const std::string& first_use_time);
static void init_user_information(std::string& user_id, std::string& first_use_time);
void enable();

void track_metric(const std::string& name, double value);
void track_buildtime(const std::string& name, double value);
Expand All @@ -32,6 +30,4 @@ namespace vcpkg
};

extern LockGuarded<Metrics> g_metrics;

std::string get_MAC_user();
}
83 changes: 21 additions & 62 deletions src/vcpkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <vcpkg/input.h>
#include <vcpkg/metrics.h>
#include <vcpkg/paragraphs.h>
#include <vcpkg/userconfig.h>
#include <vcpkg/vcpkgcmdarguments.h>
#include <vcpkg/vcpkglib.h>
#include <vcpkg/vcpkgpaths.h>
Expand Down Expand Up @@ -124,50 +123,6 @@ static void inner(vcpkg::Filesystem& fs, const VcpkgCmdArguments& args)
return invalid_command(args.command);
}

static void load_config(vcpkg::Filesystem& fs)
{
auto config = UserConfig::try_read_data(fs);

bool write_config = false;

// config file not found, could not be read, or invalid
if (config.user_id.empty() || config.user_time.empty())
{
::vcpkg::Metrics::init_user_information(config.user_id, config.user_time);
write_config = true;
}

#if defined(_WIN32)
if (config.user_mac.empty())
{
config.user_mac = get_MAC_user();
write_config = true;
}
#endif

{
LockGuardPtr<Metrics> locked_metrics(g_metrics);
locked_metrics->set_user_information(config.user_id, config.user_time);
#if defined(_WIN32)
locked_metrics->track_property("user_mac", config.user_mac);
#endif
}

if (config.last_completed_survey.empty())
{
const auto now = CTime::parse(config.user_time).value_or_exit(VCPKG_LINE_INFO);
const CTime offset = now.add_hours(-SURVEY_INITIAL_OFFSET_IN_HOURS);
config.last_completed_survey = offset.to_string();
}

LockGuardPtr<std::string>(GlobalState::g_surveydate)->assign(config.last_completed_survey);

if (write_config)
{
config.try_write_data(fs);
}
}

#if defined(_WIN32)
// note: this prevents a false positive for -Wmissing-prototypes on clang-cl
int wmain(int, const wchar_t* const* const);
Expand Down Expand Up @@ -268,8 +223,6 @@ int main(const int argc, const char* const* const argv)

register_console_ctrl_handler();

load_config(fs);

#if (defined(__aarch64__) || defined(__arm__) || defined(__s390x__) || \
((defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || defined(__PPC64LE__)) && \
defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
Expand All @@ -289,20 +242,26 @@ int main(const int argc, const char* const* const argv)
VcpkgCmdArguments::imbue_or_apply_process_recursion(args);
args.check_feature_flag_consistency();

bool to_enable_metrics = true;
if (const auto p = args.disable_metrics.get())
{
LockGuardPtr<Metrics> metrics(g_metrics);
if (const auto p = args.disable_metrics.get())
{
metrics->set_disabled(*p);
}
to_enable_metrics = false;
}

auto disable_metrics_tag_file_path = get_exe_path_of_current_process();
disable_metrics_tag_file_path.replace_filename("vcpkg.disable-metrics");

auto disable_metrics_tag_file_path = get_exe_path_of_current_process();
disable_metrics_tag_file_path.replace_filename("vcpkg.disable-metrics");
std::error_code ec;
if (fs.exists(disable_metrics_tag_file_path, ec) || ec)
{
to_enable_metrics = false;
}

std::error_code ec;
if (fs.exists(disable_metrics_tag_file_path, ec) || ec)
{
LockGuardPtr<Metrics> metrics(g_metrics);
if (to_enable_metrics)
{
metrics->set_disabled(true);
metrics->enable();
}

if (const auto p = args.print_metrics.get())
Expand All @@ -314,13 +273,13 @@ int main(const int argc, const char* const* const argv)
{
metrics->set_send_metrics(*p);
}

if (args.send_metrics.value_or(false) && !metrics->metrics_enabled())
{
msg::println(Color::warning, msgVcpkgSendMetricsButDisabled);
}
} // unlock g_metrics

if (args.send_metrics.value_or(false) && !to_enable_metrics)
{
msg::println(Color::warning, msgVcpkgSendMetricsButDisabled);
}

args.debug_print_feature_flags();
args.track_feature_flag_metrics();

Expand Down
85 changes: 47 additions & 38 deletions src/vcpkg/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vcpkg/commands.h>
#include <vcpkg/commands.version.h>
#include <vcpkg/metrics.h>
#include <vcpkg/userconfig.h>

#if defined(_WIN32)
#pragma comment(lib, "version")
Expand Down Expand Up @@ -247,11 +248,11 @@ namespace vcpkg
#endif
;
static bool g_should_print_metrics = false;
static bool g_metrics_disabled = false;
static bool g_metrics_disabled = true;

std::string get_MAC_user()
{
#if defined(_WIN32)
static std::string get_MAC_user()
{
if (!LockGuardPtr<Metrics>(g_metrics)->metrics_enabled())
{
return "{}";
Expand All @@ -276,66 +277,74 @@ namespace vcpkg
}

return "0";
#else
return "{}";
#endif
}
#endif

void Metrics::set_user_information(const std::string& user_id, const std::string& first_use_time)
static void load_config(Metrics& m, vcpkg::Filesystem& fs)
{
g_metricmessage.user_id = user_id;
g_metricmessage.user_timestamp = first_use_time;
}
(void)m;
auto config = UserConfig::try_read_data(fs);

void Metrics::init_user_information(std::string& user_id, std::string& first_use_time)
{
user_id = generate_random_UUID();
first_use_time = get_current_date_time_string();
bool write_config = false;

// config file not found, could not be read, or invalid
if (config.user_id.empty() || config.user_time.empty())
{
config.user_id = generate_random_UUID();
config.user_time = get_current_date_time_string();
write_config = true;
}

#if defined(_WIN32)
if (config.user_mac.empty())
{
config.user_mac = get_MAC_user();
write_config = true;
}
#endif

g_metricmessage.user_id = config.user_id;
g_metricmessage.user_timestamp = config.user_time;

#if defined(_WIN32)
m.track_property("user_mac", config.user_mac);
#endif

if (write_config)
{
config.try_write_data(fs);
}
}

void Metrics::set_send_metrics(bool should_send_metrics) { g_should_send_metrics = should_send_metrics; }

void Metrics::set_print_metrics(bool should_print_metrics) { g_should_print_metrics = should_print_metrics; }

void Metrics::set_disabled(bool disabled) { g_metrics_disabled = disabled; }

bool Metrics::metrics_enabled() { return !g_metrics_disabled; }

void Metrics::track_metric(const std::string& name, double value)
void Metrics::enable()
{
if (!metrics_enabled())
if (g_metrics_disabled)
{
return;
// Ensure load_config() is called exactly once
load_config(*this, get_real_filesystem());
}
g_metricmessage.track_metric(name, value);
g_metrics_disabled = false;
}

bool Metrics::metrics_enabled() { return !g_metrics_disabled; }

void Metrics::track_metric(const std::string& name, double value) { g_metricmessage.track_metric(name, value); }

void Metrics::track_buildtime(const std::string& name, double value)
{
if (!metrics_enabled())
{
return;
}
g_metricmessage.track_buildtime(name, value);
}

void Metrics::track_property(const std::string& name, const std::string& value)
{
if (!metrics_enabled())
{
return;
}
g_metricmessage.track_property(name, value);
}

void Metrics::track_feature(const std::string& name, bool value)
{
if (!metrics_enabled())
{
return;
}
g_metricmessage.track_feature(name, value);
}
void Metrics::track_feature(const std::string& name, bool value) { g_metricmessage.track_feature(name, value); }

void Metrics::upload(const std::string& payload)
{
Expand Down