Commit cc78861a authored by Alina Mihaila's avatar Alina Mihaila Committed by alinamihaila

Apply 2 suggestion(s) to 1 file(s)

Use Gitlab::AppLogger for warn

  - Return if usage ping, feature or values are nil
  - Update tests
parent 603cbbe0
......@@ -288,9 +288,9 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
end
```
1. Track event in API using `increment_unique_values(event_name, values)` helper method
1. Track event in API using `increment_unique_values(event_name, values)` helper method.
In order to be able to track the events usage ping should be enabled and event feature `usage_data_<event_name>` should be enabled.
In order to be able to track the event, Usage Ping must be enabled and the event feature `usage_data_<event_name>` must be enabled.
Arguments:
......
......@@ -540,17 +540,15 @@ module API
# @param event_name [String] the event name
# @param values [Array|String] the values counted
def increment_unique_values(event_name, values)
raise "values is empty" unless values.present?
return unless values.present?
feature_name = "usage_data_#{event_name}"
raise "Feature #{feature_name} not enabled" unless Feature.enabled?(feature_name)
raise "Usage ping not enabled" unless Gitlab::CurrentSettings.usage_ping_enabled?
return unless Feature.enabled?(feature_name)
return unless Gitlab::CurrentSettings.usage_ping_enabled?
Gitlab::UsageDataCounters::HLLRedisCounter.track_event(values, event_name)
rescue => error
Rails.logger.warn( # rubocop:disable Gitlab/RailsLogger
"Redis tracking event failed for event: #{event_name}, message: #{error.message}"
)
Gitlab::AppLogger.warn("Redis tracking event failed for event: #{event_name}, message: #{error.message}")
end
def with_api_params(&block)
......
......@@ -208,9 +208,9 @@ RSpec.describe API::Helpers do
subject.increment_unique_values(event_name, value)
end
it 'logs an exception if usage ping is not enabled' do
it 'does not track event usage ping is not enabled' do
stub_application_setting(usage_ping_enabled: false)
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{event_name}, message: Usage ping not enabled")
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
subject.increment_unique_values(event_name, value)
end
......@@ -218,15 +218,15 @@ RSpec.describe API::Helpers do
it 'logs an exception for unknown event' do
stub_application_setting(usage_ping_enabled: true)
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{unknown_event}, message: Unknown event #{unknown_event}")
expect(Gitlab::AppLogger).to receive(:warn).with("Redis tracking event failed for event: #{unknown_event}, message: Unknown event #{unknown_event}")
subject.increment_unique_values(unknown_event, value)
end
it 'log an exception for nil values' do
it 'does not track event for nil values' do
stub_application_setting(usage_ping_enabled: true)
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{unknown_event}, message: values is empty")
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
subject.increment_unique_values(unknown_event, nil)
end
......@@ -237,8 +237,8 @@ RSpec.describe API::Helpers do
stub_feature_flags(feature => false)
end
it 'logs an exception' do
expect(Rails.logger).to receive(:warn).with("Redis tracking event failed for event: #{event_name}, message: Feature #{feature} not enabled")
it 'does not track event' do
expect(Gitlab::UsageDataCounters::HLLRedisCounter).not_to receive(:track_event)
subject.increment_unique_values(event_name, value)
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