Telemetry
Security and audit SQL recipe

Audit API-Key Lifecycle Events

Review creation, use, rotation, and revocation events without logging API-key material.

Intermediateapi_key_audit_eventsReviewed 2026-07-28Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . SQL compatibility, event contract, synthetic output, and operational caveats. Editorial ownership

Question answered

Which API-key lifecycles require owner review?

A lifecycle audit identifies keys that were created but never used, remain active past rotation expectations, or were used after a revocation workflow began.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampLifecycle event time.
key_idUtf8Non-secret key record identifier.
owner_typeUtf8Service, user, or automation owner class.
scope_classUtf8Bounded scope category.
event_nameUtf8key_created, key_used, key_rotated, or key_revoked.
outcomeUtf8success, denied, or failed.
DataFusion SQL

Copy the query

sql
SELECT
  key_id,
  owner_type,
  scope_class,
  MIN(CASE WHEN event_name = 'key_created' THEN timestamp_utc END) AS created_at,
  MAX(CASE WHEN event_name = 'key_used' THEN timestamp_utc END) AS last_used_at,
  MAX(CASE WHEN event_name = 'key_rotated' THEN timestamp_utc END) AS rotated_at,
  MAX(CASE WHEN event_name = 'key_revoked' THEN timestamp_utc END) AS revoked_at,
  SUM(CASE WHEN outcome <> 'success' THEN 1 ELSE 0 END) AS failed_events
FROM api_key_audit_events
WHERE timestamp_utc >= now() - INTERVAL '90 days'
GROUP BY key_id, owner_type, scope_class
ORDER BY created_at;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions against your own data. Read the testing methodology.

Query result

API-key lifecycle review

The table preserves lifecycle timestamps and non-secret identifiers for review.

key_idowner_typescope_classcreated_atlast_used_atrotated_atrevoked_atfailed_events
key-record-17automationread-write2026-05-022026-07-280
key-record-42servicewrite2026-07-012026-07-262026-07-272026-07-271

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

API-key lifecycle review: static chart of synthetic last_used_at values from the Audit API-Key Lifecycle Events example result
Indexable SVG of the deterministic example output. Download it for an article, runbook, or design review with attribution.

Reproduce the example

Download the public fixture

The JSON bundle includes the typed event contract, illustrative input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Conditional timestamps collapse a key lifecycle into one review row.
  2. 2A non-secret key record ID supports correlation without exposing key material.
  3. 3Failed lifecycle events remain visible beside terminal state.

Edge cases to decide

  • Never log the key, token prefix, authorization header, or a reversible hash.
  • Rotation can involve overlapping old and new keys.
  • Retention and access to credential metadata need security approval.

Recommended dashboard

  • Table: key lifecycle by owner class
  • Stat: active keys without recent use
  • Trend: failed rotation and revocation events

Alert guidance

Alert on failed revocation, unexpected post-revocation use, or a privileged key outside the approved rotation window.

Read alert setup

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key