Prometheus Scrape Config DNS Resolution Error Fix
In this tutorial, you'll learn about Prometheus Scrape Config DNS Resolution Error Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus targets show Get "http://localhost:9100/metrics": dial tcp: lookup localhost: no such host — the target address cannot be resolved, the port is included in the host field, or the service discovery returns invalid endpoints.
The Problem
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100/metrics']
Prometheus expects targets in host:port format. Adding a path (/metrics) to the target address causes DNS lookup to fail — Prometheus tries to resolve localhost:9100/metrics as a hostname.
Step-by-Step Fix
1. Use host:port format for targets
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100'] # No path — just host:port
metrics_path: /metrics # Separate path configuration
2. Configure metrics path separately
scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
metrics_path: /metrics # Default is /metrics
scheme: http # Default is http
scrape_interval: 15s
scrape_timeout: 10s
3. Handle DNS failure in service discovery
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__address__]
regex: '(.*):10250'
replacement: '${1}:9100'
target_label: __address__
4. Verify DNS resolution
# Check DNS resolution for the target
nslookup localhost
# Test metric endpoint
curl http://localhost:9100/metrics
# Check Prometheus target status
# http://prometheus:9090/targets
5. Use proper target formatting for different schemes
scrape_configs:
- job_name: 'api'
scheme: https
static_configs:
- targets: ['api.example.com:443']
metrics_path: '/api/metrics'
tls_config:
insecure_skip_verify: false
Expected output:
Target: localhost:9100
Status: UP
Labels: {instance="localhost:9100", job="node"}
Metrics: 847 metrics collected
Prevention Tips
- Use
host:portformat in targets (no path) - Configure
metrics_pathseparately - Use service discovery instead of static configs for dynamic environments
- Verify DNS resolution independently before configuring Prometheus
- Check target status in Prometheus UI after config changes
Common Mistakes with scrape config
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro