Skip to content

Commit b1f4537

Browse files
authored
Merge pull request #3112 from PolicyEngine/feat/add-metadata-to-simulation-api-calls
Add ingredient metadata to simulation API calls
2 parents 254decc + 250f65e commit b1f4537

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

changelog_entry.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bump: patch
2+
changes:
3+
added:
4+
- Ingredient metadata (policy IDs, process ID) to simulation API calls for improved Logfire tracing

policyengine_api/services/economy_service.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,17 @@ def _handle_create_impact(
433433
}
434434
)
435435

436-
sim_api_execution = simulation_api.run(sim_config.model_dump())
436+
# Build params with metadata for Logfire tracing in the simulation API.
437+
# The _metadata field will be captured by the Logfire span before
438+
# SimulationOptions validation (which silently ignores extra fields).
439+
sim_params = sim_config.model_dump()
440+
sim_params["_metadata"] = {
441+
"reform_policy_id": setup_options.reform_policy_id,
442+
"baseline_policy_id": setup_options.baseline_policy_id,
443+
"process_id": setup_options.process_id,
444+
}
445+
446+
sim_api_execution = simulation_api.run(sim_params)
437447
execution_id = simulation_api.get_execution_id(sim_api_execution)
438448

439449
progress_log = {

tests/unit/services/test_economy_service.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,41 @@ def test__given_no_previous_impact__creates_new_simulation(
186186
mock_simulation_api.run.assert_called_once()
187187
mock_reform_impacts_service.set_reform_impact.assert_called_once()
188188

189+
def test__given_no_previous_impact__includes_metadata_in_simulation_params(
190+
self,
191+
economy_service,
192+
base_params,
193+
mock_country_package_versions,
194+
mock_get_dataset_version,
195+
mock_policy_service,
196+
mock_reform_impacts_service,
197+
mock_simulation_api,
198+
mock_logger,
199+
mock_datetime,
200+
mock_numpy_random,
201+
):
202+
"""Verify that _metadata with policy IDs is passed to simulation API."""
203+
mock_reform_impacts_service.get_all_reform_impacts.return_value = (
204+
[]
205+
)
206+
207+
economy_service.get_economic_impact(**base_params)
208+
209+
# Get the params passed to simulation_api.run()
210+
call_args = mock_simulation_api.run.call_args
211+
sim_params = call_args[0][0] # First positional argument
212+
213+
# Verify _metadata is included with correct values
214+
assert "_metadata" in sim_params
215+
assert (
216+
sim_params["_metadata"]["reform_policy_id"] == MOCK_POLICY_ID
217+
)
218+
assert (
219+
sim_params["_metadata"]["baseline_policy_id"]
220+
== MOCK_BASELINE_POLICY_ID
221+
)
222+
assert sim_params["_metadata"]["process_id"] == MOCK_PROCESS_ID
223+
189224
def test__given_exception__raises_error(
190225
self,
191226
economy_service,

0 commit comments

Comments
 (0)