aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2019-08-01 12:21:39 +0200
committerPatrick Spek <p.spek@tyil.nl>2019-08-01 12:21:39 +0200
commitef0f97367c39272b3dcae5af74f7b81d41951240 (patch)
tree250192ab5756d320dbf1a14939b0278456dba57f
parent992b3cd1bc85898ca059f7b4c576d1956a50d229 (diff)
downloadTemplate::Prometheus-ef0f97367c39272b3dcae5af74f7b81d41951240.tar.gz
Template::Prometheus-ef0f97367c39272b3dcae5af74f7b81d41951240.tar.bz2
Move in code from App::CPAN alpha
-rw-r--r--META6.json4
-rw-r--r--README.pod628
-rw-r--r--lib/Template/Prometheus.pm672
-rw-r--r--lib/Template/Prometheus/Metric.pm638
-rw-r--r--lib/Template/Prometheus/Metrics/Gauge.pm626
5 files changed, 166 insertions, 2 deletions
diff --git a/META6.json b/META6.json
index dd86c3b..1af0738 100644
--- a/META6.json
+++ b/META6.json
@@ -12,7 +12,9 @@
"name": "Template::Prometheus",
"perl": "6.d",
"provides": {
- "Template::Prometheus": "lib/Template/Prometheus.pm6"
+ "Template::Prometheus": "lib/Template/Prometheus.pm6",
+ "Template::Prometheus::Metric": "lib/Template/Prometheus/Metric.pm6",
+ "Template::Prometheus::Metrics::Gauge": "lib/Template/Prometheus/Metrics/Gauge.pm6"
},
"resources": [
diff --git a/README.pod6 b/README.pod6
index fd88958..0676f03 100644
--- a/README.pod6
+++ b/README.pod6
@@ -6,7 +6,7 @@
=head1 Description
-Objects to easily render templates of metrics for usage with Prometheus
+Objects to easily render templates of metrics for usage with Prometheus.
=head1 Installation
@@ -16,6 +16,32 @@ Install this module through L<zef|https://github.com/ugexe/zef>:
zef install Template::Prometheus
=end code
+=head1 Example usage
+
+=head2 Exporting stats from the C<App::CPAN> database
+
+=begin code
+Template::Prometheus
+ .new(:prefix<cpan6>)
+ .add-metric(GaugeMetric.new(
+ name => "distribution_count",
+ value => ModuleRepo::count,
+ description => "Number of distributions in the database",
+ ))
+ .add-metric(GaugeMetric.new(
+ name => "module_count",
+ value => ModuleRepo::count-unique,
+ description => "Number of unique modules in the database",
+ ))
+ .add-metric(GaugeMetric.new(
+ name => "pause_id_count",
+ value => ModuleRepo::count-pause-ids,
+ description => "Number of PAUSE IDs in the database",
+ ))
+ .Str
+ .say
+=end code
+
=head1 License
This module is distributed under the terms of the AGPL-3.0.
diff --git a/lib/Template/Prometheus.pm6 b/lib/Template/Prometheus.pm6
new file mode 100644
index 0000000..d3faed2
--- /dev/null
+++ b/lib/Template/Prometheus.pm6
@@ -0,0 +1,72 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Template::Prometheus::Metric;
+
+#| This class can hold a collection of Template::Prometheus::Metric objects, to
+#| then render into template usable with Prometheus.
+unit class Template::Prometheus;
+
+#| A collection of metrics to expose through the template.
+has %.metrics;
+
+#| An optional prefix to add to all metrics exposed.
+has Str:D $.prefix is default("");
+
+#| The seperator between the prefix and the metric name. Defaults to "_".
+has Str:D $.prefix-seperator is default("_");
+
+#| Add a metric to expose. This method returns its own class, allowing it to be
+#| chained.
+method add-metric (
+ #| The metric to expose.
+ Template::Prometheus::Metric $metric,
+
+ --> Template::Prometheus
+) {
+ %!metrics{$metric.name} = $metric;
+ self;
+}
+
+#| Turn the template into it's stringified form. This renders the template into
+#| a usable state, to be scraped by Prometheus.
+method Str
+{
+ %!metrics
+ .keys
+ .sort
+ .map({
+ my $metric = %!metrics{$_};
+ my $name = $metric.name;
+
+ if $!prefix {
+ $name = $!prefix ~ $!prefix-seperator ~ $name;
+ }
+
+ qq:to/EOF/
+ # HELP $name {$metric.description}
+ # TYPE $name {$metric.type}
+ $name {$metric.value}
+ EOF
+ })
+ .join
+}
+
+=begin pod
+
+=NAME Template::Prometheus
+=AUTHOR Patrick Spek <p.spek@tyil.work>
+=VERSION 0.0.0
+
+=head1 Synopsis
+
+=head1 Description
+
+=head1 Examples
+
+=head1 See also
+
+=end pod
+
+# vim: ft=perl6 noet
diff --git a/lib/Template/Prometheus/Metric.pm6 b/lib/Template/Prometheus/Metric.pm6
new file mode 100644
index 0000000..444c8fc
--- /dev/null
+++ b/lib/Template/Prometheus/Metric.pm6
@@ -0,0 +1,38 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+#| This role defines all the minimal required behaviours of a metric that
+#| Prometheus can use.
+unit role Template::Prometheus::Metric;
+
+#| The name of the metric.
+has $.name is required;
+
+#| The current value of the metric.
+has $.value is required;
+
+#| A human-readable description of the metric. This attribute is optional, but
+#| highly recommended. Defaults to "An undocumented metric".
+has $.description is default("An undocumented metric");
+
+#| A method to expose the type of metric. Simply returns a string.
+method type (--> Str) { self.^name.split("::").tail.fc }
+
+=begin pod
+
+=NAME Template::Prometheus::Metric
+=AUTHOR Patrick Spek <p.spek@tyil.work>
+=VERSION 0.0.0
+
+=head1 Synopsis
+
+=head1 Description
+
+=head1 Examples
+
+=head1 See also
+
+=end pod
+
+# vim: ft=perl6 noet
diff --git a/lib/Template/Prometheus/Metrics/Gauge.pm6 b/lib/Template/Prometheus/Metrics/Gauge.pm6
new file mode 100644
index 0000000..bf3fa29
--- /dev/null
+++ b/lib/Template/Prometheus/Metrics/Gauge.pm6
@@ -0,0 +1,26 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Template::Prometheus::Metric;
+
+#| A Prometheus metric of type "gauge".
+unit class Template::Prometheus::Metrics::Gauge is Template::Prometheus::Metric;
+
+=begin pod
+
+=NAME Template::Prometheus::Metrics::Gauge
+=AUTHOR Patrick Spek <p.spek@tyil.work>
+=VERSION 0.0.0
+
+=head1 Synopsis
+
+=head1 Description
+
+=head1 Examples
+
+=head1 See also
+
+=end pod
+
+# vim: ft=perl6 noet