1/vrischmann/prometheus v0.6

Prometheus client library


zig-prometheus

This is a Zig library to add Prometheus-inspired metrics to a library or application.

"Inspired" because it is not strictly compatible with Prometheus, the Histogram type is tailored for VictoriaMetrics. See this blog post from the creator of VictoriaMetrics for details.

Requirements

Zig master is the only required dependency.

Introduction

This library only provides the following types:

  • A Registry holding a number of metrics
  • A Counter metric type
  • A Gauge metric type
  • A Histogram metric type

Examples

If you want a quick overview of how to use this library check the basic example program. It showcases everything.

There's also a more specific example which integrates with the HTTP server Apple Pie. It showcases how to export metrics that are directly scrapable by Prometheus or VictoriaMetrics.

Reference

Registry

The Registry is the entry point to obtain a metric type, as well as the type capable of serializing the metrics to a writer.

In an application it might be useful to have a default, global registry; in a library you probably should take one as a parameter.

Creation

Here is how to get a registry:

var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();
...

You can also configure some options for the registry:

var registry = try prometheus.Registry(.{ .max_metrics = 40, .max_name_len = 300 }).create(allocator);
defer registry.destroy();
...

If you want to store the registry in a variable you probably want to do something like this:

const Registry = prometheus.Registry(.{ .max_metrics = 40, .max_name_len = 300 });
var registry = Registry.create(allocator);
defer registry.destroy();
...

Now you can get metric objects which we will describe later.

Serializing the metrics

Once you have a registry you can serialize its metrics to a writer:

var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();

...

var file = try std.fs.cwd().createFile("metrics.txt", .{});
defer file.close();

try registry.write(allocator, file.writer());

The write method is thread safe.

Counter

The Counter type is an atomic integer counter.

Here is an example of how to use a counter:

var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();

var total_counter = try registry.getOrCreateCounter("http_requests_total");
var api_users_counter = try registry.getOrCreateCounter(
    \\http_requests{route="/api/v1/users"}
);
var api_articles_counter = try registry.getOrCreateCounter(
    \\http_requests{route="/api/v1/articles"}
);

total_counter.inc();
total_counter.dec();
total_counter.add(200);
total_counter.set(2400);
const counter_value = total_counter.get();

All methods on a Counter are thread safe.

Gauge

The Gauge type represents a numerical value that is provided by calling a user-supplied function.

A Gauge is created with a state and a function which is given that state every time it is called.

For example, you can imagine a gauge returning the number of connections in a connection pool, the amount of memory allocated, etc. Basically anytime the value is instantly queryable it could be a gauge.

Of course, nothing stops you from using a counter to simulate a gauge and calling set on it; it's up to you.

Here is an example gauge:

var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();

const Conn = struct {};
const ConnPool = struct {
    conns: std.ArrayList(Conn),
};
var pool = ConnPool{ .conns = std.ArrayList.init(allocator) };

_ = try registry.getOrCreateGauge(
    "http_conn_pool_size",
    &pool,
    struct {
        fn get(p: *Pool) f64 {
            return @intToFloat(f64, p.conns.items.len);
        }
    }.get,
);

Histogram

The Histogram type samples observations and counts them in automatically created buckets.

It can be used to observe things like request duration, request size, etc.

Here is a (contrived) example on how to use an histogram:

var registry = try prometheus.Registry(.{}).create(allocator);
defer registry.destroy();

var request_duration_histogram = try registry.getOrCreateHistogram("http_request_duration");

// Make 100 observations of some expensive operation.
var i: usize = 0;
while (i < 100) : (i += 1) {
    const start = std.time.milliTimestamp();

    var j: usize = 0;
    var sum: usize = 0;
    while (j < 2000000) : (j += 1) {
        sum *= j;
    }

    request_duration_histogram.update(@intToFloat(f64, std.time.milliTimestamp() - start));
}

Using labels

If you're read the Prometheus data model, you've seen that a metric can have labels.

Other Prometheus clients provide helpers for this, but not this library: you need to build the proper name yourself.

If you have static labels then it's easy, just write the label directly like this:

var http_requests_route_home = try registry.getOrCreateCounter(
    \\http_requests{route="/home"}
);
var http_requests_route_login = try registry.getOrCreateCounter(
    \\http_requests{route="/login"}
);
var http_requests_route_logout = try registry.getOrCreateCounter(
    \\http_requests{route="/logout"}
);
...

If you have dynamic labels you could write a helper function like this:

fn getHTTPRequestsCounter(
    allocator: *mem.Allocator,
    registry: *Registry,
    route: []const u8,
) !*prometheus.Counter {
    const name = try std.fmt.allocPrint(allocator, "http_requests{{route=\"{s}\"}}", .{
        route,
    });
    return try registry.getOrCreateCounter(name);
}

fn handler(route: []const u8) void {
    var counter = getHTTPRequestsCounter(allocator, registry, route);
    counter.inc();
}

Package Contents

  • .gitattributes
  • zigmod.lock
  • LICENSE
  • .github/workflows/main.yml
  • examples/basic/main.zig
  • examples/apple_pie/main.zig
  • build.zig
  • src/Histogram.zig
  • src/Counter.zig
  • src/Gauge.zig
  • src/main.zig
  • src/metric.zig
  • README.md
  • zig.mod
  • .gitignore

Root Dependencies

  • git https://github.com/Luukdegram/apple_pie branch-master

Build Dependencies

  • git https://github.com/Luukdegram/apple_pie branch-master

History

Published On Tree @ Commit Size
v0.6 Sat, 08 Oct 2022 17:45:12 UTC Tree 44.786 KB
v0.5 Sun, 31 Jul 2022 16:52:52 UTC Tree 44.786 KB
v0.4 Tue, 26 Jul 2022 18:40:33 UTC Tree 45.276 KB
v0.3 Tue, 31 May 2022 21:27:52 UTC Tree 45.279 KB
v0.2 Fri, 11 Feb 2022 22:22:48 UTC Tree 45.279 KB
v0.1 Sat, 05 Feb 2022 00:17:50 UTC Tree 44.682 KB