Skip to content

Commit

Permalink
Add a unique installation_id to telemetry
Browse files Browse the repository at this point in the history
Every event will now include a unique installation_id, which is a GUID
we generate at the same time as other first-run tasks. We store it in
CHefCLI::Config.telemetry_installation_identiifer_file ->
.chef-workstation/installation_id.

Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
  • Loading branch information
marcparadise committed May 16, 2018
1 parent 48293b8 commit b24385b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
28 changes: 20 additions & 8 deletions components/chef-cli/lib/chef-cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,34 @@ def setup_cli
# Creates the tree we need under ~/.chef-workstation
# based on config settings:
Config.create_directory_tree
# TODO because we have not loaded a command, we wil lalways be using

# TODO because we have not loaded a command, we will always be using
# the default location at this step.
if Config.using_default_location? && !Config.exist?
UI::Terminal.output T.creating_config(Config.default_location)
Config.create_default_config_file
# Tell the user we're anonymously tracking, give brief opt-out
# and a link to detailed information.
UI::Terminal.output ""
UI::Terminal.output T.telemetry_enabled(Config.default_location)
UI::Terminal.output ""
setup_workstation
end

Config.load
ChefCLI::Log.setup(Config.log.location, Config.log.level.to_sym)
ChefCLI::Log.info("Initialized logger")
end

# This setup command is run if ".chef-workstation" is missing prior to
# the run. It will set up default configuration, generated an installation id
# for telemetry, and report telemetry & config info to the operator.
def setup_workstation
require "securerandom"
installation_id = SecureRandom.uuid
File.write(Config.telemetry_installation_identifier_file, installation_id)
UI::Terminal.output T.creating_config(Config.default_location)
Config.create_default_config_file
# Tell the user we're anonymously tracking, give brief opt-out
# and a link to detailed information.
UI::Terminal.output ""
UI::Terminal.output T.telemetry_enabled(Config.default_location)
UI::Terminal.output ""
end

def perform_command
update_args_for_help
update_args_for_version
Expand Down
4 changes: 4 additions & 0 deletions components/chef-cli/lib/chef-cli/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def telemetry_session_file
File.join(telemetry_path, "TELEMETRY_SESSION_ID")
end

def telemetry_installation_identifier_file
File.join(WS_BASE_PATH, "installation_id")
end

def error_output_path
File.join(File.dirname(log.location), "errors.txt")
end
Expand Down
25 changes: 20 additions & 5 deletions components/chef-cli/lib/chef-cli/telemeter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module ChefCLI
# a main 'timed_capture', and it would be good to see ordering within nested calls.
class Telemeter
include Singleton
DEFAULT_INSTALLATION_GUID = "00000000-0000-0000-0000-000000000000"

class << self
extend Forwardable
def_delegators :instance, :timed_capture, :capture, :commit, :timed_action_capture, :timed_run_capture
Expand Down Expand Up @@ -93,12 +95,25 @@ def commit
end

def make_event_payload(name, data)
properties = {
# We will submit this payload in a future run, so capture the time of actual execution:
run_timestamp: run_timestamp,
host_platform: host_platform,
{
event: name,
properties: {
installation_id: installation_id,
run_timestamp: run_timestamp,
host_platform: host_platform,
event_data: data
}
}
{ event: name, properties: properties.merge(data) }
end

def installation_id
@installation_id ||=
begin
File.read(ChefCLI::Config.telemetry_installation_identifier_file).chomp
rescue
ChefCLI::Log.info "could not read #{ChefCLI::Config.telemetry_installation_identifier_file} - using default id"
DEFAULT_INSTALLATION_GUID
end
end

# For testing.
Expand Down
24 changes: 15 additions & 9 deletions components/chef-cli/spec/unit/telemeter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,22 @@
end

context "#make_event_payload" do
context "when event is ':run'" do
it "adds expected properties" do
payload = subject.make_event_payload(:run, { hello: "world" })
expect(payload[:event]).to eq :run
props = payload[:properties]
expect(props[:host_platform]).to eq host_platform
expect(props[:run_timestamp]).to_not eq nil
expect(props[:hello]).to eq "world"
end
before do
allow(subject).to receive(:installation_id).and_return "0000"
end

it "adds expected properties" do
payload = subject.make_event_payload(:run, { hello: "world" })
expected_payload = {
event: :run,
properties: {
installation_id: "0000",
run_timestamp: subject.run_timestamp,
host_platform: host_platform,
event_data: { hello: "world" }
}
}
expect(payload).to eq expected_payload
end
end
end

0 comments on commit b24385b

Please sign in to comment.