From 99fdff0e07edce961c649676e1788361151fbd8a Mon Sep 17 00:00:00 2001 From: Victor Romero Date: Wed, 14 Sep 2022 12:46:46 -0700 Subject: [PATCH] preregister telemetry command (#687) --- .../vcpkg/commands.zpreregistertelemetry.h | 11 +++ include/vcpkg/metrics.h | 1 + src/vcpkg-test/metrics.cpp | 20 ++++- src/vcpkg/commands.cpp | 3 + src/vcpkg/commands.zpreregistertelemetry.cpp | 75 +++++++++++++++++++ src/vcpkg/metrics.cpp | 21 ++++++ 6 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 include/vcpkg/commands.zpreregistertelemetry.h create mode 100644 src/vcpkg/commands.zpreregistertelemetry.cpp diff --git a/include/vcpkg/commands.zpreregistertelemetry.h b/include/vcpkg/commands.zpreregistertelemetry.h new file mode 100644 index 0000000000..52d410238f --- /dev/null +++ b/include/vcpkg/commands.zpreregistertelemetry.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace vcpkg::Commands +{ + struct ZPreRegisterTelemetryCommand : BasicCommand + { + virtual void perform_and_exit(const VcpkgCmdArguments& args, Filesystem& fs) const override; + }; +} diff --git a/include/vcpkg/metrics.h b/include/vcpkg/metrics.h index fc5e269f91..231628c0d1 100644 --- a/include/vcpkg/metrics.h +++ b/include/vcpkg/metrics.h @@ -99,6 +99,7 @@ namespace vcpkg // exposed for testing static View> get_define_metrics(); static View> get_string_metrics(); + static View> get_string_metrics_preregister_values(); static View> get_bool_metrics(); }; diff --git a/src/vcpkg-test/metrics.cpp b/src/vcpkg-test/metrics.cpp index 36360358b0..42fd0c382c 100644 --- a/src/vcpkg-test/metrics.cpp +++ b/src/vcpkg-test/metrics.cpp @@ -45,4 +45,22 @@ TEST_CASE ("Check metric enum types", "[metrics]") { validate_enum_values_and_names(Metrics::get_bool_metrics(), static_cast(BoolMetric::COUNT)); } -} \ No newline at end of file +} + +TEST_CASE ("Check string metrics initialization values", "[metrics]") +{ + auto known_metrics = Metrics::get_string_metrics(); + auto init_values = Metrics::get_string_metrics_preregister_values(); + + // check that all init values are complete and in order + size_t enum_value = 0; + REQUIRE(init_values.size() == known_metrics.size()); + for (auto&& init_value : init_values) + { + REQUIRE(enum_value == static_cast(init_value.metric)); + ++enum_value; + + // initialization value should not be empty + REQUIRE(!init_value.name.empty()); + } +} diff --git a/src/vcpkg/commands.cpp b/src/vcpkg/commands.cpp index 66be8f04fb..e75fa6741d 100644 --- a/src/vcpkg/commands.cpp +++ b/src/vcpkg/commands.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,7 @@ namespace vcpkg::Commands static const GenerateDefaultMessageMapCommand generate_message_map{}; static const Hash::HashCommand hash{}; static const ZBootstrapStandaloneCommand zboostrap_standalone{}; + static const ZPreRegisterTelemetryCommand zpreregister_telemetry{}; #if defined(_WIN32) static const UploadMetrics::UploadMetricsCommand upload_metrics{}; #endif // defined(_WIN32) @@ -72,6 +74,7 @@ namespace vcpkg::Commands {"x-download", &xdownload}, {"x-generate-default-message-map", &generate_message_map}, {"z-bootstrap-standalone", &zboostrap_standalone}, + {"z-preregister-telemetry", &zpreregister_telemetry}, #if defined(_WIN32) {"x-upload-metrics", &upload_metrics}, #endif // defined(_WIN32) diff --git a/src/vcpkg/commands.zpreregistertelemetry.cpp b/src/vcpkg/commands.zpreregistertelemetry.cpp new file mode 100644 index 0000000000..b52e63128e --- /dev/null +++ b/src/vcpkg/commands.zpreregistertelemetry.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace vcpkg; + +namespace +{ + static void set_define_metrics() + { + auto metrics = LockGuardPtr(g_metrics); + for (auto metric : Metrics::get_define_metrics()) + { + metrics->track_define_property(metric.metric); + } + } + + static void set_bool_metrics() + { + auto metrics = LockGuardPtr(g_metrics); + for (auto metric : Metrics::get_bool_metrics()) + { + metrics->track_bool_property(metric.metric, false); + } + } + + static void set_string_metrics() + { + auto metrics = LockGuardPtr(g_metrics); + for (auto&& kv : Metrics::get_string_metrics_preregister_values()) + { + metrics->track_string_property(kv.metric, kv.name.to_string()); + } + } + + const CommandStructure COMMAND_STRUCTURE = { + create_example_string("z-preregister-telemetry"), + 0, + 0, + {}, + nullptr, + }; +} + +namespace vcpkg::Commands +{ + void ZPreRegisterTelemetryCommand::perform_and_exit(const VcpkgCmdArguments& args, Filesystem& fs) const + { + (void)args; + (void)fs; + + auto metrics_enabled = false; + + { + auto metrics = LockGuardPtr(g_metrics); + metrics->set_print_metrics(true); + metrics_enabled = metrics->metrics_enabled(); + } + + if (metrics_enabled) + { + // fills the property message with dummy data + // telemetry is uploaded via the usual mechanism + set_define_metrics(); + set_bool_metrics(); + set_string_metrics(); + } + else + { + msg::println_warning(msgVcpkgSendMetricsButDisabled); + } + + Checks::exit_success(VCPKG_LINE_INFO); + } +} diff --git a/src/vcpkg/metrics.cpp b/src/vcpkg/metrics.cpp index 616a1bca93..d4cef31168 100644 --- a/src/vcpkg/metrics.cpp +++ b/src/vcpkg/metrics.cpp @@ -101,6 +101,27 @@ namespace vcpkg return {ENTRIES.data(), ENTRIES.size()}; } + View> Metrics::get_string_metrics_preregister_values() + { + // mock values in telemetry + static constexpr std::array, static_cast(StringMetric::COUNT)> ENTRIES{{ + {StringMetric::BuildError, "gsl:x64-windows"}, + {StringMetric::CommandArgs, "0000000011111111aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffff"}, + {StringMetric::CommandContext, "artifact"}, + {StringMetric::CommandName, "z-preregister-telemetry"}, + {StringMetric::Error, "build failed"}, + {StringMetric::InstallPlan_1, "0000000011111111aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffff"}, + {StringMetric::ListFile, "update to new format"}, + {StringMetric::RegistriesDefaultRegistryKind, "builtin-files"}, + {StringMetric::RegistriesKindsUsed, "git,filesystem"}, + {StringMetric::Title, "title"}, + {StringMetric::UserMac, "0"}, + {StringMetric::VcpkgVersion, "2999-12-31-unknownhash"}, + {StringMetric::Warning, "warning"}, + }}; + return {ENTRIES.data(), ENTRIES.size()}; + } + static std::string get_current_date_time_string() { auto maybe_time = CTime::get_current_date_time();