Skip to main content

DTA Observability

A lightweight wrapper around OpenTelemetry for Python applications.

Overview

DTA Observability simplifies the use of OpenTelemetry by providing a streamlined interface for instrumentation. It handles configuration of tracing, metrics, and logging with minimal setup.

Features

  • Single function initialization of all telemetry components
  • Automatic instrumentation for Flask, FastAPI, Celery, and other frameworks
  • Structured logging with trace context correlation
  • Function decoration for easy span creation
  • System and application metrics collection
  • Support for OTLP, GCP Cloud, and console exporters
  • Optional trace-only none exporter for log correlation without trace export
  • Automatic resource detection
  • Configuration via parameters or environment variables

Installation

pip install dta-observability

Or with Poetry:

poetry add dta-observability

Basic Usage

import dta_observability
from dta_observability import get_logger, traced

# Initialize telemetry
dta_observability.init_telemetry(
    service_name="my-service",
    service_version="1.0.0",
    otlp_endpoint="http://otel-collector:4317",
    exporter_type="otlp",  # Options: "otlp", "console", "gcp"
)

# Get a logger
logger = get_logger("my-service")

# Use the traced decorator
@traced(name="my_function")
def my_function():
    logger.info("Doing work")
    return "result"

Framework Integration

Flask

from flask import Flask
import dta_observability

app = Flask(__name__)

dta_observability.init_telemetry(
    service_name="flask-service",
    flask_app=app
)

Flask Audit Logging

Flask audit logging works with any Flask application, not just DTA-based ones, through two optional resolver callbacks passed to init_telemetry(). auth_resolver maps the current request to an (identity, auth_type) pair, and context_resolver returns a dictionary of extra fields to attach to the audit record. If neither resolver is configured, audit logging still emits complete records using the WSGI identity defaults (REMOTE_USER/AUTH_TYPE) and request.remote_addr for the client IP.

from flask import Flask, g

import dta_observability

app = Flask(__name__)


def resolve_auth(request):
    user = getattr(g, "user", None)
    if user is None:
        return None, None
    return str(user.id), "session"


def resolve_context(request):
    return {
        "tenant": getattr(g, "tenant_id", None),
    }


dta_observability.init_telemetry(
    service_name="flask-service",
    flask_app=app,
    auth_resolver=resolve_auth,
    context_resolver=resolve_context,
)

Audit Log Format

Every audit record — Flask or FastAPI — follows the same schema (audit_schema: 2), emitted as structured extra fields on the audit logger:

{
  "audit_schema": 2,
  "logger": "audit",
  "path": "/api/orders/42",
  "method": "GET",
  "user_id": "user-123",
  "client_ip": "203.0.113.7",
  "client_ip_source": "socket",
  "status_code": 200,
  "outcome": "success",
  "duration_ms": 14,
  "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
  "user_agent": "Mozilla/5.0",
  "auth_type": "session",
  "host": "api.example.com",
  "referer": "https://example.com/orders",
  "session_id": null,
  "context": {
    "tenant": "acme"
  }
}

outcome is derived from status_code: success (< 400, INFO), denied (401/403, WARNING), client_error (other 4xx, WARNING), error (5xx, ERROR), unknown (no status — e.g. a propagated exception, ERROR). trace_id, session_id, and context are null when unavailable; they are never omitted from the record.

FastAPI

from fastapi import FastAPI
import dta_observability

app = FastAPI()

dta_observability.init_telemetry(
    service_name="fastapi-service",
    fastapi_app=app
)

Celery

from celery import Celery
import dta_observability

app = Celery("tasks")

dta_observability.init_telemetry(
    service_name="worker-service",
    celery_app=app
)

GCP Integration

When using the GCP exporter type:

  1. For traces: Uses Cloud Trace exporter
  2. For metrics: Uses Cloud Monitoring exporter with workload.googleapis.com prefix
  3. For logs: Uses GCP log format when log_format is set to "gcp", sending logs to stdout in the proper format

To use GCP integration:

dta_observability.init_telemetry(
    service_name="my-gcp-service",
    exporter_type="gcp",  # Uses GCP exporters for all signal types
    log_format="gcp"      # Formats logs for GCP
)

When log_format is set to "gcp", all logs will be formatted for Google Cloud Logging and sent to stdout, while metrics and traces will use their respective GCP exporters.

If you want GCP-formatted logs with trace/span correlation fields but do not want to export traces, set traces_exporter_type="none" and keep enable_traces=True:

dta_observability.init_telemetry(
    service_name="my-gcp-service",
    exporter_type="gcp",
    log_format="gcp",
    traces_exporter_type="none",
    enable_traces=True,
    enable_logs=True,
    enable_metrics=False,
)

This keeps spans active in-process for log correlation, but does not export them to Cloud Trace or to the console.

OTLP Integration

OTLP exporters send telemetry to an OpenTelemetry Collector:

dta_observability.init_telemetry(
    service_name="my-otlp-service",
    exporter_type="otlp",
    otlp_endpoint="http://otel-collector:4317"
)

Configuration

Configuration options available in init_telemetry():

Parameter Environment Variable Default Description
service_name SERVICE_NAME unnamed-service Name to identify the service
service_version SERVICE_VERSION 0.0.0 Version of the service
service_instance_id SERVICE_INSTANCE_ID Unique identifier for this service instance
resource_attributes None Additional resource attributes (dictionary)
configure_auto_instrumentation AUTO_INSTRUMENTATION_ENABLED True Whether to auto-instrument detected libraries
log_level LOG_LEVEL INFO Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
log_format LOG_FORMAT default Log format type (default or gcp)
flask_app None Flask application instance to instrument
fastapi_app None FastAPI application instance to instrument
celery_app None Celery application instance to instrument
safe_logging SAFE_LOGGING True Whether to enable safe logging with complex data types
excluded_instrumentations EXCLUDED_INSTRUMENTATIONS None Comma-separated list of instrumentations to exclude
otlp_endpoint EXPORTER_OTLP_ENDPOINT http://localhost:4317 OTLP exporter endpoint URL
otlp_insecure EXPORTER_OTLP_INSECURE True Whether to use insecure connection for OTLP
batch_export_delay_ms BATCH_EXPORT_SCHEDULE_DELAY 120000 Milliseconds between batch exports
METRICS_EXPORT_INTERVAL_MS 120000 Milliseconds between metric exports when exporter_type is gcp
enable_resource_detectors RESOURCE_DETECTORS_ENABLED True Whether to enable automatic resource detection
enable_logging_instrumentation LOGGING_INSTRUMENTATION_ENABLED True Whether to enable logging instrumentation
propagators OTEL_PROPAGATORS tracecontext,baggage,gcp_trace Comma-separated list of context propagators (w3c/tracecontext, baggage, gcp/gcp_trace, b3, b3multi)
exporter_type EXPORTER_TYPE otlp Default exporter type for all signals (otlp, console, or gcp)
traces_exporter_type TRACES_EXPORTER_TYPE otlp Exporter type for traces (otlp, console, gcp, or none)
metrics_exporter_type METRICS_EXPORTER_TYPE otlp Exporter type for metrics (otlp, console, or gcp)
logs_exporter_type LOGS_EXPORTER_TYPE otlp Exporter type for logs (otlp, console, or gcp)
enable_traces True Whether to enable trace collection
enable_metrics True Whether to enable metrics collection
enable_logs True Whether to enable logs collection
enable_system_metrics SYSTEM_METRICS_ENABLED True Whether to enable system metrics collection

Environment variables can also be prefixed with OTEL_ or DTA_ (e.g., DTA_SERVICE_NAME).

Examples

The examples directory contains sample applications demonstrating usage with different frameworks.

Download files

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

Source Distribution

dta_observability-0.0.23.tar.gz (47.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dta_observability-0.0.23-py3-none-any.whl (58.7 kB view details)

Uploaded Python 3

File details

Details for the file dta_observability-0.0.23.tar.gz.

File metadata

  • Download URL: dta_observability-0.0.23.tar.gz
  • Upload date:
  • Size: 47.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for dta_observability-0.0.23.tar.gz
Algorithm Hash digest
SHA256 5c79ef2978fe3bf9462ed9ee7bf9498831768eecedcb3e1e3e7e2f88c3bff6c4
MD5 31fb2efc63b50c2fba3f770f622cd5da
BLAKE2b-256 85203d0ee4fe7f804d17025cb0cfd23d6d7765804eb171a4a747c83b88809827

See more details on using hashes here.

File details

Details for the file dta_observability-0.0.23-py3-none-any.whl.

File metadata

  • Download URL: dta_observability-0.0.23-py3-none-any.whl
  • Upload date:
  • Size: 58.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Linux/6.17.0-1020-azure

File hashes

Hashes for dta_observability-0.0.23-py3-none-any.whl
Algorithm Hash digest
SHA256 88bd0408bcc77978d0ecf03b6b588b39756078b98723c08993c72903348e5955
MD5 57af18df07fb4215827bb51a6f2ef688
BLAKE2b-256 0a8ccb68857235993c6874802f31d7e068c5a36028f556427a7da0b0c42ec4ea

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.23 This release

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page