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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

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