Skip to content

feat: add webhook producer to service-utils #7439

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 1 commit into from
Jun 25, 2025

Conversation

arcoraven
Copy link
Contributor

@arcoraven arcoraven commented Jun 25, 2025

[SDK] Feature: Add webhook producer to service-utils

This PR adds a new WebhookEventProducer class to the service-utils package, enabling services to emit webhook events to Kafka topics. It also introduces a shared KafkaProducer pattern to improve efficiency when using multiple producers in the same service.

Key changes:

  • Added new WebhookEventProducer class for sending webhook events
  • Refactored UsageV2Producer to support shared KafkaProducer instances
  • Added documentation for the shared producer pattern
  • Exported the new webhook producer in the node index file

How to test

The implementation can be tested by creating a shared KafkaProducer and using it with both UsageV2Producer and WebhookEventProducer as shown in the documentation.

Summary by CodeRabbit

  • New Features
    • Introduced a new webhook event producer, allowing users to send webhook events to Kafka with validation and automatic ID generation.
  • Improvements
    • Updated the usage producer to support configuration with an existing Kafka producer instance and improved event handling.
  • Dependencies
    • Added a new dependency for unique ID generation to enhance event creation.

PR-Codex overview

This PR introduces a new WebhookEventProducer class to the @thirdweb-dev/service-utils package, enabling the emission of webhook events via Kafka. It also modifies the UsageV2Producer to support a shared KafkaProducer instance.

Detailed summary

  • Added WebhookEventProducer class in packages/service-utils/src/node/webhookProducer.ts.
  • Implemented sendEvents method for emitting webhook events.
  • Updated UsageV2Producer constructor to optionally accept a shared KafkaProducer.
  • Removed redundant constants TEAM_ID_PREFIX and PROJECT_ID_PREFIX from usageV2.ts.
  • Exported WebhookEventProducer from packages/service-utils/src/node/index.ts.

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

Copy link

vercel bot commented Jun 25, 2025

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

Name Status Preview Comments Updated (UTC)
docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 5:34am
nebula ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 5:34am
thirdweb_playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 5:34am
thirdweb-www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 5:34am
wallet-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 5:34am

Copy link

changeset-bot bot commented Jun 25, 2025

🦋 Changeset detected

Latest commit: 4233f2e

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
Contributor

coderabbitai bot commented Jun 25, 2025

Walkthrough

A new webhook producer feature is introduced in the @thirdweb-dev/service-utils package. This includes the addition of a WebhookEventProducer class for sending structured webhook events to Kafka, updates to the usage producer to support dependency injection, and related exports and dependency adjustments.

Changes

File(s) Change Summary
.changeset/slimy-tigers-trade.md Added changelog entry for a minor version update introducing the webhook producer feature.
packages/service-utils/package.json Added @paralleldrive/cuid2 as a new dependency.
packages/service-utils/src/node/webhookProducer.ts Introduced WebhookEventProducer class and WebhookEvent interface for producing webhook events to Kafka.
packages/service-utils/src/node/index.ts Re-exported all exports from webhookProducer.ts.
packages/service-utils/src/node/usageV2.ts Updated UsageV2Producer constructor to allow injection of a KafkaProducer; removed disconnect method and refactored code.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant WEP as WebhookEventProducer
    participant KP as KafkaProducer

    App->>WEP: new WebhookEventProducer({ kafkaProducer: KP })
    App->>WEP: sendEvents(topic, events)
    WEP->>WEP: Validate and normalize events
    WEP->>KP: sendEvents(topic, normalizedEvents)
    KP-->>WEP: Promise resolved/rejected
    WEP-->>App: Promise resolved/rejected
Loading
sequenceDiagram
    participant App as Application
    participant U2P as UsageV2Producer
    participant KP as KafkaProducer

    App->>U2P: new UsageV2Producer({ kafkaProducer: KP, source })
    App->>U2P: sendEvents(topic, events)
    U2P->>KP: sendEvents(topic, mappedEvents)
    KP-->>U2P: Promise resolved/rejected
    U2P-->>App: Promise resolved/rejected
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 407bc2c and 4233f2e.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • .changeset/slimy-tigers-trade.md (1 hunks)
  • packages/service-utils/package.json (1 hunks)
  • packages/service-utils/src/node/index.ts (1 hunks)
  • packages/service-utils/src/node/usageV2.ts (2 hunks)
  • packages/service-utils/src/node/webhookProducer.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/service-utils/package.json
🚧 Files skipped from review as they are similar to previous changes (3)
  • .changeset/slimy-tigers-trade.md
  • packages/service-utils/src/node/index.ts
  • packages/service-utils/src/node/usageV2.ts
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.@(ts|tsx)`: Accept a typed 'props' object and export a named function (e.g., export function MyComponent()). Combine class names via 'cn', expose 'className' prop if useful. ...

**/*.@(ts|tsx): Accept a typed 'props' object and export a named function (e.g., export function MyComponent()).
Combine class names via 'cn', expose 'className' prop if useful.
Reuse core UI primitives; avoid re-implementing buttons, cards, modals.
Local state or effects live inside; data fetching happens in hooks.
Merge class names with 'cn' from '@/lib/utils' to keep conditional logic readable.
Stick to design-tokens: background ('bg-card'), borders ('border-border'), muted text ('text-muted-foreground') etc.
Use the 'container' class with a 'max-w-7xl' cap for page width consistency.
Spacing utilities ('px-', 'py-', 'gap-*') are preferred over custom margins.
Responsive helpers follow mobile-first ('max-sm', 'md', 'lg', 'xl').
Never hard-code colors – always go through Tailwind variables.
Tailwind CSS is the styling system – avoid inline styles or CSS modules.
Prefix files with 'import "server-only";' so they never end up in the client bundle (for server-only code).

  • packages/service-utils/src/node/webhookProducer.ts
🧬 Code Graph Analysis (1)
packages/service-utils/src/node/webhookProducer.ts (1)
packages/service-utils/src/node/kafka.ts (1)
  • KafkaProducer (32-119)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Size
  • GitHub Check: E2E Tests (pnpm, webpack)
  • GitHub Check: E2E Tests (pnpm, esbuild)
  • GitHub Check: E2E Tests (pnpm, vite)
  • GitHub Check: Unit Tests
  • GitHub Check: Build Packages
  • GitHub Check: Lint Packages
  • GitHub Check: Socket Security: Pull Request Alerts
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (5)
packages/service-utils/src/node/webhookProducer.ts (5)

1-3: LGTM! Clean import structure.

The imports are well-organized and all necessary for the implementation.


5-14: Well-structured interface with proper TypeScript constraints.

The template literal type for id and the documentation comment about matching the api-server model are good practices.


16-25: Excellent documentation with practical example.

The JSDoc provides clear usage instructions and the example demonstrates proper dependency injection pattern.


26-31: Clean dependency injection pattern.

The constructor properly implements the dependency injection pattern with a clear configuration object.


33-39: Comprehensive method documentation.

The documentation clearly explains the throwing behavior and provides a practical non-blocking example with error handling.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@arcoraven arcoraven marked this pull request as ready for review June 25, 2025 04:44
@arcoraven arcoraven requested a review from a team as a code owner June 25, 2025 04:44
Copy link
Contributor Author


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.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

codecov bot commented Jun 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 51.96%. Comparing base (830125c) to head (4233f2e).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7439   +/-   ##
=======================================
  Coverage   51.96%   51.96%           
=======================================
  Files         947      947           
  Lines       63820    63820           
  Branches     4214     4217    +3     
=======================================
  Hits        33163    33163           
  Misses      30551    30551           
  Partials      106      106           
Flag Coverage Δ
packages 51.96% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 830125c and 407bc2c.

📒 Files selected for processing (5)
  • .changeset/slimy-tigers-trade.md (1 hunks)
  • packages/service-utils/README-shared-producer.md (1 hunks)
  • packages/service-utils/src/node/index.ts (1 hunks)
  • packages/service-utils/src/node/usageV2.ts (2 hunks)
  • packages/service-utils/src/node/webhookProducer.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.@(ts|tsx)`: Accept a typed 'props' object and export a named function (e.g., export function MyComponent()). Combine class names via 'cn', expose 'className' prop if useful. ...

**/*.@(ts|tsx): Accept a typed 'props' object and export a named function (e.g., export function MyComponent()).
Combine class names via 'cn', expose 'className' prop if useful.
Reuse core UI primitives; avoid re-implementing buttons, cards, modals.
Local state or effects live inside; data fetching happens in hooks.
Merge class names with 'cn' from '@/lib/utils' to keep conditional logic readable.
Stick to design-tokens: background ('bg-card'), borders ('border-border'), muted text ('text-muted-foreground') etc.
Use the 'container' class with a 'max-w-7xl' cap for page width consistency.
Spacing utilities ('px-', 'py-', 'gap-*') are preferred over custom margins.
Responsive helpers follow mobile-first ('max-sm', 'md', 'lg', 'xl').
Never hard-code colors – always go through Tailwind variables.
Tailwind CSS is the styling system – avoid inline styles or CSS modules.
Prefix files with 'import "server-only";' so they never end up in the client bundle (for server-only code).

  • packages/service-utils/src/node/index.ts
  • packages/service-utils/src/node/webhookProducer.ts
  • packages/service-utils/src/node/usageV2.ts
🧬 Code Graph Analysis (1)
packages/service-utils/src/node/usageV2.ts (2)
packages/service-utils/src/node/kafka.ts (1)
  • KafkaProducer (32-119)
packages/service-utils/src/core/usageV2.ts (2)
  • UsageV2Source (14-14)
  • UsageV2Event (82-87)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Size
  • GitHub Check: E2E Tests (pnpm, esbuild)
  • GitHub Check: E2E Tests (pnpm, vite)
  • GitHub Check: E2E Tests (pnpm, webpack)
  • GitHub Check: Unit Tests
  • GitHub Check: Build Packages
  • GitHub Check: Lint Packages
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (7)
.changeset/slimy-tigers-trade.md (1)

1-6: LGTM! Changelog entry is properly formatted.

The changeset correctly identifies this as a minor version update for adding the new webhook producer feature.

packages/service-utils/src/node/index.ts (1)

22-22: LGTM! Export statement follows existing patterns.

The new export for the webhook producer is correctly formatted and positioned.

packages/service-utils/README-shared-producer.md (1)

1-67: Excellent documentation for the shared producer pattern.

The README provides clear examples and explains the benefits of sharing a KafkaProducer instance across multiple event producers. The code examples are syntactically correct and demonstrate both the recommended and legacy usage patterns effectively.

packages/service-utils/src/node/usageV2.ts (2)

23-63: Well-implemented constructor pattern for shared producer support.

The union type constructor correctly supports both shared KafkaProducer instances and individual connection parameters, following the same pattern as the new WebhookEventProducer.


75-89: Prefix stripping logic is correct and simplified.

The updated logic correctly strips the prefixes using fixed slice lengths:

  • "team_" prefix (5 characters) → slice(5)
  • "prj_" prefix (4 characters) → slice(4)

This is more efficient than using constants and the logic is sound.

packages/service-utils/src/node/webhookProducer.ts (2)

4-13: Interface definition looks good.

The WebhookEvent interface is well-defined with appropriate types and clear documentation for the payload field.


28-59: Constructor pattern correctly implements shared producer support.

The constructor follows the same union type pattern as UsageV2Producer, supporting both shared KafkaProducer instances and individual connection parameters.

Copy link
Contributor

github-actions bot commented Jun 25, 2025

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
thirdweb (esm) 62.56 KB (0%) 1.3 s (0%) 410 ms (+243.15% 🔺) 1.7 s
thirdweb (cjs) 350.74 KB (0%) 7.1 s (0%) 1.2 s (+28.43% 🔺) 8.2 s
thirdweb (minimal + tree-shaking) 5.72 KB (0%) 115 ms (0%) 236 ms (+3547.15% 🔺) 351 ms
thirdweb/chains (tree-shaking) 530 B (0%) 11 ms (0%) 118 ms (+4490.24% 🔺) 128 ms
thirdweb/react (minimal + tree-shaking) 19.61 KB (0%) 393 ms (0%) 271 ms (+1532.41% 🔺) 663 ms

Copy link

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​paralleldrive/​cuid2@​2.2.210010010079100

View full report

@arcoraven arcoraven merged commit c5f9921 into main Jun 25, 2025
25 checks passed
@arcoraven arcoraven deleted the ph/06-25-feat_add_webhook_producer_to_service-utils branch June 25, 2025 05:35
@joaquim-verges joaquim-verges mentioned this pull request Jun 25, 2025
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.

1 participant