Skip to content

Commit

Permalink
preregister telemetry command (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicroms authored Sep 14, 2022
1 parent 788ea09 commit 99fdff0
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
11 changes: 11 additions & 0 deletions include/vcpkg/commands.zpreregistertelemetry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vcpkg/commands.interface.h>

namespace vcpkg::Commands
{
struct ZPreRegisterTelemetryCommand : BasicCommand
{
virtual void perform_and_exit(const VcpkgCmdArguments& args, Filesystem& fs) const override;
};
}
1 change: 1 addition & 0 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace vcpkg
// exposed for testing
static View<MetricEntry<DefineMetric>> get_define_metrics();
static View<MetricEntry<StringMetric>> get_string_metrics();
static View<MetricEntry<StringMetric>> get_string_metrics_preregister_values();
static View<MetricEntry<BoolMetric>> get_bool_metrics();
};

Expand Down
20 changes: 19 additions & 1 deletion src/vcpkg-test/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ TEST_CASE ("Check metric enum types", "[metrics]")
{
validate_enum_values_and_names(Metrics::get_bool_metrics(), static_cast<size_t>(BoolMetric::COUNT));
}
}
}

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<size_t>(init_value.metric));
++enum_value;

// initialization value should not be empty
REQUIRE(!init_value.name.empty());
}
}
3 changes: 3 additions & 0 deletions src/vcpkg/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <vcpkg/commands.xvsinstances.h>
#include <vcpkg/commands.zbootstrap-standalone.h>
#include <vcpkg/commands.zce.h>
#include <vcpkg/commands.zpreregistertelemetry.h>
#include <vcpkg/commands.zprintconfig.h>
#include <vcpkg/export.h>
#include <vcpkg/help.h>
Expand All @@ -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)
Expand All @@ -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)
Expand Down
75 changes: 75 additions & 0 deletions src/vcpkg/commands.zpreregistertelemetry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <vcpkg/commands.zpreregistertelemetry.h>
#include <vcpkg/metrics.h>
#include <vcpkg/vcpkgcmdarguments.h>

using namespace vcpkg;

namespace
{
static void set_define_metrics()
{
auto metrics = LockGuardPtr<Metrics>(g_metrics);
for (auto metric : Metrics::get_define_metrics())
{
metrics->track_define_property(metric.metric);
}
}

static void set_bool_metrics()
{
auto metrics = LockGuardPtr<Metrics>(g_metrics);
for (auto metric : Metrics::get_bool_metrics())
{
metrics->track_bool_property(metric.metric, false);
}
}

static void set_string_metrics()
{
auto metrics = LockGuardPtr<Metrics>(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<Metrics>(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);
}
}
21 changes: 21 additions & 0 deletions src/vcpkg/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ namespace vcpkg
return {ENTRIES.data(), ENTRIES.size()};
}

View<MetricEntry<StringMetric>> Metrics::get_string_metrics_preregister_values()
{
// mock values in telemetry
static constexpr std::array<MetricEntry<StringMetric>, static_cast<size_t>(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();
Expand Down

0 comments on commit 99fdff0

Please sign in to comment.