Prometheus exporter in Nginx - Time & Space Complexity
When using nginx as a Prometheus exporter via stub_status, it is important to understand how the time to collect metrics grows as the number of monitored endpoints increases.
We want to know how the work done by nginx scales when exporting metrics to Prometheus.
Analyze the time complexity of the following nginx configuration snippet for Prometheus metrics export.
server {
listen 9113;
location /metrics {
stub_status;
access_log off;
}
}
This snippet configures nginx to expose its status metrics on port 9113 for Prometheus to scrape.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx reads global counters and formats fixed status metrics each time Prometheus scrapes the /metrics endpoint.
- How many times: Once per scrape request, which happens repeatedly over time. No loops over connections.
The stub_status module maintains aggregate counters (e.g., total requests, active connections) updated atomically. Generating the output reads these counters in constant time.
| Input Size (active connections) | Approx. Operations |
|---|---|
| 10 | Constant: read counters & format |
| 100 | Constant: read counters & format |
| 1000 | Constant: read counters & format |
Pattern observation: The work to generate metrics is constant time, independent of active connections or requests, as no iteration occurs.
Time Complexity: O(1)
This means the time to generate metrics is constant per scrape, regardless of load on nginx.
[X] Wrong: "The metrics export time grows linearly with the number of active connections."
[OK] Correct: nginx stub_status uses pre-maintained global counters; no traversal of connections is needed. Output is always a fixed small number of lines.
Understanding that basic nginx monitoring is O(1) helps evaluate when to use it vs. more complex exporters. Shows knowledge of real-world performance characteristics.
"What if nginx cached the metrics output instead of generating it fresh on each scrape? How would the time complexity change?"
Practice
stub_status module in nginx when used with Prometheus?Solution
Step 1: Understand stub_status role
Thestub_statusmodule provides a simple status page with metrics like active connections, accepts, handled, and requests.Step 2: Connect stub_status to Prometheus
Prometheus scrapes these metrics from the URL wherestub_statusis enabled to monitor nginx performance.Final Answer:
To expose basic nginx metrics like active connections and requests -> Option AQuick Check:
stub_status exposes metrics = C [OK]
- Thinking stub_status caches content
- Confusing stub_status with IP blocking
- Assuming stub_status serves web pages
stub_status at the URL /metrics and restricts access to 127.0.0.1?Solution
Step 1: Check stub_status syntax
The correct directive isstub_status;without 'on'. It must be inside a location block.Step 2: Verify access control
To protect metrics, useallow 127.0.0.1;anddeny all;inside the location.Final Answer:
location /metrics { stub_status; allow 127.0.0.1; deny all; } -> Option CQuick Check:
stub_status syntax + IP allow/deny = A [OK]
- Using 'stub_status on;' which is invalid
- Not adding IP allow/deny rules
- Missing semicolon after stub_status
stub_status output scraped by Prometheus:Active connections: 5 server accepts handled requests 100 100 200 Reading: 1 Writing: 2 Waiting: 2
What is the value of 'Active connections' metric?
Solution
Step 1: Identify Active connections line
The first line shows 'Active connections: 5', so the active connections count is 5.Step 2: Differentiate other numbers
Numbers 100 and 200 relate to accepts, handled, and requests, not active connections.Final Answer:
5 -> Option DQuick Check:
Active connections = 5 [OK]
- Confusing accepts or requests with active connections
- Picking the largest number instead of the labeled one
- Ignoring the label and choosing Reading/Writing values
stub_status at /metrics but Prometheus shows 'connection refused'. What is the most likely cause?Solution
Step 1: Understand 'connection refused' meaning
This error means Prometheus cannot connect to nginx, likely because nginx is down or not listening on the port.Step 2: Check other options
Misspelling stub_status causes config errors but not connection refused. Missing slash or root directive won't cause connection refused.Final Answer:
nginx is not running or listening on the expected port -> Option AQuick Check:
Connection refused = nginx down or port closed [OK]
- Assuming URL slash causes connection refused
- Thinking config typos cause connection refused
- Believing root directive is required for stub_status
/metrics only to Prometheus server IP 10.0.0.5. Which configuration snippet correctly achieves this?Solution
Step 1: Use correct stub_status syntax
The directive isstub_status;without 'on'. It must be inside the location block.Step 2: Apply IP allow/deny rules correctly
To allow only 10.0.0.5, useallow 10.0.0.5;thendeny all;. Order matters: allow first, then deny.Final Answer:
location /metrics { stub_status; allow 10.0.0.5; deny all; } -> Option BQuick Check:
Correct stub_status + allow IP then deny all = B [OK]
- Using 'stub_status on;' which is invalid
- Reversing allow and deny order
- Allowing all and denying the IP instead
Ready to prove it?
Pass the test and this pattern moves unknown
