Skip to content

feat(jobs): propagate CorrelationId from web requests to background jobs#1225

Open
cesarcastrocuba wants to merge 1 commit intofullstackhero:developfrom
cesarcastrocuba:feat/jobs-correlationid
Open

feat(jobs): propagate CorrelationId from web requests to background jobs#1225
cesarcastrocuba wants to merge 1 commit intofullstackhero:developfrom
cesarcastrocuba:feat/jobs-correlationid

Conversation

@cesarcastrocuba
Copy link

Feat: Jobs CorrelationId Propagation

Summary

This PR implements end-to-end CorrelationId propagation from web requests to background jobs (Hangfire). This ensures that every background job processed by the system can be traced back to its originating web request, significantly improving observability and debugging capabilities.

Key Changes

Infrastructure (BuildingBlocks.Core)

  • Created ICorrelationIdContext and ICorrelationIdInitializer to manage the correlation identifier within a scoped context.
  • Implemented CorrelationIdContext as the concrete repository for the current operation's ID.

Web (BuildingBlocks.Web)

  • Added CorrelationIdMiddleware to capture the X-Correlation-Id header (or fallback to TraceIdentifier) and initialize the context for every incoming request.

Background Jobs (BuildingBlocks.Jobs)

  • Updated FshJobFilter (Client Filter) to automatically capture the current CorrelationId and persist it as a job parameter during creation.
  • Created CorrelationIdJobFilter (Server Filter) to retrieve the stored CorrelationId and push it into Serilog's LogContext, ensuring it appears in all logs generated during job execution.
  • Modified FshJobActivator to restore the CorrelationId into the DI scope, making it available to all services injected into the job.
  • Added Serilog dependency to the Jobs project to support log context enrichment.

Verification Results

  • Unit Tests: Verified that the middleware correctly captures headers, the client filter persists the ID, and the server filter restores it.
  • Solution Build: Confirmed the entire solution builds with zero errors.
  • Traceability: Background job logs now include the correlation_id property, linked to the original HTTP request.

Checklist

  • Compilation successful
  • CorrelationId captured from HTTP headers
  • CorrelationId persisted in Hangfire parameters
  • CorrelationId restored in Job DI scope and Serilog LogContext
  • Aspire settings excluded from commit

- Implemented CorrelationIdContext to store and access correlation IDs.
- Added CorrelationIdMiddleware to capture X-Correlation-Id header or TraceIdentifier.
- Updated FshJobFilter to capture CorrelationId during job creation.
- Implemented CorrelationIdJobFilter to restore CorrelationId in Serilog LogContext.
- Updated FshJobActivator to restore CorrelationId in DI scoped context.
- Registered new services and filters in BuildingBlocks.Web and BuildingBlocks.Jobs.
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