Commit b84abf5e authored by Andreas Brandl's avatar Andreas Brandl

Expose gauges to report status of partitioning

The two metrics are being reported on a per table basis. This allows
monitoring systems to detect a situation where a partition wasn't
created over an extended period of time. This is potentially a
disastrous state if a relevant partition hasn't been created yet but is
needed to create new records.

Related to https://gitlab.com/gitlab-org/gitlab/-/issues/227353
parent 1949b6bb
...@@ -9,5 +9,7 @@ class PartitionCreationWorker ...@@ -9,5 +9,7 @@ class PartitionCreationWorker
def perform def perform
Gitlab::Database::Partitioning::PartitionCreator.new.create_partitions Gitlab::Database::Partitioning::PartitionCreator.new.create_partitions
ensure
Gitlab::Database::Partitioning::PartitionMonitoring.new.report_metrics
end end
end end
...@@ -194,6 +194,15 @@ The following metrics are available: ...@@ -194,6 +194,15 @@ The following metrics are available:
|:--------------------------------- |:--------- |:------------------------------------------------------------- |:-------------------------------------- | |:--------------------------------- |:--------- |:------------------------------------------------------------- |:-------------------------------------- |
| `db_load_balancing_hosts` | Gauge | [12.3](https://gitlab.com/gitlab-org/gitlab/-/issues/13630) | Current number of load balancing hosts | | `db_load_balancing_hosts` | Gauge | [12.3](https://gitlab.com/gitlab-org/gitlab/-/issues/13630) | Current number of load balancing hosts |
## Database partitioning metrics **(PREMIUM ONLY)**
The following metrics are available:
| Metric | Type | Since | Description |
|:--------------------------------- |:--------- |:------------------------------------------------------------- |:----------------------------------------------------------------- |
| `db_partitions_present` | Gauge | [13.4](https://gitlab.com/gitlab-org/gitlab/-/issues/227353) | Number of database partitions present |
| `db_partitions_missing` | Gauge | [13.4](https://gitlab.com/gitlab-org/gitlab/-/issues/227353) | Number of database partitions currently expected, but not present |
## Connection pool metrics ## Connection pool metrics
These metrics record the status of the database These metrics record the status of the database
......
# frozen_string_literal: true
module Gitlab
module Database
module Partitioning
class PartitionMonitoring
attr_reader :models
def initialize(models = PartitionCreator.models)
@models = models
end
def report_metrics
models.each do |model|
strategy = model.partitioning_strategy
gauge_present.set({ table: model.table_name }, strategy.current_partitions.size)
gauge_missing.set({ table: model.table_name }, strategy.missing_partitions.size)
end
end
private
def gauge_present
@gauge_present ||= Gitlab::Metrics.gauge(:db_partitions_present, 'Number of database partitions present')
end
def gauge_missing
@gauge_missing ||= Gitlab::Metrics.gauge(:db_partitions_missing, 'Number of database partitions currently expected, but not present')
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Partitioning::PartitionMonitoring do
describe '#report_metrics' do
subject { described_class.new(models).report_metrics }
let(:models) { [model] }
let(:model) { double(partitioning_strategy: partitioning_strategy, table_name: table) }
let(:partitioning_strategy) { double(missing_partitions: missing_partitions, current_partitions: current_partitions) }
let(:table) { "some_table" }
let(:missing_partitions) do
[double]
end
let(:current_partitions) do
[double, double]
end
it 'reports number of present partitions' do
subject
expect(Gitlab::Metrics.registry.get(:db_partitions_present).get({ table: table })).to eq(current_partitions.size)
end
it 'reports number of missing partitions' do
subject
expect(Gitlab::Metrics.registry.get(:db_partitions_missing).get({ table: table })).to eq(missing_partitions.size)
end
end
end
...@@ -4,16 +4,26 @@ require "spec_helper" ...@@ -4,16 +4,26 @@ require "spec_helper"
RSpec.describe PartitionCreationWorker do RSpec.describe PartitionCreationWorker do
describe '#perform' do describe '#perform' do
let(:creator) { double(create_partitions: nil) } subject { described_class.new.perform }
let(:creator) { instance_double('PartitionCreator', create_partitions: nil) }
let(:monitoring) { instance_double('PartitionMonitoring', report_metrics: nil) }
before do before do
allow(Gitlab::Database::Partitioning::PartitionCreator).to receive(:new).and_return(creator) allow(Gitlab::Database::Partitioning::PartitionCreator).to receive(:new).and_return(creator)
allow(Gitlab::Database::Partitioning::PartitionMonitoring).to receive(:new).and_return(monitoring)
end end
it 'delegates to PartitionCreator' do it 'delegates to PartitionCreator' do
expect(creator).to receive(:create_partitions) expect(creator).to receive(:create_partitions)
described_class.new.perform subject
end
it 'reports partition metrics' do
expect(monitoring).to receive(:report_metrics)
subject
end end
end end
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