Wrapper around Google Cloud Platform (GCP) tracing library
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file surquest_gcp_tracer-0.1.0.tar.gz
.
File metadata
- Download URL: surquest_gcp_tracer-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea7f8bab3f450fb98d3e1b0bb04394668a886f0a0f9fbda8a6c4ab67ba0a0fa9 |
|
MD5 | 59ca1acea20fc44143da775679e779fb |
|
BLAKE2b-256 | a5766a1e694e85876e06993e4758523ac862b75b151af739798f5d2ebe3fe8c9 |
File details
Details for the file surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a84b4c55b45cf449ed577c90296d9ba1c9c9fecb6389bbdef09a3f74aa96fbd2 |
|
MD5 | 004b8eac16b17445a517951485e80d88 |
|
BLAKE2b-256 | 2063a7f2d6728becc9ed50876fea406eea6ef30ca2cebfcba08b562016c592e8 |