Introduction
This project is designed to simplify tracing for FastAPI based application running in Google Cloud Run.
The Tracer class is a wrapper around opentelemetry libraries which allows to easily add tracing to your application. By design the Tracer uses Google Cloud Trace exporter to send the traces to the Google Cloud Platform. Moreover, the Tracer class accepts the request object as a parameter which allows to extract the X-Cloud-Trace-Context header and pass it to the exporter. This allows to trace the requests between the services.
Quick Start
# import the Tracer class
from surquest.GCP.tracer import Tracer
# request is an object which allows to check the request headers
# request.headers.get('X-Cloud-Trace-Context', None)
tracer = Tracer(request=request)
with tracer.start_span(name="Task A"):
# Do something
result = 1 + 1
FastAPI integration
Following example shows how to integrate the Tracer class with FastAPI endpoints:
from surquest.GCP.tracer import Tracer
import requests
from fastapi import FastAPI, Depends, Query, Path
app = FastAPI()
@app.get("/exchange/currencies/{base}/{quote}")
async def exchange(
base: str = Path(..., description="Base currency"),
quote: str = Path(..., description="Quote currency"),
amount: float = Query(..., gt=0, description="Amount to exchange"),
tracer: Tracer = Depends(Tracer),
):
# get exchange rate from external API
with tracer.start_span("Get exchange rate"):
exchange_rate = requests.get(
f"https://api.exchangeratesapi.io/v1/latest?base={base}&symbols={quote}",
params={
"base": base,
"symbols": quote,
"access_key": "<your_api_key>" # please set your own API key
}
).json()["rates"][quote]
with tracer.start_span("Calculate exchange"):
result = amount * exchange_rate
return {
"currencies": {
"base": base,
"quote": quote
},
"amount": {
"base": amount,
"quote": result
}
}
Local development
You are more than welcome to contribute to this project. To make your start easier we have prepared a docker image with all the necessary tools to run it as interpreter for Pycharm or to run tests.
Build docker image
docker build `
--tag surquest/gcp/tracer `
--file package.base.dockerfile `
--target test .
Run tests
docker run --rm -it `
-v "${pwd}:/opt/project" `
-e "GOOGLE_APPLICATION_CREDENTIALS=/opt/project/credentials/keyfile.json" `
-w "/opt/project/test" `
surquest/gcp/tracer pytest
Metadata
Release files for surquest-GCP-tracer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| surquest_gcp_tracer-0.1.0.tar.gz | 7.7 kB | Without provenance | Details for surquest_gcp_tracer-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl |
Total release size: 12.4 kB
Release files / surquest_gcp_tracer-0.1.0.tar.gz
| Download URL | surquest_gcp_tracer-0.1.0.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea7f8bab3f450fb98d3e1b0bb04394668a886f0a0f9fbda8a6c4ab67ba0a0fa9
|
|
BLAKE2b-256 checksum How to use checksums |
a5766a1e694e85876e06993e4758523ac862b75b151af739798f5d2ebe3fe8c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|
Release files / surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl
| Download URL | surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a84b4c55b45cf449ed577c90296d9ba1c9c9fecb6389bbdef09a3f74aa96fbd2
|
|
BLAKE2b-256 checksum How to use checksums |
2063a7f2d6728becc9ed50876fea406eea6ef30ca2cebfcba08b562016c592e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|
Security for surquest-GCP-tracer 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.