Skip to content

Commit

Permalink
feat: usage/cost exceeded events (#25)
Browse files Browse the repository at this point in the history
Save usage and cost exceeded events in separate tables to get further insights and make them queryable. We plan to activate some alerts without any actual actions, but want to see how good it works at our scale first.

Also upgraded to Node 22 and latest deps.
  • Loading branch information
kevcodez authored Nov 20, 2024
1 parent 1db616b commit 32349e7
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 271 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20
v22
2 changes: 1 addition & 1 deletion apps/node-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"pino": "^9.5.0"
},
"devDependencies": {
"pino-pretty": "^11.3.0",
"pino-pretty": "^13.0.0",
"tsx": "^4.19.2"
}
}
33 changes: 33 additions & 0 deletions db/migrations/0006_subscription_exceeded_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
create table
if not exists orb.subscription_usage_exceeded (
id BIGSERIAL primary key,
billable_metric_id varchar(255) not null,
subscription_id varchar(255) not null,
customer_id varchar(255) not null,
external_customer_id varchar(255),
timeframe_start timestamp not null,
timeframe_end timestamp not null,
quantity_threshold decimal not null,
created_at timestamp not null default now()
);

create index if not exists orb_subscription_usage_exceeded_subscription_idx on orb.subscription_usage_exceeded (subscription_id);
create index if not exists orb_subscription_usage_exceeded_customer_idx on orb.subscription_usage_exceeded (customer_id);
create index if not exists orb_subscription_usage_exceeded_external_customer_idx on orb.subscription_usage_exceeded (external_customer_id);

create table
if not exists orb.subscription_cost_exceeded (
id BIGSERIAL primary key,
subscription_id varchar(255) not null,
customer_id varchar(255) not null,
external_customer_id varchar(255),
timeframe_start timestamp not null,
timeframe_end timestamp not null,
amount_threshold decimal not null,
created_at timestamp not null default now()
);


create index if not exists orb_subscription_cost_exceeded_subscription_idx on orb.subscription_cost_exceeded (subscription_id);
create index if not exists orb_subscription_cost_exceeded_customer_idx on orb.subscription_cost_exceeded (customer_id);
create index if not exists orb_subscription_cost_exceeded_external_customer_idx on orb.subscription_cost_exceeded (external_customer_id);
4 changes: 2 additions & 2 deletions docker/Dockerfile.node-fastify
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Build step
FROM node:20-alpine
FROM node:22-alpine
WORKDIR /app
COPY . .
RUN npm ci
RUN npm run build
RUN npm prune --production

## Build step complete, copy to working image
FROM node:20-alpine
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=0 /app/apps/node-fastify .
Expand Down
Loading

0 comments on commit 32349e7

Please sign in to comment.