Skip to content

feat: add onSuccess and onFailure lifecycle hooks for telemetry - #58

Merged
AryanSharma48 merged 1 commit into
mainfrom
feat/onSuccess-onFailure-hooks
Sep 26, 2026
Merged

AryanSharma48 merged 1 commit into
mainfrom
feat/onSuccess-onFailure-hooks

Conversation

@AryanSharma48

Copy link
Copy Markdown
Owner

Description

Resolves a part of #7

This PR implements the onSuccess and onFailure lifecycle event hooks across both TypeScript (smooth-api-ts) and Python (smooth-api-py) clients, providing end-to-end request observability and telemetry integration (Datadog, Prometheus, OpenTelemetry).

Key Features & Design Choices:

  • onSuccess:
    • Only clean HTTP 2xx completions trigger onSuccess.
    • Context includes url, domain, attempts taken to succeed, durationMs (or .duration_ms / .duration), status, and response.
  • onFailure:
    • Terminal HTTP errors (status >= 400), exhausted retries, unhandled exceptions, and CircuitOpenError trigger onFailure.
    • Context includes url, domain, attempts, durationMs, error, status, response, and isCircuitOpen / is_circuit_open.
  • Fail-Safe Execution:
    • User exceptions inside hook callbacks are safely caught and logged via safeInvoke / _safe_invoke, ensuring client requests never crash due to telemetry bugs.
  • Python Parity:
    • Supports both snake_case (on_success, on_failure) and camelCase (onSuccess, onFailure) configuration aliases.
    • Automatically extracts target URLs from request arguments, response objects, or error contexts.

Affected Packages:

  • packages/smooth-api-ts
  • packages/smooth-api-py
  • Root README.md

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation/Website update (changes to READMEs, examples, inline comments, or the documentation website)

Checklist

Design & Parity

  • If this introduces a new configuration option or public API, I have implemented equivalent options/behavior in both TypeScript and Python packages.
  • The core packages remain dependency-free (no new external runtime dependencies added).
  • I have updated the relevant package-specific README.md or general documentation where necessary.
  • If applicable, I have updated the examples or website to reflect these changes.

Quality & Testing

  • I started the sandbox Express server (cd sandbox && npm install && npm start) in a separate terminal before running the tests.
  • TypeScript Package: I have run the TypeScript tests (npm install && npm run build && npm test inside packages/smooth-api-ts) and all tests passed (51/51 passing).
  • Python Package: I have run the Python tests (pip install -e ".[dev]" && pytest inside packages/smooth-api-py) and all tests passed (51/51 passing, 0 ruff lint errors).
  • I have added new tests to cover my changes.
  • I have commented my code, particularly in hard-to-understand areas, and updated JSDoc/docstrings.

Signed-off-by: Aryan Sharma <aryansharma24106@gmail.com>
@github-actions github-actions Bot added docs Improvements or additions to documentation python typescript packages Needs Review labels Sep 26, 2026
@AryanSharma48
AryanSharma48 merged commit ba00e73 into main Sep 26, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation packages python typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant