Skip to content

This is an example Code written in go, that exposes metrics to prometheus

Notifications You must be signed in to change notification settings

noemi-dresden/expose-metrics-to-prometheus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Expose metrics to prometheus with go

This is an example code written with the progamming language go, that exposes metrics to prometheus. In this post under prometheus configuration, you can see the highighted text saying that you should have an application running, that exposes metrics to prometheus, so this is one of many possibilities.

How it works?

We need 2 files:

  • main.go, which is the start of our code

    • We are using the framework iris for the webservice
    • We take advantage of the prometheus middleware of iris to pass the data into /metrics
  • collector.go, where we implement the heart of the task

Deep in collector.go

As the name already teels us, we goal is here to collect the data, that we want to expose. We are using the go library for prometheus.There are 4 main tasks in there:

  1. Definition of the collector
type MyCollector struct {
	myFirstMetric  *prometheus.Desc
    mySecondMetric *prometheus.Desc
    // all metrics you want
}
  1. Contructor of the collector
func NewCollector() *MyCollector {
	return &MyCollector{
		myFirstMetric: prometheus.NewDesc("first_metric",
			"description of first_metric",
			nil, nil,
		),
		...
	}
}
  1. Writting each collector into prometheus channel
func (collector *MyCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- collector.myFirstMetric
    ch <- collector.mySecondMetric
    ...
}
  1. Collecting the metrics and update each channel
func (collector *MyCollector) Collect(ch chan<- prometheus.Metric) {

	// implement the logic giving us the value, that we want to expose, here I just took an example
	// those value could be from kafka or simple database

	//Write latest value for each metric in the prometheus metric channel.
	ch <- prometheus.MustNewConstMetric(collector.myFirstMetric, prometheus.CounterValue, firstValue)
	ch <- prometheus.MustNewConstMetric(collector.mySecondMetric, prometheus.CounterValue, secondValue)
}

Run

go run main.go collector.go

Test your collector

Enter http://localhost:8080/metrics in your browser, you should get something like this

# HELP first_metric description of first_metric
# TYPE first_metric counter
first_metric 1
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.0049994
go_gc_duration_seconds{quantile="0.25"} 0.0049994
go_gc_duration_seconds{quantile="0.5"} 0.0049994
go_gc_duration_seconds{quantile="0.75"} 0.0049994
go_gc_duration_seconds{quantile="1"} 0.0049994
go_gc_duration_seconds_sum 0.0049994
go_gc_duration_seconds_count 1
...
# HELP second_metric description of second_metric
# TYPE second_metric counter
second_metric 2

As you see, first_metric and second_metric are listed in there. Note that the other metrics are automatically generated by the prometheus golang library. If you have a running prometheus, your prometheus will make a call periodically to your application. Read this for more.

About

This is an example Code written in go, that exposes metrics to prometheus

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages