Sunday, 8 June 2025

Getting Started with Prometheus Client Libraries for Monitoring Your Apps

1. Prometheus Client Libraries

Before Prometheus can monitor your application, you need to add instrumentation code using client libraries. These libraries support different metric types that Prometheus understands.

 

Following table summarizes the official Prometheus libraries.

 

Language 

Library Name 

Reference Link

Go 

client_golang

https://github.com/prometheus/client_golang

Java / Scala 

client_java

https://github.com/prometheus/client_java

 

Python 

client_python

https://github.com/prometheus/client_python

 

Ruby 

client_ruby

https://github.com/prometheus/client_ruby

Rust 

client_rust

https://github.com/prometheus/client_rust

 

Here's the table for the Unofficial (Third-party) Prometheus Client Libraries:

Language

Library Name

Reference Link

Bash 

client_bash

https://github.com/aecolley/client_bash

C

prometheus-client-c

https://github.com/digitalocean/prometheus-client-c

C++

prometheus-cpp 

https://github.com/jupp0r/prometheus-cpp

 

Common Lisp

prometheus.cl

https://github.com/deadtrickster/prometheus.cl

Dart 

prometheus_client

https://github.com/tentaclelabs/prometheus_client

 

Delphi 

prometheus-client-delphi 

https://github.com/marcobreveglieri/prometheus-client-delphi

 

Elixir 

prometheus.ex

https://github.com/deadtrickster/prometheus.ex

Erlang 

prometheus.erl 

https://github.com/deadtrickster/prometheus.erl

Haskell

prometheus-haskell

https://github.com/fimad/prometheus-haskell

Julia

Prometheus.jl

https://github.com/fredrikekre/Prometheus.jl

Lua (Nginx)

nginx-lua-prometheus 

https://github.com/knyar/nginx-lua-prometheus

Lua (Tarantool)

metrics

https://github.com/tarantool/metrics

 

.NET / C#

prometheus-net 

https://github.com/prometheus-net/prometheus-net

Node.js

prom-client

https://github.com/siimon/prom-client

OCaml

prometheus 

https://github.com/mirage/prometheus

Perl 

Net::Prometheus

https://metacpan.org/pod/Net::Prometheus

PHP

prometheus_client_php

https://github.com/promphp/prometheus_client_php

R

pRometheus 

https://github.com/cfmack/pRometheus

 

2. Prometheus Metric Types Exposed by these libraries

Before Prometheus can collect data from your applications, you need to instrument your code using one of its client libraries (based on your programming language). These libraries expose four primary metric types, each suited to a specific kind of measurement.

 

2.1 Counter

A Counter is a metric that only increases. Counters start at 0 and can only go up (or reset to 0 when the application restarts). Use counters to track things that only increase, never decrease.

 

Don't use counters for things that can decrease (like current memory or thread count).

 

Common Methods:

·      inc(): Increments by 1

·      inc(amount): Increments by a specific value

 

Examples:

·      Number of HTTP requests received

·      Number of errors occurred

·      Total tasks processed

·      Number of files uploaded

 

Sample Java Code

Counter requestCounter = Counter.build()
    .name("http_requests_total")
    .help("Total HTTP requests received")
    .register();

requestCounter.inc();        // Increment by 1
requestCounter.inc(5);       // Increment by 5

2.2 Gauge

A Gauge is used for values that can go up and down. Use gauges to track things that fluctuate.

 

Common Methods:

·      set(value): Set to a specific value

·      inc(): Increment by 1

·      dec(): Decrement by 1

 

Examples:

·      Current number of active threads

·      Memory usage

·      Temperature readings

·      Current queue size

·      CPU usage

 

Sample Java Code

 

Gauge memoryUsage = Gauge.build()
    .name("current_memory_usage_bytes")
    .help("Tracks the current memory usage")
    .register();

memoryUsage.set(1024);    // Set to specific value
memoryUsage.inc();        // Increment by 1
memoryUsage.dec();        // Decrement by 1

 

2.3 Summary

A Summary is used to track the distribution of events over time, especially useful for measuring things like request durations or payload sizes. It provides total sum, count, and percentiles.

 

Primary Method

observe(value): Records the size/duration of the event

 

Examples:

·      Response time of a web request

·      Size of incoming requests

·      Latency of database operations

 

Sample Java Code

 

Summary requestLatency = Summary.build()
    .name("request_latency_seconds")
    .help("Tracks request durations in seconds")
    .register();

requestLatency.observe(0.35);  // Observe a request that took 350ms

2.4 Histogram

A Histogram tracks the distribution of events, but instead of percentiles, it places observations into predefined buckets. You can count how many requests fell into which time ranges.

 

Primary Method:

observe(value): Records an event’s value into a bucket

 

Examples:

·      Request duration distribution (e.g., how many requests took <100ms, <500ms, etc.)

·      File sizes

·      DB query execution time

 

Key Difference from Summary:

·      Histogram buckets are configurable

·      Summary gives quantiles (client-side), Histogram gives buckets (server-side)

 

Sample Java Code

 

Histogram requestDuration = Histogram.build()
    .name("http_request_duration_seconds")
    .help("HTTP request latency buckets")
    .buckets(0.1, 0.3, 0.5, 1, 2, 5)  // Custom buckets in seconds
    .register();

requestDuration.observe(0.25); // 250ms
requestDuration.observe(0.42); // 420ms
requestDuration.observe(1.3);  // 1300ms
requestDuration.observe(0.05); // 50ms
requestDuration.observe(2.1);  // 2100ms

 

Each call to .observe() adds a data point to:

 

·      A counter that tracks the total number of observations (count)

·      A sum that accumulates total observed value

·      The appropriate bucket (like < 0.3, < 1, etc.)

 

In summary,

·      Use Counter for totals like “how many files processed”.

·      Use Gauge for real-time metrics like “how many users are online now”.

·      Use Summary for approximate quantiles like “95% of responses completed in under X ms”.

·      Use Histogram when you want bucketed distribution data (ideal for alerts and visualizations).

 


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment