""" Generates random `pp_auth.rollout.would_deny` count metrics to Datadog. Intended for generating demo data for the PP Auth rollout dashboard. """ import os import random import sys import time from datadog import api, initialize DD_API_KEY = os.environ.get("DD_API_KEY") DD_APP_KEY = os.environ.get("DD_APP_KEY") LOOP_INTERVAL = int(os.environ.get("LOOP_INTERVAL_SECONDS", "5")) METRICS_PER_LOOP = int(os.environ.get("METRICS_PER_LOOP", "10")) SERVICES = [ "ows-catalog", "ows-content", "ows-media", "ows-releases", "ows-product-staging", ] ENDPOINTS_BY_SERVICE = { "ows-catalog": ["/catalog/", "/catalog/", "/catalog//tracks"], "ows-content": ["/content/", "/content/", "/content//assets"], "ows-media": ["/media/", "/media/", "/media//preview"], "ows-releases": ["/release/", "/release/", "/release//tracks"], "ows-product-staging": ["/bulk-session/", "/bulk-session/", "/bulk-session-ingestion/"], } ENVIRONMENTS = ["dev"] REASONS = ["no_pp_resource", "pp_denied", "fallback_grass", "fallback_access_rules", "exception"] METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH"] def send_one() -> None: service = random.choice(SERVICES) env = random.choice(ENVIRONMENTS) reason = random.choice(REASONS) method = random.choice(METHODS) endpoint = random.choice(ENDPOINTS_BY_SERVICE[service]) has_auth = random.random() < 0.8 # 80 % of requests carry an auth header metric = f"pp_auth.rollout.would_deny" tags = [ f"environment:{env}", f"service_name:{service}", f"reason:{reason}", f"method:{method}", f"endpoint:{endpoint}", f"has_authorization_header:{str(has_auth).lower()}", ] api.Metric.send( metric=metric, points=[(time.time(), 1)], type="count", tags=tags, ) tag_str = ", ".join(tags) print(f"sent {metric} [{tag_str}]", flush=True) def main() -> None: if not DD_API_KEY or not DD_APP_KEY: print("ERROR: DD_API_KEY and DD_APP_KEY must be set", file=sys.stderr) sys.exit(1) initialize(api_key=DD_API_KEY, app_key=DD_APP_KEY) print(f"initialized — sending {METRICS_PER_LOOP} metrics every {LOOP_INTERVAL}s", flush=True) while True: for _ in range(METRICS_PER_LOOP): send_one() time.sleep(LOOP_INTERVAL) if __name__ == "__main__": main()