Skip to content

VarshaUN/Prometheus-Instrumentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Prometheus-Instrumentation

This repo contains Prometheus instrumentation for Go applications.

Minimal-Application

This is a simple Go application that exposes Prometheus metrics via HTTP.

Install the prometheus, promauto, and promhttp libraries necessary for this demo.

go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promauto
go get github.com/prometheus/client_golang/prometheus/promhttp

How it works?

To expose Prometheus metrics in a Go application, you need to provide a /metrics HTTP endpoint. You can use the prometheus/promhttp library's HTTP Handler as the handler function.

The code in main.go would expose the default metrics for Go applications via http://localhost:2112/metrics

Start the application

go run main.go

Access the metrics

curl http://localhost:2112/metrics

This was a minimal go application demo of Prometheus Instrumentation.The application above exposes only the default Go metrics.


Adding your own metrics(Counter metric type used for this demo)

In this demo we will be using Counter metric type for our go application.

What is a counter metric type? |Checkout my blog on [Prometheus] |(https://varshaun.hashnode.dev/introduction-to-prometheus) to know more!

This main.go application exposes a myapp_processed_ops_total counter that counts the number of operations that have been processed thus far. Every 2 seconds, the counter is incremented by one.

Run the application

go run main.go

Access the metrics

curl http://localhost:2112/metrics

In the metrics output, you'll see the help text, type information, and current value of the myapp_processed_ops_total counter:

# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total 5

You can configure a locally running Prometheus instance to scrape metrics from the application. Here's an example prometheus.yml configuration:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - localhost:2112

About

This repo contains Prometheus instrumentation for Go applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages