DEV Community

Maksim
Maksim

Posted on • Originally published at maksimrv.Medium on

Note: Setup Prometheus playground

  1. Download latest version of Prometheus from website and extract from archive, or just use script like this
PROME_VERSION="2.45.1" &&\
PROME_OS="darwin-amd64" &&\
PROME_FILE="prometheus-${PROME_VERSION}.${PROME_OS}.tar.gz" &&\
curl -L "https://github.com/prometheus/prometheus/releases/download/v${PROME_VERSION}/${PROME_FILE}" -o "/tmp/${PROME_FILE}" &&\
tar xvzf "/tmp/${PROME_FILE}" -C "/tmp"
Enter fullscreen mode Exit fullscreen mode
  1. Create Prometheus config file
cat > /tmp/prometheus.yml <<EOF
global:
  scrape_interval: 15s
  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'test'
    scrape_interval: 1s
    static_configs:
      - targets: ['localhost:3000']
EOF
Enter fullscreen mode Exit fullscreen mode
  1. Run Prometheus passing config file using --config.file argument
/tmp/prometheus-2.45.1.darwin-amd64/prometheus --config.file=/tmp/prometheus.yml
Enter fullscreen mode Exit fullscreen mode
  1. In our prometheus.yaml we have defined getting metrics from localhost:3000 . Let's create simple http server which hold metrics
cat > /tmp/index.js <<'EOF'
require('http').createServer(function (req, res) {
  if (req.url === '/metrics') {
    const metricName = 'foo';
    const metricType = 'gauge';
    const metricValue = 1;
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(`# TYPE ${metricName} ${metricType}\n`);
    res.end(`${metricName} ${metricValue}\n`);
    return;
  }
  res.writeHead(404, {'Content-Type': 'text/plain'});
  res.end('Not found\n');
}).listen(3000, () => console.log(new Date(), 'Listening on port 3000'));
EOF

Enter fullscreen mode Exit fullscreen mode
  1. Start server
node --watch /tmp/index.js
Enter fullscreen mode Exit fullscreen mode
  1. Open Prometheus graph UI
open http://localhost:9090/graph
Enter fullscreen mode Exit fullscreen mode
  1. Play with Prometheus 🎉

P.S.

Instead of handwritten server, you can use Pushgateway + curl

Top comments (0)