Skip to content

Conversation

@willibrandon
Copy link
Owner

@willibrandon willibrandon commented Aug 23, 2025

Description

Implements automatic detection and warning when operations approach context deadlines, helping developers catch timeout-related issues before they fail in production. The feature provides zero-overhead deadline tracking (2.7ns, 0 allocations when no deadline present) with configurable absolute and percentage-based thresholds.

Type of change

  • Bug fix
  • New feature
  • Performance improvement
  • Documentation update

Checklist

  • Tests pass (go test ./...)
  • Linter passes (golangci-lint run)
  • Benchmarks checked (if performance-related)
  • Documentation updated (if needed)
  • Zero-allocation promise maintained (if applicable)

Additional notes

  • Maintains zero allocations when deadlines aren't present (2.7ns overhead)
  • Adds OTEL-style properties: deadline.remaining_ms, deadline.at, deadline.approaching
  • Includes comprehensive examples for metrics integration and common context patterns
  • Breaking change: Logger interface now includes WithDeadlineWarning() and DeadlineStats() methods
  • All adapter test mocks have been updated to implement the new interface methods

Fixes #48

…48)

- Add automatic warnings when operations approach context deadlines
- Implement zero-overhead deadline detection (2.7ns, 0 allocations when no deadline)
- Add context-aware logging methods (InfoContext, ErrorContext, etc.)
- Add separate LRU set for first warning tracking to ensure semantic correctness
- Add WithDeadlineWarning() method to Logger interface for derived logger configuration
- Add WithDeadlinePercentageOnly() for cleaner percentage-based thresholds
- Expose deadline statistics via DeadlineStats() for production observability
- Add comprehensive documentation with troubleshooting and SLA configuration guides
- Add examples for metrics integration and common context patterns
- Fix memory leak by properly tracking first warnings in bounded LRU set
- Add DeadlineStats() and WithDeadlineWarning() to mock loggers in otel adapter tests
- Add context-aware methods (VerboseContext, DebugContext, etc.) to all test mocks
- Update noOpLogger in middleware adapter to implement complete Logger interface
- Fix build failures in adapters/otel and adapters/middleware test suites
- Remove unused clear() methods from deadline cache and first warning set
- Remove unused hasWarned() method from first warning set
- Remove unused hasDeadlineAwareness() method from logger
- Fix empty else branch warning in deadline enricher
@willibrandon willibrandon self-assigned this Aug 23, 2025
@willibrandon willibrandon added documentation Improvements or additions to documentation enhancement New feature or request observability Enhancements that improve production observability and debugging capabilities labels Aug 23, 2025
@willibrandon willibrandon requested a review from Copilot August 23, 2025 19:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements automatic context deadline awareness feature that detects and warns when operations approach their context deadlines. The feature provides zero-overhead deadline tracking with configurable thresholds and comprehensive metrics integration.

  • Adds context-aware logging methods (InfoContext, ErrorContext, etc.) to all logging interfaces
  • Implements deadline detection with both absolute and percentage-based thresholds
  • Adds comprehensive caching system with LRU eviction and TTL support for efficient deadline tracking

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
options.go Adds deadline enricher configuration options and integration with logger pipeline
logger.go Implements context-aware logging methods and deadline enricher integration
internal/enrichers/deadline.go Core deadline detection logic with configurable thresholds and custom handlers
internal/enrichers/deadline_cache.go Sharded LRU cache implementation for efficient deadline context tracking
internal/enrichers/first_warning_set.go Bounded set for tracking first warnings to prevent duplicate alerts
core/logger.go Extends Logger interface with context-aware methods and deadline statistics
examples/deadline-awareness/main.go Comprehensive examples demonstrating deadline awareness usage patterns
integration/deadline_integration_test.go Integration tests for concurrent contexts and real-world scenarios
docs/ Documentation updates including quick reference and context guide

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

- Replace magic string "__context__" with internalContextKey constant
- Replace unsafe.Pointer sharding with safe hash-based approach using FNV-1a
@willibrandon willibrandon merged commit 1e658f6 into main Aug 23, 2025
26 checks passed
@willibrandon willibrandon deleted the feature/context-deadline-awareness branch August 23, 2025 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request observability Enhancements that improve production observability and debugging capabilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add context deadline awareness for automatic timeout warnings

1 participant