Skip to main content

Options

OptionTypeDefaultDescription
api_keystrrequiredYour TruLayer API key (tl_...)
projectstrrequiredProject identifier — any string; created on first use
environmentstr"production"Free-form label; filter in the dashboard
endpointstrhttps://api.trulayer.aiOverride for self-hosted or private deployments
batch_sizeint50Flush when this many spans are buffered
flush_intervalfloat2.0Flush after this many seconds, whichever comes first
timeoutfloat5.0HTTP request timeout in seconds
sample_ratefloat1.0Fraction of traces to send (0.0 – 1.0)
scrub_fnCallable[[Any], Any] | NoneNoneRun every input/output through this function before ingest
metadata_validatorCallable[[dict], bool] | NoneNoneReturn False to reject malformed metadata
debugboolFalseLog trace payloads locally instead of (or in addition to) sending

Example — production

import os
import re
import trulayer

def scrub(value):
    if isinstance(value, str):
        # Emails
        value = re.sub(r"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b", "[email]", value, flags=re.I)
        # Credit cards (loose)
        value = re.sub(r"\b\d{13,16}\b", "[cc]", value)
    return value

trulayer.init(
    api_key=os.environ["TRULAYER_API_KEY"],
    project="prod",
    environment="production",
    sample_rate=1.0,              # keep at 1.0 unless ingest cost is an issue
    batch_size=50,
    flush_interval=2.0,
    scrub_fn=scrub,
)

Example — local/offline

For CI, local development without network to the TruLayer backend, or sandboxed tests:
trulayer.init(
    api_key="tl_local",
    project="dev",
    debug=True,         # log payloads to stdout instead of shipping
)
Or point at a mock server:
trulayer.init(
    api_key="tl_local",
    project="dev",
    endpoint="http://localhost:4010",
)

Sampling

sample_rate=0.1 means 10% of traces are sent. Sampling is applied per trace (not per span), so a sampled trace always contains all of its spans. For deterministic sampling (same trace always in or out based on its ID), use the SDK’s default hash-based sampler. For random sampling, pass a callable:
import random
trulayer.init(
    api_key="...",
    project="...",
    sample_rate=lambda: random.random() < 0.1,
)

Flush control

Manual flush (useful in tests):
client = trulayer.get_client()
client.flush()  # blocks until all buffered spans are shipped
Shutdown (wait for flush + stop background thread):
trulayer.shutdown()