Skip to main content

Scaffold library

Project description

KLYM Telemetry

A small library to add instrumentation in KLYM apps.

Publish in PYPI

pip install twine
python setup.py sdist
twine upload dist/*

Installation

pip install klym-telemetry

Get started

Instrumenting a fastapi app

  1. Import klym instrumenter
from klym_telemetry.instrumenters import instrument_app
  1. Import instrument decorator
from klym_telemetry.utils import instrument
  1. Initialize automatic instrumentation
instrument_app(app_type='fastapi', app=app, service_name="test-klym-microservice", endpoint="http://localhost:4317")

Full example:

import time

from fastapi import FastAPI
from klym_telemetry.instrumenters import instrument_app
from klym_telemetry.utils import instrument, klym_telemetry

app = FastAPI()
instrument_app(app_type='fastapi', app=app, service_name="test-klym-microservice", endpoint="http://localhost:4317")


@instrument(private_methods=True, attributes={"description": "Class to say hello"})
class Hello:

    @instrument(span_name="Get start message (private method)")
    def _get_start_message(self):
        return "Hello"

    def say_hello(self):
        return {"message": self._get_start_message() + " World"}

    def say_hello_with_name(self, name: str):
        return {"message": f"{self._get_start_message()} {name}"}


@app.get("/")
def root():
    klym_telemetry.add_event_curr_span("Start sleeping")  # Custom event example
    for _ in range(10):
        time.sleep(0.2)
    klym_telemetry.add_event_curr_span("Finished sleeping")
    return Hello().say_hello()


@app.get("/hello/{name}")
@instrument(span_name="Say hello with name", attributes={"description": "Class to say hello asynchrounously"})
async def say_hello(name: str):
    return {"message": f"Hello {name}"}

Instrumenting a celery app

  1. Import klym instrumenter
from klym_telemetry.instrumenters import instrument_app
  1. Import celery signal when worker starts
from celery.signals import worker_init
  1. Initialize automatic instrumentation
instrument_app(app_type='celery', service_name="integrations", endpoint="http://localhost:4317")

Full example:

import os

from celery import Celery
from celery.signals import worker_init
from klym_telemetry.instrumenters import instrument_app

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.local')

app = Celery('integrations')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

app.conf.update(
    worker_pool_restarts=True,
)


@worker_init.connect()
def init_celery_tracing(*args, **kwargs):
    instrument_app(app_type='celery', service_name="integrations", endpoint="http://localhost:4317")

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

klym-telemetry-0.1.12.1.tar.gz (8.2 kB view details)

Uploaded Source

File details

Details for the file klym-telemetry-0.1.12.1.tar.gz.

File metadata

  • Download URL: klym-telemetry-0.1.12.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for klym-telemetry-0.1.12.1.tar.gz
Algorithm Hash digest
SHA256 0113c1609650e191982ded22210c697a2ff80cf8d72e15e385c2d29f8717caee
MD5 1c62af196eb17a0a3c8f7897226b33cd
BLAKE2b-256 543181c6e7d680fe15b6f1dc1676184b5c38e805306fa7f25f677d644d294471

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