Skip to main content

Telemetry

The SDK provides telemetry hooks that let you observe every HTTP request, response, and error. Use them for logging, metrics collection, or integration with observability platforms like OpenTelemetry.

Telemetry is enabled by default. Hooks are opt-in — no callbacks fire unless you register them.

Registering Hooks​

The Python SDK uses a global Telemetry class with three hook methods:

from scope_client import Telemetry, RequestInfo, ResponseInfo, ErrorInfo

def on_request(info: RequestInfo):
print(f"[{info.request_id}] {info.method} {info.url}")

def on_response(info: ResponseInfo):
print(f"[{info.request_id}] {info.status_code} in {info.elapsed_ms:.0f}ms")

def on_error(info: ErrorInfo):
print(f"[{info.request_id}] Error: {info.error} after {info.elapsed_ms:.0f}ms")

Telemetry.on_request(on_request)
Telemetry.on_response(on_response)
Telemetry.on_error(on_error)

Hook Data​

RequestInfo​

FieldTypeDescription
request_idstringUnique ID for correlating request/response/error
methodstringHTTP method (GET, POST, etc.)
urlstringFull request URL
headersdict/hashRequest headers (authorization is redacted)

ResponseInfo​

FieldTypeDescription
request_idstringMatches the originating request
status_code / statusintegerHTTP status code
headersdict/hashResponse headers
elapsed_ms / durationfloatRequest duration (ms in Python, seconds in Ruby)

ErrorInfo​

FieldTypeDescription
request_idstringMatches the originating request
errorexceptionThe error that occurred
elapsed_ms / durationfloatTime before failure (ms in Python, seconds in Ruby)

Integration with Logging​

import logging
from scope_client import Telemetry, RequestInfo, ResponseInfo, ErrorInfo

logger = logging.getLogger("scope_client")

def log_request(info: RequestInfo):
logger.info("Scope API request: %s %s", info.method, info.url)

def log_response(info: ResponseInfo):
logger.info(
"Scope API response: %s in %.0fms",
info.status_code,
info.elapsed_ms,
)

def log_error(info: ErrorInfo):
logger.error(
"Scope API error: %s after %.0fms",
info.error,
info.elapsed_ms,
)

Telemetry.on_request(log_request)
Telemetry.on_response(log_response)
Telemetry.on_error(log_error)

Integration with OpenTelemetry​

from opentelemetry import trace
from scope_client import Telemetry, RequestInfo, ResponseInfo, ErrorInfo

tracer = trace.get_tracer("scope_client")

def trace_request(info: RequestInfo):
span = tracer.start_span(f"scope.{info.method}")
span.set_attribute("http.method", info.method)
span.set_attribute("http.url", info.url)
span.set_attribute("scope.request_id", info.request_id)

def trace_response(info: ResponseInfo):
span = trace.get_current_span()
span.set_attribute("http.status_code", info.status_code)
span.set_attribute("scope.elapsed_ms", info.elapsed_ms)

Telemetry.on_request(trace_request)
Telemetry.on_response(trace_response)

Clearing Hooks​

Telemetry.clear_callbacks()

Disabling Telemetry​

To disable the telemetry middleware entirely:

client = ScopeClient(
credentials=credentials,
telemetry_enabled=False,
)
Was this page helpful?