From 9eeb28c9c51b8b39a03c92021cde1b43ac9be697 Mon Sep 17 00:00:00 2001 From: Aaron Elkiss Date: Tue, 5 Nov 2024 08:26:36 -0500 Subject: [PATCH 1/2] Warn if PUSHGATEWAY is not provided Previously, if PUSHGATEWAY was not set then the constructor would die; this made it difficult to run in test environments where we did not want to report. --- Dockerfile | 1 + Makefile.PL | 1 + README.md | 5 +++++ lib/ProgressTracker.pm | 12 +++++++++++- t/progress_tracker.t | 14 ++++++++++++-- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index bddea27..fd38f83 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Makefile.PL b/Makefile.PL index 506bcff..b404252 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -11,6 +11,7 @@ WriteMakefile( TEST_REQUIRES => { 'Test::Spec' => 0, 'Test::Time' => 0, + 'Test::Warn' => 0, 'LWP::UserAgent' => 0, }, CONFIGURE_REQUIRES => { "ExtUtils::MakeMaker" => 0 }, diff --git a/README.md b/README.md index d66caf6..fa401ae 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/ProgressTracker.pm b/lib/ProgressTracker.pm index a79720c..d410e01 100644 --- a/lib/ProgressTracker.pm +++ b/lib/ProgressTracker.pm @@ -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; @@ -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; @@ -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; diff --git a/t/progress_tracker.t b/t/progress_tracker.t index fb31f20..1fbcd3d 100644 --- a/t/progress_tracker.t +++ b/t/progress_tracker.t @@ -1,6 +1,7 @@ use Test::Spec; use Test::Exception; use Test::Time; +use Test::Warn; use ProgressTracker; use LWP::UserAgent; @@ -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; }; @@ -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 { From daaca0ba3448e18e166d49248001d97e67af6cd4 Mon Sep 17 00:00:00 2001 From: Aaron Elkiss Date: Tue, 5 Nov 2024 09:31:24 -0500 Subject: [PATCH 2/2] Update docker-compose to latest version * use healthcheck instead of wait-for * docker-compose -> docker compose --- .github/workflows/ci.yml | 4 +- bin/test_and_cover.sh | 4 -- bin/wait-for | 118 --------------------------------------- docker-compose.yml | 19 +++++-- 4 files changed, 16 insertions(+), 129 deletions(-) delete mode 100755 bin/test_and_cover.sh delete mode 100755 bin/wait-for diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b14f6eb..46c0f87 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} diff --git a/bin/test_and_cover.sh b/bin/test_and_cover.sh deleted file mode 100755 index 000cdbd..0000000 --- a/bin/test_and_cover.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -bin/wait-for --timeout=300 pushgateway:9091 -cover -test -report Coveralls diff --git a/bin/wait-for b/bin/wait-for deleted file mode 100755 index 9fd63d8..0000000 --- a/bin/wait-for +++ /dev/null @@ -1,118 +0,0 @@ -#!/bin/bash - -# From https://github.com/eficode/wait-for - -# The MIT License (MIT) -# -# Copyright (c) 2017 Eficode Oy -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -TIMEOUT=15 -QUIET=0 - -echoerr() { - if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi -} - -usage() { - exitcode="$1" - cat << USAGE >&2 -Usage: - $cmdname host:port [host:port ...] [-t timeout] [-- command args] - -q | --quiet Do not output any status messages - -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit "$exitcode" -} - -wait_for() { - command="$*" - for i in `seq $TIMEOUT` ; do - - result=0 - - for dep in $DEPENDENCIES; do - host=$(printf "%s\n" "$dep"| cut -d : -f 1) - port=$(printf "%s\n" "$dep"| cut -d : -f 2) - if [ "$host" = "" -o "$port" = "" ]; then - echoerr "Error: you need to provide a host and port to test." - usage 2 - fi - nc -z "$host" "$port" > /dev/null 2>&1 - nc_result=$? - if [ $nc_result -ne 0 ] ; then - result=1 - fi - done - - if [ $result -eq 0 ] ; then - if [ -n "$command" ] ; then - exec $command - fi - exit 0 - fi - sleep 1 - done - echo "Operation timed out" >&2 - exit 1 -} - -DEPENDENCIES="" -while [ $# -gt 0 ] -do - case "$1" in - *:* ) - DEPENDENCIES+=" $1" - shift 1 - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -t) - TIMEOUT="$2" - if [ "$TIMEOUT" = "" ]; then break; fi - shift 2 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - break - ;; - --help) - usage 0 - ;; - *) - echoerr "Unknown argument: $1" - usage 1 - ;; - esac -done - -if [ "${#DEPENDENCIES[@]}" -eq "0" ]; then - echoerr "Error: you need to provide a host and port to test." - usage 2 -fi - -wait_for "$@" diff --git a/docker-compose.yml b/docker-compose.yml index 502d323..73e1214 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3' +--- services: @@ -6,11 +6,12 @@ services: 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: . @@ -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