Skip to content

A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.

License

Notifications You must be signed in to change notification settings

betofigueiredo/diff_tracer

Repository files navigation

DIFF TRACER

A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.





Preview

Warning

Please keep in mind that Diff Tracer is still under active development


Installation

❯ pip install diff_tracer

Basic Usage

To compare two results:

from diff_tracer import compare_async

@app.get("/")
async def get_data():
    return await compare_async(
        current_fn=lambda: currentUseCase(), # current function in production
        new_fn=lambda: newUseCase(), # new refactored function
        percentage=80, # percentage of requests to compare, good to control expensive endpoints
    )

Active the dashboard endpoint in your main file:

from fastapi import FastAPI
from diff_tracer import init_web_view


app = FastAPI()


init_web_view(app=app, security_token="362a9b3f302542deb3184671bbc3e7da")

Access http://localhost:{PORT}/diff-tracer-view/362a9b3f302542deb3184671bbc3e7da to view the dashboard.

Check a full working example on ./diff_tracer/fastapi_example.py

Why

I used this aproach while working on a major endpoint refactor at my current company. The endpoint was critical but lacked comprehensive tests, making it challenging to ensure the refactored function behaved identically to the original. While I wrote some tests, I wanted an extra layer of confidence before deploying to production.

Known issues

  • The dashboard endpoint is not secure. You can set a token to make it harder to access, but it's still not secure.
  • The files are saved local on your API, so everytime you make a new deploy they will be erased.

Contributing

For local development just install the libraries and start the FastAPI example file:

❯ poetry install
❯ poetry run task start_api

Access http://localhost:8000/users to simulate requests.

Access http://localhost:8000/diff-tracer-view/1234 to view de dashboard. 1234 is the default token.

To run the tests:

❯ poetry run task test

Thanks to

The code that makes the comparison is from Google Diff, Match and Patch Library written by Neil Fraser Copyright (c) 2006 Google Inc. http://code.google.com/p/google-diff-match-patch/

About

A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published