Commit 7ed1e686 authored by Tiger's avatar Tiger

Add kubernetes_agent_k8s_api_proxy_request to usage ping

The value is stored in a redis counter, which is incremented
by an additional parameter in the /kubernetes/usage_metrics
internal API endpoint.

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/60978

Changelog: changed
parent a48b0f16
---
title: Add kubernetes_agent_proxy_request to usage ping
merge_request: 60978
author:
type: changed
...@@ -453,7 +453,8 @@ metric counters. ...@@ -453,7 +453,8 @@ metric counters.
| Attribute | Type | Required | Description | | Attribute | Type | Required | Description |
|:----------|:-------|:---------|:------------| |:----------|:-------|:---------|:------------|
| `gitops_sync_count` | integer| yes | The number to increase the `gitops_sync_count` counter by | | `gitops_sync_count` | integer| no | The number to increase the `gitops_sync_count` counter by |
| `k8s_api_proxy_request_count` | integer| no | The number to increase the `k8s_api_proxy_request_count` counter by |
```plaintext ```plaintext
POST /internal/kubernetes/usage_metrics POST /internal/kubernetes/usage_metrics
......
...@@ -2900,6 +2900,18 @@ Status: `data_available` ...@@ -2900,6 +2900,18 @@ Status: `data_available`
Tiers: `premium`, `ultimate` Tiers: `premium`, `ultimate`
### `counts.kubernetes_agent_k8s_api_proxy_request`
Count of Kubernetes API proxy requests
[YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/config/metrics/counts_all/20210505015532_kubernetes_agent_k8s_api_proxy_request.yml)
Group: `group::configure`
Status: `implemented`
Tiers: `premium`, `ultimate`
### `counts.kubernetes_agents` ### `counts.kubernetes_agents`
Count of Kubernetes agents Count of Kubernetes agents
...@@ -4692,7 +4704,7 @@ Tiers: `free`, `premium`, `ultimate` ...@@ -4692,7 +4704,7 @@ Tiers: `free`, `premium`, `ultimate`
Projects with repository mirroring enabled Projects with repository mirroring enabled
[YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/metrics/counts_all/20210216181920_projects_mirrored_with_pipelines_enabled.yml) [YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/config/metrics/counts_all/20210216181920_projects_mirrored_with_pipelines_enabled.yml)
Group: `group::continuous integration` Group: `group::continuous integration`
...@@ -16312,7 +16324,7 @@ Tiers: `free` ...@@ -16312,7 +16324,7 @@ Tiers: `free`
Count creator_id from projects with repository mirroring enabled. Count creator_id from projects with repository mirroring enabled.
[YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/metrics/counts_all/20210216181934_projects_mirrored_with_pipelines_enabled.yml) [YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/config/metrics/counts_all/20210216181934_projects_mirrored_with_pipelines_enabled.yml)
Group: `group::continuous integration` Group: `group::continuous integration`
...@@ -18232,7 +18244,7 @@ Tiers: `free` ...@@ -18232,7 +18244,7 @@ Tiers: `free`
Count creator_id from projects with repository mirroring enabled. Count creator_id from projects with repository mirroring enabled.
[YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/config/metrics/counts_28d/20210216181943_projects_mirrored_with_pipelines_enabled.yml) [YAML definition](https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/config/metrics/counts_28d/20210216181943_projects_mirrored_with_pipelines_enabled.yml)
Group: `group::continuous integration` Group: `group::continuous integration`
......
---
key_path: counts.kubernetes_agent_k8s_api_proxy_request
description: Count of Kubernetes API proxy requests
product_section: ops
product_stage: configure
product_group: group::configure
product_category: kubernetes_management
value_type: number
status: implemented
milestone: "13.12"
introduced_by_url:
time_frame: all
data_source: redis
distribution:
- ee
tier:
- premium
- ultimate
...@@ -107,18 +107,18 @@ module API ...@@ -107,18 +107,18 @@ module API
detail 'Updates usage metrics for agent' detail 'Updates usage metrics for agent'
end end
params do params do
requires :gitops_sync_count, type: Integer, desc: 'The count to increment the gitops_sync metric by' optional :gitops_sync_count, type: Integer, desc: 'The count to increment the gitops_sync metric by'
optional :k8s_api_proxy_request_count, type: Integer, desc: 'The count to increment the k8s_api_proxy_request_count metric by'
end end
post '/' do post '/' do
gitops_sync_count = params[:gitops_sync_count] events = params.slice(:gitops_sync_count, :k8s_api_proxy_request_count)
events.transform_keys! { |event| event.to_s.chomp('_count') }
if gitops_sync_count < 0 Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_event_counts(events)
bad_request!('gitops_sync_count must be greater than or equal to zero')
else
Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_gitops_sync(gitops_sync_count)
no_content! no_content!
end rescue ArgumentError => e
bad_request!(e.message)
end end
end end
end end
......
...@@ -4,17 +4,27 @@ module Gitlab ...@@ -4,17 +4,27 @@ module Gitlab
module UsageDataCounters module UsageDataCounters
class KubernetesAgentCounter < BaseCounter class KubernetesAgentCounter < BaseCounter
PREFIX = 'kubernetes_agent' PREFIX = 'kubernetes_agent'
KNOWN_EVENTS = %w[gitops_sync].freeze KNOWN_EVENTS = %w[gitops_sync k8s_api_proxy_request].freeze
class << self class << self
def increment_gitops_sync(incr) def increment_event_counts(events)
raise ArgumentError, 'must be greater than or equal to zero' if incr < 0 validate!(events)
# rather then hitting redis for this no-op, we return early events.each do |event, incr|
# note: redis returns the increment, so we mimic this here # rather then hitting redis for this no-op, we return early
return 0 if incr == 0 next if incr == 0
increment_by(redis_key(:gitops_sync), incr) increment_by(redis_key(event), incr)
end
end
private
def validate!(events)
events.each do |event, incr|
raise ArgumentError, "unknown event #{event}" unless event.in?(KNOWN_EVENTS)
raise ArgumentError, "#{event} count must be greater than or equal to zero" if incr < 0
end
end end
end end
end end
......
...@@ -3,21 +3,57 @@ ...@@ -3,21 +3,57 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::UsageDataCounters::KubernetesAgentCounter do RSpec.describe Gitlab::UsageDataCounters::KubernetesAgentCounter do
it_behaves_like 'a redis usage counter', 'Kubernetes Agent', :gitops_sync described_class::KNOWN_EVENTS.each do |event|
it_behaves_like 'a redis usage counter', 'Kubernetes Agent', event
it_behaves_like 'a redis usage counter with totals', :kubernetes_agent, event => 1
end
describe '.increment_event_counts' do
let(:events) do
{
'gitops_sync' => 1,
'k8s_api_proxy_request' => 2
}
end
it_behaves_like 'a redis usage counter with totals', :kubernetes_agent, gitops_sync: 1 subject { described_class.increment_event_counts(events) }
describe '.increment_gitops_sync' do it 'increments the specified counters by the new increment amount' do
it 'increments the gtops_sync counter by the new increment amount' do described_class.increment_event_counts(events)
described_class.increment_gitops_sync(7) described_class.increment_event_counts(events)
described_class.increment_gitops_sync(2) described_class.increment_event_counts(events)
described_class.increment_gitops_sync(0)
expect(described_class.totals).to eq(kubernetes_agent_gitops_sync: 9) expect(described_class.totals).to eq(kubernetes_agent_gitops_sync: 3, kubernetes_agent_k8s_api_proxy_request: 6)
end end
it 'raises for negative numbers' do context 'event is unknown' do
expect { described_class.increment_gitops_sync(-1) }.to raise_error(ArgumentError) let(:events) do
{
'gitops_sync' => 1,
'other_event' => 2
}
end
it 'raises an ArgumentError' do
expect(described_class).not_to receive(:increment_by)
expect { subject }.to raise_error(ArgumentError, 'unknown event other_event')
end
end
context 'increment is negative' do
let(:events) do
{
'gitops_sync' => -1,
'k8s_api_proxy_request' => 2
}
end
it 'raises an ArgumentError' do
expect(described_class).not_to receive(:increment_by)
expect { subject }.to raise_error(ArgumentError, 'gitops_sync count must be greater than or equal to zero')
end
end end
end end
end end
...@@ -774,6 +774,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do ...@@ -774,6 +774,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.usage_counters } subject { described_class.usage_counters }
it { is_expected.to include(:kubernetes_agent_gitops_sync) } it { is_expected.to include(:kubernetes_agent_gitops_sync) }
it { is_expected.to include(:kubernetes_agent_k8s_api_proxy_request) }
it { is_expected.to include(:static_site_editor_views) } it { is_expected.to include(:static_site_editor_views) }
it { is_expected.to include(:package_events_i_package_pull_package) } it { is_expected.to include(:package_events_i_package_pull_package) }
it { is_expected.to include(:package_events_i_package_delete_package_by_user) } it { is_expected.to include(:package_events_i_package_delete_package_by_user) }
......
...@@ -67,26 +67,26 @@ RSpec.describe API::Internal::Kubernetes do ...@@ -67,26 +67,26 @@ RSpec.describe API::Internal::Kubernetes do
context 'is authenticated for an agent' do context 'is authenticated for an agent' do
let!(:agent_token) { create(:cluster_agent_token) } let!(:agent_token) { create(:cluster_agent_token) }
it 'returns no_content for valid gitops_sync_count' do it 'returns no_content for valid events' do
send_request(params: { gitops_sync_count: 10 }) send_request(params: { gitops_sync_count: 10, k8s_api_proxy_request_count: 5 })
expect(response).to have_gitlab_http_status(:no_content) expect(response).to have_gitlab_http_status(:no_content)
end end
it 'returns no_content 0 gitops_sync_count' do it 'returns no_content for counts of zero' do
send_request(params: { gitops_sync_count: 0 }) send_request(params: { gitops_sync_count: 0, k8s_api_proxy_request_count: 0 })
expect(response).to have_gitlab_http_status(:no_content) expect(response).to have_gitlab_http_status(:no_content)
end end
it 'returns 400 for non number' do it 'returns 400 for non number' do
send_request(params: { gitops_sync_count: 'string' }) send_request(params: { gitops_sync_count: 'string', k8s_api_proxy_request_count: 1 })
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
end end
it 'returns 400 for negative number' do it 'returns 400 for negative number' do
send_request(params: { gitops_sync_count: '-1' }) send_request(params: { gitops_sync_count: -1, k8s_api_proxy_request_count: 1 })
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
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