Skip to main content

Tenants

For a company that keeps the traces of many customers. With --tenants-dir, each tenant has its own store, in its own folder: no query can see another tenant’s spans, a tenant can be deleted by deleting its folder, and the same trace id in two tenants is two traces.
  • With --auth keys, the tenant of a request is the tenant of its API key (section API keys). A request may also send x-magpie-tenant, but only with the key’s tenant (else 403).
  • With --auth trusted-header, every request names its tenant in the header x-magpie-tenant (gRPC: the metadata key x-magpie-tenant): OTLP exports, queries and scores. Without it: 400. The server trusts the header: use this only behind a proxy or an OpenTelemetry Collector that authenticates the caller and sets the header (the Collector’s headers_setter extension). --tenants-dir without --auth does not start.
  • A server with --dir and no keys refuses the header (400), so a caller never thinks its data is apart when it is not.
  • The tenant is not read from the spans’ resource attributes: those come from the client, and one export could then mix tenants.
  • A tenant id is 1 to 64 characters of a-z, 0-9, _ and -. Lower case only: on a file system that ignores case (macOS by default), Acme and acme would be one folder.
  • A write (OTLP export, POST /v1/scores) makes the tenant’s store if it has none. A query of a tenant with no store is a 404, and no folder is made.
  • A tenant’s store opens at its first request and closes after --tenant-idle-secs without requests. When --max-open-tenants stores are open, the one used longest ago closes first; if every open store is in use by a request, the request gets 503 with Retry-After: 1 (OTLP clients retry). A close commits; with durability os or disk, what a failed close did not commit is replayed from the write log at the next open.
  • Why a limit: an open store holds 13 threads and one more for each CPU up to 8 (21 on a machine with 8 or more CPUs, 17 with 4) and about 7 MB of memory with no data, 11 to 25 MB after small writes (bench/tenants, results/tenants-*.json). The Mac of that test allows 6,144 threads per process (sysctl kern.num_taskthreads): the 293rd open store failed, and the next commit of an open store failed too. Each tenant store gets a 32 MB writer heap and a 32 MB queue (one store alone gets 256 MB each). By magpiedb’s rule (Options::max_queued_bytes: 2 to 3 times the queue plus the heap) a store that takes writes then holds about 100 to 130 MB at most; not measured.
  • Threads: the server reads the thread limit of its system (macOS kern.num_taskthreads; Linux pids.max of its cgroup, RLIMIT_NPROC, /proc/sys/kernel/threads-max) and keeps a reserve of an eighth of it (16 to 512 threads). At its start it writes a WARNING line when --max-open-tenants x the threads of one store does not fit. Before it opens a store with no room for the threads of one more store it closes the store used longest ago; if every open store is in use, the request gets 503 with Retry-After: 1 and a text that starts with THREADS: (gRPC UNAVAILABLE), and no folder is made for a new tenant. An open that the system refuses a thread for all the same gets the same answer. In a container set the pids limit to at least --max-open-tenants x 21 + 1,024. A pids limit of a Kubernetes pod is not seen from inside the container (cgroup v2): set --max-open-tenants by hand then (docs/guide/operations.md, “Memory and threads”).
  • A store whose commit got no thread cannot write (503). The next request for its tenant opens the store again when no other request holds it; a query waits up to 1 s for the others to end, a write never waits (it holds one of the 16 write places of all tenants): the writes that were answered OK before are then shown (they are in the write log). Until then its queries answer from its last commit.
  • Every 503 over HTTP has Retry-After: 1.
  • Shutdown closes every tenant store. A store that could not write is opened once more and closed, so its accepted writes are applied; the exit code is 0 when every store closed, and a store that did not is named in the log.

API keys

--auth keys --keys-file keys.jsonl (src/auth.rs). Every request needs a key, except GET /healthz.
  • Send it as Authorization: Bearer <key> (HTTP), or as the gRPC metadata authorization: Bearer <key>. The OpenTelemetry SDKs send it from OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20<key>".
  • A key belongs to one tenant and has the right read (queries: search, count, find, aggregate, get a trace, its span list, one span, get scores, sums of scores, the two exports), write (OTLP exports, POST /v1/scores, POST /v1/traces/delete, POST /v1/purge), or both. An app that only sends traces needs write; a dashboard needs read.
  • No key, a key that is not in the file, or not Bearer <key>: 401 with WWW-Authenticate: Bearer. A key without the right: 403. An x-magpie-tenant header that is not the key’s tenant: 403, and nothing is read or written. A key never decides more than its own tenant’s store.
  • A server with one store (--dir) takes keys of tenant default only (or lines with no tenant), so nobody thinks their data is apart. Keys are optional there.
  • A wrong method on a path of the API is 405 with Allow, as without keys, before the key is looked at.
  • A new HTTP route must be listed with its right in ROUTES in src/auth.rs. With keys, a route that is not listed gets 500 and does not run.
Make a key:
The line for the key file goes to stdout, the key to stderr. The key is shown only then: the file holds its SHA-256, not the key. The key is mgp_ and 64 hex characters (32 random bytes from the system). A plain SHA-256 is enough because the key is random, not a password a person picks; a slow password hash would only make every request slower. The key file is JSON Lines, one key per line. Blank lines and lines that start with # are skipped. Unknown fields, a bad sha256, an empty rights, a bad tenant id or the same key twice: the file is refused.
  • The server reads the file every 2 s. A new line works, and a removed line stops working, within about 2 s, with no restart. A file that cannot be read or has a bad line is reported once on stderr, and the keys from before stay in use. At start, a bad file stops the server.
  • Each request compares the hash of its key with every hash in the file, each in constant time, so the time of a request says nothing about how close a guess was. The work grows with the number of keys; not measured.
  • The server never logs keys, and no error message holds one.
  • JSON, not TOML: the server reads JSON already (no new crate), and a new key is one line that >> appends.

TLS

--tls-cert cert.pem --tls-key key.pem turns on TLS on both ports (src/tls.rs): rustls with the ring crypto provider, TLS 1.2 and 1.3. The certificate file holds the chain (the server’s certificate first); the key file holds one private key (PEM; rustls reads PKCS#8, PKCS#1 and SEC1; the tests use an RSA key in PKCS#8). The HTTP port then speaks HTTPS only (HTTP/1.1), the gRPC port gRPC over TLS (HTTP/2, ALPN h2). A client that does not finish its TLS handshake in 10 s is dropped. No client certificates (mTLS). Why in the server and not only behind a proxy: an API key sent without TLS can be read on the network, and one binary with keys and TLS is a complete setup. rustls, tokio-rustls and ring were already in Cargo.lock (magpie-s3’s HTTP client uses them), so Cargo.lock has no new crate. A proxy that ends TLS (nginx, Caddy, a cloud load balancer) still works: run the server without --tls-* on a private address behind it, and pass the authorization header through. For gRPC through a proxy, the proxy must speak HTTP/2 to the server (for example nginx grpc_pass grpc://...). A changed certificate needs a restart. The dates of the certificate. At start the server reads notBefore and notAfter of every certificate in --tls-cert and prints the end of the server’s own certificate, the first of the file (TLS certificate cert.pem: valid until 2027-03-01T12:00:00Z). When that one is not valid now, the server does not start (exit code 1, the dates in UTC): every client would refuse it and drop its spans. Another certificate of the file (an intermediate) that is not valid now, ends soon or cannot be read is a warning that names its place, at start and once a day: WARNING: certificate 3 of 3 in cert.pem expired at ...: a client that needs it to trust the chain refuses the chain. It never stops the start: a file can keep an old cross-signed copy of an intermediate after that copy has expired, and clients that find the chain through the valid copy connect. When the end is less than 30 days away (or less than a third of the certificate’s whole time, for a certificate of under 90 days), the server warns at start, and once a day while it runs; after the end, an error line every hour. A failed TLS handshake is logged on stderr: the first one at once, then at most one line a minute per port with their count and the last reason (for example received fatal alert: CertificateExpired or UnknownCA from the client). A connection that closes before its handshake (a TCP health check) is not counted; the client’s address is not logged. Test certificates: e2e/run.mjs and tests/auth.rs make a test CA and a server certificate for localhost and 127.0.0.1 with the openssl command. A rustls client refuses a server certificate that says it is a CA (CaUsedAsEndEntity, rustls-webpki verify_cert.rs), and openssl req -x509 makes CA certificates by default: so the tests sign a server certificate with basicConstraints=CA:FALSE with their CA.