or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-6/

Async Request Processor with Tracing

A simple Express.js application that processes requests through multiple asynchronous middleware stages with proper distributed tracing support.

Problem

Build an Express.js application with a middleware pipeline that performs asynchronous operations at each stage. The application should be instrumented with OpenTelemetry to enable distributed tracing, ensuring trace context flows correctly through all async operations.

Requirements

Express Application Setup

Create an Express server that:

  • Listens on port 3000
  • Has OpenTelemetry tracing enabled
  • Includes a /process endpoint that uses the middleware chain

Middleware Pipeline

Implement three middleware functions that execute in sequence:

  1. Authentication Middleware: Performs async authentication (simulate with setTimeout for 10ms)
  2. Data Enrichment Middleware: Fetches additional data asynchronously (simulate with setTimeout for 15ms)
  3. Business Logic Handler: Processes the enriched request with async database query (simulate with setTimeout for 20ms)

Async Context Propagation

Each middleware should:

  • Use async/await or Promises for asynchronous operations
  • Ensure OpenTelemetry context is maintained through async boundaries
  • Pass control to the next middleware using Express conventions

Response

The final handler should return a JSON response with:

  • Status: "success"
  • Message indicating processing completed
  • Timestamp of response

Test Cases

  • Starting the server and making a GET request to /process returns status 200 with a JSON response containing "success" status @test
  • The authentication middleware completes its async operation and calls next() to pass control to the enrichment middleware @test
  • All three middleware functions execute in the correct sequence when processing a request to /process @test

Implementation

@generates

API

/**
 * Creates and configures an Express application with OpenTelemetry instrumentation.
 *
 * @returns {Express.Application} Configured Express app with tracing enabled
 */
function createApp() {
  // IMPLEMENTATION HERE
}

/**
 * Authentication middleware that performs async user validation.
 *
 * @param {Express.Request} req - Express request object
 * @param {Express.Response} res - Express response object
 * @param {Function} next - Express next middleware function
 */
async function authMiddleware(req, res, next) {
  // IMPLEMENTATION HERE
}

/**
 * Data enrichment middleware that fetches additional data asynchronously.
 *
 * @param {Express.Request} req - Express request object
 * @param {Express.Response} res - Express response object
 * @param {Function} next - Express next middleware function
 */
async function enrichmentMiddleware(req, res, next) {
  // IMPLEMENTATION HERE
}

/**
 * Business logic handler that processes the request with async operations.
 *
 * @param {Express.Request} req - Express request object
 * @param {Express.Response} res - Express response object
 */
async function processHandler(req, res) {
  // IMPLEMENTATION HERE
}

module.exports = {
  createApp,
  authMiddleware,
  enrichmentMiddleware,
  processHandler
};

Dependencies { .dependencies }

@opentelemetry/instrumentation-express { .dependency }

Provides automatic instrumentation for Express.js applications enabling distributed tracing and context propagation through middleware chains and async operations.

express { .dependency }

Web application framework for building the HTTP server and middleware pipeline.

@opentelemetry/api { .dependency }

Core OpenTelemetry API for accessing trace context and spans.

@opentelemetry/sdk-trace-node { .dependency }

OpenTelemetry SDK for Node.js providing tracing infrastructure.