mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 09:03:37 +01:00
feat(logging): label billing routes and checkout outcomes
Billing API calls and Stripe return pages were logged as route "other", so the purchase funnel could only be read from Stripe. Give them fixed labels in the app, Nginx and the collector allowlist, and record the ?billing= outcome (success, cancelled, portal) on edge requests. Only fixed values are kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d34df81c8c
commit
4099953d8a
5 files changed
+41
-3
No files matched your search
@@ -31,6 +31,8 @@ if service == "mongodb" {
|
||||
record.ms = (to_float(record.request_time) ?? 0.0) * 1000.0
|
||||
if is_string(record.edge_request_id) && match(string!(record.edge_request_id), r'^[a-f0-9]{32}$') { out.edge_request_id = record.edge_request_id }
|
||||
if is_string(record.cf_ray) && match(string!(record.cf_ray), r'^[a-f0-9]{16}-[A-Z]{3}$') { out.cf_ray = record.cf_ray }
|
||||
# Checkout return pages carry a fixed outcome; nothing else from the query string.
|
||||
if includes(["success", "cancelled", "portal"], record.billing_outcome) { out.billingOutcome = record.billing_outcome }
|
||||
if includes(["PASSED", "DELAYED", "REJECTED", "DELAYED_DRY_RUN", "REJECTED_DRY_RUN"], record.limit_req_status) { out.limit_req_status = record.limit_req_status }
|
||||
# Deliberately omit IPs by default. Original edge JSON has short, restricted
|
||||
# host retention for incident response; no plain or unsalted hashed IP archive.
|
||||
@@ -64,7 +66,7 @@ for_each(["memoryUsedBytes", "memoryFileCacheBytes", "memoryWorkingSetBytes", "m
|
||||
if is_integer(value) || is_float(value) { out = set!(out, [key], value) }
|
||||
}
|
||||
if includes(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "OTHER"], record.method) { out.method = record.method }
|
||||
if includes(["/", "other", "/dashboard", "/anonymize", "/healthcheck", "/api/healthcheck", "/api", "/api/download", "/api/stat", "/api/stat/history", "/api/options", "/api/message", "/api/user", "/api/user/quota", "/api/user/dashboard", "/api/repo", "/api/anonymize-preview", "/api/admin/overview", "/api/admin/performance", "/api/admin/errors", "/api/admin/errors/stats", "/api/admin/queues", "/api/admin/queues/metrics", "/github/app/webhook", "/api/repo/:repoId", "/api/repo/:repoId/file/:path", "/api/repo/:repoId/files", "/api/repo/:repoId/files/counts", "/api/repo/:repoId/files/search", "/api/repo/:repoId/zip", "/api/repo/:repoId/options", "/api/repo/:repoId/refresh", "/api/repo/:repoId/coauthors", "/api/repo/:repoId/extend", "/api/repo/:owner/:repo", "/api/repo/:owner/:repo/tree", "/api/repo/:owner/:repo/file", "/api/repo/:owner/:repo/readme", "/api/repo/:owner/:repo/branches", "/w/:repoId/:path", "/r/:repoId/:path", "/api/pr/:id/:path", "/api/gist/:id/:path", "/assets/:path", "/github/:action", "/api/admin/:action", "/api/user/:action", "/api/repo/:id/file/:path", "/api/repo/:id/files/counts", "/api/repo/:id/files/search", "/api/repo/:id/files", "/api/repo/:id/zip", "/api/repo/:id", "/viewer/:id/:path"], record.route) { out.route = record.route }
|
||||
if includes(["/", "other", "/dashboard", "/anonymize", "/healthcheck", "/api/healthcheck", "/api", "/api/download", "/api/stat", "/api/stat/history", "/api/options", "/api/message", "/api/user", "/api/user/quota", "/api/user/dashboard", "/api/repo", "/api/anonymize-preview", "/api/admin/overview", "/api/admin/performance", "/api/admin/errors", "/api/admin/errors/stats", "/api/admin/queues", "/api/admin/queues/metrics", "/github/app/webhook", "/api/repo/:repoId", "/api/repo/:repoId/file/:path", "/api/repo/:repoId/files", "/api/repo/:repoId/files/counts", "/api/repo/:repoId/files/search", "/api/repo/:repoId/zip", "/api/repo/:repoId/options", "/api/repo/:repoId/refresh", "/api/repo/:repoId/coauthors", "/api/repo/:repoId/extend", "/api/repo/:owner/:repo", "/api/repo/:owner/:repo/tree", "/api/repo/:owner/:repo/file", "/api/repo/:owner/:repo/readme", "/api/repo/:owner/:repo/branches", "/w/:repoId/:path", "/r/:repoId/:path", "/api/pr/:id/:path", "/api/gist/:id/:path", "/assets/:path", "/github/:action", "/api/admin/:action", "/api/user/:action", "/api/repo/:id/file/:path", "/api/repo/:id/files/counts", "/api/repo/:id/files/search", "/api/repo/:id/files", "/api/repo/:id/zip", "/api/repo/:id", "/viewer/:id/:path", "/api/billing", "/api/billing/draft", "/api/billing/draft/:id", "/api/billing/checkout", "/api/billing/portal", "/api/billing/refresh", "/api/billing/webhook", "/api/conferences/plans", "/profile", "/conference/:id"], record.route) { out.route = record.route }
|
||||
# VictoriaLogs consumes its message field; keep event independently searchable.
|
||||
# Anonymous application request context at every outcome, never query strings.
|
||||
{
|
||||
|
||||
@@ -40,6 +40,17 @@ map $uri $anonymous_log_route {
|
||||
/ "/";
|
||||
/api/healthcheck "/api/healthcheck";
|
||||
/api/user "/api/user";
|
||||
/api/billing "/api/billing";
|
||||
/api/billing/draft "/api/billing/draft";
|
||||
~^/api/billing/draft/[^/]+$ "/api/billing/draft/:id";
|
||||
/api/billing/checkout "/api/billing/checkout";
|
||||
/api/billing/portal "/api/billing/portal";
|
||||
/api/billing/refresh "/api/billing/refresh";
|
||||
/api/billing/webhook "/api/billing/webhook";
|
||||
/api/conferences/plans "/api/conferences/plans";
|
||||
/profile "/profile";
|
||||
~^/anonymize(/[^/]+)?$ "/anonymize";
|
||||
~^/conference/[^/]+$ "/conference/:id";
|
||||
~^/api/repo/[^/]+/file/ "/api/repo/:id/file/:path";
|
||||
~^/api/repo/[^/]+/files/counts "/api/repo/:id/files/counts";
|
||||
~^/api/repo/[^/]+/files/search "/api/repo/:id/files/search";
|
||||
@@ -52,12 +63,19 @@ map $uri $anonymous_log_route {
|
||||
~^/api/user/ "/api/user/:action";
|
||||
~^/(css|script|fonts|partials)/ "/assets/:path";
|
||||
}
|
||||
# Stripe returns buyers with ?billing=<outcome>; log only these fixed values.
|
||||
map $arg_billing $anonymous_billing_outcome {
|
||||
default "";
|
||||
success "success";
|
||||
cancelled "cancelled";
|
||||
portal "portal";
|
||||
}
|
||||
log_format anonymous_json escape=json
|
||||
'{"time":"$time_iso8601","host":"$host","server":"$server_name",'
|
||||
'"client_ip":"$anonymous_client_ip","peer_ip":"$remote_addr",'
|
||||
'"cloudflare_peer":"$anonymous_cloudflare_peer","cf_ray":"$anonymous_cf_ray",'
|
||||
'"edge_request_id":"$request_id","app_request_id":"$upstream_http_x_request_id",'
|
||||
'"method":"$request_method","route":"$anonymous_log_route","protocol":"$server_protocol",'
|
||||
'"method":"$request_method","route":"$anonymous_log_route","billing_outcome":"$anonymous_billing_outcome","protocol":"$server_protocol",'
|
||||
'"status":$status,"limit_req_status":"$limit_req_status","upstream_status":"$upstream_status",'
|
||||
'"request_time":$request_time,"upstream_connect_time":"$upstream_connect_time",'
|
||||
'"upstream_header_time":"$upstream_header_time","upstream_response_time":"$upstream_response_time",'
|
||||
|
||||
@@ -2926,4 +2926,4 @@ export const LOG_SOURCE_FUNCTIONS: Record<string, readonly string[]> = {
|
||||
]
|
||||
};
|
||||
export const LOG_ERROR_TYPES = ["Error","TypeError","RangeError","ReferenceError","SyntaxError","URIError","EvalError","AggregateError","AnonymousError","HTTPError","RequestError","MongoServerError","MongoNetworkError","MongoServerSelectionError","ValidationError","TimeoutError","AbortError","UnknownError"];
|
||||
export const LOG_ROUTES = ["/","other","/dashboard","/anonymize","/healthcheck","/api/healthcheck","/api","/api/download","/api/stat","/api/stat/history","/api/options","/api/message","/api/user","/api/user/quota","/api/user/dashboard","/api/repo","/api/anonymize-preview","/api/admin/overview","/api/admin/performance","/api/admin/errors","/api/admin/errors/stats","/api/admin/queues","/api/admin/queues/metrics","/github/app/webhook","/api/repo/:repoId","/api/repo/:repoId/file/:path","/api/repo/:repoId/files","/api/repo/:repoId/files/counts","/api/repo/:repoId/files/search","/api/repo/:repoId/zip","/api/repo/:repoId/options","/api/repo/:repoId/refresh","/api/repo/:repoId/coauthors","/api/repo/:repoId/extend","/api/repo/:owner/:repo","/api/repo/:owner/:repo/tree","/api/repo/:owner/:repo/file","/api/repo/:owner/:repo/readme","/api/repo/:owner/:repo/branches","/w/:repoId/:path","/r/:repoId/:path","/api/pr/:id/:path","/api/gist/:id/:path","/assets/:path","/github/:action","/api/admin/:action","/api/user/:action","/api/repo/:id/file/:path","/api/repo/:id/files/counts","/api/repo/:id/files/search","/api/repo/:id/files","/api/repo/:id/zip","/api/repo/:id","/viewer/:id/:path"];
|
||||
export const LOG_ROUTES = ["/","other","/dashboard","/anonymize","/healthcheck","/api/healthcheck","/api","/api/download","/api/stat","/api/stat/history","/api/options","/api/message","/api/user","/api/user/quota","/api/user/dashboard","/api/repo","/api/anonymize-preview","/api/admin/overview","/api/admin/performance","/api/admin/errors","/api/admin/errors/stats","/api/admin/queues","/api/admin/queues/metrics","/github/app/webhook","/api/repo/:repoId","/api/repo/:repoId/file/:path","/api/repo/:repoId/files","/api/repo/:repoId/files/counts","/api/repo/:repoId/files/search","/api/repo/:repoId/zip","/api/repo/:repoId/options","/api/repo/:repoId/refresh","/api/repo/:repoId/coauthors","/api/repo/:repoId/extend","/api/repo/:owner/:repo","/api/repo/:owner/:repo/tree","/api/repo/:owner/:repo/file","/api/repo/:owner/:repo/readme","/api/repo/:owner/:repo/branches","/w/:repoId/:path","/r/:repoId/:path","/api/pr/:id/:path","/api/gist/:id/:path","/assets/:path","/github/:action","/api/admin/:action","/api/user/:action","/api/repo/:id/file/:path","/api/repo/:id/files/counts","/api/repo/:id/files/search","/api/repo/:id/files","/api/repo/:id/zip","/api/repo/:id","/viewer/:id/:path","/api/billing","/api/billing/draft","/api/billing/draft/:id","/api/billing/checkout","/api/billing/portal","/api/billing/refresh","/api/billing/webhook","/api/conferences/plans","/profile","/conference/:id"];
|
||||
@@ -64,6 +64,8 @@ const staticRoutes = new Set([
|
||||
"/api/options", "/api/message", "/api/user", "/api/user/quota", "/api/user/dashboard", "/api/repo",
|
||||
"/api/anonymize-preview", "/api/admin/overview", "/api/admin/performance", "/api/admin/errors",
|
||||
"/api/admin/errors/stats", "/api/admin/queues", "/api/admin/queues/metrics", "/github/app/webhook",
|
||||
"/api/billing", "/api/billing/draft", "/api/billing/checkout", "/api/billing/portal", "/api/billing/refresh",
|
||||
"/api/billing/webhook", "/api/conferences/plans", "/profile",
|
||||
]);
|
||||
|
||||
/** Fixed labels: never use repository IDs, file names or query strings as metric keys. */
|
||||
@@ -86,6 +88,9 @@ export function requestRoute(url: string, service: Service, method = "GET"): str
|
||||
if (github) return `/api/repo/:owner/:repo/${github[1]}`;
|
||||
return "/api/repo/:repoId";
|
||||
}
|
||||
if (/^\/api\/billing\/draft\/[^/]+$/.test(path)) return "/api/billing/draft/:id";
|
||||
if (/^\/anonymize\/[^/]+$/.test(path)) return "/anonymize";
|
||||
if (/^\/conference\/[^/]+$/.test(path)) return "/conference/:id";
|
||||
if (path.startsWith("/w/")) return "/w/:repoId/:path";
|
||||
if (/^\/(r|repository)\//.test(path)) return "/r/:repoId/:path";
|
||||
if (/^\/api\/(pr|gist)\//.test(path)) return `/api/${path.split("/")[2]}/:id/:path`;
|
||||
|
||||
@@ -97,6 +97,19 @@ describe("bounded request monitoring", function () {
|
||||
const labels = new Set(Array.from({ length: 10000 }, (_, i) => requestRoute(`/unknown-${i}/private-${i}`, "api")));
|
||||
expect([...labels]).to.deep.equal(["other"]);
|
||||
expect(requestRoute("/healthcheck", "streamer")).to.equal("/healthcheck");
|
||||
const { LOG_ROUTES } = require("../src/core/log-diagnostic-schema");
|
||||
const nginx = require("fs").readFileSync(require("path").join(__dirname, "../deploy/nginx/anonymous-logging.conf"), "utf8");
|
||||
for (const [path, method, route] of [["/api/billing", "GET", "/api/billing"], ["/api/billing/checkout?x=1", "POST", "/api/billing/checkout"],
|
||||
["/api/billing/draft", "POST", "/api/billing/draft"], ["/api/billing/draft/3f2a-private", "GET", "/api/billing/draft/:id"],
|
||||
["/api/billing/portal", "POST", "/api/billing/portal"], ["/api/billing/refresh", "POST", "/api/billing/refresh"],
|
||||
["/api/billing/webhook", "POST", "/api/billing/webhook"], ["/api/conferences/plans", "GET", "/api/conferences/plans"],
|
||||
["/profile?billing=success", "GET", "/profile"], ["/conference/private-conf?billing=success", "GET", "/conference/:id"]]) {
|
||||
expect(requestRoute(path, "api", method)).to.equal(route);
|
||||
expect(LOG_ROUTES).to.include(route);
|
||||
expect(nginx).to.include(`"${route}";`);
|
||||
}
|
||||
expect(requestRoute("/api/billing/draft/a/b", "api")).to.equal("other");
|
||||
expect(requestRoute("/anonymize/private-repo?billingDraft=x", "api")).to.equal("/anonymize");
|
||||
const bounded = new RequestMetrics(3);
|
||||
for (let i = 0; i < 1000; i++) bounded.observe("request", String(i), "GET", 10);
|
||||
expect(bounded.size).to.equal(3); expect(bounded.dropped).to.equal(997);
|
||||
|
||||
Reference in new issue
Block a user