|
| 1 | +# Add Telemetry to a SageMaker Python SDK Feature |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This SOP instruments a SageMaker Python SDK V3 feature with usage telemetry using the |
| 6 | +SDK's standard `@_telemetry_emitter` decorator from `sagemaker.core.telemetry`. It is |
| 7 | +intended for partner teams who contribute features and need a clear, consistent, |
| 8 | +low-overhead way to add telemetry to their own public APIs. |
| 9 | + |
| 10 | +Use this SOP when a new (or existing) public method or module-level function in one of the |
| 11 | +V3 modules (`sagemaker-core`, `sagemaker-train`, `sagemaker-serve`, `sagemaker-mlops`) |
| 12 | +should report usage. The outcome is: the feature's public entry points carry the |
| 13 | +`@_telemetry_emitter(Feature.<X>, "<name>")` decorator, a valid `Feature` is used (creating |
| 14 | +one if none fits), matching unit tests exist, and the module builds green — without changing |
| 15 | +the feature's behavior. |
| 16 | + |
| 17 | +Do NOT use this SOP to design new telemetry infrastructure or to emit metrics to a custom |
| 18 | +backend; it only applies the existing SDK telemetry contract to feature code. |
| 19 | + |
| 20 | +## Parameters |
| 21 | + |
| 22 | +- **target_path** (required): Repo-relative path to the feature's Python file or package |
| 23 | + directory to instrument (e.g., `sagemaker-train/src/sagemaker/train/ai_registry/dataset.py`). |
| 24 | +- **feature_name** (required): The telemetry feature to attribute usage to. Either the name |
| 25 | + of an existing `Feature` enum member (e.g., `MODEL_CUSTOMIZATION`, `FEATURE_STORE`, |
| 26 | + `MODEL_TRAINER`) or a new `UPPER_SNAKE_CASE` name to create if no existing member fits. |
| 27 | +- **entry_points** (optional): Explicit list of public entry points to instrument, each as |
| 28 | + `Class.method` or `module.function`. If omitted, the SOP detects the public entry points |
| 29 | + in `target_path`. |
| 30 | +- **telemetry_params** (optional): Granular parameters to capture per entry point, each as |
| 31 | + a `(name, type)` pair where `type` is one of `ATTR_VALUE`, `ATTR_EXISTS`, `ATTR_CALL`, |
| 32 | + `KWARG_VALUE`, `KWARG_EXISTS` (from `TelemetryParamType`). |
| 33 | +- **workspace_root** (optional, default: current directory): Absolute path to the |
| 34 | + `sagemaker-python-sdk` repository root that contains the `sagemaker-core` module. |
| 35 | + |
| 36 | +**Constraints for parameter acquisition:** |
| 37 | +- If all required parameters are already provided, You MUST proceed to the Steps |
| 38 | +- If any required parameters are missing, You MUST ask for them before proceeding |
| 39 | +- When asking for parameters, You MUST request all parameters in a single prompt |
| 40 | +- When asking for parameters, You MUST use the exact parameter names as defined |
| 41 | + |
| 42 | +## Steps |
| 43 | + |
| 44 | +### 1. Locate the feature and its public entry points |
| 45 | +Open `target_path` under `workspace_root` and determine the set of public entry points to |
| 46 | +instrument. A public entry point is a user-facing method or module-level function that a |
| 47 | +customer calls directly. |
| 48 | + |
| 49 | +**Constraints:** |
| 50 | +- You MUST resolve `target_path` relative to `workspace_root` and read the file(s) before deciding anything, because instrumenting the wrong functions produces misleading telemetry. |
| 51 | +- If `entry_points` is provided, You MUST instrument exactly those and no others. |
| 52 | +- If `entry_points` is not provided, You MUST select only public entry points and You MUST NOT select names beginning with an underscore (private/internal helpers) because decorating internal helpers inflates and double-counts usage when public methods call them. |
| 53 | +- You MUST exclude `__init__`, simple property getters/setters, and trivial pass-through wrappers, because they do not represent a feature invocation. |
| 54 | +- You MUST produce an explicit list of fully-qualified targets in the form `Class.method` or `module.function` and confirm it is non-empty before advancing. |
| 55 | + |
| 56 | +### 2. Resolve the Feature enum value |
| 57 | +Decide which `Feature` enum member attributes this usage. Use an existing member when one |
| 58 | +fits; otherwise create a new one. |
| 59 | + |
| 60 | +**Constraints:** |
| 61 | +- You MUST read `sagemaker-core/src/sagemaker/core/telemetry/constants.py` (the `Feature` enum) and the `FEATURE_TO_CODE` dictionary in `sagemaker-core/src/sagemaker/core/telemetry/telemetry_logging.py` before deciding. |
| 62 | +- If `feature_name` matches an existing `Feature` member, You MUST reuse it and You MUST NOT create a duplicate, because duplicate members fragment the usage data for one feature across two codes. |
| 63 | +- If `feature_name` does not match an existing member, You MUST add it in TWO places using the same next unused integer: (a) the `Feature` enum in `constants.py`, and (b) the `FEATURE_TO_CODE` dictionary in `telemetry_logging.py`. |
| 64 | +- You MUST NOT add the new member to only one of those two locations, because the emitter looks up `FEATURE_TO_CODE[str(feature)]` and a missing entry raises `KeyError` that the non-blocking wrapper swallows, so telemetry silently never emits. |
| 65 | +- You MUST NOT reuse or renumber an existing integer code, because codes are a stable contract consumed by downstream telemetry analysis. |
| 66 | +- Validation: You MUST confirm the chosen `Feature` member exists in both the enum and `FEATURE_TO_CODE` (for a new feature, that both edits are present with matching integers) before advancing. |
| 67 | + |
| 68 | +### 3. Apply the `@_telemetry_emitter` decorator to each entry point |
| 69 | +Add the required import(s) and decorate every target from Step 1. |
| 70 | + |
| 71 | +**Constraints:** |
| 72 | +- You MUST import the decorator with `from sagemaker.core.telemetry import Feature, _telemetry_emitter`. |
| 73 | +- If `telemetry_params` is provided, You MUST additionally import `from sagemaker.core.telemetry.telemetry_logging import TelemetryParamType`. |
| 74 | +- You MUST decorate each target as `@_telemetry_emitter(Feature.<FEATURE_NAME>, "<func_name>")`, where `<func_name>` is `"<Class>.<method>"` for methods and `"<function>"` or `"<module>.<function>"` for module-level functions, matching the existing convention in `sagemaker-mlops/src/sagemaker/mlops/feature_store/` and `sagemaker-train/src/sagemaker/ai_registry/`. |
| 75 | +- If `telemetry_params` is provided, You MUST pass it as the `telemetry_params=` keyword argument using the `(name, TelemetryParamType.<TYPE>)` tuple form. |
| 76 | +- You MUST place the `@_telemetry_emitter` decorator as the outermost decorator unless another decorator must wrap it, because ordering changes which call is measured. |
| 77 | +- You MUST NOT change the decorated function's signature, arguments, return value, or logic, because this SOP adds observability only and must not alter feature behavior. |
| 78 | +- You MUST NOT add any code that can raise outside the decorator, and You MUST NOT catch or re-raise telemetry errors yourself, because the emitter already guarantees telemetry never blocks the SDK call and must return the underlying result unchanged. |
| 79 | +- You MUST NOT decorate the same function more than once, because duplicate emitters double-count usage. |
| 80 | +- Validation: You MUST confirm every target now carries exactly one `@_telemetry_emitter` decorator and the imports resolve. |
| 81 | + |
| 82 | +### 4. Add or extend unit tests |
| 83 | +Create or update unit tests that assert the telemetry decorator is applied and, when |
| 84 | +`telemetry_params` is used, that the expected parameters are emitted. |
| 85 | + |
| 86 | +**Constraints:** |
| 87 | +- You MUST mirror the patterns in `sagemaker-core/tests/unit/telemetry/test_granular_telemetry.py` (use `unittest.mock` `Mock`/`patch`, assert on the emitted `&x-...` query parameters and the `Feature` used). |
| 88 | +- You MUST place tests in the same module's `tests/unit/` telemetry test location as the code you changed. |
| 89 | +- If you created a new `Feature` in Step 2, You MUST add an assertion that the new member is present in both the `Feature` enum and `FEATURE_TO_CODE`, because that mapping is the silent-failure point. |
| 90 | +- You MUST NOT weaken or delete existing telemetry assertions to make new tests pass, because that erases regression coverage. |
| 91 | +- Validation: the new/updated tests MUST exist and MUST be runnable by the module's test runner. |
| 92 | + |
| 93 | +### 5. Build and verify the module |
| 94 | +Run the affected module's build and tests to confirm the change compiles, the tests pass, |
| 95 | +and telemetry does not block the instrumented calls. |
| 96 | + |
| 97 | +**Constraints:** |
| 98 | +- You MUST run the module's unit tests (for `sagemaker-core`, `python -m pytest sagemaker-core/tests/unit/telemetry/`; for a partner module, the equivalent unit-test target) and You MUST NOT proceed while any telemetry-related test fails, because shipping unverified telemetry can emit incorrect usage data. |
| 99 | +- You MUST confirm the instrumented entry points still return their original results when invoked in the tests, because a broken non-blocking guarantee would surface as an altered or missing return value. |
| 100 | +- If verification fails, You MUST diagnose and fix the root cause and re-run, and You MUST NOT mark the SOP complete with failing tests, because a green telemetry test suite is the completion criterion for this SOP. |
| 101 | +- Validation: the build/test run MUST complete with the telemetry tests green. |
| 102 | + |
| 103 | +### 6. Summarize the changes |
| 104 | +Produce a concise summary of what was instrumented for the requesting partner team. |
| 105 | + |
| 106 | +**Constraints:** |
| 107 | +- You MUST list each instrumented entry point with its `Feature` and `func_name`, whether a new `Feature` was created (with its integer code), any `telemetry_params` added, and the files changed. |
| 108 | +- You MUST state the test command that was run and its result. |
| 109 | +- You MUST NOT run `git commit`, `git push`, or create a code review unless the user explicitly asks, because publishing changes is the author's decision. |
| 110 | + |
| 111 | +## Examples |
| 112 | + |
| 113 | +### Example 1: Instrument an existing feature with an existing Feature |
| 114 | +**Input:** |
| 115 | +- target_path: `sagemaker-mlops/src/sagemaker/mlops/feature_store/feature_utils.py` |
| 116 | +- feature_name: `FEATURE_STORE` |
| 117 | + |
| 118 | +**Expected Behavior:** |
| 119 | +The agent detects the public functions (e.g., `ingest_dataframe`, `get_feature_group_as_dataframe`), |
| 120 | +decorates each with `@_telemetry_emitter(Feature.FEATURE_STORE, "<function>")`, reuses the |
| 121 | +existing `FEATURE_STORE` member, adds/extends telemetry unit tests, runs them green, and |
| 122 | +summarizes the changes. |
| 123 | + |
| 124 | +### Example 2: New feature with granular parameters |
| 125 | +**Input:** |
| 126 | +- target_path: `sagemaker-train/src/sagemaker/train/my_feature/client.py` |
| 127 | +- feature_name: `MY_FEATURE` |
| 128 | +- entry_points: ["MyClient.run", "MyClient.submit"] |
| 129 | +- telemetry_params: [("instance_type", "KWARG_VALUE"), ("networking", "ATTR_EXISTS")] |
| 130 | + |
| 131 | +**Expected Behavior:** |
| 132 | +The agent adds `Feature.MY_FEATURE = 22` to `constants.py` and `str(Feature.MY_FEATURE): 22` |
| 133 | +to `FEATURE_TO_CODE`, decorates `MyClient.run` and `MyClient.submit` with the granular |
| 134 | +`telemetry_params`, adds unit tests asserting `x-instanceType` and `x-hasNetworking` are |
| 135 | +emitted plus the new-member/mapping assertions, runs the tests green, and summarizes. |
| 136 | + |
| 137 | +## Troubleshooting |
| 138 | + |
| 139 | +### Telemetry never emits at runtime |
| 140 | +If the decorator is applied but no telemetry appears: |
| 141 | +- Confirm the `Feature` member exists in BOTH the `Feature` enum and `FEATURE_TO_CODE` with the same integer; a missing `FEATURE_TO_CODE` entry raises a swallowed `KeyError`. |
| 142 | +- Confirm a `sagemaker_session` is resolvable in the call path, because the emitter only fires when a session is available. |
| 143 | + |
| 144 | +### ImportError on `_telemetry_emitter` or `TelemetryParamType` |
| 145 | +- Import `Feature` and `_telemetry_emitter` from `sagemaker.core.telemetry`. |
| 146 | +- Import `TelemetryParamType` from `sagemaker.core.telemetry.telemetry_logging` (it is not re-exported from the package root). |
| 147 | + |
| 148 | +### Tests fail with an unexpected return value |
| 149 | +- Verify no logic was changed and the `@_telemetry_emitter` decorator is the only addition; the emitter must return the wrapped function's result unchanged. |
| 150 | + |
| 151 | +### Cannot determine which functions to instrument |
| 152 | +- Ask the partner team for the `entry_points`, or instrument only the documented public API surface and exclude underscore-prefixed helpers. |
0 commit comments