Coder Social home page Coder Social logo

fastapi-opentracing's Introduction

fastapi-opentracing

fastapi opentracing middleware works with istio

install:

pip install fastapi-opentracing

example:

from fastapi import FastAPI
import uvicorn
from fastapi_opentracing import get_opentracing_span_headers
from fastapi_opentracing.middleware import OpenTracingMiddleware

app = FastAPI()

app.add_middleware(OpenTracingMiddleware)


@app.get("/")
async def root():
    carrier = await get_opentracing_span_headers()
    return {'span': carrier}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

if your application uses tortoise-orm, you can execute the install_all_patch or specify the client mysql_client.install_patch to patch you SQLClient

example:

from fastapi import FastAPI
import uvicorn
from fastapi_opentracing import get_opentracing_span_headers
from fastapi_opentracing.middleware import OpenTracingMiddleware
from fastapi_opentracing.client_hooks.mysql_client import install_patch
from fastapi_opentracing.client_hooks import install_all_patch


app = FastAPI()

app.add_middleware(OpenTracingMiddleware)
TORTOISE_ORM = {
    "connections": {"default": "mysql://root:[email protected]:3306/test"},
    "apps": {
        "models": {
            "models": ["tests.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}
register_tortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True
)

install_all_patch()

@app.get("/")
async def root():
    carrier = await get_opentracing_span_headers()
    return {'span': carrier}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

Contributing and Developing

To install all dependencies, run:

python3 -m venv venv
source venv/bin/activate
make bootstrap

Running Tests

make test

Check the style and quality of python code

make lint

fastapi-opentracing's People

Contributors

mizhexiaoxiao avatar wesdu avatar yprocfly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastapi-opentracing's Issues

Trying to get unit tests to run so I can contribute

Having some issues getting the unit tests to run. I followed the README and did a make bootstrap followed by a make test but I'm getting a bunch of errors about not having redis installed. I installed the additional dependencies in the /tests/requirements.txt file as well.

I'm guessing there are a bunch of other pre-requisites to contribute? I think if there are external services like Redis, these should either be mocked (for unit testing) or part of a some separate integration tests that use docker so they are platform independent. That will make it easier to get up and running and allow others to develop.

I'd be happy to put some time into this if it would be a welcome PR?

In terms of contriution, all I'm really trying to do at the moment is bump up Fast API and uvicorn to the latest versions.

New Release

Hi! I really liked the project for its practicality and I believe that this will help the solution I'm developing a lot, but a point that worries me is related to the versioning of the project, because I realized that the version available on pip (https://pypi.org/ project/fastapi-opentracing/) is at 0.5.6 while the project is already at version 0.7.3 . Would it be possible for you to release the updates on pip and fix the versions?

invalid parent span IDs; skipping clock skew adjustment

Hi, I have encountered invalid parent span IDs; skipping clock skew adjustment while using the middleware:
image
image

Here is the code I have written:

@router.get("/route")
async def curl2():
    headers = await get_opentracing_span_headers()
    # carrier = {}
    response = requests.get("http://fastapi-cicd-example-2.bookinfo.svc.cluster.local:9999/api/getThirdService",headers=headers)
    return {"headers2": response.request.headers, "response": response.json()}

@router.get("/getThirdService")
async def curl2():
    headers = await get_opentracing_span_headers()
    # carrier = {}
    response = requests.get("http://fastapi-cicd-example-3.bookinfo.svc.cluster.local:9999/api/end",headers=headers)
    return {"headers3": response.request.headers, "response": response.json()}

@router.get("/end")
async def curl2():
    # carrier = await get_opentracing_span_headers()
    return {"message": "end of span"}

Here is the output when I access https://example.com/route:

{
  "headers2": {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
    "Accept-Encoding": "gzip, deflate",
    "Accept": "*/*",
    "Connection": "keep-alive",
    "X-B3-TraceId": "6c4d064ebd60142b32dcf9f92d363d06",
    "X-B3-SpanId": "f7715f86110f1e17",
    "X-B3-ParentSpanId": "315756030b48695f",
    "x-request-id": "ce407db4-04e7-4f72-a77b-5776278ca9df"
  },
  "response": {
    "headers3": {
      "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
      "Accept-Encoding": "gzip, deflate",
      "Accept": "*/*",
      "Connection": "keep-alive",
      "X-B3-TraceId": "6c4d064ebd60142b32dcf9f92d363d06",
      "X-B3-SpanId": "42bbc3ded7ceeed2",
      "X-B3-ParentSpanId": "46d84cdd4b6ea9e7",
      "x-request-id": "ce407db4-04e7-4f72-a77b-5776278ca9df"
    },
    "response": {
      "message": "end of span"
    }
  }
}

The X-B3-ParentSpanId cannot inherit from the last SpanId correctly. Do you have idea on it?

Locked library versions

This has several specific versions pinned in requirements.txt.

That's a very unfriendly thing for a library to do.

I'm using poetry, and it refuses to add fastapi-opentracing to my project because I'm not using 0.61.1.

If there are changes in newer versions of the dependencies that break this library, that's one thing. But there should at least be a range of values that can work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.