Skip to content

Commit 59cf5e0

Browse files
authored
ref(cleanup): Minor changes (#101558)
Changes included: * Start transaction for each process * Add metric to track process errors
1 parent 622aa37 commit 59cf5e0

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/sentry/runner/commands/cleanup.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
7878
configure()
7979

8080
from sentry import deletions, models, similarity
81+
from sentry.utils import metrics
8182

82-
skip_models = [
83+
skip_child_relations_models = [
8384
# Handled by other parts of cleanup
8485
models.EventAttachment,
8586
models.UserReport,
@@ -98,19 +99,25 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
9899
return
99100

100101
model_name, chunk = j
101-
model = import_string(model_name)
102+
102103
try:
103-
task = deletions.get(
104-
model=model,
105-
query={"id__in": chunk},
106-
skip_models=skip_models,
107-
transaction_id=uuid4().hex,
108-
)
104+
with sentry_sdk.start_transaction(
105+
op="cleanup", name="multiprocess_worker"
106+
) as transaction:
107+
transaction.set_tag("model", model_name)
108+
model = import_string(model_name)
109+
task = deletions.get(
110+
model=model,
111+
query={"id__in": chunk},
112+
skip_models=skip_child_relations_models,
113+
transaction_id=uuid4().hex,
114+
)
109115

110-
while True:
111-
if not task.chunk(apply_filter=True):
112-
break
116+
while True:
117+
if not task.chunk(apply_filter=True):
118+
break
113119
except Exception:
120+
metrics.incr("cleanup.error", instance=model_name, sample_rate=1.0)
114121
logger.exception("Error in multiprocess_worker.")
115122
finally:
116123
task_queue.task_done()
@@ -137,7 +144,8 @@ def multiprocess_worker(task_queue: _WorkQueue) -> None:
137144
"-t",
138145
default=False,
139146
is_flag=True,
140-
help="Send the duration of this command to internal metrics.",
147+
hidden=True,
148+
help="(deprecated) Send the duration of this command to internal metrics.",
141149
)
142150
@log_options()
143151
def cleanup(
@@ -166,7 +174,6 @@ def cleanup(
166174
concurrency=concurrency,
167175
silent=silent,
168176
router=router,
169-
timed=timed,
170177
)
171178

172179

@@ -178,7 +185,6 @@ def _cleanup(
178185
concurrency: int,
179186
silent: bool,
180187
router: str | None,
181-
timed: bool,
182188
) -> None:
183189
_validate_and_setup_environment(concurrency, silent)
184190
# Make sure we fork off multiprocessing pool
@@ -189,18 +195,14 @@ def _cleanup(
189195
# transaction context issues in child processes. This ensures only the
190196
# main process tracks the overall cleanup operation performance.
191197
with sentry_sdk.start_transaction(op="cleanup", name="cleanup") as transaction:
192-
transaction.set_tag("router", router)
193-
transaction.set_tag("model", model)
194198
try:
195199
from sentry.runner import configure
196200

197201
configure()
198202

199203
from sentry.utils import metrics
200204

201-
start_time = None
202-
if timed:
203-
start_time = time.time()
205+
start_time = time.time()
204206

205207
# list of models which this query is restricted to
206208
model_list = {m.lower() for m in model}
@@ -286,7 +288,7 @@ def is_filtered(model: type[Model]) -> bool:
286288
# Shut down our pool
287289
_stop_pool(pool, task_queue)
288290

289-
if timed and start_time:
291+
if start_time:
290292
duration = int(time.time() - start_time)
291293
metrics.timing("cleanup.duration", duration, instance=router, sample_rate=1.0)
292294
click.echo("Clean up took %s second(s)." % duration)

0 commit comments

Comments
 (0)