Skip to content
/ nestjs-context Public template

Useful nestjs libs using AsyncLocalStorage

Notifications You must be signed in to change notification settings

codibre/nestjs-context

Repository files navigation

nestjs-context

Useful nestjs libs using AsyncLocalStorage.

License GitHub Issues GitHub Stars


Overview

This monorepo provides useful libraries for NestJS applications, leveraging AsyncLocalStorage for advanced context management and instrumentation. It is designed to support a variety of production and advanced use cases, and will grow to include more libraries over time.


πŸ“¦ Packages

Comprehensive New Relic instrumentation for NestJS applications with advanced transaction management, distributed tracing, and event-driven monitoring.

Key Features:

  • βœ… Controller-based Transaction Management - Automatic transaction creation and context management
  • βœ… Message Queue Instrumentation - SQS, Kafka, and custom queue consumers
  • βœ… HTTP/2 Compatibility - Works with modern HTTP/2 applications
  • βœ… Background Job Tracing - Cron jobs, scheduled tasks, and async workers
  • βœ… Distributed Tracing - Cross-service transaction correlation
  • βœ… Event-Driven Monitoring - Real-time transaction lifecycle events
  • βœ… Async Context Preservation - Maintains transaction context across async operations
  • βœ… Custom Transaction Naming - Based on controller and handler names

Installation:

npm install newrelic-nestjs-instrumentation
# or
pnpm add newrelic-nestjs-instrumentation

Quick Start:

import { Module } from '@nestjs/common';
import { NestJsNewrelicInstrumentationModule } from 'newrelic-nestjs-instrumentation';

@Module({
  imports: [NestJsNewrelicInstrumentationModule],
})
export class AppModule {}

πŸ—οΈ Monorepo Architecture

This project uses a modern monorepo setup optimized for development efficiency and CI/CD automation.

Tech Stack

  • πŸ“¦ Package Manager: pnpm - Fast, disk space efficient package manager
  • πŸš€ Build System: Turbo - High-performance build system for JavaScript/TypeScript monorepos
  • πŸ§ͺ Testing: Jest - Comprehensive testing framework with coverage reporting
  • πŸ“ Code Quality: ESLint + Prettier - Linting and formatting
  • πŸ“ Commit Standards: Commitlint - Conventional commit message validation
  • πŸ”„ Git Hooks: Husky - Pre-commit and pre-push validation
  • 🏷️ Release Management: Automated semantic versioning and publishing

Project Structure

nestjs-context/
β”œβ”€β”€ .github/
β”‚   └── workflows/          # GitHub Actions CI/CD pipelines
β”œβ”€β”€ libs/                   # Package libraries (add your own!)
β”‚   β”œβ”€β”€ newrelic-nestjs-instrumentation/
β”‚   └── otel-nestjs-instrumentation/
β”œβ”€β”€ scripts/                # Build and utility scripts
β”œβ”€β”€ .husky/                 # Git hooks
β”œβ”€β”€ .jest/                  # Jest configuration
β”œβ”€β”€ turbo.json              # Turbo build configuration
β”œβ”€β”€ pnpm-workspace.yaml     # pnpm workspace configuration
β”œβ”€β”€ commitlint.config.js    # Commit message rules
└── package.json            # Root package configuration

πŸ› οΈ Development

Prerequisites

  • Node.js >= 22
  • pnpm >= 10.12.4
  • Git

Getting Started

  1. Clone the repository

    git clone https://github.com/codibre/nestjs-context.git
    cd nestjs-context
  2. Install dependencies

    pnpm install
  3. Run tests

    pnpm test
  4. Build packages

    pnpm build

Development Commands

# Build all packages
pnpm build

# Run all tests
pnpm test

# Run tests with coverage
pnpm test:cov

# Lint all packages
pnpm lint

# Auto-fix linting issues
pnpm lint:fix

# Format code
pnpm prettier:fix

# Validate commit messages
pnpm commit:validate

πŸ’‘ Development Tip: Don't Worry About Formatting While Coding!

Focus on your logic, not formatting while developing. The project has automated tools to handle code style:

# After writing your code, run this to fix all formatting/linting issues:
pnpm lint:fix

This command will automatically:

  • βœ… Fix ESLint rule violations
  • βœ… Format code with Prettier
  • βœ… Organize imports
  • βœ… Remove unused variables
  • βœ… Apply consistent code style

Pre-commit hooks will also run these fixes automatically when you commit, so you can stay in the flow of coding without interruption!

Code Quality Standards

TypeScript

  • Strict TypeScript configuration
  • Comprehensive type definitions
  • TSDoc documentation for all public APIs

Testing

  • Unit Tests: All business logic and utilities
  • Integration Tests: Real HTTP request testing with spies
  • Coverage: Maintained above 80%
  • Test Structure: Descriptive names, success/error scenarios

Commit Convention

We use Conventional Commits:

# Examples
feat(newrelic): add support for HTTP/2 instrumentation
fix(events): resolve memory leak in event emitter
docs(readme): update installation instructions
test(guard): add integration tests for transaction context

Commit Types:

  • feat: New features
  • fix: Bug fixes
  • docs: Documentation changes
  • test: Test additions or modifications
  • refactor: Code refactoring
  • perf: Performance improvements
  • chore: Build process or auxiliary tool changes

πŸš€ CI/CD & Publishing

Automated Publishing

The monorepo uses intelligent change detection to automatically:

  1. Detect Changed Packages: Only builds and tests packages that have changes
  2. Semantic Versioning: Automatically bumps versions based on conventional commits
  3. Automated Publishing: Publishes to npm registry when changes are merged to main
  4. Release Notes: Generates changelogs from commit messages

GitHub Actions Workflow

# Automatically triggers on:
# - Pull requests (run tests)
# - Push to main (run tests + publish if needed)
# - Manual dispatch (force publish)

Pipeline Steps:

  1. Setup: Node.js, pnpm, dependencies
  2. Build: Compile TypeScript, generate types
  3. Test: Unit tests, integration tests, coverage
  4. Lint: Code style and formatting checks
  5. Publish: Automatic npm publishing for changed packages

Release Process

Releases are fully automated:

  1. Commit Changes: Using conventional commit format
  2. Create PR: CI runs all tests and checks
  3. Merge to Main: Automatic version bump and publishing
  4. Monitor: Check npm and GitHub releases

🀝 Contributing

We welcome contributions! Here's how to get started:

1. Fork & Clone

# Fork on GitHub, then clone your fork
git clone https://github.com/your-username/nestjs-context.git
cd nestjs-context

2. Create Feature Branch

git checkout -b feat/your-feature-name

3. Development Workflow

# Install dependencies
pnpm install

# Make your changes
# Add tests for new functionality
# Update documentation

# Validate your changes
pnpm build
pnpm test
pnpm lint

4. Commit & Push

# Use conventional commit format
git add .
git commit -m "feat(scope): description of your changes"
git push origin feat/your-feature-name

5. Create Pull Request

  • Provide clear description of changes
  • Link any related issues
  • Ensure all CI checks pass

Pull Request Checklist

  • Code follows style guidelines
  • Tests pass locally
  • New functionality has tests
  • Documentation is updated
  • Commit messages follow conventional format
  • No breaking changes (or properly documented)

πŸ“‹ Adding New Features

For New Instrumentation Types

  1. Identify Use Case: What isn't covered by existing instrumentation?
  2. Design Solution: Integration with existing components vs. new components
  3. Implement with Tests: Comprehensive unit and integration tests
  4. Documentation: Update README, add examples, document APIs

For Bug Fixes

  1. Reproduce Issue: Create test demonstrating the bug
  2. Fix Minimally: Smallest change to resolve the issue
  3. Test Thoroughly: Verify fix + full test suite + edge cases

πŸ”§ Package Development

Adding a New Package

  1. Create Package Directory

    mkdir libs/your-new-package
    cd libs/your-new-package
  2. Package Configuration

    # Create package.json with proper workspace config
    # Add to pnpm-workspace.yaml
    # Configure turbo.json for build pipeline
  3. Development Setup

    # Add scripts in turbo.json
    # Configure Jest for testing
    # Set up TypeScript configuration

Testing Integration

  • Use --runInBand for consistent test execution
  • Mock external dependencies (New Relic, databases, etc.)
  • Test both success and error scenarios
  • Maintain high coverage (>80%)

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🌟 Support

πŸš€ Why Choose This Library?

Production Ready

  • βœ… Battle-tested in high-traffic production environments
  • βœ… Comprehensive testing with >90% coverage
  • βœ… Performance optimized with minimal overhead
  • βœ… Error resilient with graceful degradation

Developer Experience

  • βœ… TypeScript first with complete type definitions
  • βœ… Easy integration with minimal configuration
  • βœ… Extensive documentation with real-world examples
  • βœ… Active maintenance with regular updates

Enterprise Features

  • βœ… Distributed tracing across microservices
  • βœ… Custom transaction naming for better monitoring
  • βœ… Event-driven architecture for real-time insights
  • βœ… Background job instrumentation for complete visibility

Built with ❀️ by Codibre

About

Useful nestjs libs using AsyncLocalStorage

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •