A comprehensive tool to analyze Elastic Common Schema (ECS) field usage in TypeScript and JavaScript repositories. This detective tool scans through code files to identify and categorize field references as core ECS fields, vendor fields, or custom fields.
- π Comprehensive Scanning: Analyzes JavaScript, TypeScript, JSON, YAML, and other file types
- π Deep TypeScript Analysis: Advanced Elasticsearch client API introspection for TypeScript files
- π Three-Way Field Categorization: Separates fields into core ECS, vendor-specific, and custom categories
- π― ECS Field Detection: Automatically downloads and parses core ECS field definitions
- π¦ Vendor Field Recognition: Identifies known vendor fields (e.g., Tanium, Kibana, SentinelOne)
- π§ Configurable: Flexible options for directories, field definitions, and output
- π Rich Reporting: Detailed console output and optional JSON export with separate vendor/custom breakdowns
- π¬ ES Client API Parsing: Detects field usage in search queries, aggregations, mappings, and bulk operations
Analyze a repository (assumes ./repo directory exists):
npm start# Analyze specific directories
npm start -- --directories "src/plugins,x-pack/plugins"
# Include test directories in analysis (excluded by default)
npm start -- --include-tests
# Use custom ECS fields file
npm start -- --fields-csv ./my-ecs-fields.csv
# Use custom vendor fields file
npm start -- --vendor-fields ./my-vendor-fields.txt
# Specify custom repository path
npm start -- --repo /path/to/your-repo
# Save results to file
npm start -- --output results.json
# Enable verbose logging
npm start -- --verbose| Option | Description | Default |
|---|---|---|
-d, --directories <dirs> |
Comma-separated list of directories to scan (relative to repo) | Entire repository |
-f, --fields-csv <path> |
Path to ECS fields CSV file | fields.csv |
-r, --repo <path> |
Path to repository directory to analyze | ./repo |
-o, --output <path> |
Output file for results (JSON format) | Console only |
--vendor-fields <path> |
Path to vendor fields file | vendor_fields.txt |
--include-tests |
Include test directories in analysis | Excluded by default |
--include-json |
Include JSON files in analysis | JS/TS only by default |
--include-yaml |
Include YAML/YML files in analysis | JS/TS only by default |
--include-markdown |
Include Markdown files in analysis | JS/TS only by default |
--verbose |
Enable verbose logging | Disabled |
By default, ECS Detective excludes test-related directories and files to focus on production code:
Excluded patterns:
**/test/**- Test directories**/tests/**- Tests directories**/__tests__/**- Jest test directories**/*.test.*- Test files (e.g.,*.test.js,*.test.ts)**/*.spec.*- Spec files (e.g.,*.spec.js,*.spec.ts)
Use --include-tests to analyze test code and verify ECS field usage in test fixtures, mocks, and test utilities.
ECS Detective now supports three-way field categorization:
Fields that are part of the official Elastic Common Schema specification.
Fields from known security vendors and platforms (e.g., Tanium, SentinelOne, Kibana-specific fields). These are defined in the vendor_fields.txt file.
Everything else - your organization's custom field definitions.
The vendor_fields.txt file should contain one vendor field pattern per line:
.tanium.reporting.model
.tanium.reporting.computer_name
.sentinel_one.agent.agent.id
.kibana.stats.snapshot
.winlog.logon.type
resource.id
- Lines starting with
#are treated as comments - Empty lines are ignored
- Fields can be specified with or without leading dots
- Supports prefix matching (e.g.,
.tanium.matches all Tanium fields)
Use the --vendor-fields option to specify a custom vendor fields file location.
The tool provides comprehensive statistics including:
- Total number of files parsed
- Files containing only core ECS fields
- Files containing vendor fields
- Files containing custom fields
- File type breakdown (TypeScript, JavaScript, JSON, YAML, etc.)
- Skipped files list with reasons (file type not supported, empty files, etc.)
- Total number of unique core fields referenced
- Top core fields by usage count (descending order)
- Total occurrences of core field references
- Total number of unique vendor fields referenced
- Top vendor fields by usage count (descending order)
- Total occurrences of vendor field references
- Total number of unique custom fields referenced
- Top custom fields by usage count (descending order)
- Total occurrences of custom field references
π Analysis Results:
==================================================
π File Statistics:
Total files parsed: 15,432
Files with ONLY core ECS fields: 1,234
Files with vendor fields: 789
Files with custom fields: 1,556
Files skipped: 23
π File Types Analyzed:
π typescript: 8,901 files
π json: 3,210 files
π javascript: 2,456 files
π markdown: 654 files
π yaml: 211 files
β οΈ Skipped Files:
1. /path/to/large_graph.json
β File type not supported or excluded by patterns
2. /path/to/empty_file.js
β Empty file or failed to read content
... and 21 more files
π― Core ECS Field Usage:
Total core fields referenced: 156
Top 10 core fields by usage:
1. @timestamp - 1,234 occurrences
2. message - 987 occurrences
3. host.name - 756 occurrences
4. user.name - 543 occurrences
5. event.category - 432 occurrences
...
π¦ Vendor Field Usage:
Total vendor fields referenced: 45
Top 10 vendor fields by usage:
1. sentinel_one.agent.agent.id - 234 occurrences
2. tanium.client.version - 123 occurrences
3. winlog.logon.type - 98 occurrences
4. kibana.space.id - 87 occurrences
5. resource.id - 76 occurrences
π§ Custom Field Usage:
Total custom fields referenced: 44
Top 10 custom fields by usage:
1. custom.organization.department - 345 occurrences
2. app.custom.session_id - 234 occurrences
3. vendor.specific.field - 98 occurrences
...
- π ECS Field Loading: Downloads the latest ECS field definitions from the official GitHub repository
- π Repository Scanning: Recursively scans specified directories for relevant file types
- π§ Enhanced TypeScript Analysis:
- Detects Elasticsearch client method calls (
client.search,client.index, etc.) - Extracts fields from query DSL, aggregations, and mappings
- Parses TypeScript interfaces and type definitions
- Analyzes script fields and bulk operations
- Detects Elasticsearch client method calls (
- β‘ Field Extraction: Uses advanced pattern matching to extract field references from various file formats
- π― Classification: Compares extracted fields against core ECS definitions to categorize them
- π§Ή Artifact Filtering: Applies comprehensive filtering to exclude development artifacts, UI configurations, and non-field references
- π Report Generation: Produces detailed statistics and optionally exports results to JSON
The analyzer supports the following file types:
- JavaScript/TypeScript:
.js,.jsx,.ts,.tsx - Configuration:
.json,.yml,.yaml - Documentation:
.md - Text files:
.txt
The tool uses sophisticated pattern matching to detect field references:
- String literals:
'field.name',"field.name" - Object property access:
obj.field.name - Template literals:
`${field.name}` - JSON keys and values
- YAML field definitions
- Elasticsearch Client Queries:
client.search({ query: { term: { 'user.name': value } } }) - Aggregations:
{ aggs: { by_user: { terms: { field: 'user.id' } } } } - Index Operations:
client.index({ body: { 'event.category': 'security' } }) - Mapping Definitions:
{ properties: { 'host.ip': { type: 'ip' } } } - Script Fields:
doc['field.name'].value,params._source['field.name'] - Bulk Operations: Field extraction from bulk document bodies
- TypeScript Interfaces:
interface Log { 'user.name': string } - Type Definitions:
type Fields = { 'event.action': string }
By default, the tool downloads the latest ECS field definitions from:
https://raw.githubusercontent.com/elastic/ecs/master/generated/csv/fields.csv
You can provide a custom CSV file using the --fields-csv option.
The scanner automatically excludes:
node_modulesdirectories- Build artifacts (
dist,build,target) - Test files and directories
- Minified files (
.min.js,.bundle.js) - Source maps (
.mapfiles) - Documentation directories
- Repository not found: Ensure the Kibana repository path is correct
- No fields detected: Check that the ECS fields CSV is accessible and properly formatted
- Memory issues: For very large repositories, consider scanning specific directories instead of the entire repo
Use --verbose flag to get detailed logging for debugging:
npm start -- --verbosenpm testsrc/
βββ index.js # CLI interface and main entry point
βββ analyzer.js # Main analysis orchestrator
βββ ecs-fetcher.js # ECS field definitions fetcher
βββ field-parser.js # Field extraction and parsing logic
βββ file-scanner.js # Repository file scanning
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see LICENSE file for details.