Commit f95d9fdc authored by Matija Čupić's avatar Matija Čupić

Count all billing_state transitions with labels

parent 55535245
...@@ -7,6 +7,7 @@ class CheckGcpProjectBillingWorker ...@@ -7,6 +7,7 @@ class CheckGcpProjectBillingWorker
LEASE_TIMEOUT = 3.seconds.to_i LEASE_TIMEOUT = 3.seconds.to_i
SESSION_KEY_TIMEOUT = 5.minutes SESSION_KEY_TIMEOUT = 5.minutes
BILLING_TIMEOUT = 1.hour BILLING_TIMEOUT = 1.hour
BILLING_CHANGED_LABELS = { state_transition: nil }.freeze
def self.get_session_token(token_key) def self.get_session_token(token_key)
Gitlab::Redis::SharedState.with do |redis| Gitlab::Redis::SharedState.with do |redis|
...@@ -70,26 +71,22 @@ class CheckGcpProjectBillingWorker ...@@ -70,26 +71,22 @@ class CheckGcpProjectBillingWorker
def billing_changed_counter def billing_changed_counter
@billing_changed_counter ||= Gitlab::Metrics.counter( @billing_changed_counter ||= Gitlab::Metrics.counter(
:gcp_billing_change_count, :gcp_billing_change_count,
"Counts the number of times a GCP project changed billing_enabled state from false to true" "Counts the number of times a GCP project changed billing_enabled state from false to true",
BILLING_CHANGED_LABELS
) )
end end
def log_transition(previous_state, current_state) def state_transition(previous_state, current_state)
state_message = if previous_state.nil? && !current_state if previous_state.nil? && !current_state
"no_billing" 'no_billing'
elsif previous_state.nil? && current_state elsif previous_state.nil? && current_state
"with_billing" 'with_billing'
elsif !previous_state && current_state elsif !previous_state && current_state
"billing_configured" 'billing_configured'
end end
Rails.logger.info "#{self.class}: state: #{state_message}"
end end
def update_billing_change_counter(previous_state, current_state) def update_billing_change_counter(previous_state, current_state)
log_transition(previous_state, current_state) billing_changed_counter.increment(state_transition: state_transition(previous_state, current_state))
return unless !previous_state && current_state
billing_changed_counter.increment
end end
end end
...@@ -80,8 +80,8 @@ describe CheckGcpProjectBillingWorker do ...@@ -80,8 +80,8 @@ describe CheckGcpProjectBillingWorker do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([]) expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([])
end end
it 'does not increment the billing change counter' do it 'increments the billing change counter' do
expect_any_instance_of(described_class).not_to receive(:billing_changed_counter) expect_any_instance_of(described_class).to receive_message_chain(:billing_changed_counter, :increment)
subject subject
end end
...@@ -106,8 +106,8 @@ describe CheckGcpProjectBillingWorker do ...@@ -106,8 +106,8 @@ describe CheckGcpProjectBillingWorker do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double]) expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
end end
it 'does not increment the billing change counter' do it 'increment the billing change counter' do
expect_any_instance_of(described_class).not_to receive(:billing_changed_counter) expect_any_instance_of(described_class).to receive_message_chain(:billing_changed_counter, :increment)
subject subject
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment