Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clickhouse migration #2 #2135

Merged
merged 34 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dc19429
feat: clickhouse keyverification tables
chronark Sep 26, 2024
016bc0a
feat(clickhouse): add new file latest_verifications.ts for querying l…
chronark Sep 26, 2024
da476be
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 26, 2024
28728e3
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Sep 26, 2024
0b1ecfb
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 6, 2024
a63a55e
feat: add ratelimits
chronark Oct 6, 2024
87089f4
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 7, 2024
1dc863c
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 7, 2024
68bd7e2
refactor: clickhouse table names
chronark Oct 7, 2024
92d5d7f
refactor: replace more pipes
chronark Oct 7, 2024
a180ebc
refactor: replace more pipes
chronark Oct 7, 2024
884a127
refactor: replace more pipes
chronark Oct 8, 2024
c759550
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 8, 2024
1914227
wip: everything broken
chronark Oct 9, 2024
879ffc3
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 9, 2024
8562c03
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 9, 2024
ee5757d
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 9, 2024
df5a4e1
wip
chronark Oct 9, 2024
4c61745
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 10, 2024
7e3a75e
feat: billing verifications
chronark Oct 10, 2024
66584f4
wip
chronark Oct 10, 2024
ca01f03
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Oct 20, 2024
296e27d
fix: merge conflicts
chronark Oct 20, 2024
cb0e0b4
Merge branch 'main' of https://github.com/unkeyed/unkey into clickhou…
chronark Nov 4, 2024
e1a7ba6
feat: ratelimits on clickhouse
chronark Nov 4, 2024
3a878fd
feat: ratelimit logs
chronark Nov 4, 2024
b453a1b
wip
chronark Nov 5, 2024
538392b
feat: it builds again
chronark Nov 5, 2024
966890f
feat: downmigrations
chronark Nov 5, 2024
fa0b112
feat: downmigrations
chronark Nov 5, 2024
b0650ed
fix: type export
chronark Nov 5, 2024
737262c
Update internal/clickhouse/src/ratelimits.ts
chronark Nov 5, 2024
69093ae
fix: table engine
chronark Nov 6, 2024
ed3fa8d
Merge branch 'clickhouse-migration-#2' of https://github.com/unkeyed/…
chronark Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tasks:
env:
GOOSE_DRIVER: clickhouse
GOOSE_DBSTRING: "tcp://default:password@127.0.0.1:9000"
GOOSE_MIGRATION_DIR: ./apps/agent/pkg/clickhouse/schema
GOOSE_MIGRATION_DIR: ./internal/clickhouse/schema
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Path change requires code updates

The migration files have been successfully moved to the new location, but there are several files still referencing the old path that need to be updated:

  • apps/agent/services/eventrouter/service.go
  • apps/agent/pkg/clickhouse/client.go
  • apps/agent/pkg/clickhouse/noop.go
  • apps/agent/pkg/clickhouse/interface.go
  • apps/agent/pkg/api/interface.go
  • apps/agent/pkg/api/mw_logging.go

These files need to have their import statements updated from github.com/unkeyed/unkey/apps/agent/pkg/clickhouse/schema to match the new path.

🔗 Analysis chain

LGTM! Verify migration files transfer.

The path change from ./apps/agent/pkg/clickhouse/schema to ./internal/clickhouse/schema aligns with standard Go project layout.

Let's verify all migration files were transferred correctly:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify migration files transfer
# Check both old and new locations for SQL files
echo "Checking old location for any remaining files:"
fd -e sql . "apps/agent/pkg/clickhouse/schema" || true

echo -e "\nChecking new location for transferred files:"
fd -e sql . "internal/clickhouse/schema" || true

Length of output: 194


Script:

#!/bin/bash
# Let's try a different approach to verify the migration files

# 1. Check if the old directory structure exists
echo "Checking if old directory exists:"
ls -la apps/agent/pkg/clickhouse/schema 2>/dev/null || echo "Old directory not found"

# 2. Check if the new directory structure exists
echo -e "\nChecking if new directory exists:"
ls -la internal/clickhouse/schema 2>/dev/null || echo "New directory not found"

# 3. Search for any references to both old and new paths in the codebase
echo -e "\nSearching for references to old path:"
rg "apps/agent/pkg/clickhouse/schema" --type-not sql

echo -e "\nSearching for references to new path:"
rg "internal/clickhouse/schema" --type-not sql

# 4. Look for SQL files in the entire repository
echo -e "\nListing all SQL files in the repository:"
find . -name "*.sql" -type f

Length of output: 6027

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Migration files successfully moved, but cleanup needed in old location

The migration files have been successfully moved to the new location at internal/clickhouse/schema/ with a complete sequence of SQL migrations (001-024). However, there's still a requests.go file remaining in the old location apps/agent/pkg/clickhouse/schema/ that should be reviewed and cleaned up.

  • apps/agent/pkg/clickhouse/schema/requests.go needs to be reviewed and either moved or removed
🔗 Analysis chain

Verify migration files location.

The migration path has been updated from ./apps/agent/pkg/clickhouse/schema to ./internal/clickhouse/schema, which aligns with centralizing ClickHouse-related code.

Let's verify that the migration files exist in the new location:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify migration files have been moved to the new location
# Expected: Migration files should exist in the new location

# Check if migration files exist in the new location
fd -t f . "internal/clickhouse/schema"

# Ensure no migration files are left in the old location
fd -t f . "apps/agent/pkg/clickhouse/schema"

Length of output: 2070

cmds:
- goose up

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@hono/zod-validator": "^0.2.1",
"@planetscale/database": "^1.16.0",
"@unkey/cache": "workspace:^",
"@unkey/clickhouse-zod": "workspace:^",
"@unkey/clickhouse": "workspace:^",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Update remaining import in internal/clickhouse/src/last_used.ts to use new package

There is still one file using the old @unkey/clickhouse-zod package:

  • internal/clickhouse/src/last_used.ts needs to update its import from @unkey/clickhouse-zod to @unkey/clickhouse
🔗 Analysis chain

Verify migration from @unkey/clickhouse-zod.

Let's ensure all imports have been updated to use the new package.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to the old package
# and verify that the new package is properly imported where needed

# Check for any remaining references to the old package
echo "Checking for remaining references to @unkey/clickhouse-zod:"
rg "@unkey/clickhouse-zod"

# Check current usage of the new package
echo -e "\nVerifying imports of @unkey/clickhouse:"
rg -A 2 "from ['\"']@unkey/clickhouse['\"]"

Length of output: 848

"@unkey/db": "workspace:^",
"@unkey/encryption": "workspace:^",
"@unkey/error": "workspace:^",
Expand Down
36 changes: 27 additions & 9 deletions apps/api/src/pkg/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NoopTinybird, Tinybird } from "@chronark/zod-bird";
import * as ch from "@unkey/clickhouse-zod";
import { ClickHouse } from "@unkey/clickhouse";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for required ClickHouse configuration.

The optional chaining on opts.clickhouse?.url could lead to runtime issues if the ClickHouse configuration is missing. Consider making this configuration required or adding explicit validation.

 constructor(opts: {
   tinybirdToken?: string;
   tinybirdProxy?: {
     url: string;
     token: string;
   };
-  clickhouse?: {
+  clickhouse: {
     url: string;
   };
 }) {
   this.readClient = opts.tinybirdToken
     ? new Tinybird({ token: opts.tinybirdToken })
     : new NoopTinybird();

   this.writeClient = opts.tinybirdProxy
     ? new Tinybird({ token: opts.tinybirdProxy.token, baseUrl: opts.tinybirdProxy.url })
     : this.readClient;

-  this.clickhouse = new ClickHouse({ url: opts.clickhouse?.url });
+  if (!opts.clickhouse?.url) {
+    throw new Error("ClickHouse configuration is required");
+  }
+  this.clickhouse = new ClickHouse({ url: opts.clickhouse.url });
 }

Also applies to: 21-21, 41-41

import { newId } from "@unkey/id";
import { auditLogSchemaV1, unkeyAuditLogEvents } from "@unkey/schema/src/auditlog";
import { ratelimitSchemaV1 } from "@unkey/schema/src/ratelimit-tinybird";
Expand All @@ -18,7 +18,7 @@ const dateToUnixMilli = z.string().transform((t) => new Date(t.split(" ").at(0)
export class Analytics {
public readonly readClient: Tinybird | NoopTinybird;
public readonly writeClient: Tinybird | NoopTinybird;
private clickhouse: ch.Clickhouse;
private clickhouse: ClickHouse;

constructor(opts: {
tinybirdToken?: string;
Expand All @@ -38,12 +38,12 @@ export class Analytics {
? new Tinybird({ token: opts.tinybirdProxy.token, baseUrl: opts.tinybirdProxy.url })
: this.readClient;

this.clickhouse = opts.clickhouse ? new ch.Client({ url: opts.clickhouse.url }) : new ch.Noop();
this.clickhouse = new ClickHouse({ url: opts.clickhouse?.url });
}

public get insertSdkTelemetry() {
return this.clickhouse.insert({
table: "default.raw_telemetry_sdks_v1",
return this.clickhouse.client.insert({
table: "telemetry.raw_sdks_v1",
schema: z.object({
request_id: z.string(),
time: z.number().int(),
Expand Down Expand Up @@ -107,6 +107,20 @@ export class Analytics {
})),
});
}
public get insertRatelimit() {
return this.clickhouse.client.insert({
table: "ratelimits.raw_ratelimits_v1",
schema: z.object({
request_id: z.string(),
time: z.number().int(),
workspace_id: z.string(),
namespace_id: z.string(),
identifier: z.string(),
passed: z.boolean(),
}),
});
}

//tinybird
public get ingestRatelimit() {
return this.writeClient.buildIngestEndpoint({
Expand All @@ -116,8 +130,8 @@ export class Analytics {
}

public get insertKeyVerification() {
return this.clickhouse.insert({
table: "default.raw_key_verifications_v1",
return this.clickhouse.client.insert({
table: "verifications.raw_key_verifications_v1",
schema: z.object({
request_id: z.string(),
time: z.number().int(),
Expand All @@ -140,8 +154,8 @@ export class Analytics {
}

public get insertApiRequest() {
return this.clickhouse.insert({
table: "default.raw_api_requests_v1",
return this.clickhouse.client.insert({
table: "metrics.raw_api_requests_v1",
schema: z.object({
request_id: z.string(),
time: z.number().int(),
Expand All @@ -158,6 +172,10 @@ export class Analytics {
service_latency: z.number().int(),
user_agent: z.string(),
ip_address: z.string(),
continent: z.string().nullable().default(""),
city: z.string().nullable().default(""),
country: z.string().nullable().default(""),
colo: z.string().nullable().default(""),
}),
});
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/pkg/keys/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export class KeyService {
}

return [
res.val.pass,
res.val.passed,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Inconsistency found in property naming

The change from pass to passed is part of a larger pattern, but there's an inconsistency in apps/api/src/pkg/keys/service.ts. While the property access uses passed, the destructured variable and documentation still use pass:

  • Line 681: Uses res.val.passed
  • But the code still has:
    • const [pass, ratelimit] = await this.ratelimit()
    • @returns [pass, ratelimit]
🔗 Analysis chain

LGTM! Property rename for consistency.

The change from pass to passed aligns with the standardized property naming across the codebase.

Let's verify that all usages of this property have been updated consistently:


Let me analyze the search results further to ensure all instances are properly updated.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining instances of the old property name 'pass'
# that might need to be updated to 'passed' in rate limiting related code

# Search for 'pass' property in rate limit related code
rg -i 'pass' --type ts | grep -i 'ratelimit'

# Search for rate limit response type definitions
ast-grep --pattern 'interface $name {
  $$$
  pass: boolean
  $$$
}'

Length of output: 6189


Script:

#!/bin/bash
# Let's check the specific interface definitions and usages in the ratelimit related code
ast-grep --pattern 'interface $name {
  $$$
  passed: boolean
  $$$
}'

# Also check the specific file for any remaining 'pass' references
rg 'pass' apps/api/src/pkg/keys/service.ts

Length of output: 263

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Property name change needs attention in _verifyKey method

The search results show that while the property name has been changed to passed in most places, there's an inconsistency in the _verifyKey method where the old variable name pass is still being used:

const [pass, ratelimit] = await this.ratelimit(c, data.key, ratelimits);

if (!pass) {
  // ...
}

This should be updated to match the new property name for consistency:

  • apps/api/src/pkg/keys/service.ts: Update the variable name from pass to passed in the _verifyKey method to match the property name change.
🔗 Analysis chain

Property name change from pass to passed looks good.

The change aligns with the broader refactoring effort across the codebase to use consistent property names.

Let's verify the consistency of this naming change across the codebase:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining instances of the old property name 'pass' in rate limiting context
rg -A 3 -B 3 'pass.*ratelimit|ratelimit.*pass' 

# Search for the new property name 'passed' to ensure consistent usage
rg -A 3 -B 3 'passed.*ratelimit|ratelimit.*passed'

Length of output: 22429

{
remaining: res.val.remaining,
limit: ratelimits.default?.limit,
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/pkg/middleware/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ export function metrics(): MiddlewareHandler<HonoEnv> {
service_latency: Date.now() - c.get("requestStartedAt"),
ip_address: c.req.header("True-Client-IP") ?? c.req.header("CF-Connecting-IP") ?? "",
user_agent: c.req.header("User-Agent") ?? "",
// @ts-ignore - this is a bug in the types
continent: c.req.raw?.cf?.continent,
// @ts-ignore - this is a bug in the types
country: c.req.raw?.cf?.country,
// @ts-ignore - this is a bug in the types
colo: c.req.raw?.cf?.colo,
// @ts-ignore - this is a bug in the types
city: c.req.raw?.cf?.city,
Comment on lines +128 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix TypeScript type issues for Cloudflare request data.

Instead of using @ts-ignore comments, consider properly typing the Cloudflare-specific request data. This will improve type safety and maintainability.

// Add this type declaration in a separate types file
interface CloudflareRequestData {
  cf?: {
    continent?: string;
    country?: string;
    colo?: string;
    city?: string;
  };
}

// Extend the Request type
declare global {
  interface Request extends CloudflareRequestData {}
}

Comment on lines +128 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider fixing TypeScript types and reducing code duplication.

The geographical data collection has two issues:

  1. TypeScript type issues: The repeated use of @ts-ignore suggests missing type definitions for Cloudflare's request data.
  2. Code duplication: These fields are already being collected earlier in the file (around line 20).

Consider these improvements:

  1. Add proper type definitions for Cloudflare's request data:
// Add this at the top of the file
interface CloudflareRequestData {
  cf?: {
    continent?: string;
    country?: string;
    colo?: string;
    city?: string;
  };
}

// Update the HonoEnv to include the CF data
interface HonoEnv {
  // ... existing types ...
  Bindings: {
    req: Request & {
      raw: Request & CloudflareRequestData;
    };
  };
}
  1. Reduce duplication by extracting the geographical data once:
export function metrics(): MiddlewareHandler<HonoEnv> {
  return async (c, next) => {
    const { metrics, analytics, logger } = c.get("services");

+   // Extract CF data once
+   const cfData = {
+     continent: c.req.raw?.cf?.continent,
+     country: c.req.raw?.cf?.country,
+     colo: c.req.raw?.cf?.colo,
+     city: c.req.raw?.cf?.city,
+   };

    let requestBody = await c.req.raw.clone().text();
    requestBody = requestBody.replaceAll(/"key":\s*"[a-zA-Z0-9_]+"/g, '"key": "<REDACTED>"');
    const start = performance.now();
    const m = {
      isolateId: c.get("isolateId"),
      isolateLifetime: Date.now() - c.get("isolateCreatedAt"),
      metric: "metric.http.request",
      path: c.req.path,
      host: new URL(c.req.url).host,
      method: c.req.method,
-     // @ts-ignore - this is a bug in the types
-     continent: c.req.raw?.cf?.continent,
-     // @ts-ignore - this is a bug in the types
-     country: c.req.raw?.cf?.country,
-     // @ts-ignore - this is a bug in the types
-     colo: c.req.raw?.cf?.colo,
-     // @ts-ignore - this is a bug in the types
-     city: c.req.raw?.cf?.city,
+     ...cfData,
      userAgent: c.req.header("user-agent"),
      fromAgent: c.req.header("Unkey-Redirect"),
      context: {},
    } as DiscriminateMetric<"metric.http.request">;

    // ... rest of the code ...

    c.executionCtx.waitUntil(
      analytics.insertApiRequest({
        // ... other fields ...
-       // @ts-ignore - this is a bug in the types
-       continent: c.req.raw?.cf?.continent,
-       // @ts-ignore - this is a bug in the types
-       country: c.req.raw?.cf?.country,
-       // @ts-ignore - this is a bug in the types
-       colo: c.req.raw?.cf?.colo,
-       // @ts-ignore - this is a bug in the types
-       city: c.req.raw?.cf?.city,
+       ...cfData,
      }),
    );

}),
);
}
Expand Down
16 changes: 8 additions & 8 deletions apps/api/src/pkg/ratelimit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class AgentRatelimiter implements RateLimiter {
source: "cloudflare",
});
return Ok({
pass: res.success,
passed: res.success,
reset: -1,
current: -1,
remaining: -1,
Expand All @@ -109,7 +109,7 @@ export class AgentRatelimiter implements RateLimiter {
identifier: req.identifier,
mode: req.async ? "async" : "sync",
error: !!res.err,
success: res?.val?.pass,
success: res?.val?.passed,
source: "agent",
});
return res;
Expand All @@ -127,7 +127,7 @@ export class AgentRatelimiter implements RateLimiter {
if (r.err) {
return r;
}
if (!r.val.pass) {
if (!r.val.passed) {
return r;
}
}
Expand All @@ -137,7 +137,7 @@ export class AgentRatelimiter implements RateLimiter {

return Ok({
current: -1,
pass: true,
passed: true,
reset: -1,
remaining: -1,
triggered: null,
Expand All @@ -162,7 +162,7 @@ export class AgentRatelimiter implements RateLimiter {
const cached = this.cache.get(id) ?? { current: 0, reset: 0 };
if (cached.current >= req.limit) {
return Ok({
pass: false,
passed: false,
current: cached.current,
reset,
remaining: 0,
Expand Down Expand Up @@ -224,7 +224,7 @@ export class AgentRatelimiter implements RateLimiter {
if (cached.current + cost > req.limit) {
return Ok({
current: cached.current,
pass: false,
passed: false,
reset,
remaining: req.limit - cached.current,
triggered: req.name,
Expand All @@ -234,7 +234,7 @@ export class AgentRatelimiter implements RateLimiter {
this.setCacheMax(id, cached.current, reset);

return Ok({
pass: true,
passed: true,
current: cached.current,
reset,
remaining: req.limit - cached.current,
Expand Down Expand Up @@ -288,7 +288,7 @@ export class AgentRatelimiter implements RateLimiter {
return Ok({
current: Number(res.limit - res.remaining),
reset: Number(res.reset),
pass: res.success,
passed: res.success,
remaining: Number(res.remaining),
triggered: res.success ? null : req.name,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/pkg/ratelimit/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const ratelimitResponseSchema = z.object({
current: z.number(),
remaining: z.number(),
reset: z.number(),
pass: z.boolean(),
passed: z.boolean(),
/**
* The name of the limit that triggered a rejection
*/
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/pkg/ratelimit/noop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export class NoopRateLimiter implements RateLimiter {
_c: Context,
_req: RatelimitRequest,
): Promise<Result<RatelimitResponse, RatelimitError>> {
return Ok({ current: 0, pass: true, reset: 0, remaining: 0, triggered: null });
return Ok({ current: 0, passed: true, reset: 0, remaining: 0, triggered: null });
}
public async multiLimit(
_c: Context,
_req: Array<RatelimitRequest>,
): Promise<Result<RatelimitResponse, RatelimitError>> {
return Ok({ current: 0, pass: true, reset: 0, remaining: 0, triggered: null });
return Ok({ current: 0, passed: true, reset: 0, remaining: 0, triggered: null });
}
}
23 changes: 17 additions & 6 deletions apps/api/src/routes/v1_ratelimit_limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ export const registerV1RatelimitLimit = (app: App) =>
});
}
const remaining = Math.max(0, limit - ratelimitResponse.current);

c.executionCtx.waitUntil(
analytics.insertRatelimit({
workspace_id: rootKey.authorizedWorkspaceId,
namespace_id: namespace.id,
request_id: c.get("requestId"),
identifier: req.identifier,
time: Date.now(),
passed: ratelimitResponse.passed,
}),
);
c.executionCtx.waitUntil(
analytics
.ingestRatelimit({
Expand All @@ -353,7 +364,7 @@ export const registerV1RatelimitLimit = (app: App) =>

time: Date.now(),
serviceLatency: -1,
success: ratelimitResponse.pass,
success: ratelimitResponse.passed,
remaining,
config: {
limit,
Expand Down Expand Up @@ -392,12 +403,12 @@ export const registerV1RatelimitLimit = (app: App) =>
id: rootKey.key.id,
},
description: "ratelimit",
event: ratelimitResponse.pass ? "ratelimit.success" : "ratelimit.denied",
event: ratelimitResponse.passed ? "ratelimit.success" : "ratelimit.denied",
meta: {
requestId: c.get("requestId"),
namespacId: namespace.id,
identifier: req.identifier,
success: ratelimitResponse.pass,
success: ratelimitResponse.passed,
},
time: Date.now(),
resources: req.resources ?? [],
Expand All @@ -417,12 +428,12 @@ export const registerV1RatelimitLimit = (app: App) =>
id: rootKey.key.id,
},
description: "ratelimit",
event: ratelimitResponse.pass ? "ratelimit.success" : "ratelimit.denied",
event: ratelimitResponse.passed ? "ratelimit.success" : "ratelimit.denied",
meta: {
requestId: c.get("requestId"),
namespacId: namespace.id,
identifier: req.identifier,
success: ratelimitResponse.pass,
success: ratelimitResponse.passed,
},
time: Date.now(),
resources: req.resources,
Expand All @@ -438,6 +449,6 @@ export const registerV1RatelimitLimit = (app: App) =>
limit,
remaining,
reset: ratelimitResponse.reset,
success: ratelimitResponse.pass,
success: ratelimitResponse.passed,
});
});
Loading
Loading