A comprehensive APM and instrumentation library for Python microservices
Project description
MS Common Python Library
A comprehensive Application Performance Monitoring (APM) and instrumentation library for Python microservices at FinAccel.
Features
- APM Instrumentation: Automatic tracing and monitoring with Datadog integration
- Exception Handling: Standardized exception classes for microservices
- Configuration Management: Thread-safe configuration management for APM settings
- Class Instrumentation: Automatic instrumentation of class methods
- Function Decorators: Easy-to-use decorators for tracing individual functions
Installation
pip install ms-common-py
Quick Start
Basic Function Tracing
from apm import trace
@trace(name="my_function", service="my-service")
def my_function():
# Your code here
return "Hello World"
Class Instrumentation
from apm import instrument_class
@instrument_class(prefix="MyService", service="my-service")
class MyService:
def process_data(self):
# This method will be automatically traced
pass
def validate_input(self):
# This method will also be traced
pass
Configuration
Set the following environment variables:
# APM Configuration
export APM_PROVIDER=datadog
export DD_SERVICE=my-service-name
export DD_ENV=production
export APM_VERSION=1.0.0
export DD_APM_ENABLED=true
# Datadog Agent Configuration (optional)
export DD_AGENT_HOST=localhost
export DD_TRACE_AGENT_PORT=8126
APM Configuration
The library uses a thread-safe singleton configuration that automatically refreshes when environment variables change:
from apm.const import APMConfig
# Get current configuration
config = APMConfig.get()
print(f"Service: {config.service_name}")
print(f"Environment: {config.environment}")
print(f"Enabled: {config.enabled}")
# Force refresh configuration
config = APMConfig.get(force=True)
Exception Handling
Use standardized exceptions for consistent error handling across services:
from apm.exceptions import (
ServiceException,
AuthenticationFailure,
ForbiddenException,
BadRequestException,
NotFoundException
)
# Raise a custom service exception
raise AuthenticationFailure(
message="Invalid credentials",
service="my-service"
)
Advanced Usage
Custom Tags and Resource Names
from apm import trace
@trace(
name="complex_operation",
service="data-processor",
resource="process_user_data",
tags={"operation_type": "batch", "version": "v2"}
)
def process_user_data(user_id):
# Your processing logic
pass
Excluding Methods from Class Instrumentation
from apm import instrument_class
@instrument_class(
prefix="UserService",
exclude_methods=["_private_method", "health_check"]
)
class UserService:
def create_user(self):
# This will be traced
pass
def _private_method(self):
# This will NOT be traced
pass
def health_check(self):
# This will NOT be traced
pass
Service Type Auto-Detection
The library automatically detects service types based on class inheritance:
- Consumer: Classes inheriting from
BaseConsumerorConsumer - Cron: Classes inheriting from
BaseCommandorCommand - API: Classes inheriting from
APIView,ViewSet, orHandler - Worker: Classes inheriting from
WorkerorTask - Service: Default for other classes
Available Exception Classes
| Exception | HTTP Code | Description |
|---|---|---|
ServiceException |
500 | Base exception class |
AuthenticationFailure |
401 | Authentication failed |
ForbiddenException |
403 | Access forbidden |
BadRequestException |
400 | Bad request |
MissingParamException |
406 | Missing required parameters |
ValidateException |
422 | Validation error |
NotFoundException |
404 | Resource not found |
BusinessException |
422 | Business logic error |
ProductClosedException |
450 | Product/service closed |
BillNotValidException |
451 | Bill validation error |
Requirements
- Python 3.9+
- ddtrace>=2.2.0 (for Datadog integration)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
License
This project is proprietary to FinAccel.
Support
For issues and questions, please contact the FinAccel development team or create an issue in the Bitbucket repository.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ms_common_py-0.2.0.tar.gz.
File metadata
- Download URL: ms_common_py-0.2.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cd727123cc1b08a765aa82b339ef056901e71b4abf74f30a65b11d53105cb24
|
|
| MD5 |
67890f543a0bb187c29d6fa74b708f1a
|
|
| BLAKE2b-256 |
a4c1fbafec3a923587e24d8b2acbc66b5d7caa4b82cf70abca4c05c73db4be3e
|
File details
Details for the file ms_common_py-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ms_common_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3f59e11fb901af44f6acaab2c447f988ef35f6e3e088a2b7b28bc7714fc6704
|
|
| MD5 |
0aca4c360916818834b7c1ad912bd206
|
|
| BLAKE2b-256 |
eb1eff69bbd0ed3fc8f362964664b7c8bc891df0fcb669bc494d713ab4e6c046
|