By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Monitoring Golang Services with Prometheus: Choosing Between Pull and Push Models | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Monitoring Golang Services with Prometheus: Choosing Between Pull and Push Models | HackerNoon
Computing

Monitoring Golang Services with Prometheus: Choosing Between Pull and Push Models | HackerNoon

News Room
Last updated: 2025/03/02 at 6:59 PM
News Room Published 2 March 2025
Share
SHARE

What is Prometheus and Why Do You Need It?

Prometheus is a powerful monitoring system that collects and processes numerical data (metrics) from applications. It helps you track key indicators such as:

  • The number of requests handled by your service.
  • The response time for each request.
  • Memory and CPU usage.
  • The number of errors occurring in the system.

By using Prometheus, you can answer critical questions like:

  • “Is my service running efficiently?”

  • “What are the performance bottlenecks?”

  • “Do we need to scale up our infrastructure?”

And how does Prometheus Collect Metrics?

There are two primary ways Prometheus gathers data:

  1. Pull model – Prometheus actively queries services for their metrics.
  2. Push model (Pushgateway) – Services push their metrics to an intermediary, which Prometheus then collects.

Let’s break them down.


Pull Model

In the pull model, Prometheus actively fetches metrics from your application via HTTP (e.g., http://your-service:8080/metrics).

This is the default and most commonly used method.

Setting up Prometheus with Golang (Pull Model)

  1. Install the necessary libraries:

    go get github.com/prometheus/client_golang/prometheus
    go get github.com/prometheus/client_golang/prometheus/promhttp
    
  2. Define your metrics (e.g., counting HTTP requests):

    import (
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
    )
    
    var httpRequestsTotal = promauto.NewCounter(prometheus.CounterOpts{
        Name: "http_requests_total",
        Help: "Total number of HTTP requests",
    })
    
  3. Expose a /metrics endpoint:

    import (
        "net/http"
    
        "github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
    func main() {
        http.Handle("/metrics", promhttp.Handler())
    }
    
  4. Configure Prometheus to scrape metrics from your service in prometheus.yml:

    scrape_configs:
      - job_name: "example_service"
        static_configs:
          - targets: ["localhost:8080"]
    

Now, Prometheus will automatically query http://localhost:8080/metrics every few seconds to collect data.

Why is the Pull Model Preferred?

  • Simplicity – Prometheus controls the scraping schedule and frequency.
  • Fewer points of failure – No need for an additional service to receive metrics.
  • Automatic cleanup – If a service stops responding, Prometheus simply stops receiving data, avoiding stale metrics.

Push Model (Pushgateway Approach)

push it!push it!

In the push model, a service sends its metrics to an intermediary service called Pushgateway, which stores them until Prometheus fetches them.

How it Works (Push Model)

  1. Your application pushes metrics to Pushgateway:

    import (
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/push"
    )
    
    func main() {
        registry := prometheus.NewRegistry()
        jobCounter := prometheus.NewCounter(prometheus.CounterOpts{
            Name: "job_execution_count",
            Help: "Number of executed jobs",
        })
    
        registry.MustRegister(jobCounter)
        jobCounter.Inc()
    
        err := push.New("http://localhost:9090", "my_service_or_job").
            Collector(jobCounter).
            Grouping("instance", "worker_1").
            Push()
        if err != nil {
            panic(err)
        }
    }
    
  2. Configure Prometheus to collect data from Pushgateway:

    scrape_configs:
      - job_name: "pushgateway"
        static_configs:
          - targets: ["localhost:9091"]
    

When is the Push Model Actually Useful?

  • Short-lived jobs (batch tasks, cron jobs) that complete before Prometheus can scrape them.
  • Network restrictions where Prometheus cannot directly access the service.
  • External data sources (IoT devices, external APIs) that cannot be scraped directly.

Which Model Should You Use?

Method

Best for…

Pros

Cons

Pull (Recommended)

Web services, APIs, long-running applications

Simple setup, fewer dependencies, automatic cleanup

Not suitable for very short-lived tasks

Push (Pushgateway)

Batch jobs, tasks without stable network access

Allows pushing data from short-lived jobs

Stale data, extra complexity, risk of bottlenecks

Why Push Model is Not Ideal?

Although Pushgateway solves some problems (e.g., short-lived processes that terminate before Prometheus scrapes them), it introduces several new issues:

  1. Difficult to manage stale data
    • If a service dies, its old metrics remain in Pushgateway.

    • Prometheus has no way of knowing if the service is still running.

    • You must manually delete outdated metrics using push.Delete(...) or configure expiry policies.

  2. Additional complexity
    • Instead of a direct Service → Prometheus link, you now have Service → Pushgateway → Prometheus.

    • Pushgateway is an extra dependency, increasing maintenance overhead.

  3. Potential bottlenecks
    • If many services push metrics frequently, Pushgateway can become overwhelmed.

    • Unlike direct Prometheus scrapes (which distribute the load), all requests hit a single Pushgateway instance.

  4. Data consistency issues
    • If multiple services push metrics with the same name but different values, data may be overwritten, leading to incorrect results.

Conclusion

Prometheus is a powerful and reliable tool for monitoring services. For most applications, the pull model is the best choice—it’s simple, efficient, and ensures fresh data without additional complexity. However, if you’re working with short-lived processes like Lambda functions or batch jobs, the push model via Pushgateway can be useful to capture metrics before the process exits.

Choosing the right approach ensures better observability and maintainability of your system.

Take care!

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Snap On a Screen! Lenovo Teases Wild Modular Designs for Laptop Side Displays
Next Article Today's NYT Connections Hints, Answers for March 3, #631
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

BYD’s new 2024 EV models to feature advanced driving functions · TechNode
Computing
Google adding chatbot-like 'AI Mode' to search
News
Tech Layoffs: US Companies With Job Cuts In 2024 And 2025
News
The Most Dangerous Lie in Startups | HackerNoon
Computing

You Might also Like

Computing

BYD’s new 2024 EV models to feature advanced driving functions · TechNode

1 Min Read
Computing

The Most Dangerous Lie in Startups | HackerNoon

7 Min Read
Computing

Xiaomi launches Weibo legal account amid unverified reports on upcoming car SU7 · TechNode

1 Min Read
Computing

Huawei sets up smart car subsidiary, selling shares to Changan and more · TechNode

1 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?