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

[service-utils] feat: Add usageV2 support #6021

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

[service-utils] feat: Add usageV2 support #6021

wants to merge 3 commits into from

Conversation

arcoraven
Copy link
Contributor

@arcoraven arcoraven commented Jan 22, 2025

https://linear.app/thirdweb/issue/CORE-711/usage-v2-service-utils


PR-Codex overview

This PR introduces support for usageV2 in the service-utils package, enhancing event handling and integration with Kafka for usage tracking.

Detailed summary

  • Added kafkajs as a dependency.
  • Created sendUsageV2Events function to send events to Kafka.
  • Defined UsageV2Event interface with detailed event properties.
  • Implemented UsageV2Producer class for managing Kafka connections and sending events.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

@arcoraven arcoraven requested a review from a team as a code owner January 22, 2025 02:41
Copy link

changeset-bot bot commented Jan 22, 2025

🦋 Changeset detected

Latest commit: 32fe958

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@thirdweb-dev/service-utils Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
thirdweb-www ❌ Failed (Inspect) Jan 22, 2025 3:11am
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
docs-v2 ⬜️ Skipped (Inspect) Jan 22, 2025 3:11am
thirdweb_playground ⬜️ Skipped (Inspect) Jan 22, 2025 3:11am
wallet-ui ⬜️ Skipped (Inspect) Jan 22, 2025 3:11am

Comment on lines +31 to +36
if (!resp.ok) {
throw new Error(
`[UsageV2] unexpected response ${resp.status}: ${await resp.text()}`,
);
}
resp.body?.cancel();
Copy link

Choose a reason for hiding this comment

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

The response body is consumed by resp.text() before throwing the error, making the subsequent resp.body?.cancel() call potentially unsafe since the body stream has already been read. Consider either:

  1. Moving the text() call before constructing the error:
const text = await resp.text();
if (!resp.ok) {
  throw new Error(`[UsageV2] unexpected response ${resp.status}: ${text}`);
}
  1. Or removing the cancel() call since the body is already consumed

Either approach would prevent attempting to operate on an already consumed response body.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

Copy link

graphite-app bot commented Jan 22, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • merge-queue - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Copy link

socket-security bot commented Jan 22, 2025

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] environment, network 0 732 kB tulios

View full report↗︎

@vercel vercel bot temporarily deployed to Preview – wallet-ui January 22, 2025 02:44 Inactive
@vercel vercel bot temporarily deployed to Preview – docs-v2 January 22, 2025 02:44 Inactive
@vercel vercel bot temporarily deployed to Preview – thirdweb_playground January 22, 2025 02:44 Inactive
Copy link
Contributor

github-actions bot commented Jan 22, 2025

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
thirdweb (esm) 46.68 KB (0%) 934 ms (0%) 325 ms (+63.28% 🔺) 1.3 s
thirdweb (cjs) 116.87 KB (0%) 2.4 s (0%) 364 ms (-17.36% 🔽) 2.8 s
thirdweb (minimal + tree-shaking) 5.59 KB (0%) 112 ms (0%) 117 ms (+862.24% 🔺) 229 ms
thirdweb/chains (tree-shaking) 506 B (0%) 10 ms (0%) 10 ms (+44.93% 🔺) 20 ms
thirdweb/react (minimal + tree-shaking) 19.19 KB (0%) 384 ms (0%) 109 ms (+47.96% 🔺) 493 ms

Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.89%. Comparing base (b38604c) to head (32fe958).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6021      +/-   ##
==========================================
+ Coverage   54.87%   54.89%   +0.01%     
==========================================
  Files        1151     1151              
  Lines       61169    61175       +6     
  Branches     5152     5156       +4     
==========================================
+ Hits        33566    33579      +13     
+ Misses      26876    26869       -7     
  Partials      727      727              
Flag Coverage Δ *Carryforward flag
legacy_packages 65.68% <ø> (ø) Carriedforward from d643c36
packages 52.55% <ø> (+0.01%) ⬆️

*This pull request uses carry forward flags. Click here to find out more.

see 3 files with indirect coverage changes

@vercel vercel bot temporarily deployed to Preview – docs-v2 January 22, 2025 03:10 Inactive
@vercel vercel bot temporarily deployed to Preview – wallet-ui January 22, 2025 03:10 Inactive
@vercel vercel bot temporarily deployed to Preview – thirdweb_playground January 22, 2025 03:10 Inactive
return {
id: id ?? randomUUID(),
created_at: created_at ?? new Date(),
data: JSON.stringify(data),
Copy link

Choose a reason for hiding this comment

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

According to the schema, the data field is already defined as a JSON object (Record<string, unknown>). The JSON.stringify() call here will convert it to a string, which is incorrect. Please remove the JSON.stringify() wrapper around data to maintain the correct type.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

@arcoraven arcoraven changed the title feat: Add usageV2 support [service-utils] feat: Add usageV2 support Jan 22, 2025
@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path instead of absolute path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path instead of absolute path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path instead of src-based path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path instead of absolute path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Import path should use relative path '../core/usageV2.js' instead of absolute path 'src/core/usageV2.js'

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to use relative path instead of absolute path

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link

Choose a reason for hiding this comment

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

Change import path from 'src/core/usageV2.js' to '../core/usageV2.js' to fix TypeScript module resolution

Spotted by Graphite Reviewer (based on CI logs)

Is this helpful? React 👍 or 👎 to let us know.

@@ -46,6 +46,7 @@
"sideEffects": false,
"dependencies": {
"aws4fetch": "1.0.20",
"kafkajs": "^2.2.4",
Copy link
Member

Choose a reason for hiding this comment

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

pin the version

Suggested change
"kafkajs": "^2.2.4",
"kafkajs": "2.2.4",

@@ -0,0 +1,45 @@
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link
Member

Choose a reason for hiding this comment

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

use relative imports

import { randomUUID } from "node:crypto";
import { checkServerIdentity } from "node:tls";
import { Kafka, type Producer } from "kafkajs";
import type { UsageV2Event } from "src/core/usageV2.js";
Copy link
Member

Choose a reason for hiding this comment

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

relative import pls

* A unique identifier for the event. Defaults to a random UUID.
* Useful if your service retries sending events.
*/
id?: `${string}-${string}-${string}-${string}-${string}`;
Copy link
Member

Choose a reason for hiding this comment

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

might be too strict on the type here, just string should be fine - unless you want to specifically enforce uuidv4

/**
* The source of the event. Example: "storage"
*/
source: string;
Copy link
Member

Choose a reason for hiding this comment

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

COULD consider enforcing the actual allowed service names here

id: id ?? randomUUID(),
created_at: created_at ?? new Date(),
data: JSON.stringify(data),
...rest,
Copy link
Member

Choose a reason for hiding this comment

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

might be better to just spell out each field here

Copy link
Member

Choose a reason for hiding this comment

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

for team_id field we MAY want to de-normalize or normalize it - since it might be passed in as team_<id> or just <id> based on how the service consumes it

Comment on lines +92 to +97
await this.producer.send({
topic: TOPIC_USAGE_V2,
messages: parsedEvents.map((event) => ({
value: JSON.stringify(event),
})),
});
Copy link
Member

Choose a reason for hiding this comment

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

what should happen if this were to throw?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants