json_exporter
json_exporter copied to clipboard
Add support for dynamic object key extraction in JSON Exporter
This enhancement allows extracting dynamic JSON object keys as Prometheus labels using the special JSONPath syntax {__name__}.
Key Features:
- Extract dynamic object keys as label values using
{__name__}syntax - Maintain full backward compatibility with existing configurations
- Support for mixed label types (dynamic keys + static values + regular JSONPath)
- Enhanced object scraping with dynamic label extraction
Use Cases:
- Cloud provider monitoring with dynamic instance/account names
- Service discovery endpoints with generated names
- Infrastructure monitoring with environment-specific identifiers
- API gateway monitoring with dynamic endpoint names
Implementation:
- Added
extractDynamicLabels()function for enhanced label extraction - Added
extractDynamicValue()function for dynamic object value extraction - Extended object scraping logic to support dynamic key extraction
- Added comprehensive test coverage for new functionality
- Included test data and configuration files for validation
Files Added/Modified:
- exporter/collector.go: Core implementation
- exporter/util_test.go: Unit tests
- README.md: Updated documentation
- test/local-test-. and test/multi-provider-test-.: Test configurations and data
Example
{"providers": [{"aws-prod-123": {"ok": true}}, {"gcp-dev-456": {"ok": false}}]}
Config
- name: provider
type: object
help: Provider account status (1=ok, 0=failed)
path: '{ .providers[*] }'
labels:
provider: '{__name__}' # Extract dynamic object key as label
values:
status: '{.ok}' # Boolean converted to 1/0
Generates:
provider_status{provider_name="aws-prod-123"} 1
provider_status{provider_name="gcp-dev-456"} 0