Commit e21aff62 authored by allison.browne's avatar allison.browne

Add grafana embedded chart usage data

Add a new class to lib that caches usage data
and write to grafana embed usage data to the Rails
cache during the usage ping background job prior to
sending the usage ping POST request.
parent 3c80169a
......@@ -18,6 +18,9 @@ class GitlabUsagePingWorker
# Splay the request over a minute to avoid thundering herd problems.
sleep(rand(0.0..60.0).round(3))
# Cache to decouple long running work from the http request context
Gitlab::GrafanaEmbedUsageData.write_issue_count
SubmitUsagePingService.new.execute
end
......
# frozen_string_literal: true
module Gitlab
class GrafanaEmbedUsageData
class << self
def issue_count
Rails.cache.read(:grafana_embed_issue_count)
end
def write_issue_count
count = get_embed_count
Rails.cache.write(:grafana_embed_issue_count, count)
count
end
private
def get_embed_count
Issue.class_eval { include EachBatch } unless Issue < EachBatch
count = 0
Issue.each_batch do |issue_batch|
embed_count_per_batch = issue_batch.map do |issue|
has_grafana_url?(issue)
end.count(&:itself)
count += embed_count_per_batch
end
count
end
def has_grafana_url?(issue)
html = Banzai.render(issue.description, project: issue.project)
result = Banzai::Filter::InlineGrafanaMetricsFilter.new(
html, { project: issue.project }
).call
metric_node = result.at_css('.js-render-metrics')
metric_node ? metric_node&.attribute('data-dashboard-url').to_s : nil
end
end
end
end
......@@ -84,6 +84,7 @@ module Gitlab
issues: count(Issue),
issues_with_associated_zoom_link: count(ZoomMeeting.added_to_issue),
issues_using_zoom_quick_actions: count(ZoomMeeting.select(:issue_id).distinct),
issues_with_embeded_grafana_charts: Gitlab::GrafanaEmbedUsageData.count_issues,
keys: count(Key),
label_lists: count(List.label),
lfs_objects: count(LfsObject),
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::GrafanaEmbedUsageData do
describe '#issue_count', :use_clean_rails_memory_store_caching do
subject do
described_class.write_issue_count
described_class.issue_count
end
let(:project) { create(:project) }
let(:description_with_embed) { "Some comment\n\nhttps://grafana.example.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
shared_examples "zero count" do
context "without grafana embeds" do
it "does not count the issue" do
expect(subject).to eq(0)
end
end
end
context 'with grafana integrated project' do
before do
create(:grafana_integration, project: project)
end
it 'counts multiple issues with grafana links' do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
expect(subject).to eq(2)
end
context 'issue description has no grafana link' do
before do
create(:issue, project: project)
end
it_behaves_like('zero count')
end
context 'issue description is nil' do
before do
create(:issue, project: project, description: nil)
end
it_behaves_like('zero count')
end
context 'issue description is empty' do
before do
create(:issue, project: project, description: '')
end
it_behaves_like('zero count')
end
context 'issue description has grafana link for un-integrated grafana instance' do
before do
create(:issue, project: project, description: description_with_unintegrated_embed)
end
it_behaves_like('zero count')
end
end
context 'with an un-integrated project' do
context 'with one issue having a grafana link in the description and one without' do
before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end
it_behaves_like('zero count')
end
context 'with issue description nil' do
before do
create(:issue, project: project, description: nil)
end
it_behaves_like('zero count')
end
context 'with issue description empty' do
before do
create(:issue, project: project, description: '')
end
it_behaves_like('zero count')
end
end
end
end
......@@ -8,6 +8,7 @@ describe GitlabUsagePingWorker do
it 'delegates to SubmitUsagePingService' do
allow(subject).to receive(:try_obtain_lease).and_return(true)
expect(Gitlab::GrafanaEmbedUsageData).to receive(:write_issue_count)
expect_any_instance_of(SubmitUsagePingService).to receive(:execute)
subject.perform
......
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