Commit d930a215 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch 'report-log-usage' into 'master'

Include pod log usage in the usage ping

Closes gitlab-ce#56615

See merge request gitlab-org/gitlab-ee!10370
parents 1b4668ae 2a59235d
...@@ -15,6 +15,7 @@ module EE ...@@ -15,6 +15,7 @@ module EE
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json do
::Gitlab::PodLogsUsageCounter.increment(project.id)
::Gitlab::PollingInterval.set_header(response, interval: 3_000) ::Gitlab::PollingInterval.set_header(response, interval: 3_000)
render json: { render json: {
......
---
title: Collect usage of pod logs feature
merge_request: 10370
author:
type: added
...@@ -132,6 +132,13 @@ module EE ...@@ -132,6 +132,13 @@ module EE
usage_data usage_data
end end
override :usage_counters
def usage_counters
super.merge(
pod_logs_usages: ::Gitlab::PodLogsUsageCounter.usage_totals
)
end
override :jira_usage override :jira_usage
def jira_usage def jira_usage
super.merge( super.merge(
......
# frozen_string_literal: true
module Gitlab
module PodLogsUsageCounter
BASE_KEY = "POD_LOGS_USAGE_COUNTS"
class << self
def increment(project_id)
Gitlab::Redis::SharedState.with { |redis| redis.hincrby(BASE_KEY, project_id, 1) }
end
def usage_totals
Gitlab::Redis::SharedState.with do |redis|
total_sum = 0
totals = redis.hgetall(BASE_KEY).each_with_object({}) do |(project_id, count), result|
total_sum += result[project_id.to_i] = count.to_i
end
totals[:total] = total_sum
totals
end
end
end
end
end
...@@ -117,6 +117,12 @@ describe Projects::EnvironmentsController do ...@@ -117,6 +117,12 @@ describe Projects::EnvironmentsController do
expect(json_response["logs"]).to match_array(["Log 1", "Log 2", "Log 3"]) expect(json_response["logs"]).to match_array(["Log 1", "Log 2", "Log 3"])
expect(json_response["pods"]).to match_array([pod_name]) expect(json_response["pods"]).to match_array([pod_name])
end end
it 'registers a usage of the endpoint' do
expect(::Gitlab::PodLogsUsageCounter).to receive(:increment).with(project.id)
get :logs, params: environment_params(pod_name: pod_name, format: :json)
end
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::PodLogsUsageCounter, :clean_gitlab_redis_shared_state do
describe '.increment' do
it 'intializes and increments the counter for the project by 1' do
expect do
described_class.increment(12)
end.to change { described_class.usage_totals[12] }.from(nil).to(1)
end
end
describe '.usage_totals' do
context 'when the feature has not been used' do
it 'returns the total counts and counts per project' do
expect(described_class.usage_totals.keys).to eq([:total])
expect(described_class.usage_totals[:total]).to eq(0)
end
end
context 'when the feature has been used in multiple projects' do
before do
described_class.increment(12)
described_class.increment(16)
end
it 'returns the total counts and counts per project' do
expect(described_class.usage_totals[12]).to eq(1)
expect(described_class.usage_totals[16]).to eq(1)
expect(described_class.usage_totals[:total]).to eq(2)
end
end
end
end
...@@ -52,6 +52,7 @@ describe Gitlab::UsageData do ...@@ -52,6 +52,7 @@ describe Gitlab::UsageData do
license_id license_id
elasticsearch_enabled elasticsearch_enabled
geo_enabled geo_enabled
pod_logs_usages
)) ))
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