Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Experiment with a singleton version for imgsrv. #36

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ RUN cpan \
Test::Exception \
Devel::Cycle \
Test::Memory::Cycle \
Mozilla::CA
Mozilla::CA \
Prometheus::Tiny::Shared

RUN mkdir /htapps
RUN mkdir /htapps/babel
Expand Down
12 changes: 4 additions & 8 deletions MdpItem.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use Context;
use Auth::Auth;
use Identifier;
use MarcMetadata;
use Metrics;
use MetsReadingOrder;
use DataTypes;

Expand Down Expand Up @@ -291,10 +292,8 @@ sub GetMdpItem {
}

my $delta = Time::HiRes::time() - $time0;
if(my $metrics = $C->{Metrics}) {
$mdpItem->{_metrics} = $C->{Metrics};
$metrics->observe("mdpitem_get_mdpitem_seconds",$delta, { cache => $cache_status });
}

Metrics->new->observe("mdpitem_get_mdpitem_seconds", $delta, { cache => $cache_status });
Utils::Logger::__Log_benchmark($C,
[["id", $id],["delta",$delta],["label","GetMdpItem"],["cache",$cache_status]], 'mdpitem');

Expand Down Expand Up @@ -1838,7 +1837,6 @@ sub GetDirPathMaybeExtract {
$fileSystemLocation,
$pattern_arr_ref,
$exclude_pattern_arr_ref,
$self->{_metrics}
);
}
else
Expand Down Expand Up @@ -1893,9 +1891,7 @@ sub GetFilePathMaybeExtract {
$self->GetId(),
$fileSystemLocation,
$fileName,
$suffix,
undef,
$self->{_metrics}
$suffix
);
}
else
Expand Down
59 changes: 59 additions & 0 deletions Metrics.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package Metrics;

use Prometheus::Tiny::Shared;

my $singleton = undef;

sub new {
# Re-use singleton if defined.
if (!defined $singleton) {
my $class = shift;
my $self = {};

$self->{prom} = Prometheus::Tiny::Shared->new(
#filename => $self->{file}
);
$self->{declared_metrics} = {};
$singleton = bless($self, $class);
}
return $singleton;
}

sub declare {
my $self = shift;
my $metric = shift;

$self->{declared_metrics}->{$metric} = 1;
$self->{prom}->declare($metric, @_);
}

sub is_declared {
my $self = shift;
my $metric = shift;

defined $self->{declared_metrics}->{$metric};
}

sub observe {
my $self = shift;
my $metric = shift;

return unless $self->is_declared($metric);
$self->{prom}->histogram_observe($metric, @_);
}

sub add {
my $self = shift;
my $metric = shift;

return unless $self->is_declared($metric);
$self->{prom}->add($metric, @_);
}

sub format {
my $self = shift;

$self->{prom}->format;
}

1;
18 changes: 8 additions & 10 deletions Utils/Extract.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Utils::Logger;
use Utils::MonitorRun;
use Identifier;
use Debug::DUtils;
use Metrics;
use Utils::Logger;

# Perl
Expand Down Expand Up @@ -204,10 +205,9 @@ sub extract_file_to_temp_cache {
my @unzip;
push @unzip, $UNZIP_PROG, "-j", "-qq", "-d", $input_cache_dir, $zip_file, "$stripped_pairtree_id/$filename";

__run(id => $id,
unzip => \@unzip,
test_filename => "$input_cache_dir/$filename",
metrics => $metrics);
__run(id => $id,
unzip => \@unzip,
test_filename => "$input_cache_dir/$filename");

return
(-e qq{$input_cache_dir/$filename})
Expand Down Expand Up @@ -292,7 +292,6 @@ sub extract_dir_to_temp_cache {
my $file_sys_location = shift;
my $patterns_arr_ref = shift;
my $exclude_patterns_arr_ref = shift;
my $metrics = shift;

my $stripped_pairtree_id = Identifier::get_pairtree_id_wo_namespace($id);
my $zip_file = $file_sys_location . qq{/$stripped_pairtree_id.zip};
Expand All @@ -307,7 +306,7 @@ sub extract_dir_to_temp_cache {
push @unzip, "-x", @$exclude_patterns_arr_ref;
}

__run(id => $id, unzip => \@unzip, metrics => $metrics);
__run(id => $id, unzip => \@unzip);

return $input_cache_dir;
}
Expand All @@ -317,7 +316,6 @@ sub __run {
my $id = $params{id};
my $unzip = $params{unzip};
my $test_filename = $params{test_filename};
my $metrics = $params{metrics};

my $error_file = Utils::get_tmp_logdir() . '/extract-error';

Expand Down Expand Up @@ -350,9 +348,9 @@ sub __run {
my $delta = Time::HiRes::time() - $time0;
# TODO: Add io stats to __Log_benchmark output?
Utils::Logger::__Log_benchmark(undef, [["id", $id],["delta",$delta],["label","__run"],["cmd",$cmd]], 'extract');

if($metrics and $test_filename) {
$metrics->observe("utils_extract_run_seconds",$delta);
if ($test_filename) {
my $metrics = Metrics->new;
$metrics->observe("utils_extract_run_seconds", $delta);
# TODO add extracted size when extracting more than one file?
$metrics->add("utils_extract_extracted_size_bytes",(-s $test_filename));
while (my ($k, $v) = each %$run_stats) {
Expand Down
Loading