All Projects → vrischmann → zig-prometheus

vrischmann / zig-prometheus

Licence: MIT license
Prometheus/VictoriaMetrics client library for Zig

Programming Languages

Zig
133 projects
AMPL
153 projects

Projects that are alternatives of or similar to zig-prometheus

victoriametrics-ru-links
Список постов и видеозаписей об VictoriaMetrics на русском языке
Stars: ✭ 1 (-96.15%)
Mutual labels:  victoriametrics
network exporter
ICMP & MTR & TCP Port & HTTP Get - Network Prometheus exporter
Stars: ✭ 162 (+523.08%)
Mutual labels:  victoriametrics
promate
Graphite On VictoriaMetrics
Stars: ✭ 68 (+161.54%)
Mutual labels:  victoriametrics
VictoriaLogs
Loki On VictoriaMetrics
Stars: ✭ 58 (+123.08%)
Mutual labels:  victoriametrics
vmctl
Victoria Metrics command line tool
Stars: ✭ 36 (+38.46%)
Mutual labels:  victoriametrics
sqlmetrics
Prometheus metrics for Go database/sql via VictoriaMetrics/metrics
Stars: ✭ 21 (-19.23%)
Mutual labels:  victoriametrics

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();
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].