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

Shared queue consumer telemetry improvements and perf changes #1619

Merged
merged 4 commits into from
Jan 17, 2025

Conversation

ericallam
Copy link
Member

@ericallam ericallam commented Jan 16, 2025

Summary by CodeRabbit

  • Configuration Changes

    • Updated environment configuration for OpenTelemetry trace exporter authentication headers.
    • Consolidated multiple header-related environment variables into a single JSON-based configuration.
  • Tracing and Logging Improvements

    • Enhanced message processing and tracing in queue consumers.
    • Added more granular control over debug log output based on span recording status.
  • Code Refactoring

    • Simplified database query methods.
    • Improved modularity in message handling logic.
    • Streamlined header management for trace exporters.

Copy link

changeset-bot bot commented Jan 16, 2025

⚠️ No Changeset found

Latest commit: 7b8fc17

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link
Contributor

coderabbitai bot commented Jan 16, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

apps/webapp/app/v3/tracer.server.ts

Oops! Something went wrong! :(

ESLint: 8.45.0

ESLint couldn't find the config "custom" to extend from. Please check that the name of the config is correct.

The config "custom" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

Walkthrough

This pull request introduces several modifications across different components of the application, primarily focusing on OpenTelemetry tracing and queue message handling. The changes consolidate environment configuration for trace exporter authentication, refactor message processing in queue consumers, and adjust logging behavior. The modifications aim to improve code structure, enhance tracing capabilities, and provide more flexible configuration for trace exporters.

Changes

File Change Summary
apps/webapp/app/env.server.ts Replaced separate INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_NAME and INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_VALUE with a single INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS environment variable.
apps/webapp/app/models/runtimeEnvironment.server.ts Replaced findUnique with findFirst in multiple database query methods.
apps/webapp/app/v3/marqs/devQueueConsumer.server.ts Removed [SEMINTATTRS_FORCE_RECORDING] attribute setting for span creation.
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts Added new message body types, refactored message processing logic, and improved error handling.
apps/webapp/app/v3/tracer.server.ts Updated header handling to use a single JSON-based environment variable for trace exporter authentication.
packages/core/src/logger.ts Added conditional check to prevent logging debug messages when the current span is not recording.

Sequence Diagram

sequenceDiagram
    participant ENV as Environment Config
    participant Tracer as OTLPTraceExporter
    participant Logger as Structured Logger

    ENV->>Tracer: Provide auth headers as JSON string
    Tracer->>Tracer: Parse headers
    Logger->>Logger: Check span recording status
    alt Span not recording
        Logger-->>Logger: Suppress debug logs
    end
Loading

Possibly related PRs

  • Add custom telemetry exporter support #1602: Changes in this PR relate to the consolidation of authentication headers for OpenTelemetry exporters, which aligns with the modifications made in the main PR regarding the INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS property.

Poem

🐰 Tracing rabbits hop with glee,
Through queues and logs, now light and free!
Headers consolidated, spans refined,
A cleaner path, expertly designed.
Debug messages dance, only when bright,
Our code now shines with tracing might! 🌟

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

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: 3

🧹 Nitpick comments (2)
packages/core/src/logger.ts (1)

112-115: Consider moving the early return check before structuredLog preparation.

While the optimization to skip debug logs when spans aren't recording is good, the check occurs after preparing the structuredLog object. Moving it before would avoid unnecessary object creation and JSON operations.

   #structuredLog(
     loggerFunction: (message: string, ...args: any[]) => void,
     message: string,
     level: string,
     ...args: Array<Record<string, unknown> | undefined>
   ) {
     // Get the current context from trace if it exists
     const currentSpan = trace.getSpan(context.active());
+
+    // If the span is not recording, and it's a debug log, don't log it
+    if (currentSpan && !currentSpan.isRecording() && level === "debug") {
+      return;
+    }

     const structuredLog = {
       ...structureArgs(safeJsonClone(args) as Record<string, unknown>[], this.#filteredKeys),
       ...this.#additionalFields(),
       timestamp: new Date(),
       name: this.#name,
       message,
       level,
       traceId:
         currentSpan && currentSpan.isRecording() ? currentSpan?.spanContext().traceId : undefined,
       parentSpanId:
         currentSpan && currentSpan.isRecording() ? currentSpan?.spanContext().spanId : undefined,
     };

-    // If the span is not recording, and it's a debug log, don't log it
-    if (currentSpan && !currentSpan.isRecording() && level === "debug") {
-      return;
-    }

     loggerFunction(JSON.stringify(structuredLog, this.#jsonReplacer));
   }
apps/webapp/app/env.server.ts (1)

193-193: Document the expected format for INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS.

The consolidation of auth headers into a single property is good, but needs documentation for the expected JSON format.

Add JSDoc comment above the property:

+  /** JSON string containing auth headers. Example: {"Authorization": "Bearer token", "Custom-Header": "value"} */
   INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS: z.string().optional(),
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 11f2832 and 240ea1b.

📒 Files selected for processing (6)
  • apps/webapp/app/env.server.ts (1 hunks)
  • apps/webapp/app/models/runtimeEnvironment.server.ts (4 hunks)
  • apps/webapp/app/v3/marqs/devQueueConsumer.server.ts (0 hunks)
  • apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (14 hunks)
  • apps/webapp/app/v3/tracer.server.ts (1 hunks)
  • packages/core/src/logger.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • apps/webapp/app/v3/marqs/devQueueConsumer.server.ts
🧰 Additional context used
🪛 Biome (1.9.4)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts

[error] 154-154: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - npm)
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
apps/webapp/app/models/runtimeEnvironment.server.ts (1)

Line range hint 8-15: Reconsider replacing findUnique with findFirst to ensure correct environment retrieval

Replacing findUnique with findFirst may lead to unintended behavior if multiple records match the query criteria, as findFirst will return the first matching record, which may not be unique. This could result in incorrect environments being retrieved.

Please ensure that the uniqueness of the queried fields is enforced at the database level to prevent duplicate records. If duplicates are possible, consider handling them appropriately or reverting to findUnique.

Also applies to: 28-35, 48-55, 88-95

apps/webapp/app/env.server.ts (1)

193-193: Verify usage of the new consolidated headers property.

Need to ensure all existing configurations using the old header properties are migrated correctly.

✅ Verification successful

Migration to consolidated headers property is complete and correct

The codebase shows a clean implementation of the consolidated INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS property, with proper JSON parsing and type-safe usage. No traces of the old header properties were found, indicating a successful migration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for usage of old header properties in configuration files
rg -l "INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_(NAME|VALUE)"

# Check for header parsing logic in tracer configuration
ast-grep --pattern 'INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS'

Length of output: 126


Script:

#!/bin/bash
# Broader search for OTEL-related environment variables
rg -i "otel.*header" -A 5 -B 5

# Look for OpenTelemetry configuration files
fd -e ts -e js -e json . | grep -i "otel\|telemetry"

# Search for environment variable schema definitions
ast-grep --pattern 'z.string().optional()'

Length of output: 35094


Script:

#!/bin/bash
# Look for OTEL configuration files
fd -e ts -e js -e json . | grep -i "otel" | xargs cat

# Search for any header-related configuration in OTEL files
rg -i "header" -A 5 -B 5 $(fd -e ts -e js -e json . | grep -i "otel")

Length of output: 80976

apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts Outdated Show resolved Hide resolved
Comment on lines 133 to 135
const headers = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
? (JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>)
: undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle potential JSON parsing errors of environment variable

Parsing env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS without error handling may cause runtime crashes if the environment variable contains invalid JSON. It's important to catch and handle any parsing errors to prevent application failure.

Apply this diff to add error handling:

 const headers = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
-  ? (JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>)
+  ? (() => {
+      try {
+        return JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>;
+      } catch (error) {
+        console.error('Failed to parse INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS:', error);
+        return undefined;
+      }
+    })()
   : undefined;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const headers = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
? (JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>)
: undefined;
const headers = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
? (() => {
try {
return JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>;
} catch (error) {
console.error('Failed to parse INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS:', error);
return undefined;
}
})()
: undefined;

packages/core/src/logger.ts Outdated Show resolved Hide resolved
Copy link

pkg-pr-new bot commented Jan 16, 2025

@trigger.dev/build

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/build@1619

trigger.dev

npm i https://pkg.pr.new/triggerdotdev/trigger.dev@1619

@trigger.dev/react-hooks

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/react-hooks@1619

@trigger.dev/sdk

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/sdk@1619

@trigger.dev/core

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/core@1619

@trigger.dev/rsc

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/rsc@1619

commit: 7b8fc17

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: 2

♻️ Duplicate comments (1)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (1)

144-163: ⚠️ Potential issue

Fix variable declaration order to prevent potential issues.

The _currentSpan variable is used before its declaration in the #endCurrentSpan method. Reorder the private properties to ensure variables are declared before use.

Apply this diff to fix the variable declaration order:

 export class SharedQueueConsumer {
+  private _currentSpan: Span | undefined;
   private _backgroundWorkers: Map<string, BackgroundWorkerWithTasks> = new Map();
   private _deprecatedWorkers: Map<string, BackgroundWorkerWithTasks> = new Map();
   private _enabled = false;
   private _options: Required<SharedQueueConsumerOptions>;
   private _perTraceCountdown: number | undefined;
   private _traceStartedAt: Date | undefined;
   private _currentSpanContext: Context | undefined;
   private _reasonStats: Record<string, number> = {};
   private _actionStats: Record<string, number> = {};
-  private _currentSpan: Span | undefined;
   private _endSpanInNextIteration = false;
   private _tasks = sharedQueueTasks;
   private _id: string;
   private _connectedAt: Date;
   private _iterationsCount = 0;
   private _totalIterationsCount = 0;
   private _runningDurationInMs = 0;
   private _currentMessage: MessagePayload | undefined;
   private _currentMessageData: SharedQueueMessageBody | undefined;
🧰 Tools
🪛 Biome (1.9.4)

[error] 156-156: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)

🧹 Nitpick comments (4)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (4)

1-10: Consider using the OpenTelemetry SDK convenience package.

Instead of importing individual OpenTelemetry components, consider using the @opentelemetry/sdk-trace-base package which provides a more convenient way to access commonly used tracing components.

-import {
-  Context,
-  ROOT_CONTEXT,
-  Span,
-  SpanKind,
-  SpanOptions,
-  SpanStatusCode,
-  context,
-  trace,
-} from "@opentelemetry/api";
+import { Context, Span, SpanKind, SpanOptions, SpanStatusCode } from "@opentelemetry/sdk-trace-base";
+import { ROOT_CONTEXT, context, trace } from "@opentelemetry/api";

144-163: Consider grouping related properties for better maintainability.

The class properties could be better organized by grouping them based on their purpose (e.g., telemetry, state management, configuration).

Consider reorganizing the properties into these groups:

  1. Configuration properties (_options, _enabled)
  2. Worker management (_backgroundWorkers, _deprecatedWorkers)
  3. Telemetry properties (_currentSpan, _reasonStats, etc.)
  4. State management (_currentMessage, _currentMessageData)
  5. Metrics (_iterationsCount, _totalIterationsCount, etc.)
🧰 Tools
🪛 Biome (1.9.4)

[error] 156-156: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)


497-525: Consider implementing a message handler factory pattern.

The #handleMessage method uses a switch statement to delegate to specific handlers. Consider implementing a message handler factory pattern to make it more maintainable and extensible.

Example implementation:

private messageHandlers = new Map<SharedQueueMessageBody['type'], MessageHandler>([
  ['EXECUTE', this.#handleExecuteMessage],
  ['RESUME', this.#handleResumeMessage],
  ['RESUME_AFTER_DURATION', this.#handleResumeAfterDurationMessage],
  ['FAIL', this.#handleFailMessage],
]);

async #handleMessage(message: MessagePayload, data: SharedQueueMessageBody): Promise<HandleMessageResult> {
  return await this.#startActiveSpan("handleMessage()", async (span) => {
    const handler = this.messageHandlers.get(data.type);
    if (!handler) {
      throw new Error(`No handler found for message type: ${data.type}`);
    }
    return handler.call(this, message, data);
  });
}

1395-1427: Add JSDoc documentation for the #startActiveSpan helper method.

The #startActiveSpan method is a crucial helper for telemetry but lacks documentation explaining its purpose and parameters.

Add JSDoc documentation:

/**
 * Creates and manages an active span for tracing operations.
 * @param name - The name of the span
 * @param fn - The function to execute within the span
 * @param options - Optional span configuration
 * @returns The result of the executed function
 * @throws Rethrows any error from the executed function after recording it in the span
 */
async #startActiveSpan<T>(
  name: string,
  fn: (span: Span) => Promise<T>,
  options?: SpanOptions
): Promise<T> {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 240ea1b and c669b44.

📒 Files selected for processing (1)
  • apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (14 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts

[error] 156-156: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - pnpm)
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Analyze (javascript-typescript)

@ericallam ericallam force-pushed the shared-queue-consumer-telemetry-improvements branch from c669b44 to 7bc2ded Compare January 16, 2025 18:55
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: 0

♻️ Duplicate comments (3)
apps/webapp/app/v3/tracer.server.ts (1)

133-135: ⚠️ Potential issue

Handle potential JSON parsing errors of environment variable.

Parsing env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS without error handling may cause runtime crashes if the environment variable contains invalid JSON.

Apply this diff to add error handling:

 const headers = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
-  ? (JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>)
+  ? (() => {
+      try {
+        return JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>;
+      } catch (error) {
+        console.error('Failed to parse INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS:', error);
+        return undefined;
+      }
+    })()
   : undefined;
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (2)

150-163: ⚠️ Potential issue

Fix variable declaration order to prevent use before declaration.

The variable _currentSpan is used before its declaration in the #endCurrentSpan method. This could lead to runtime issues.

Apply this diff to fix the variable declaration order:

  private _perTraceCountdown: number | undefined;
  private _traceStartedAt: Date | undefined;
  private _currentSpanContext: Context | undefined;
+ private _currentSpan: Span | undefined;
  private _reasonStats: Record<string, number> = {};
  private _actionStats: Record<string, number> = {};
- private _currentSpan: Span | undefined;
  private _endSpanInNextIteration = false;
  private _tasks = sharedQueueTasks;
  private _id: string;
  private _connectedAt: Date;
  private _iterationsCount = 0;
  private _totalIterationsCount = 0;
  private _runningDurationInMs = 0;
  private _currentMessage: MessagePayload | undefined;
  private _currentMessageData: SharedQueueMessageBody | undefined;
🧰 Tools
🪛 Biome (1.9.4)

[error] 156-156: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)


527-918: 🛠️ Refactor suggestion

Break down the large #handleExecuteMessage method.

The method is 391 lines long and handles multiple concerns. Consider breaking it down into smaller, focused methods for better maintainability and testability.

Suggested breakdown:

  1. Task run validation
  2. Worker deployment validation
  3. Task execution
  4. Error handling

Example refactor:

async #handleExecuteMessage(message: MessagePayload, data: SharedQueueExecuteMessageBody): Promise<HandleMessageResult> {
  const existingTaskRun = await this.#validateTaskRun(message);
  if (!existingTaskRun) {
    return this.#generateNoTaskRunResponse(message);
  }

  const deployment = await this.#validateAndGetDeployment(existingTaskRun);
  if (!deployment) {
    return this.#generateNoDeploymentResponse(existingTaskRun);
  }

  return this.#executeTask(message, data, existingTaskRun, deployment);
}
🧹 Nitpick comments (1)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (1)

920-1294: Extract common error handling patterns.

The message handling methods share similar error handling and response generation patterns. Consider extracting these into utility methods to reduce duplication and improve maintainability.

Example:

private generateErrorResponse(action: HandleMessageAction, reason: string, attrs?: Record<string, any>, error?: Error | string): HandleMessageResult {
  return {
    action,
    reason,
    attrs,
    error,
    interval: this._options.nextTickInterval,
    retryInMs: 5_000,
  };
}

private generateCheckpointResponse(eventId: string): HandleMessageResult {
  return {
    action: "noop",
    reason: "restored_checkpoint",
    attrs: {
      checkpoint_event_id: eventId,
    },
  };
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c669b44 and 7bc2ded.

📒 Files selected for processing (6)
  • apps/webapp/app/env.server.ts (1 hunks)
  • apps/webapp/app/models/runtimeEnvironment.server.ts (4 hunks)
  • apps/webapp/app/v3/marqs/devQueueConsumer.server.ts (0 hunks)
  • apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (14 hunks)
  • apps/webapp/app/v3/tracer.server.ts (1 hunks)
  • packages/core/src/logger.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • apps/webapp/app/v3/marqs/devQueueConsumer.server.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • apps/webapp/app/env.server.ts
  • packages/core/src/logger.ts
  • apps/webapp/app/models/runtimeEnvironment.server.ts
🧰 Additional context used
🪛 Biome (1.9.4)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts

[error] 156-156: This variable is used before its declaration.

The variable is declared here:

(lint/correctness/noInvalidUseBeforeDeclaration)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - pnpm)
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: typecheck / typecheck
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - npm)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts (2)

1373-1381: Add retry logic for message acknowledgment operations.

The #ack and #nack methods don't handle potential failures in the message queue operations. Consider adding retry logic for better reliability.

Example implementation:

async #ack(messageId: string, retries = 3): Promise<void> {
  for (let i = 0; i < retries; i++) {
    try {
      await marqs?.acknowledgeMessage(messageId, "Acking and doing more work in SharedQueueConsumer");
      return;
    } catch (error) {
      if (i === retries - 1) throw error;
      await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
    }
  }
}

1395-1427: Well-implemented span management!

The #startActiveSpan method is well-structured with proper error handling, attribute management, and cleanup. Good job on ensuring spans are properly ended in the finally block.

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: 0

🧹 Nitpick comments (2)
apps/webapp/app/v3/tracer.server.ts (2)

207-216: Add error logging for configuration parsing failures.

While the error handling is good, silently failing without logging could make it difficult to diagnose configuration issues in production.

Consider adding error logging:

 function parseInternalTraceHeaders(): Record<string, string> | undefined {
   try {
     return env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
       ? (JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS) as Record<string, string>)
       : undefined;
-  } catch {
+  } catch (error) {
+    console.error('Failed to parse INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS:', error);
     return;
   }
 }

208-216: Consider adding header structure validation.

Since these headers are used for authentication, it would be beneficial to validate their structure to ensure they meet expected format and contain required fields.

Consider:

  1. Adding type validation for the parsed JSON
  2. Checking for required header fields
  3. Validating header values against expected formats (if applicable)

Example validation approach:

interface TraceHeaders {
  // Define expected header structure
  [key: string]: string;
}

function validateHeaders(headers: unknown): headers is TraceHeaders {
  if (!headers || typeof headers !== 'object') return false;
  
  return Object.entries(headers).every(
    ([key, value]) => typeof key === 'string' && typeof value === 'string'
  );
}

function parseInternalTraceHeaders(): Record<string, string> | undefined {
  try {
    const parsed = env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS
      ? JSON.parse(env.INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS)
      : undefined;
    
    if (parsed && !validateHeaders(parsed)) {
      console.error('Invalid header structure in INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS');
      return;
    }
    
    return parsed;
  } catch (error) {
    console.error('Failed to parse INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADERS:', error);
    return;
  }
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7bc2ded and 7b8fc17.

📒 Files selected for processing (2)
  • apps/webapp/app/v3/tracer.server.ts (2 hunks)
  • packages/core/src/logger.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/logger.ts
🔇 Additional comments (1)
apps/webapp/app/v3/tracer.server.ts (1)

133-138: LGTM! Good improvements to header configuration.

The changes effectively consolidate header configuration into a single environment variable and handle parsing failures gracefully by defaulting to an empty object. This is a good defensive programming practice.

@ericallam ericallam merged commit 5750f35 into main Jan 17, 2025
12 checks passed
@ericallam ericallam deleted the shared-queue-consumer-telemetry-improvements branch January 17, 2025 09:15
@coderabbitai coderabbitai bot mentioned this pull request Jan 21, 2025
@coderabbitai coderabbitai bot mentioned this pull request Jan 28, 2025
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.

1 participant