Skip to content

Commit

Permalink
Merge branch 'feat/DEX-2707/add-metrics-for-delete-items' into 'master'
Browse files Browse the repository at this point in the history
[DEX-2707] feat: add metrics for delete items

Closes DEX-2707

See merge request nstmrt/rubygems/outbox!110
  • Loading branch information
Arlantir committed Jan 15, 2025
2 parents 15bb401 + 77e4fa8 commit bcaf620
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [6.13.0] - 2025-01-15

### Added

- Add metrics `delete_latency` and `deleted_counter`

## [6.12.0] - 2025-01-10

### Added
Expand Down
37 changes: 32 additions & 5 deletions app/jobs/sbmt/outbox/base_delete_stale_items_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ def delete_items_in_batches(table, condition)
delete_statement = Arel::Nodes::DeleteStatement.new
delete_statement.relation = table
delete_statement.wheres = [table[:id].in(subquery)]
deleted_count = nil

loop do
deleted_count = item_class
.connection
.execute(delete_statement.to_sql)
.cmd_tuples
track_deleted_latency do
deleted_count = item_class
.connection
.execute(delete_statement.to_sql)
.cmd_tuples
end

track_deleted_counter(deleted_count)

logger.log_info("Deleted #{deleted_count} #{box_type} items for #{box_name} items")
break if deleted_count == 0
Expand Down Expand Up @@ -158,8 +163,14 @@ def mysql_delete_in_batches(waterline_failed, waterline_delivered)
end

def delete_items_in_batches_mysql(query)
deleted_count = nil

loop do
deleted_count = query.limit(BATCH_SIZE).delete_all
track_deleted_latency do
deleted_count = query.limit(BATCH_SIZE).delete_all
end

track_deleted_counter(deleted_count)

logger.log_info("Deleted #{deleted_count} #{box_type} items for #{box_name} items")
break if deleted_count == 0
Expand All @@ -179,6 +190,22 @@ def database_type
:unknown
end
end

def track_deleted_counter(deleted_count)
::Yabeda
.outbox
.deleted_counter
.increment({box_type: box_type, box_name: box_name}, by: deleted_count)
end

def track_deleted_latency
::Yabeda
.outbox
.delete_latency
.measure({box_type: box_type, box_name: box_name}) do
yield
end
end
end
end
end
10 changes: 10 additions & 0 deletions config/initializers/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
unit: :seconds,
buckets: [0.5, 1, 2.5, 5, 10, 15, 20, 30, 45, 60, 300].freeze,
comment: "A histogram outbox process latency"

histogram :delete_latency,
tags: %i[box_type box_name],
unit: :seconds,
buckets: [0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 20, 30].freeze,
comment: "A histogram for outbox/inbox deletion latency"

counter :deleted_counter,
tags: %i[box_type box_name],
comment: "A counter for the number of deleted outbox/inbox items"
end

group :box_worker do
Expand Down
2 changes: 1 addition & 1 deletion lib/sbmt/outbox/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sbmt
module Outbox
VERSION = "6.12.0"
VERSION = "6.13.0"
end
end
8 changes: 6 additions & 2 deletions spec/jobs/sbmt/outbox/base_delete_stale_items_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ def item_classes
end
end

it "deletes items with status 2 and old items with status 1" do
it "deletes items with status 2 and old items with status 1 and tracks metrics" do
expect { job_class.perform_now("OutboxItem") }
.to change(OutboxItem, :count).by(-2)
.and increment_yabeda_counter(Yabeda.outbox.deleted_counter).with_tags(box_name: "outbox_item", box_type: :outbox).by(2)
.and measure_yabeda_histogram(Yabeda.outbox.delete_latency).with_tags(box_name: "outbox_item", box_type: :outbox)
end

context "when an element with status 1 does not retention" do
let(:created_at) { 6.hours.ago }

it "doesn't delete item with status 1 but deletes item with status 2" do
it "doesn't delete item with status 1 but deletes item with status 2 and tracks metrics" do
expect { job_class.perform_now("OutboxItem") }
.to change(OutboxItem, :count).by(-1)
.and increment_yabeda_counter(Yabeda.outbox.deleted_counter).with_tags(box_name: "outbox_item", box_type: :outbox).by(1)
.and measure_yabeda_histogram(Yabeda.outbox.delete_latency).with_tags(box_name: "outbox_item", box_type: :outbox)
end
end
end

0 comments on commit bcaf620

Please sign in to comment.