Skip to content

Commit c0dde6f

Browse files
authored
Add code location tag to runs (#32715)
## Summary & Motivation This PR automatically adds the `dagster/code_location` tag to runs when a `remote_job_origin` is available. This enables filtering runs by code location in the web UI and controlling concurrency by code location using tag concurrency limits. ## How I Tested These Changes How I Tested These Changes Verified that runs created with `remote_job_origin` now include the `dagster/code_location` tag Confirmed the tag value matches the code location name from `remote_job_origin.repository_origin.code_location_origin.location_name` Tested that runs without remote_job_origin are not affected ## Changelog Runs now automatically include a `dagster/code_location` tag when created with a `remote_job_origin`, enabling filtering and concurrency control by code location.
1 parent a9e5542 commit c0dde6f

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

python_modules/dagster/dagster/_core/instance/runs/run_domain.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
ASSET_RESUME_RETRY_TAG,
3636
BACKFILL_ID_TAG,
3737
BACKFILL_TAGS,
38+
CODE_LOCATION_TAG,
3839
PARENT_RUN_ID_TAG,
3940
PARTITION_NAME_TAG,
4041
RESUME_RETRY_TAG,
@@ -222,6 +223,15 @@ def create_run(
222223
"must include a remote job origin when creating a run using a remote asset graph",
223224
)
224225

226+
# Add the code location tag to the validated tags
227+
# This is useful for filtering runs by code location in the web server
228+
# or control concurrency by code location with tag concurrency limits.
229+
if remote_job_origin:
230+
validated_tags = {
231+
**validated_tags,
232+
CODE_LOCATION_TAG: remote_job_origin.repository_origin.code_location_origin.location_name,
233+
}
234+
225235
dagster_run = self.construct_run_with_snapshots(
226236
job_name=job_name,
227237
run_id=run_id, # type: ignore # (possible none)

python_modules/dagster/dagster/_core/storage/tags.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
REPOSITORY_LABEL_TAG = f"{HIDDEN_TAG_PREFIX}repository"
99

10+
CODE_LOCATION_TAG = f"{SYSTEM_TAG_PREFIX}code_location"
11+
1012
SCHEDULE_NAME_TAG = f"{SYSTEM_TAG_PREFIX}schedule_name"
1113

1214
SENSOR_NAME_TAG = f"{SYSTEM_TAG_PREFIX}sensor_name"

python_modules/dagster/dagster_tests/core_tests/instance_tests/test_instance.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from dagster._core.storage.tags import (
3939
ASSET_PARTITION_RANGE_END_TAG,
4040
ASSET_PARTITION_RANGE_START_TAG,
41+
CODE_LOCATION_TAG,
4142
PARTITION_NAME_TAG,
4243
)
4344
from dagster._core.test_utils import (
@@ -365,6 +366,40 @@ def test_submit_run():
365366
assert instance.run_coordinator.queue()[0].run_id == run.run_id # pyright: ignore[reportAttributeAccessIssue]
366367

367368

369+
def test_create_run_adds_code_location_tag():
370+
"""Test that CODE_LOCATION_TAG is automatically added when remote_job_origin is provided."""
371+
with dg.instance_for_test() as instance:
372+
with get_bar_workspace(instance) as workspace:
373+
remote_job = (
374+
workspace.get_code_location("bar_code_location")
375+
.get_repository("bar_repo")
376+
.get_full_job("foo")
377+
)
378+
379+
run = create_run_for_test(
380+
instance=instance,
381+
job_name=remote_job.name,
382+
remote_job_origin=remote_job.get_remote_origin(),
383+
job_code_origin=remote_job.get_python_origin(),
384+
)
385+
386+
# Verify CODE_LOCATION_TAG is automatically added
387+
assert CODE_LOCATION_TAG in run.tags
388+
assert run.tags[CODE_LOCATION_TAG] == "bar_code_location"
389+
390+
391+
def test_create_run_without_remote_job_origin_has_no_code_location_tag():
392+
"""Test that CODE_LOCATION_TAG is not added when remote_job_origin is not provided."""
393+
with dg.instance_for_test() as instance:
394+
run = create_run_for_test(
395+
instance=instance,
396+
job_name="foo_job",
397+
)
398+
399+
# Verify CODE_LOCATION_TAG is not present
400+
assert CODE_LOCATION_TAG not in run.tags
401+
402+
368403
def test_create_run_without_asset_execution_type_on_snapshot():
369404
# verify that even runs created on older versions of dagster still store the
370405
# execution type on the execution plan snapshot

0 commit comments

Comments
 (0)