Web basics: HTTP, ports and URLs

Prometheus · Essentials · 8 min

Every scrape, every query from a dashboard and every alert delivery is a web request, so these words appear in every lesson that follows.

Programs on different machines talk by sending messages over the network. The most common set of rules for those messages is HTTP, the same one your web browser uses. One side sends a request ("give me this page") and the other side sends back a response (the page, or an error).

The program that waits for requests and answers them is a server; the program that asks is the client. During a scrape, Prometheus is the client and your target is the server.

One machine can run many servers at once, so each listens on its own port: a number from 1 to 65535, like a flat number in an apartment building. Prometheus listens on port 9090, node_exporter on 9100. The machine is called the host, and host plus port, written web1:9100 or 10.0.0.5:9100, points to exactly one program. The localhost always means "this same machine".

A URL is a full web address. http://web1:9100/metrics means: use HTTP, go to host web1, port 9100, and ask for the path /metrics. An address like this that does one specific job is called an endpoint; the /metrics page and Prometheus's /api/v1/query are both endpoints.

Requests come in a few kinds, called HTTP methods. GET asks for data and is what a scrape uses. POST sends data to the server, such as a form or a set of metrics. A URL can also carry settings after a ?, such as ?query=up; that part is the query string.

Every response starts with a status code, a three-digit number saying how it went. 200 means OK, 404 means "no such page", and anything from 500 to 599 means the server itself failed. You'll count these codes in many queries later.

Answers meant for programs rather than people are often written in JSON, a simple text format of named values in braces, like {"status": "success"}. Prometheus answers queries in JSON. To try all this from the terminal, use curl, a command that sends a request and prints the response.

Example

# Ask node_exporter (host localhost, port 9100) for its metrics page
curl -s http://localhost:9100/metrics | head -n 3

# -i also prints the response's first line, with the status code
curl -si http://localhost:9100/metrics | head -n 1
# HTTP/1.1 200 OK

# A URL with a query string: ask Prometheus (port 9090) a question
curl -s 'http://localhost:9090/api/v1/query?query=up'
# {"status":"success","data":{...}}     <- a JSON response

# A page that doesn't exist: print only the status code
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:9090/nope
# 404

Remember

  • HTTP: a client sends a request, a server sends back a response with a status code
  • host:port picks one program on one machine; a URL adds the path, like /metrics
  • GET reads data, POST sends it; JSON is the usual format for answers to programs

Watch out for

  • Leaving out the port: http://web1/metrics goes to port 80, not to the exporter listening on 9100.
New words in this lesson (3)
hostname
The human-readable name of a computer on a network, like api.example.com or localhost. Learn it →
GET
The HTTP method for reading data without changing anything, e.g. loading a web page.
POST
The HTTP method for sending data to a server to create or process something, like submitting a form.

Check yourself

  1. What does the URL http://db1:9187/metrics point to?
  2. A scrape gets back status code 500. What does that mean?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developer.mozilla.org/en-US/docs/Web/HTTP/Overview