Skip to content

Conversation

sub-ld
Copy link

@sub-ld sub-ld commented Sep 6, 2025

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

This PR introduces a new afterEventEnqueue hook that allows developers to observe when events are enqueued for processing in the LaunchDarkly SDK.

Describe the solution you've provided

New Feature: afterEventEnqueue Hook

This PR introduces a complete implementation of the afterEventEnqueue hook functionality, allowing developers to hook into the event enqueuing process for monitoring, analytics, and debugging purposes.

Implementation Details

1. Hook Runner Implementation (HookRunner.js)

  • Added AFTER_ENQUEUE_STAGE_NAME constant for consistent stage naming
  • Implemented executeAfterEnqueue() function that executes hooks in reverse order (consistent with other "after" hooks)
  • Added afterEventEnqueue() method to the HookRunner interface with proper empty hooks handling
  • Integrated with existing error handling using tryExecuteStage() for robust exception handling

2. SDK Integration (index.js)

  • Added hook call in enqueueEvent() function: hookRunner.afterEventEnqueue(event)
  • Positioned after event enqueuing to ensure the event has been successfully processed
  • Respects event sending configuration - only called when events are actually enqueued

3. TypeScript Type Definitions (typings.d.ts)

  • Introduced EventEnqueueContext interface with comprehensive typing for all event types:
    • identify events: context, creationDate
    • feature events: key, context, value, variation, version, default, reason, trackEvents, debugEventsUntilDate
    • custom events: key, context, data, metricValue, url, contextKind, creationDate
    • debug events: all feature event properties
  • Extended Hook interface to include afterEventEnqueue?(hookContext: EventEnqueueContext): void

Usage Example

const myHook = {
  getMetadata() {
    return { name: 'Event Monitor Hook' };
  },
  
  afterEventEnqueue(event) {
    console.log(`Event enqueued: ${event.kind}`, event);
    
    // Monitor different event types
    switch(event.kind) {
      case 'feature':
        console.log(`Flag ${event.key} evaluated to ${event.value}`);
        break;
      case 'custom':
        console.log(`Custom event ${event.key} tracked`);
        break;
      case 'identify':
        console.log(`User identified: ${event.context.key}`);
        break;
    }
  }
};

// Register hook during SDK initialization
const client = initialize('your-sdk-key', context, {
  hooks: [myHook]
});

Additional context

This new hook provides developers with observability into the SDK's event processing pipeline. It's particularly useful for:

  • Debugging: Log events for troubleshooting flag evaluations and tracking
  • Monitoring: Track SDK usage patterns and event volumes

@sub-ld sub-ld requested a review from a team as a code owner September 6, 2025 14:24
@pranjal-jately-ld
Copy link
Contributor

@sub-ld I'm going to close this PR as per our discussion:

the existing hooks (afterEvaluation, afterIdentify, and afterTrack) can be used to achieve this functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants