From 0367180ab6cc3036f635e1caa56ab09c101823e0 Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Wed, 21 May 2025 20:38:59 +1000 Subject: [PATCH 1/6] Add metric for active workspaces --- ee/app/models/remote_development/workspace.rb | 19 +++++++ ee/config/events/active_workspaces_count.yml | 19 +++++++ .../count_total_active_workspaces.yml | 22 ++++++++ .../remote_development/workspace_spec.rb | 54 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 ee/config/events/active_workspaces_count.yml create mode 100644 ee/config/metrics/counts_all/count_total_active_workspaces.yml diff --git a/ee/app/models/remote_development/workspace.rb b/ee/app/models/remote_development/workspace.rb index 690afc94f916c1..263e8f769ef31f 100644 --- a/ee/app/models/remote_development/workspace.rb +++ b/ee/app/models/remote_development/workspace.rb @@ -6,6 +6,7 @@ module RemoteDevelopment class Workspace < ApplicationRecord include Sortable include RemoteDevelopment::WorkspaceOperations::States + include ::Gitlab::InternalEventsTracking include ::Gitlab::Utils::StrongMemoize include SafelyChangeColumnDefault @@ -100,6 +101,8 @@ class Workspace < ApplicationRecord workspace.new_record? || workspace.actual_state_changed? end + after_save :trigger_active_workspace_event, if: -> { saved_change_to_desired_state? && desired_state == "Running" } + # @return [RemoteDevelopment::WorkspacesAgentConfig] def workspaces_agent_config unless unversioned_workspaces_agent_config_present? @@ -317,5 +320,21 @@ def touch_actual_state_updated_at # noinspection RubyMismatchedArgumentType - RBS type for #utc is Time, but db field is 'timestamp with time zone' self.actual_state_updated_at = Time.current.utc end + + # @return [void] + def trigger_active_workspace_event + track_internal_event( + "active_workspaces_count", + user: user, + project: project, + additional_properties: { + label: previously_new_record? ? "started" : "restarted" + } + ) + nil + rescue StandardError => e + logger.error("Failed to track active workspace event: #{e.message}") + nil + end end end diff --git a/ee/config/events/active_workspaces_count.yml b/ee/config/events/active_workspaces_count.yml new file mode 100644 index 00000000000000..3c2a94cb302027 --- /dev/null +++ b/ee/config/events/active_workspaces_count.yml @@ -0,0 +1,19 @@ +--- +description: GitLab Active Workspaces Count +internal_events: true +action: active_workspaces_count +identifiers: +- project +- namespace +- user +additional_properties: + label: + description: started or re-started workspace +product_group: remote_development +product_categories: +- workspaces +milestone: '18.1' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 +tiers: +- premium +- ultimate diff --git a/ee/config/metrics/counts_all/count_total_active_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_workspaces.yml new file mode 100644 index 00000000000000..53e5f5c1e7673b --- /dev/null +++ b/ee/config/metrics/counts_all/count_total_active_workspaces.yml @@ -0,0 +1,22 @@ +--- +key_path: counts.count_total_active_workspaces +description: Count of Total Number of Active Workspaces +product_group: remote_development +product_categories: +- workspaces +performance_indicator_type: [] +value_type: number +status: active +milestone: '18.2' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 +time_frame: +- 28d +- 7d +- all +data_source: internal_events +data_category: optional +tiers: +- premium +- ultimate +events: +- name: active_workspaces_count diff --git a/ee/spec/models/remote_development/workspace_spec.rb b/ee/spec/models/remote_development/workspace_spec.rb index ba22055b70096d..50be14a75c3c4e 100644 --- a/ee/spec/models/remote_development/workspace_spec.rb +++ b/ee/spec/models/remote_development/workspace_spec.rb @@ -159,6 +159,60 @@ end end end + + describe "after_save" do + describe "trigger_active_workspace_event callback" do + context "when desired_state changes to Running" do + it "triggers the event" do + expect(workspace).to receive(:trigger_active_workspace_event) + workspace.update!(desired_state: "Running") + end + + it "triggers internal event with started label on new record" do + expect { workspace.update!(desired_state: "Running") } + .to trigger_internal_events('active_workspaces_count') + .with(user: user, project: project, additional_properties: { + label: "started" + }) + .and increment_usage_metrics('counts.count_total_active_workspaces') + end + + it "triggers internal event with restarted label on existing record" do + workspace.save!(desired_state: "Stopped") + expect { workspace.update!(desired_state: "Running") } + .to trigger_internal_events('active_workspaces_count') + .with(user: user, project: project, additional_properties: { + label: "restarted" + }) + .and increment_usage_metrics('counts.count_total_active_workspaces') + end + end + + context "when desired_state changes to a value other than Running" do + it "does not trigger the event" do + expect(workspace).not_to receive(:trigger_active_workspace_event) + workspace.update!(desired_state: "Stopped") + end + end + + context "when desired_state doesn't change" do + it "does not trigger the event" do + workspace.update!(desired_state: "Running") + expect(workspace).not_to receive(:trigger_active_workspace_event) + workspace.update!(name: "workspace_new_name") + end + end + + context "when track_internal_event raises an error" do + it "logs the error message" do + error_message = "Connection failed" + allow(workspace).to receive(:track_internal_event).and_raise(StandardError.new(error_message)) + expect(workspace.logger).to receive(:error).with("Failed to track active workspace event: #{error_message}") + workspace.update!(desired_state: "Running") + end + end + end + end end describe "validations" do -- GitLab From 0b53e460071b5ad3866956a8d0ee38a2360d7542 Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Wed, 21 May 2025 20:43:50 +1000 Subject: [PATCH 2/6] Add metrics for started and restarted --- ...ount_total_active_restarted_workspaces.yml | 24 +++++++++++++++++++ .../count_total_active_started_workspaces.yml | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml create mode 100644 ee/config/metrics/counts_all/count_total_active_started_workspaces.yml diff --git a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml new file mode 100644 index 00000000000000..7444e107b52a90 --- /dev/null +++ b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml @@ -0,0 +1,24 @@ +--- +key_path: counts.count_total_active_restarted_workspaces +description: Count of Total Number of Active Re-started Workspaces +product_group: remote_development +product_categories: +- workspaces +performance_indicator_type: [] +value_type: number +status: active +milestone: '18.2' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 +time_frame: +- 28d +- 7d +- all +data_source: internal_events +data_category: optional +tiers: +- premium +- ultimate +events: +- name: active_workspaces_count + filter: + label: restarted \ No newline at end of file diff --git a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml new file mode 100644 index 00000000000000..b0feb6acd1151d --- /dev/null +++ b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml @@ -0,0 +1,24 @@ +--- +key_path: counts.count_total_active_started_workspaces +description: Count of Total Number of Active Started Workspaces +product_group: remote_development +product_categories: +- workspaces +performance_indicator_type: [] +value_type: number +status: active +milestone: '18.2' +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 +time_frame: +- 28d +- 7d +- all +data_source: internal_events +data_category: optional +tiers: +- premium +- ultimate +events: +- name: active_workspaces_count + filter: + label: started \ No newline at end of file -- GitLab From ebb5927b2151855addb51b5d6285ad7ce0888ff8 Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Fri, 23 May 2025 09:35:49 +1000 Subject: [PATCH 3/6] Address comments --- ee/app/models/remote_development/workspace.rb | 2 +- ee/config/events/active_workspaces_count.yml | 2 +- .../count_total_active_restarted_workspaces.yml | 2 +- .../count_total_active_started_workspaces.yml | 2 +- .../counts_all/count_total_active_workspaces.yml | 2 +- ee/spec/models/remote_development/workspace_spec.rb | 10 +++++----- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ee/app/models/remote_development/workspace.rb b/ee/app/models/remote_development/workspace.rb index 263e8f769ef31f..a90bd16bbe1e4f 100644 --- a/ee/app/models/remote_development/workspace.rb +++ b/ee/app/models/remote_development/workspace.rb @@ -333,7 +333,7 @@ def trigger_active_workspace_event ) nil rescue StandardError => e - logger.error("Failed to track active workspace event: #{e.message}") + logger.error("Failed to track active workspace event for workspace #{id}: #{e.message}") nil end end diff --git a/ee/config/events/active_workspaces_count.yml b/ee/config/events/active_workspaces_count.yml index 3c2a94cb302027..043b0c3d553f67 100644 --- a/ee/config/events/active_workspaces_count.yml +++ b/ee/config/events/active_workspaces_count.yml @@ -8,7 +8,7 @@ identifiers: - user additional_properties: label: - description: started or re-started workspace + description: started or restarted workspace product_group: remote_development product_categories: - workspaces diff --git a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml index 7444e107b52a90..1afd6de1e01efc 100644 --- a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml @@ -7,7 +7,7 @@ product_categories: performance_indicator_type: [] value_type: number status: active -milestone: '18.2' +milestone: '18.1' introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 time_frame: - 28d diff --git a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml index b0feb6acd1151d..f36f2cd7badabd 100644 --- a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml @@ -7,7 +7,7 @@ product_categories: performance_indicator_type: [] value_type: number status: active -milestone: '18.2' +milestone: '18.1' introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 time_frame: - 28d diff --git a/ee/config/metrics/counts_all/count_total_active_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_workspaces.yml index 53e5f5c1e7673b..5e927fb32d2bd9 100644 --- a/ee/config/metrics/counts_all/count_total_active_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_workspaces.yml @@ -7,7 +7,7 @@ product_categories: performance_indicator_type: [] value_type: number status: active -milestone: '18.2' +milestone: '18.1' introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/191902 time_frame: - 28d diff --git a/ee/spec/models/remote_development/workspace_spec.rb b/ee/spec/models/remote_development/workspace_spec.rb index 50be14a75c3c4e..1d5cac1d45d47a 100644 --- a/ee/spec/models/remote_development/workspace_spec.rb +++ b/ee/spec/models/remote_development/workspace_spec.rb @@ -61,7 +61,7 @@ it { is_expected.to belong_to(:user) } it { is_expected.to belong_to(:personal_access_token) } - it 'has correct relation setup' do + it "has correct relation setup" do is_expected .to belong_to(:agent) .class_name("Clusters::Agent") @@ -170,21 +170,21 @@ it "triggers internal event with started label on new record" do expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events('active_workspaces_count') + .to trigger_internal_events("active_workspaces_count") .with(user: user, project: project, additional_properties: { label: "started" }) - .and increment_usage_metrics('counts.count_total_active_workspaces') + .and increment_usage_metrics("counts.count_total_active_workspaces") end it "triggers internal event with restarted label on existing record" do workspace.save!(desired_state: "Stopped") expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events('active_workspaces_count') + .to trigger_internal_events("active_workspaces_count") .with(user: user, project: project, additional_properties: { label: "restarted" }) - .and increment_usage_metrics('counts.count_total_active_workspaces') + .and increment_usage_metrics("counts.count_total_active_workspaces") end end -- GitLab From 91d50cac79de1a8f868a467123f8b576c46730b0 Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Fri, 23 May 2025 10:51:13 +1000 Subject: [PATCH 4/6] Address comments --- ee/app/models/remote_development/workspace.rb | 11 ++++--- ...unt.yml => update_workspace_to_active.yml} | 4 +-- ...ount_total_active_restarted_workspaces.yml | 4 +-- .../count_total_active_started_workspaces.yml | 2 +- .../count_total_active_workspaces.yml | 2 +- .../remote_development/workspace_spec.rb | 30 +++++++------------ 6 files changed, 22 insertions(+), 31 deletions(-) rename ee/config/events/{active_workspaces_count.yml => update_workspace_to_active.yml} (79%) diff --git a/ee/app/models/remote_development/workspace.rb b/ee/app/models/remote_development/workspace.rb index a90bd16bbe1e4f..0ce5fe81bf30cd 100644 --- a/ee/app/models/remote_development/workspace.rb +++ b/ee/app/models/remote_development/workspace.rb @@ -101,7 +101,9 @@ class Workspace < ApplicationRecord workspace.new_record? || workspace.actual_state_changed? end - after_save :trigger_active_workspace_event, if: -> { saved_change_to_desired_state? && desired_state == "Running" } + after_save :trigger_update_workspace_to_active_event, if: -> { + saved_change_to_desired_state? && desired_state == "Running" + } # @return [RemoteDevelopment::WorkspacesAgentConfig] def workspaces_agent_config @@ -322,9 +324,9 @@ def touch_actual_state_updated_at end # @return [void] - def trigger_active_workspace_event + def trigger_update_workspace_to_active_event track_internal_event( - "active_workspaces_count", + "update_workspace_to_active", user: user, project: project, additional_properties: { @@ -332,9 +334,6 @@ def trigger_active_workspace_event } ) nil - rescue StandardError => e - logger.error("Failed to track active workspace event for workspace #{id}: #{e.message}") - nil end end end diff --git a/ee/config/events/active_workspaces_count.yml b/ee/config/events/update_workspace_to_active.yml similarity index 79% rename from ee/config/events/active_workspaces_count.yml rename to ee/config/events/update_workspace_to_active.yml index 043b0c3d553f67..b4c8e7a898ec21 100644 --- a/ee/config/events/active_workspaces_count.yml +++ b/ee/config/events/update_workspace_to_active.yml @@ -1,7 +1,7 @@ --- -description: GitLab Active Workspaces Count +description: Tracks updates when set workspaces active internal_events: true -action: active_workspaces_count +action: update_workspace_to_active identifiers: - project - namespace diff --git a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml index 1afd6de1e01efc..1a5e1e2e036b03 100644 --- a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml @@ -1,6 +1,6 @@ --- key_path: counts.count_total_active_restarted_workspaces -description: Count of Total Number of Active Re-started Workspaces +description: Count of Total Number of Active Restarted Workspaces product_group: remote_development product_categories: - workspaces @@ -19,6 +19,6 @@ tiers: - premium - ultimate events: -- name: active_workspaces_count +- name: update_workspace_to_active filter: label: restarted \ No newline at end of file diff --git a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml index f36f2cd7badabd..b29d8a99faa1d7 100644 --- a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml @@ -19,6 +19,6 @@ tiers: - premium - ultimate events: -- name: active_workspaces_count +- name: update_workspace_to_active filter: label: started \ No newline at end of file diff --git a/ee/config/metrics/counts_all/count_total_active_workspaces.yml b/ee/config/metrics/counts_all/count_total_active_workspaces.yml index 5e927fb32d2bd9..94d5e881540301 100644 --- a/ee/config/metrics/counts_all/count_total_active_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_active_workspaces.yml @@ -19,4 +19,4 @@ tiers: - premium - ultimate events: -- name: active_workspaces_count +- name: update_workspace_to_active diff --git a/ee/spec/models/remote_development/workspace_spec.rb b/ee/spec/models/remote_development/workspace_spec.rb index 1d5cac1d45d47a..adf54ebfc13d38 100644 --- a/ee/spec/models/remote_development/workspace_spec.rb +++ b/ee/spec/models/remote_development/workspace_spec.rb @@ -161,16 +161,16 @@ end describe "after_save" do - describe "trigger_active_workspace_event callback" do + describe "trigger_update_workspace_to_active_event callback" do context "when desired_state changes to Running" do it "triggers the event" do - expect(workspace).to receive(:trigger_active_workspace_event) + expect(workspace).to receive(:trigger_update_workspace_to_active_event) workspace.update!(desired_state: "Running") end it "triggers internal event with started label on new record" do expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events("active_workspaces_count") + .to trigger_internal_events("update_workspace_to_active") .with(user: user, project: project, additional_properties: { label: "started" }) @@ -180,7 +180,7 @@ it "triggers internal event with restarted label on existing record" do workspace.save!(desired_state: "Stopped") expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events("active_workspaces_count") + .to trigger_internal_events("update_workspace_to_active") .with(user: user, project: project, additional_properties: { label: "restarted" }) @@ -189,26 +189,18 @@ end context "when desired_state changes to a value other than Running" do - it "does not trigger the event" do - expect(workspace).not_to receive(:trigger_active_workspace_event) - workspace.update!(desired_state: "Stopped") + it "does not trigger the event and metric" do + expect { workspace.update!(desired_state: "Stopped") } + .to not_trigger_internal_events("update_workspace_to_active") + .and not_increment_usage_metrics('counts.count_total_active_workspaces') end end context "when desired_state doesn't change" do it "does not trigger the event" do - workspace.update!(desired_state: "Running") - expect(workspace).not_to receive(:trigger_active_workspace_event) - workspace.update!(name: "workspace_new_name") - end - end - - context "when track_internal_event raises an error" do - it "logs the error message" do - error_message = "Connection failed" - allow(workspace).to receive(:track_internal_event).and_raise(StandardError.new(error_message)) - expect(workspace.logger).to receive(:error).with("Failed to track active workspace event: #{error_message}") - workspace.update!(desired_state: "Running") + expect { workspace.update!(name: "workspace_new_name") } + .to not_trigger_internal_events("update_workspace_to_active") + .and not_increment_usage_metrics('counts.count_total_active_workspaces') end end end -- GitLab From caecf76b40be3b197f4ad129d66094ef8f76501c Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Mon, 26 May 2025 12:00:31 +1000 Subject: [PATCH 5/6] Address comments --- ...paces.yml => count_total_workspaces_restarted.yml} | 6 +++--- ...kspaces.yml => count_total_workspaces_started.yml} | 6 +++--- ... count_total_workspaces_started_and_restarted.yml} | 4 ++-- ee/spec/models/remote_development/workspace_spec.rb | 11 +++++++---- 4 files changed, 15 insertions(+), 12 deletions(-) rename ee/config/metrics/counts_all/{count_total_active_started_workspaces.yml => count_total_workspaces_restarted.yml} (74%) rename ee/config/metrics/counts_all/{count_total_active_restarted_workspaces.yml => count_total_workspaces_started.yml} (73%) rename ee/config/metrics/counts_all/{count_total_active_workspaces.yml => count_total_workspaces_started_and_restarted.yml} (74%) diff --git a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml b/ee/config/metrics/counts_all/count_total_workspaces_restarted.yml similarity index 74% rename from ee/config/metrics/counts_all/count_total_active_started_workspaces.yml rename to ee/config/metrics/counts_all/count_total_workspaces_restarted.yml index b29d8a99faa1d7..aa87cac3842809 100644 --- a/ee/config/metrics/counts_all/count_total_active_started_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_workspaces_restarted.yml @@ -1,6 +1,6 @@ --- -key_path: counts.count_total_active_started_workspaces -description: Count of Total Number of Active Started Workspaces +key_path: counts.count_total_workspaces_restarted +description: Count of Total Number of Restarted Workspaces product_group: remote_development product_categories: - workspaces @@ -21,4 +21,4 @@ tiers: events: - name: update_workspace_to_active filter: - label: started \ No newline at end of file + label: restarted diff --git a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml b/ee/config/metrics/counts_all/count_total_workspaces_started.yml similarity index 73% rename from ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml rename to ee/config/metrics/counts_all/count_total_workspaces_started.yml index 1a5e1e2e036b03..8e3a9ecac2d211 100644 --- a/ee/config/metrics/counts_all/count_total_active_restarted_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_workspaces_started.yml @@ -1,6 +1,6 @@ --- -key_path: counts.count_total_active_restarted_workspaces -description: Count of Total Number of Active Restarted Workspaces +key_path: counts.count_total_workspaces_started +description: Count of Total Number of Started Workspaces product_group: remote_development product_categories: - workspaces @@ -21,4 +21,4 @@ tiers: events: - name: update_workspace_to_active filter: - label: restarted \ No newline at end of file + label: started diff --git a/ee/config/metrics/counts_all/count_total_active_workspaces.yml b/ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml similarity index 74% rename from ee/config/metrics/counts_all/count_total_active_workspaces.yml rename to ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml index 94d5e881540301..d156a7f2348f4f 100644 --- a/ee/config/metrics/counts_all/count_total_active_workspaces.yml +++ b/ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml @@ -1,6 +1,6 @@ --- -key_path: counts.count_total_active_workspaces -description: Count of Total Number of Active Workspaces +key_path: counts.count_total_workspaces_started_and_restarted +description: Count of Total Number of Started and Restarted Workspaces product_group: remote_development product_categories: - workspaces diff --git a/ee/spec/models/remote_development/workspace_spec.rb b/ee/spec/models/remote_development/workspace_spec.rb index adf54ebfc13d38..d4910a669aa83a 100644 --- a/ee/spec/models/remote_development/workspace_spec.rb +++ b/ee/spec/models/remote_development/workspace_spec.rb @@ -38,6 +38,9 @@ dns_zone: dns_zone, enabled: workspaces_agent_config_enabled ) + # Some tests would trigger method trigger_update_workspace_to_active_event with HTTPS call + stub_request(:post, "https://events-stg.gitlab.net/com.snowplowanalytics.snowplow/tp2").to_return(status: 200, + body: "", headers: {}) end describe "default values" do @@ -174,7 +177,7 @@ .with(user: user, project: project, additional_properties: { label: "started" }) - .and increment_usage_metrics("counts.count_total_active_workspaces") + .and increment_usage_metrics("counts.count_total_workspaces_started_and_restarted") end it "triggers internal event with restarted label on existing record" do @@ -184,7 +187,7 @@ .with(user: user, project: project, additional_properties: { label: "restarted" }) - .and increment_usage_metrics("counts.count_total_active_workspaces") + .and increment_usage_metrics("counts.count_total_workspaces_started_and_restarted") end end @@ -192,7 +195,7 @@ it "does not trigger the event and metric" do expect { workspace.update!(desired_state: "Stopped") } .to not_trigger_internal_events("update_workspace_to_active") - .and not_increment_usage_metrics('counts.count_total_active_workspaces') + .and not_increment_usage_metrics('counts.count_total_workspaces_started_and_restarted') end end @@ -200,7 +203,7 @@ it "does not trigger the event" do expect { workspace.update!(name: "workspace_new_name") } .to not_trigger_internal_events("update_workspace_to_active") - .and not_increment_usage_metrics('counts.count_total_active_workspaces') + .and not_increment_usage_metrics('counts.count_total_workspaces_started_and_restarted') end end end -- GitLab From 0e26a89ba47a315fa1077cbde8a1900f4ebbae67 Mon Sep 17 00:00:00 2001 From: Zhaochen Li Date: Tue, 27 May 2025 12:21:10 +1000 Subject: [PATCH 6/6] Update event and metric naming --- ee/app/models/remote_development/workspace.rb | 8 +++--- ...ctive.yml => track_started_workspaces.yml} | 6 ++-- ...unt_total_existing_workspaces_started.yml} | 8 ++++-- ...=> count_total_new_workspaces_started.yml} | 8 +++--- .../count_total_workspaces_started.yml | 6 ++-- .../remote_development/workspace_spec.rb | 28 +++++++++---------- 6 files changed, 32 insertions(+), 32 deletions(-) rename ee/config/events/{update_workspace_to_active.yml => track_started_workspaces.yml} (67%) rename ee/config/metrics/counts_all/{count_total_workspaces_started_and_restarted.yml => count_total_existing_workspaces_started.yml} (65%) rename ee/config/metrics/counts_all/{count_total_workspaces_restarted.yml => count_total_new_workspaces_started.yml} (68%) diff --git a/ee/app/models/remote_development/workspace.rb b/ee/app/models/remote_development/workspace.rb index 0ce5fe81bf30cd..9c00871587f1e8 100644 --- a/ee/app/models/remote_development/workspace.rb +++ b/ee/app/models/remote_development/workspace.rb @@ -101,7 +101,7 @@ class Workspace < ApplicationRecord workspace.new_record? || workspace.actual_state_changed? end - after_save :trigger_update_workspace_to_active_event, if: -> { + after_save :track_started_workspace, if: -> { saved_change_to_desired_state? && desired_state == "Running" } @@ -324,13 +324,13 @@ def touch_actual_state_updated_at end # @return [void] - def trigger_update_workspace_to_active_event + def track_started_workspace track_internal_event( - "update_workspace_to_active", + "track_started_workspaces", user: user, project: project, additional_properties: { - label: previously_new_record? ? "started" : "restarted" + label: previously_new_record? ? "new" : "existing" } ) nil diff --git a/ee/config/events/update_workspace_to_active.yml b/ee/config/events/track_started_workspaces.yml similarity index 67% rename from ee/config/events/update_workspace_to_active.yml rename to ee/config/events/track_started_workspaces.yml index b4c8e7a898ec21..296123d3144240 100644 --- a/ee/config/events/update_workspace_to_active.yml +++ b/ee/config/events/track_started_workspaces.yml @@ -1,14 +1,14 @@ --- -description: Tracks updates when set workspaces active +description: Tracks Started New or Exisitng Workspaces internal_events: true -action: update_workspace_to_active +action: track_started_workspaces identifiers: - project - namespace - user additional_properties: label: - description: started or restarted workspace + description: new or exisitng workspace product_group: remote_development product_categories: - workspaces diff --git a/ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml b/ee/config/metrics/counts_all/count_total_existing_workspaces_started.yml similarity index 65% rename from ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml rename to ee/config/metrics/counts_all/count_total_existing_workspaces_started.yml index d156a7f2348f4f..4f0c9b66f500b4 100644 --- a/ee/config/metrics/counts_all/count_total_workspaces_started_and_restarted.yml +++ b/ee/config/metrics/counts_all/count_total_existing_workspaces_started.yml @@ -1,6 +1,6 @@ --- -key_path: counts.count_total_workspaces_started_and_restarted -description: Count of Total Number of Started and Restarted Workspaces +key_path: counts.count_total_existing_workspaces_started +description: Count of Total Number of Existing Workspaces Started product_group: remote_development product_categories: - workspaces @@ -19,4 +19,6 @@ tiers: - premium - ultimate events: -- name: update_workspace_to_active +- name: track_started_workspaces + filter: + label: existing diff --git a/ee/config/metrics/counts_all/count_total_workspaces_restarted.yml b/ee/config/metrics/counts_all/count_total_new_workspaces_started.yml similarity index 68% rename from ee/config/metrics/counts_all/count_total_workspaces_restarted.yml rename to ee/config/metrics/counts_all/count_total_new_workspaces_started.yml index aa87cac3842809..9f80c2d204e38e 100644 --- a/ee/config/metrics/counts_all/count_total_workspaces_restarted.yml +++ b/ee/config/metrics/counts_all/count_total_new_workspaces_started.yml @@ -1,6 +1,6 @@ --- -key_path: counts.count_total_workspaces_restarted -description: Count of Total Number of Restarted Workspaces +key_path: counts.count_total_new_workspaces_started +description: Count of Total Number of New Workspaces Started product_group: remote_development product_categories: - workspaces @@ -19,6 +19,6 @@ tiers: - premium - ultimate events: -- name: update_workspace_to_active +- name: track_started_workspaces filter: - label: restarted + label: new diff --git a/ee/config/metrics/counts_all/count_total_workspaces_started.yml b/ee/config/metrics/counts_all/count_total_workspaces_started.yml index 8e3a9ecac2d211..923110c9b52ffe 100644 --- a/ee/config/metrics/counts_all/count_total_workspaces_started.yml +++ b/ee/config/metrics/counts_all/count_total_workspaces_started.yml @@ -1,6 +1,6 @@ --- key_path: counts.count_total_workspaces_started -description: Count of Total Number of Started Workspaces +description: Count of Total Number of Workspaces Started product_group: remote_development product_categories: - workspaces @@ -19,6 +19,4 @@ tiers: - premium - ultimate events: -- name: update_workspace_to_active - filter: - label: started +- name: track_started_workspaces diff --git a/ee/spec/models/remote_development/workspace_spec.rb b/ee/spec/models/remote_development/workspace_spec.rb index d4910a669aa83a..28b5bcdfef657b 100644 --- a/ee/spec/models/remote_development/workspace_spec.rb +++ b/ee/spec/models/remote_development/workspace_spec.rb @@ -164,46 +164,46 @@ end describe "after_save" do - describe "trigger_update_workspace_to_active_event callback" do + describe "track_started_workspace callback" do context "when desired_state changes to Running" do it "triggers the event" do - expect(workspace).to receive(:trigger_update_workspace_to_active_event) + expect(workspace).to receive(:track_started_workspace) workspace.update!(desired_state: "Running") end - it "triggers internal event with started label on new record" do + it "triggers internal event with new label on new record" do expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events("update_workspace_to_active") + .to trigger_internal_events("track_started_workspaces") .with(user: user, project: project, additional_properties: { - label: "started" + label: "new" }) - .and increment_usage_metrics("counts.count_total_workspaces_started_and_restarted") + .and increment_usage_metrics("counts.count_total_workspaces_started") end - it "triggers internal event with restarted label on existing record" do + it "triggers internal event with existing label on existing record" do workspace.save!(desired_state: "Stopped") expect { workspace.update!(desired_state: "Running") } - .to trigger_internal_events("update_workspace_to_active") + .to trigger_internal_events("track_started_workspaces") .with(user: user, project: project, additional_properties: { - label: "restarted" + label: "existing" }) - .and increment_usage_metrics("counts.count_total_workspaces_started_and_restarted") + .and increment_usage_metrics("counts.count_total_workspaces_started") end end context "when desired_state changes to a value other than Running" do it "does not trigger the event and metric" do expect { workspace.update!(desired_state: "Stopped") } - .to not_trigger_internal_events("update_workspace_to_active") - .and not_increment_usage_metrics('counts.count_total_workspaces_started_and_restarted') + .to not_trigger_internal_events("track_started_workspaces") + .and not_increment_usage_metrics('counts.count_total_workspaces_started') end end context "when desired_state doesn't change" do it "does not trigger the event" do expect { workspace.update!(name: "workspace_new_name") } - .to not_trigger_internal_events("update_workspace_to_active") - .and not_increment_usage_metrics('counts.count_total_workspaces_started_and_restarted') + .to not_trigger_internal_events("track_started_workspaces") + .and not_increment_usage_metrics('counts.count_total_workspaces_started') end end end -- GitLab