Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if PUSHGATEWAY is not provided #2

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
- uses: actions/checkout@v3

- name: Build docker image
run: docker-compose build
run: docker compose build

- name: Run tests
run: docker-compose run test_and_cover
run: docker compose run test_and_cover
env:
GITHUB_TOKEN: ${{ secrets.github_token }}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
libtest-exception-perl \
libtest-spec-perl \
libtest-time-perl \
libtest-warn-perl \
libwww-perl \
libyaml-perl \
netcat
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ WriteMakefile(
TEST_REQUIRES => {
'Test::Spec' => 0,
'Test::Time' => 0,
'Test::Warn' => 0,
'LWP::UserAgent' => 0,
},
CONFIGURE_REQUIRES => { "ExtUtils::MakeMaker" => 0 },
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ $tracker->finalize;
This will report the number of lines processed to the push gateway every 1,000
lines.

In order to report to the pushgateway, the `PUSHGATEWAY` environment variable
must be set, or a `pushgateway` argument can be supplied to
`ProgressTracker->new()`. Otherwise, `ProgressTracker` will warn this is not
provided.

### Stages

```perl
Expand Down
4 changes: 0 additions & 4 deletions bin/test_and_cover.sh

This file was deleted.

118 changes: 0 additions & 118 deletions bin/wait-for

This file was deleted.

19 changes: 14 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
version: '3'
---

services:

test:
build: .
volumes:
- .:/src
command: bin/wait-for --timeout=300 pushgateway:9091 -- prove
command: prove
environment:
- PUSHGATEWAY=http://pushgateway:9091
depends_on:
- pushgateway
pushgateway:
condition: service_healthy

test_and_cover:
build: .
Expand All @@ -24,11 +25,19 @@ services:
- GITHUB_REF
- GITHUB_ACTIONS
- PUSHGATEWAY=http://pushgateway:9091
command: bin/test_and_cover.sh
command: cover -test -report Coveralls
depends_on:
- pushgateway
pushgateway:
condition: service_healthy

pushgateway:
image: prom/pushgateway
command:
- --web.enable-admin-api
healthcheck:
test: [ "CMD", "wget", "--quiet", "--tries=1", "-O", "/dev/null", "pushgateway:9091/-/healthy" ]
x-healthcheck-defaults: &healthcheck-defaults
interval: 5s
timeout: 5s
start_period: 5s
retries: 5
12 changes: 11 additions & 1 deletion lib/ProgressTracker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sub new {
$self->{labels} = { };
$self->{job} = $params{job} || $ENV{'JOB_NAME'} || basename($0);
$self->{pushgateway} = $params{pushgateway} || $ENV{'PUSHGATEWAY'};
die("Must specify pushgateway base URL with pushgateway param or PUSHGATEWAY env var") unless $self->{pushgateway};
$self->warn_not_reporting if !$self->{pushgateway};

my $namespace = $params{namespace} || $ENV{'JOB_NAMESPACE'};
$self->{labels}{namespace} = $namespace if $namespace;
Expand All @@ -48,6 +48,11 @@ sub new {

}

sub warn_not_reporting {
my $self = shift;
warn("Push gateway base URL is not set; not reporting (set PUSHGATEWAY env var)");
}

sub label_names {
my $self = shift;

Expand Down Expand Up @@ -107,6 +112,11 @@ sub finalize {
sub push_metrics {
my $self = shift;

if(!$self->{pushgateway}) {
$self->warn_not_reporting;
return;
}

my $job = $self->{job};
my $url = $self->{pushgateway} . "/metrics/job/$job";
my $data = $self->{prom}->render;
Expand Down
14 changes: 12 additions & 2 deletions t/progress_tracker.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use Test::Spec;
use Test::Exception;
use Test::Time;
use Test::Warn;
use ProgressTracker;
use LWP::UserAgent;

Expand Down Expand Up @@ -32,10 +33,10 @@ describe "ProgressTracker" => sub {
ok($tracker);
};

it "requires pushgateway env var is set" => sub {
it "warns if pushgateway env var is not set" => sub {
my $old_gateway = $ENV{PUSHGATEWAY};
delete $ENV{PUSHGATEWAY};
dies_ok { ProgressTracker->new() };
warning_like { ProgressTracker->new() } qr/not reporting.*PUSHGATEWAY/;

$ENV{PUSHGATEWAY} = $old_gateway;
};
Expand Down Expand Up @@ -187,6 +188,15 @@ describe "ProgressTracker" => sub {
ok(metrics !~ /^job_last_success/m);
};

it "warns if pushgateway env var is not set" => sub {
my $old_gateway = $ENV{PUSHGATEWAY};
delete $ENV{PUSHGATEWAY};
my $tracker = ProgressTracker->new();
warning_like { $tracker->update_metrics; } qr/not reporting.*PUSHGATEWAY/;

$ENV{PUSHGATEWAY} = $old_gateway;
}

};

context "without stage" => sub {
Expand Down
Loading