Skip to main content

Wrapper around Google Cloud Platform (GCP) tracing library

Project description

GitHub GitHub Workflow Status (with branch) Coverage PyPI - Downloads

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

surquest_gcp_tracer-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

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

Hashes for surquest_gcp_tracer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea7f8bab3f450fb98d3e1b0bb04394668a886f0a0f9fbda8a6c4ab67ba0a0fa9
MD5 59ca1acea20fc44143da775679e779fb
BLAKE2b-256 a5766a1e694e85876e06993e4758523ac862b75b151af739798f5d2ebe3fe8c9

See more details on using hashes here.

File details

Details for the file surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for surquest_gcp_tracer-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a84b4c55b45cf449ed577c90296d9ba1c9c9fecb6389bbdef09a3f74aa96fbd2
MD5 004b8eac16b17445a517951485e80d88
BLAKE2b-256 2063a7f2d6728becc9ed50876fea406eea6ef30ca2cebfcba08b562016c592e8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page