Commit 56dec4d4 authored by Thong Kuah's avatar Thong Kuah

Merge branch '327414-agent-proxy-request-count' into 'master'

Add kubernetes_agent_k8s_api_proxy_request to usage ping

See merge request gitlab-org/gitlab!60978
parents a7a5ab68 7ed1e686
---
title: Add kubernetes_agent_proxy_request to usage ping
merge_request: 60978
author:
type: changed
......@@ -453,7 +453,8 @@ metric counters.
| 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
POST /internal/kubernetes/usage_metrics
......
......@@ -2902,6 +2902,18 @@ Status: `data_available`
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`
Count of Kubernetes agents
......
---
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
detail 'Updates usage metrics for agent'
end
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
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
bad_request!('gitops_sync_count must be greater than or equal to zero')
else
Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_gitops_sync(gitops_sync_count)
Gitlab::UsageDataCounters::KubernetesAgentCounter.increment_event_counts(events)
no_content!
end
no_content!
rescue ArgumentError => e
bad_request!(e.message)
end
end
end
......
......@@ -4,17 +4,27 @@ module Gitlab
module UsageDataCounters
class KubernetesAgentCounter < BaseCounter
PREFIX = 'kubernetes_agent'
KNOWN_EVENTS = %w[gitops_sync].freeze
KNOWN_EVENTS = %w[gitops_sync k8s_api_proxy_request].freeze
class << self
def increment_gitops_sync(incr)
raise ArgumentError, 'must be greater than or equal to zero' if incr < 0
def increment_event_counts(events)
validate!(events)
# rather then hitting redis for this no-op, we return early
# note: redis returns the increment, so we mimic this here
return 0 if incr == 0
events.each do |event, incr|
# rather then hitting redis for this no-op, we return early
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
......
......@@ -3,21 +3,57 @@
require 'spec_helper'
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 gtops_sync counter by the new increment amount' do
described_class.increment_gitops_sync(7)
described_class.increment_gitops_sync(2)
described_class.increment_gitops_sync(0)
it 'increments the specified counters by the new increment amount' do
described_class.increment_event_counts(events)
described_class.increment_event_counts(events)
described_class.increment_event_counts(events)
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
it 'raises for negative numbers' do
expect { described_class.increment_gitops_sync(-1) }.to raise_error(ArgumentError)
context 'event is unknown' do
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
......@@ -774,6 +774,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
subject { described_class.usage_counters }
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(:package_events_i_package_pull_package) }
it { is_expected.to include(:package_events_i_package_delete_package_by_user) }
......
......@@ -67,26 +67,26 @@ RSpec.describe API::Internal::Kubernetes do
context 'is authenticated for an agent' do
let!(:agent_token) { create(:cluster_agent_token) }
it 'returns no_content for valid gitops_sync_count' do
send_request(params: { gitops_sync_count: 10 })
it 'returns no_content for valid events' do
send_request(params: { gitops_sync_count: 10, k8s_api_proxy_request_count: 5 })
expect(response).to have_gitlab_http_status(:no_content)
end
it 'returns no_content 0 gitops_sync_count' do
send_request(params: { gitops_sync_count: 0 })
it 'returns no_content for counts of zero' do
send_request(params: { gitops_sync_count: 0, k8s_api_proxy_request_count: 0 })
expect(response).to have_gitlab_http_status(:no_content)
end
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)
end
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)
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