Saturday, 17 May 2025

Start Prometheus by Supplying a Config File

If you installed Prometheus via Homebrew on macOS and didn’t find the default prometheus.yml config file, don't worry! Here's how you can create your own and start Prometheus manually with it.

Step 1: Check where Prometheus binary is installed

Run this command:

 

which prometheus

$which prometheus
/opt/homebrew/bin/prometheus

If you get output like above, this confirms that Prometheus is installed.

 

Step 2: Create a basic configuration file

You can create your own config file prometheus.yml in your home directory or any preferred location:

 

prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

Step 3: Start Prometheus with your config

Now run Prometheus by pointing it to your new config file:

prometheus --config.file={PATH TO prometheus.yml}

$ prometheus --config.file=./prometheus.yml 
time=2025-04-10T16:05:44.546Z level=INFO source=main.go:640 msg="No time or size retention was set so using the default time retention" duration=15d
time=2025-04-10T16:05:44.546Z level=INFO source=main.go:687 msg="Starting Prometheus Server" mode=server version="(version=3.2.1, branch=non-git, revision=non-git)"
time=2025-04-10T16:05:44.546Z level=INFO source=main.go:692 msg="operational information" build_context="(go=go1.24.0, platform=darwin/arm64, user=reproducible@reproducible, date=20250225-19:11:52, tags=netgo,builtinassets,stringlabels)" host_details=(darwin) fd_limits="(soft=122880, hard=unlimited)" vm_limits="(soft=unlimited, hard=unlimited)"
time=2025-04-10T16:05:44.548Z level=INFO source=main.go:768 msg="Leaving GOMAXPROCS=12: CPU quota undefined" component=automaxprocs
time=2025-04-10T16:05:44.550Z level=INFO source=web.go:654 msg="Start listening for connections" component=web address=0.0.0.0:9090
time=2025-04-10T16:05:44.551Z level=INFO source=main.go:1228 msg="Starting TSDB ..."
time=2025-04-10T16:05:44.552Z level=INFO source=tls_config.go:347 msg="Listening on" component=web address=[::]:9090
time=2025-04-10T16:05:44.553Z level=INFO source=tls_config.go:350 msg="TLS is disabled." component=web http2=false address=[::]:9090
time=2025-04-10T16:05:44.556Z level=INFO source=head.go:628 msg="Replaying on-disk memory mappable chunks if any" component=tsdb
time=2025-04-10T16:05:44.557Z level=INFO source=head.go:715 msg="On-disk memory mappable chunks replay completed" component=tsdb duration=11.042µs
time=2025-04-10T16:05:44.557Z level=INFO source=head.go:723 msg="Replaying WAL, this may take a while" component=tsdb
time=2025-04-10T16:05:44.558Z level=INFO source=head.go:795 msg="WAL segment loaded" component=tsdb segment=0 maxSegment=0
time=2025-04-10T16:05:44.558Z level=INFO source=head.go:832 msg="WAL replay completed" component=tsdb checkpoint_replay_duration=293.625µs wal_replay_duration=1.100291ms wbl_replay_duration=42ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=11.042µs total_replay_duration=1.731667ms
time=2025-04-10T16:05:44.560Z level=INFO source=main.go:1249 msg="filesystem information" fs_type=1a
time=2025-04-10T16:05:44.560Z level=INFO source=main.go:1252 msg="TSDB started"
time=2025-04-10T16:05:44.560Z level=INFO source=main.go:1437 msg="Loading configuration file" filename=./prometheus.yml
time=2025-04-10T16:05:44.568Z level=INFO source=main.go:1476 msg="updated GOGC" old=100 new=75
time=2025-04-10T16:05:44.568Z level=INFO source=main.go:1486 msg="Completed loading of configuration file" db_storage=667ns remote_storage=5.5µs web_handler=250ns query_engine=666ns scrape=7.575958ms scrape_sd=26.375µs notify=584ns notify_sd=333ns rules=4.333µs tracing=23.416µs filename=./prometheus.yml totalDuration=8.315708ms
time=2025-04-10T16:05:44.568Z level=INFO source=main.go:1213 msg="Server is ready to receive web requests."
time=2025-04-10T16:05:44.568Z level=INFO source=manager.go:175 msg="Starting rule manager..." component="rule manager"

That’s it you are done.

 

Understanding Config file

global:
  scrape_interval: 15s

This sets global configurations for Prometheus. "scrape_interval: 15s" means, Prometheus will scrape metrics (i.e., fetch data) from each configured target every 15 seconds. This is the default interval unless overridden in specific scrape_configs.

scrape_configs:
  - job_name: 'prometheus'

 

This section tells Prometheus where to get metrics from.

·      job_name: 'prometheus' gives a label to this group of targets.

·      You’ll see this job_name in the Prometheus UI when querying metrics.

static_configs and targets
    static_configs:
      - targets: ['localhost:9090']

This tells Prometheus to statically monitor a list of targets. This configuraiton Scrape metrics from Prometheus itself, which runs its HTTP server on port 9090.

 

Querying Metrics for the prometheus Job

Step 1: Access Prometheus UI

Open your browser. Go to http://localhost:9090

This is the Prometheus web interface.

 

Step 2: Use the "Expression" input box

At the top of the Prometheus UI, you’ll see an input box labeled "Enter Expression".


 

Type the following query

up

 

Then click Execute button. The result should show something like:


 

Output of ‘up’ query looks like below

up{instance="localhost:9090", job="prometheus"}

 

Here, up is a built-in Prometheus metric and {instance="localhost:9090", job="prometheus"} is a label set. Following table explains the labels in detail.

 

Label

Value

Meaning

instance

localhost:9090

This is the hostname and port of the monitored target

job

prometheus

This is the job name defined in your prometheus.yml

 

Value 1 indicates that the scrape is successful. It print 0, in case of unsuccessful scraping.

 

Following table summarizes some useful queries just for practice:

Query

What it shows

up{job="prometheus"} 

Only the prometheus job

prometheus_http_requests_total 

Total HTTP requests received by Prometheus

process_cpu_seconds_total

CPU time Prometheus process used

scrape_duration_seconds

How long it took to scrape each target

scrape_samples_scraped 

How many metrics were scraped in each cycle

 


 

You can toggle between "Table" and "Graph" view right below the query box.


 


Previous                                                    Next                                                    Home

No comments:

Post a Comment