Skip to main content

Shared OpenTelemetry bootstrap for BetShareMarket Python services

Project description

betshare-infra-telemetry

Shared OpenTelemetry bootstrap for BetShareMarket Python services. One initialize_telemetry(TelemetryConfig(...)) call wires up OTLP/HTTP trace and metric exporters, a per-process resource (service name/version/namespace, instance id, deployment environment, pipeline family), a starts_total counter and an uptime_seconds observable gauge, and registers an atexit shutdown.

from betshare_infra_telemetry import TelemetryConfig, initialize_telemetry

initialize_telemetry(
    TelemetryConfig(
        service_name="consumer",
        service_version="0.1.0",
        pipeline_family="ingest",
    )
)

Import name: betshare_infra_telemetry. Distribution name: betshare-infra-telemetry.

Why

This module was copy-pasted byte-identical (172 lines, md5 6b16c9d0…) across consumer, betfair-exchange-scraper, joiner-events and crawler, and existed as a ~266-line superset in azure-proxy-pool. Centralising it stops the four copies from drifting and gives azure-proxy-pool a clean place to opt into its extras.

Public interface

@dataclass
class TelemetryConfig:
    service_name: str
    service_version: str
    pipeline_family: str
    environment: str | None = None
    service_namespace: str = "betsharemarket"

def initialize_telemetry(
    config: TelemetryConfig,
    *,
    enable_log_export: bool = False,
    loguru_logger_name: str | None = None,
) -> TelemetryRuntime: ...

initialize_telemetry is idempotent (a module-level singleton; a second call returns the same TelemetryRuntime) and degrades gracefully:

  • if the OTel SDK is not importable → returns a no-provider runtime;
  • if OTEL_SDK_DISABLED=true → bare providers, no exporters;
  • if no OTLP endpoint env var is set → defaults to http://127.0.0.1:4318 (exporters are built; a missing collector only fails at delivery time, not at init).

Environment variables

  • OBSERVABILITY_OTLP_ENDPOINT (preferred) / OTEL_EXPORTER_OTLP_ENDPOINT — base OTLP/HTTP endpoint; any /v1/{traces,metrics,logs} suffix is stripped.
  • OTEL_EXPORTER_OTLP_HEADERSkey=value,key=value header list.
  • OTEL_METRIC_EXPORT_INTERVAL — metric export interval ms (default 15000).
  • OTEL_SDK_DISABLEDtrue disables exporters.
  • ENV / NODE_ENV — fallback for deployment.environment (default development); overridden by TelemetryConfig.environment.
  • LOG_LEVEL — only when enable_log_export=True; loguru sink level.

Opt-in log export (used by azure-proxy-pool)

azure-proxy-pool's original superset additionally exported logs over OTLP and bridged loguru into the OTel logging pipeline. That behaviour is preserved here behind a flag, off by default so the four common services keep their exact prior behaviour:

initialize_telemetry(
    TelemetryConfig(service_name="azure-proxy-pool", service_version="0.1.0",
                    pipeline_family="proxy"),
    enable_log_export=True,
    loguru_logger_name="azure-proxy-pool",
)

When enabled (and the OTel logs SDK + loguru are installed) this sets a LoggerProvider with an OTLP log exporter and adds a loguru sink that forwards records to a named stdlib logger carrying the OTLP handler. If the logs SDK or loguru is missing it silently no-ops (logger_provider stays None).

Install

Production (private registry, version-pinned per service):

betshare-infra-telemetry = "^0.1.0"

Local development (PATH dependency, used by the migration):

betshare-infra-telemetry = { path = "../../packages/py-infra-telemetry", develop = true }

Or with pip: pip install -e packages/py-infra-telemetry.

Test

cd packages/py-infra-telemetry && pip install -e . && pip install pytest && python -m 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

betshare_infra_telemetry-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

betshare_infra_telemetry-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file betshare_infra_telemetry-0.1.0.tar.gz.

File metadata

  • Download URL: betshare_infra_telemetry-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/25.5.0

File hashes

Hashes for betshare_infra_telemetry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 745103f5864cc271931ede95c09bd1a783f56db247229f4dc11582ca6c0f5a2f
MD5 f9e2d169be9b0770165c1080567953ee
BLAKE2b-256 56dc5b2c1b82b9c165a499e954bcca39ef075daa891e4b8a24e896781d5bddc0

See more details on using hashes here.

File details

Details for the file betshare_infra_telemetry-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betshare_infra_telemetry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3421e1601144f48e56e95d669f81d76335547f92c4bb32e49581aa878f1c1774
MD5 b337a80e7709e8a406ad77c889374573
BLAKE2b-256 182bee2878200319330af2768b754887fbf73553de20954a50d17ea54f4d9a11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page