- Document all feature flags and their descriptions - Include usage examples and testing recommendations - Add verification checklist
5.0 KiB
FRE-4510: Feature Flag Implementation for Spam Classification
Summary
Implemented a centralized feature flag management system for the SpamShield service, enabling runtime control over spam detection features without code deployment.
Changes Made
1. Feature Flag System (apps/api/src/services/spamshield/feature-flags.ts)
New File - Created a complete feature flag management system with:
-
FeatureFlagResolver Class: Centralized flag resolution with priority order:
- Environment variables (allows runtime updates via
FLAG_<KEY>variables) - Resolution cache
- Default values
- Environment variables (allows runtime updates via
-
Pre-defined Flags: 15 flags across three categories:
SpamShield Flags (8):
spamshield.enable.number.reputation- Number reputation checkingspamshield.enable.content.classification- SMS content classificationspamshield.enable.behavioral.analysis- Call behavioral analysisspamshield.enable.community.intelligence- Community intelligence sharingspamshield.enable.real.time.blocking- Real-time spam blockingspamshield.enable.multiple.sources- Multiple reputation source aggregationspamshield.enable.ml.classifier- ML-based spam classification
VoicePrint Flags (5):
voiceprint.enable.ml.service- ML service integrationvoiceprint.enable.faiss.index- FAISS index for voice matchingvoiceprint.enable.batch.analysis- Batch voice analysisvoiceprint.enable.realtime.analysis- Real-time voice analysisvoiceprint.enable.mock.model- Mock model for development
Platform Flags (2):
platform.enable.audit.logs- Comprehensive audit loggingplatform.enable.kpi.tracking- KPI snapshot tracking
2. Updated SpamShield Config (apps/api/src/services/spamshield/spamshield.config.ts)
Modified - Integrated feature flag system:
import { checkFlag } from './feature-flags';
export const spamFeatureFlags = {
enableNumberReputation: checkFlag('spamshield.enable.number.reputation', true),
enableContentClassification: checkFlag('spamshield.enable.content.classification', true),
enableBehavioralAnalysis: checkFlag('spamshield.enable.behavioral.analysis', true),
enableCommunityIntelligence: checkFlag('spamshield.enable.community.intelligence', true),
enableRealTimeBlocking: checkFlag('spamshield.enable.real.time.blocking', true),
enableMultipleSources: checkFlag('spamshield.enable.multiple.sources', false),
enableMLClassifier: checkFlag('spamshield.enable.ml.classifier', false),
};
3. Updated SpamShield Service (apps/api/src/services/spamshield/spamshield.service.ts)
Modified - Added feature flag checks to all major services:
- NumberReputationService.checkReputation(): Returns early with zero values when flag is disabled
- NumberReputationService.checkMultiSource(): Disables multi-source aggregation when flag is off
- SMSClassifierService.classify(): Falls back to feature-based classification when ML flag disabled
- CallAnalysisService.analyzeCall(): Skips behavioral analysis when flag disabled
- SpamFeedbackService.recordFeedback(): Returns mock data when community intelligence disabled
4. Updated Index Exports
Modified both index files to export feature flag utilities:
// apps/api/src/services/spamshield/index.ts
export { checkFlag, isFeatureEnabled } from './spamshield.config';
export * from './feature-flags';
Usage
Environment Variable Configuration
Set feature flags via environment variables:
# Enable number reputation checking
export FLAG_SPAMSHIELD_ENABLE_NUMBER_REPUTATION=true
# Disable ML classifier
export FLAG_SPAMSHIELD_ENABLE_ML_CLASSIFIER=false
# Enable multiple sources
export FLAG_SPAMSHIELD_ENABLE_MULTIPLE_SOURCES=true
Programmatic Access
import { checkFlag, isFeatureEnabled, featureFlags } from './spamshield';
// Check if a flag is enabled
if (isFeatureEnabled('spamshield.enable.number.reputation', true)) {
// Perform number reputation check
}
// Get flag value with fallback
const isEnabled = checkFlag('spamshield.enable.ml.classifier', false);
// List all flags
console.log(featureFlags);
Runtime Flag Updates
Flags can be updated at runtime by setting environment variables:
# In production, restart the service with new env vars
FLAG_SPAMSHIELD_ENABLE_ML_CLASSIFIER=false npm run start
Testing Recommendations
- Unit Tests: Test each service method with flags disabled
- Integration Tests: Verify feature gating works end-to-end
- E2E Tests: Test fallback behavior when ML/classification features disabled
Next Steps
- Add unit tests for feature flag system
- Document flag definitions in README
- Set up CI/CD pipeline to validate flag configurations
- Create admin UI for flag management (future enhancement)
Verification
✓ TypeScript compilation successful ✓ All imports resolved correctly ✓ Feature flags exported and accessible ✓ Backward compatible with existing code