-
Notifications
You must be signed in to change notification settings - Fork 609
feat: billing with Lago #3927
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
Merged
Merged
feat: billing with Lago #3927
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
61e53d9
feat(billing): send events to metronome
TBonnin 94ac509
feat: orb poc
TBonnin 7caf79b
feat: poc lago
TBonnin 2866989
Merge branch 'master' into tbonnin/NAN-3001/lago-poc
bodinsamuel 2765a15
log only paying
bodinsamuel f95d6fa
self review
bodinsamuel 9d0fa2e
review
bodinsamuel d389dab
Merge branch 'master' into tbonnin/NAN-3001/lago-poc
bodinsamuel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { uuidv7 } from 'uuidv7'; | ||
|
|
||
| import { Err, Ok, flagHasUsage, report } from '@nangohq/utils'; | ||
|
|
||
| import type { BillingClient, BillingIngestEvent, BillingMetric } from './types.js'; | ||
| import type { Result } from '@nangohq/utils'; | ||
|
|
||
| export class Billing { | ||
| constructor(private client: BillingClient) { | ||
| this.client = client; | ||
| } | ||
|
|
||
| async send(type: BillingMetric['type'], value: number, props: BillingMetric['properties']): Promise<Result<void>> { | ||
| return this.sendAll([{ type, value, properties: props }]); | ||
| } | ||
|
|
||
| async sendAll(events: BillingMetric[]): Promise<Result<void>> { | ||
| const mapped = events.flatMap((event) => { | ||
| if (event.value === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| type: event.type, | ||
| accountId: event.properties.accountId, | ||
| idempotencyKey: event.properties.idempotencyKey || uuidv7(), | ||
| timestamp: event.properties.timestamp || new Date(), | ||
| properties: { | ||
| count: event.value | ||
| } | ||
| } | ||
| ]; | ||
| }); | ||
|
|
||
| return this.ingest(mapped); | ||
| } | ||
|
|
||
| // Note: Events are sent immediately | ||
| private async ingest(events: BillingIngestEvent[]): Promise<Result<void>> { | ||
| if (!flagHasUsage) { | ||
| return Ok(undefined); | ||
| } | ||
|
|
||
| try { | ||
| await this.client.ingest(events); | ||
| return Ok(undefined); | ||
| } catch (err: unknown) { | ||
| const e = new Error(`Failed to send billing event`, { cause: err }); | ||
| report(e); | ||
| return Err(e); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { Client as LagoClient, getLagoError } from 'lago-javascript-client'; | ||
|
|
||
| import { envs } from '../envs.js'; | ||
|
|
||
| import type { BillingClient, BillingIngestEvent } from '../types.js'; | ||
| import type { EventInput } from 'lago-javascript-client'; | ||
|
|
||
| const lagoClient = LagoClient(envs.LAGO_API_KEY || ''); | ||
|
|
||
| export const lago: BillingClient = { | ||
| ingest: async (events: BillingIngestEvent[]): Promise<void> => { | ||
| const batchSize = 100; | ||
| for (let i = 0; i < events.length; i += batchSize) { | ||
| try { | ||
| await lagoClient.events.createBatchEvents({ | ||
| events: events.slice(i, i + batchSize).map(toLagoEvent) | ||
| }); | ||
| } catch (err) { | ||
| throw await getLagoError<typeof lagoClient.events.createBatchEvents>(err); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| function toLagoEvent(event: BillingIngestEvent): EventInput['event'] { | ||
| return { | ||
| code: event.type, | ||
| transaction_id: event.idempotencyKey, | ||
| external_subscription_id: event.accountId.toString(), | ||
| timestamp: event.timestamp.getTime(), | ||
| properties: event.properties | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { ENVS, parseEnvs } from '@nangohq/utils'; | ||
|
|
||
| export const envs = parseEnvs(ENVS); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { Billing } from './billing.js'; | ||
| import { lago } from './clients/lago.js'; | ||
|
|
||
| export type { BillingIngestEvent, BillingMetric } from './types.js'; | ||
|
|
||
| export const billing = new Billing(lago); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export interface BillingClient { | ||
| ingest: (events: BillingIngestEvent[]) => Promise<void>; | ||
| } | ||
|
|
||
| export interface BillingIngestEvent { | ||
| type: 'monthly_active_records' | 'billable_connections' | 'billable_actions'; | ||
| idempotencyKey: string; | ||
| accountId: number; | ||
| timestamp: Date; | ||
| properties: Record<string, string | number>; | ||
| } | ||
|
|
||
| export interface BillingMetric { | ||
| type: BillingIngestEvent['type']; | ||
| value: number; | ||
| properties: { accountId: number; timestamp?: Date | undefined; idempotencyKey?: string | undefined }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "name": "@nangohq/billing", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "main": "./dist/index.js", | ||
| "types": "./dist/index.js", | ||
| "private": true, | ||
| "scripts": {}, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/NangoHQ/nango.git", | ||
| "directory": "packages/billing" | ||
| }, | ||
| "dependencies": { | ||
| "@nangohq/utils": "file:../utils", | ||
| "lago-javascript-client": "1.22.0", | ||
| "uuidv7": "1.0.2" | ||
| }, | ||
| "devDependencies": {}, | ||
| "files": [ | ||
| "dist/**/*" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "rootDir": "lib", | ||
| "outDir": "dist" | ||
| }, | ||
| "references": [ | ||
| { | ||
| "path": "../utils" | ||
| } | ||
| ], | ||
| "include": ["lib/**/*", "../utils/lib/vitest.d.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,9 @@ | |
| }, | ||
| { | ||
| "path": "../records" | ||
| }, | ||
| { | ||
| "path": "../billing" | ||
| } | ||
| ], | ||
| "include": ["lib/**/*"] | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would retry certain errors here like network ones for instance? wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added