Skip to content
代码片段 群组 项目
未验证 提交 a8997c1c 编辑于 作者: Niko Belokolodov's avatar Niko Belokolodov 提交者: GitLab
浏览文件

Merge branch '522831-add-count-of-internal-events' into 'master'

Add metric to count how many times internal events is invoked

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/184038



Merged-by: default avatarNiko Belokolodov <nbelokolodov@gitlab.com>
Approved-by: default avatarMichał Wielich <mwielich@gitlab.com>
Approved-by: default avatarNiko Belokolodov <nbelokolodov@gitlab.com>
Reviewed-by: default avatarMichał Wielich <mwielich@gitlab.com>
Reviewed-by: default avatarJonas Larsen <jlarsen@gitlab.com>
Co-authored-by: default avatarJonas Larsen <jlarsen@gitlab.com>
No related branches found
No related tags found
无相关合并请求
---
key_path: counts.count_total_invocations_of_internal_events
description: Count of number of times internal events was invoked
product_group: analytics_instrumentation
product_categories:
- application_instrumentation
performance_indicator_type: []
value_type: number
status: active
milestone: '17.10'
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/184038
time_frame:
- 28d
- 7d
data_source: system
instrumentation_class: SumNumberOfInternalEventInvocationsMetric
data_category: optional
tiers:
- free
- premium
- ultimate
# frozen_string_literal: true
module Gitlab
module Usage
module Metrics
module Instrumentations
class SumNumberOfInternalEventInvocationsMetric < GenericMetric
include Gitlab::UsageDataCounters::RedisCounter
def value
all_internal_event_keys.sum do |key|
redis_usage_data do
total_count(key)
end
end
end
private
def all_internal_event_keys
internal_event_definitions = Gitlab::Tracking::EventDefinition.definitions.select(&:internal_events?)
internal_event_definitions.flat_map do |event_definition|
keys_for_event_definition(event_definition)
end
end
def keys_for_event_definition(event_definition)
event_selection_rule = EventSelectionRule.new(name: event_definition.action, time_framed: true)
event_selection_rule.redis_keys_for_time_frame(time_frame)
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::Instrumentations::SumNumberOfInternalEventInvocationsMetric,
:clean_gitlab_redis_shared_state, feature_category: :product_analytics do
before do
event_definition1 = instance_double(
Gitlab::Tracking::EventDefinition,
action: 'internal_event1',
internal_events?: true
)
event_definition2 = instance_double(
Gitlab::Tracking::EventDefinition,
action: 'internal_event2',
internal_events?: true
)
# Non-internal event that should be excluded
event_definition3 = instance_double(
Gitlab::Tracking::EventDefinition,
action: 'non_internal_event',
internal_events?: false
)
definitions = [event_definition1, event_definition2, event_definition3]
allow(Gitlab::Tracking::EventDefinition)
.to receive_messages(internal_event_exists?: true, definitions: definitions)
end
context 'with multiple internal events' do
let(:event_selection_rule1) { Gitlab::Usage::EventSelectionRule.new(name: 'internal_event1', time_framed: true) }
let(:event_selection_rule2) { Gitlab::Usage::EventSelectionRule.new(name: 'internal_event2', time_framed: true) }
before do
last_week = Time.zone.today - 7.days
two_weeks_ago = last_week - 1.week
# Create Redis data for internal_event1
2.times do
redis_counter_key = event_selection_rule1.redis_key_for_date(last_week)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
3.times do
redis_counter_key = event_selection_rule1.redis_key_for_date(two_weeks_ago)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
# Create Redis data for internal_event2
4.times do
redis_counter_key = event_selection_rule2.redis_key_for_date(last_week)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
5.times do
redis_counter_key = event_selection_rule2.redis_key_for_date(two_weeks_ago)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
# Create Redis data for non-internal event (should be ignored)
non_internal_event_rule = Gitlab::Usage::EventSelectionRule.new(name: 'non_internal_event', time_framed: true)
8.times do
redis_counter_key = non_internal_event_rule.redis_key_for_date(last_week)
Gitlab::Redis::SharedState.with { |redis| redis.incr(redis_counter_key) }
end
end
context "with a '7d' time_frame" do
let(:expected_value) { 6 } # 2 from event1 + 4 from event2
it_behaves_like 'a correct instrumented metric value', { time_frame: '7d' }
end
context "with a '28d' time_frame" do
let(:expected_value) { 14 } # 5 from event1 + 9 from event2
it_behaves_like 'a correct instrumented metric value', { time_frame: '28d' }
end
end
context "with an invalid time_frame" do
let(:metric) { described_class.new(time_frame: '14d') }
it 'raises an exception' do
expect { metric.value }.to raise_error(/Unknown time frame/)
end
end
context "with no internal events" do
before do
allow(Gitlab::Tracking::EventDefinition).to receive(:definitions).and_return([])
end
context "with a '7d' time_frame" do
let(:expected_value) { 0 }
it_behaves_like 'a correct instrumented metric value', { time_frame: '7d' }
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册