From 43c0a8c85446c38f23c82f1ffc0fcbfaef109c10 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Thu, 6 May 2021 12:45:20 +0000 Subject: [PATCH] Fix cwl-metrics extension --- .../cwl-metrics/bin/sendMetricsFailureLog | 53 +++++++++++++++++++ lib/ep3/extensions/cwl-metrics/extension.yml | 10 +++- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100755 lib/ep3/extensions/cwl-metrics/bin/sendMetricsFailureLog diff --git a/lib/ep3/extensions/cwl-metrics/bin/sendMetricsFailureLog b/lib/ep3/extensions/cwl-metrics/bin/sendMetricsFailureLog new file mode 100755 index 0000000..e4f2289 --- /dev/null +++ b/lib/ep3/extensions/cwl-metrics/bin/sendMetricsFailureLog @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby +require 'json' +require 'optparse' + +if $0 == __FILE__ + opt = OptionParser.new + opt.banner = "#{$0} [options] " + + opt.parse!(ARGV) + unless ARGV.length == 4 + puts opt.help + exit + end + + resfile, errfile, tag, interrupted = *ARGV + + unless File.exist? resfile + raise "File not found: #{resfile}" + end + res_str = open(resfile).read + begin + response = JSON.load(res_str) + message = 'Failed to send metrics' + rescue JSON::ParserError + response = res_str + message = 'Invalid response from metrics server' + end + + unless File.exist? errfile + raise "File not found: #{errfile}" + end + err = open(errfile).read + + level = if interrupted == 'true' + 'warning' + else + 'error' + end + + ret = { + 'log-level' => level, + 'interrupted' => interrupted == 'true', + 'tag' => tag, + 'message' => message, + 'response' => response, + 'stderr' => err, + 'metrics_server' => ENV['ES_HOST'], + 'metrics_port' => ENV['ES_PORT'], + } + + puts JSON.dump(ret) +end + diff --git a/lib/ep3/extensions/cwl-metrics/extension.yml b/lib/ep3/extensions/cwl-metrics/extension.yml index 56b68c9..28d971e 100644 --- a/lib/ep3/extensions/cwl-metrics/extension.yml +++ b/lib/ep3/extensions/cwl-metrics/extension.yml @@ -39,6 +39,8 @@ hooks: out: - place: uuid pattern: ~(newfile) + - place: es_response + pattern: ~(newfile) command: | uuid=$(uuidgen) echo $uuid > ~(out.uuid) @@ -47,7 +49,11 @@ hooks: -H "Content-Type: application/json" \ -X POST \ "${ES_HOST}:${ES_PORT}/workflow/_doc/${uuid}?pretty" \ - --data-binary @"~(in.cwl.metrics.json)" > /dev/null + --data-binary @"~(in.cwl.metrics.json)" > ~(out.es_response) + result=$(jq -r .result ~(out.es_response)) + test "$result" = 'created' + log: + failure: sendMetricsFailureLog ~(tr.newfile.es_response) ~(tr.stderr) ~(tag) ~(interrupted) transitions: - name: prepare-metrics-directory type: shell @@ -246,7 +252,7 @@ hooks: - place: cid pattern: ~(in.cid) command: | - mem=$(docker info 2> /dev/null | grep "Total Memory" | awk '{ print $3 }') + mem=$(docker info | grep "Total Memory" | awk '{ print $3 }') docker info --format '{{json .}}' | jq "{ running_containers: .ContainersRunning, server_version: .ServerVersion, storage_driver: .Driver, number_of_cpu: .NCPU, total_memory: \"$mem\" }" > ~(out.metrics.runtime.json) cid=$(cat ~(in.cid)) docker inspect $cid | jq "{ id: \"$cid\", image: .[0].Config.Image, cmd: .[0].Config.Cmd | join(\" \"), status: .[0].State.Status, start_time: .[0].State.StartedAt, end_time: .[0].State.FinishedAt, exit_code: .[0].State.ExitCode }" > ~(out.metrics.process.json)