Skip to content

Commit

Permalink
change get_instance back to new
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarin committed Jul 24, 2024
1 parent 75b7ae1 commit de8df11
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 30 deletions.
28 changes: 13 additions & 15 deletions bin/metrics_exporter.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
# Metrics exporter for prometheus statistics gathered via ingest.
# Usage: plackup -p 9090 ./bin/metrics_exporter.pl

my $metrics = HTFeed::JobMetrics->get_instance();
my $metrics = HTFeed::JobMetrics->new();

my $app = sub {
my $env = shift;
my $env = shift;

my $rendered = $metrics->pretty();
my $rendered = $metrics->pretty();
my $response = Plack::Response->new();
$response->content_type('text/plain');

my $response = Plack::Response->new();
$response->content_type('text/plain');

if ($rendered) {
$response->status(200);
$response->body($rendered);
} else {
$response->status(500);
$response->body("# metrics rendering failed");
}

return $response->finalize;
if ($rendered) {
$response->status(200);
$response->body($rendered);
} else {
$response->status(500);
$response->body("# metrics rendering failed");
}

return $response->finalize;
};
12 changes: 4 additions & 8 deletions lib/HTFeed/JobMetrics.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ the various stages of an ingest job, such as number of items downloaded,
number of bytes downloaded and amount of time spent downloading
(as well as the other job stages, not just download).
This class implements the singleton pattern, and as such has a
get_instance() method instead of a "normal" new() function.
The singleton instance is stored in the aptly named
$singleton variable.
This class implements the singleton pattern. The singleton instance is
stored in the aptly named $singleton variable.
CLI provided by and documented in JobMetricsCLI.pm.
Expand All @@ -40,9 +37,8 @@ CLI provided by and documented in JobMetricsCLI.pm.

my $singleton = undef;

# This class implements the singleton pattern.
# Call get_instance instead of new.
sub get_instance {
# This class implements the singleton pattern (but constructor is still new()).
sub new {
# Re-use singleton if defined.
if (!defined $singleton) {
my $class = shift;
Expand Down
2 changes: 1 addition & 1 deletion lib/HTFeed/JobMetricsCLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Note that histograms must be set up with buckets ahead of time.
=cut

my $job_metrics = HTFeed::JobMetrics->get_instance();
my $job_metrics = HTFeed::JobMetrics->new();

if (!caller) {
GetOptions(
Expand Down
2 changes: 1 addition & 1 deletion lib/HTFeed/QueueRunner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sub new {
# for testing
$self->{should_fork} = $params{should_fork};
$self->{should_fork} = 1 if not defined $self->{should_fork};
$self->{job_metrics} = HTFeed::JobMetrics->get_instance;
$self->{job_metrics} = HTFeed::JobMetrics->new;
set_config($self->{staging_root}, 'staging_root');
HTFeed::StagingSetup::make_stage();

Expand Down
2 changes: 1 addition & 1 deletion lib/HTFeed/Stage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sub new {
@_,
has_run => 0,
failed => 0,
job_metrics => HTFeed::JobMetrics->get_instance
job_metrics => HTFeed::JobMetrics->new
};

unless ( $self->{volume} && $self->{volume}->isa("HTFeed::Volume") ) {
Expand Down
3 changes: 2 additions & 1 deletion lib/HTFeed/Stage/Unpack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ sub _extract_file {
my $infile = shift;
my $outdir = shift;
my $otheroptions = shift || '';
my $job_metrics = HTFeed::JobMetrics->get_instance;

my $job_metrics = HTFeed::JobMetrics->new;
my $start_time = $job_metrics->time;

my $full_command = sprintf($command, $infile, $outdir, $otheroptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/HTFeed/Storage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ sub new {
did_encryption => 0,
name => $name,
timestamp => $timestamp,
job_metrics => HTFeed::JobMetrics->get_instance(),
job_metrics => HTFeed::JobMetrics->new(),
};
bless($self, $class);

Expand Down
4 changes: 2 additions & 2 deletions t/job_metrics.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use HTFeed::JobMetrics;

describe "HTFeed::JobMetrics" => sub {
# SETUP start
my $jm = HTFeed::JobMetrics->get_instance;
my $jm = HTFeed::JobMetrics->new;
my $item_metric = "ingest_pack_items_total";
my $byte_metric = "ingest_pack_bytes_w_total";
my $invalid_metric = "not a valid metric";
Expand Down Expand Up @@ -75,7 +75,7 @@ describe "HTFeed::JobMetrics" => sub {
# match($item_metric).
my $expected_count = 5;
# Get 5 jm instances
my @jms = (map { HTFeed::JobMetrics->get_instance } (1..5));
my @jms = (map { HTFeed::JobMetrics->new } (1..5));
foreach my $sequence_jm (@jms) {
$sequence_jm->inc($item_metric);
}
Expand Down

0 comments on commit de8df11

Please sign in to comment.