Skip to main content

diagnyx (Python)

Thin Python client for the Diagnyx structured logging CLI.

DiagnyxLogger calls the diagnyx binary in a subprocess. The binary writes structured JSON log entries to whichever sinks diagnyx.config.json configures: file, SQLite, PostgreSQL, MySQL, SQL Server, OTLP, or Loki. The package has no runtime dependencies and supports Python 3.9+.

Prerequisites

You don't need to install anything else on win-x64, linux-x64 (glibc 2.34+), or osx-arm64. PyPI publishes a separate wheel for each of these platforms, and each wheel bundles the matching diagnyx binary, so pip install diagnyx is all you need.

On other platforms (or with pip install --no-binary diagnyx), pip installs the pure wheel or the sdist instead. Neither one includes a binary. Install the Diagnyx CLI from the releases page or build it from source. Then point DiagnyxLogger at it with the binary_path constructor argument or the DIAGNYX_PATH environment variable (see Binary Discovery):

git clone https://github.com/nachiketg/diagnyx
cd diagnyx/src/core
dotnet publish -c Release -r linux-x64 -o /usr/local/bin

Installation

pip install diagnyx

Quick Start

from diagnyx import DiagnyxLogger

logger = DiagnyxLogger("my-api")

logger.info("App started")
logger.warn("Cache miss", {"key": "user:42", "ttl": 300})
logger.error("Payment failed", {"orderId": "ord-9921", "amount": 49.99})

Each call runs diagnyx log and returns the exit code (0 = success, 1 = failure).

Binary Discovery

DiagnyxLogger looks for the CLI once, when it is constructed, in this order:

  1. The binary_path constructor argument.
  2. The DIAGNYX_PATH environment variable.
  3. The binary bundled in this package's platform wheel (diagnyx/bin/, only on win-x64, linux-x64, and osx-arm64).
  4. diagnyx on the system PATH (diagnyx.exe on Windows).

If none of these finds a binary, or the binary fails to run, DiagnyxLogger does not raise an exception. It prints a warning to stderr and the call returns 1, so a missing or broken CLI never crashes the host application:

diagnyx: warning: diagnyx binary not found. Install it from https://github.com/nachiketg/diagnyx/releases or set the DIAGNYX_PATH environment variable. Log entry dropped.
# Explicit path (str or pathlib.Path)
logger = DiagnyxLogger("my-api", "/opt/diagnyx/diagnyx")

# Via environment variable (e.g. in CI)
# DIAGNYX_PATH=/opt/diagnyx/diagnyx
logger = DiagnyxLogger("my-api")

API Reference

DiagnyxLogger(source, binary_path=None)

Parameter Type Description
source str Application or service name written to the source field of every log entry. Raises ValueError if empty.
binary_path str or os.PathLike (optional) Explicit path to the diagnyx binary.

Methods

Method Level
debug(message, context=None, trace_context=None) debug
info(message, context=None, trace_context=None) info
warn(message, context=None, trace_context=None) warn
warning(...) alias of warn, matching the standard library's logging
error(message, context=None, trace_context=None) error
fatal(message, context=None, trace_context=None) fatal
log(level, message, context=None, trace_context=None) any

message is converted with str(), so passing an exception object works. context accepts a JSON-serializable object (usually a dict) or a JSON object string that is already serialized. Values with an isoformat() method (datetime, date, time) are serialized that way, and other values that aren't JSON types (such as UUID, Decimal, or Path) are serialized with str(). If the context can't be serialized at all, for example because it contains a circular reference, the logger prints a warning and the entry is dropped, without raising. Every method returns the CLI exit code (0 = success, 1 = failure).

Trace Context

To link one log entry to a trace, pass a (trace_id, span_id) pair explicitly. TraceContext is a named tuple, and a plain tuple works too:

from diagnyx import DiagnyxLogger, TraceContext

logger.info(
    "processing order",
    {"orderId": "ord-9921"},
    TraceContext("4bf92f3577b34da6a3ce929d0e0e4736", "00f067aa0ba902b7"),
)

You can also set an ambient trace context for a whole scope, such as one request, with the DiagnyxLogger.trace_context context manager. It is built on the standard library's contextvars. Every log call inside the scope picks up the trace context automatically, including calls after an await and calls in asyncio tasks created inside the scope:

async def handle(request):
    with DiagnyxLogger.trace_context(request.trace_id, request.span_id):
        await process(request)  # every logger call in here is correlated

If you use OpenTelemetry, you don't need to do anything else. When opentelemetry-api is installed and a span is active, its trace and span IDs are used automatically. Diagnyx doesn't depend on OpenTelemetry. It only reads the current span when the package is available.

When more than one source is present, they are used in this order:

  1. The explicit trace_context argument.
  2. The ambient DiagnyxLogger.trace_context(...) scope.
  3. The active OpenTelemetry span.

A trace context that is invalid or missing leaves traceId/spanId as null on the log entry. It never causes an error.

Configuration

DiagnyxLogger gets all sink and output configuration from diagnyx.config.json. See the Config Reference for all options.

{
  "sink": {
    "type": "postgres",
    "postgres": {
      "connectionString": "Host=localhost;Database=myapp;Username=app;Password=secret"
    }
  },
  "defaults": {
    "source": "my-api"
  }
}

Building the package

The scripts/build-dists.sh script builds the sdist, the pure wheel, and the three platform wheels from a directory of release binaries. The release workflow uses it, and you can run it locally (pip install build first):

packages/python/scripts/build-dists.sh <dir-with-release-binaries> dist-python

See docs/RELEASING.md for details.

Metadata

Release files for diagnyx 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for diagnyx 1.1.0
File Size Uploaded Provenance
diagnyx-1.1.0.tar.gz 7.9 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for diagnyx 1.1.0
File
diagnyx-1.1.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
diagnyx-1.1.0-py3-none-manylinux_2_34_x86_64.whl Python 3 none Linux glibc 2.34+ x86-64 Details
diagnyx-1.1.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details
diagnyx-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.2 MB

Release files / diagnyx-1.1.0.tar.gz

Download URL diagnyx-1.1.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8834cab2f3fa5b6644d2a963efe96224961a1c94ef53d02920d55bec3edf182f
BLAKE2b-256 checksum
How to use checksums
43378a3c50c54b6f4f2a2a7b4daabc7f7c2f474a07bdcf18a6dfd6496101c552
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / diagnyx-1.1.0-py3-none-win_amd64.whl

Download URL diagnyx-1.1.0-py3-none-win_amd64.whl
Size 11.6 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
a62b5f185160c8d102febffcf235d5f09e8d80008900d34caf80baa6c3e6f3bd
BLAKE2b-256 checksum
How to use checksums
64c74e40c8594d7bcddb711e76f244dc8b5892bb3a3d204f1ad66d31e5aaee98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / diagnyx-1.1.0-py3-none-manylinux_2_34_x86_64.whl

Download URL diagnyx-1.1.0-py3-none-manylinux_2_34_x86_64.whl
Size 11.6 MB
Tags Linux glibc 2.34+ x86-64 Python 3
SHA-256 checksum
How to use checksums
1e08ab3794cae0ab298a15081817258151927c14114935310f73261bf263928c
BLAKE2b-256 checksum
How to use checksums
33a0bdaaa07addb7dd2d30d0738732b705fc23974afcfdbf91462981cab0969b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / diagnyx-1.1.0-py3-none-macosx_11_0_arm64.whl

Download URL diagnyx-1.1.0-py3-none-macosx_11_0_arm64.whl
Size 11.0 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e33fd3104948e48a904c8d962c41a0c174a20382af44201d3f12da0dfd9f67af
BLAKE2b-256 checksum
How to use checksums
92386088a534b8580a173a3fa7b91accfcf2d1c522c20c018da0462299b00128
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / diagnyx-1.1.0-py3-none-any.whl

Download URL diagnyx-1.1.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
365ef6e91478a65bb3b6fb20f1ec7444f97d26dba323186141ad664017a3726f
BLAKE2b-256 checksum
How to use checksums
94009e5abdd04b976cce6d46e54655f1ca9ef9dfae8e00c3b1de16b710c4b5b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for diagnyx 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.1.0 This release

5 release files No provenance

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