2020-11-02 19:08:51 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'aggregated metrics' do
|
|
|
|
RSpec::Matchers.define :be_known_event do
|
|
|
|
match do |event|
|
|
|
|
Gitlab::UsageDataCounters::HLLRedisCounter.known_event?(event)
|
|
|
|
end
|
|
|
|
|
2020-11-23 19:09:13 -05:00
|
|
|
failure_message do |event|
|
2020-11-02 19:08:51 -05:00
|
|
|
"Event with name: `#{event}` can not be found within `#{Gitlab::UsageDataCounters::HLLRedisCounter::KNOWN_EVENTS_PATH}`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-09 07:09:48 -05:00
|
|
|
RSpec::Matchers.define :has_known_source do
|
|
|
|
match do |aggregate|
|
|
|
|
Gitlab::Usage::Metrics::Aggregates::SOURCES.include?(aggregate[:source])
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |aggregate|
|
|
|
|
"Aggregate with name: `#{aggregate[:name]}` uses not allowed source `#{aggregate[:source]}`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-08 13:09:12 -05:00
|
|
|
RSpec::Matchers.define :have_known_time_frame do
|
|
|
|
allowed_time_frames = [
|
2021-05-25 14:10:42 -04:00
|
|
|
Gitlab::Usage::TimeFrame::ALL_TIME_TIME_FRAME_NAME,
|
|
|
|
Gitlab::Usage::TimeFrame::TWENTY_EIGHT_DAYS_TIME_FRAME_NAME,
|
|
|
|
Gitlab::Usage::TimeFrame::SEVEN_DAYS_TIME_FRAME_NAME
|
2021-03-08 13:09:12 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
match do |aggregate|
|
|
|
|
(aggregate[:time_frame] - allowed_time_frames).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |aggregate|
|
|
|
|
"Aggregate with name: `#{aggregate[:name]}` uses not allowed time_frame`#{aggregate[:time_frame] - allowed_time_frames}`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-02 19:08:51 -05:00
|
|
|
let_it_be(:known_events) do
|
2020-11-06 10:09:14 -05:00
|
|
|
Gitlab::UsageDataCounters::HLLRedisCounter.known_events
|
2020-11-02 19:08:51 -05:00
|
|
|
end
|
|
|
|
|
2021-02-09 07:09:48 -05:00
|
|
|
Gitlab::Usage::Metrics::Aggregates::Aggregate.new(Time.current).send(:aggregated_metrics).tap do |aggregated_metrics|
|
2020-11-02 19:08:51 -05:00
|
|
|
it 'all events has unique name' do
|
|
|
|
event_names = aggregated_metrics&.map { |event| event[:name] }
|
|
|
|
|
|
|
|
expect(event_names).to eq(event_names&.uniq)
|
|
|
|
end
|
|
|
|
|
2021-02-09 07:09:48 -05:00
|
|
|
it 'all aggregated metrics has known source' do
|
|
|
|
expect(aggregated_metrics).to all has_known_source
|
|
|
|
end
|
|
|
|
|
2021-03-08 13:09:12 -05:00
|
|
|
it 'all aggregated metrics has known source' do
|
|
|
|
expect(aggregated_metrics).to all have_known_time_frame
|
|
|
|
end
|
|
|
|
|
2021-02-09 07:09:48 -05:00
|
|
|
aggregated_metrics&.select { |agg| agg[:source] == Gitlab::Usage::Metrics::Aggregates::REDIS_SOURCE }&.each do |aggregate|
|
2020-11-02 19:08:51 -05:00
|
|
|
context "for #{aggregate[:name]} aggregate of #{aggregate[:events].join(' ')}" do
|
|
|
|
let_it_be(:events_records) { known_events.select { |event| aggregate[:events].include?(event[:name]) } }
|
|
|
|
|
2021-03-08 13:09:12 -05:00
|
|
|
it "does not include 'all' time frame for Redis sourced aggregate" do
|
2021-05-25 14:10:42 -04:00
|
|
|
expect(aggregate[:time_frame]).not_to include(Gitlab::Usage::TimeFrame::ALL_TIME_TIME_FRAME_NAME)
|
2021-03-08 13:09:12 -05:00
|
|
|
end
|
|
|
|
|
2020-11-02 19:08:51 -05:00
|
|
|
it "only refers to known events" do
|
|
|
|
expect(aggregate[:events]).to all be_known_event
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has expected structure" do
|
2020-11-16 16:09:02 -05:00
|
|
|
expect(aggregate.keys).to include(*%w[name operator events])
|
2020-11-02 19:08:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses allowed aggregation operators" do
|
2021-02-01 07:09:03 -05:00
|
|
|
expect(Gitlab::Usage::Metrics::Aggregates::ALLOWED_METRICS_AGGREGATIONS).to include aggregate[:operator]
|
2020-11-02 19:08:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses events from the same Redis slot" do
|
|
|
|
event_slots = events_records.map { |event| event[:redis_slot] }.uniq
|
|
|
|
|
|
|
|
expect(event_slots).to contain_exactly(be_present)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses events with the same aggregation period" do
|
|
|
|
event_slots = events_records.map { |event| event[:aggregation] }.uniq
|
|
|
|
|
|
|
|
expect(event_slots).to contain_exactly(be_present)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|