enable -m <alias> / -r <alias>
This commit is contained in:
parent
b518c807d1
commit
449c1b9289
|
@ -56,5 +56,4 @@ TODO: how to write custom reporters
|
|||
- README
|
||||
- config file docs
|
||||
- load custom/contrib metrics and reporters
|
||||
- enable -m <alias> / -r <alias>
|
||||
- allow multiple reporters?
|
49
lib/main.sh
49
lib/main.sh
|
@ -30,8 +30,6 @@ main_load () {
|
|||
local filename=$(basename $file)
|
||||
local reporter=${filename%.*}
|
||||
|
||||
load_reporter_with_prefix __r_${reporter}_ $file
|
||||
|
||||
__AVAILABLE_REPORTERS=$(trim "$__AVAILABLE_REPORTERS $reporter")
|
||||
done
|
||||
|
||||
|
@ -52,35 +50,23 @@ main_init () {
|
|||
|
||||
# create temp dir
|
||||
TEMP_DIR=$(make_temp_dir)
|
||||
}
|
||||
|
||||
main_collect () {
|
||||
# check if reporter exists
|
||||
if ! in_array $__REPORTER "$__AVAILABLE_REPORTERS"; then
|
||||
if ! in_array $(resolve_reporter $__REPORTER) "$__AVAILABLE_REPORTERS"; then
|
||||
echo "Error: reporter '$__REPORTER' is not available"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# check if metrics exist
|
||||
for metric in $__METRICS; do
|
||||
metric=$(get_name $metric)
|
||||
if ! in_array $metric "$__AVAILABLE_METRICS"; then
|
||||
if ! in_array $(resolve_metric $metric) "$__AVAILABLE_METRICS"; then
|
||||
echo "Error: metric '$metric' is not available"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# init reporter
|
||||
if is_function __r_${__REPORTER}_defaults; then
|
||||
__r_${__REPORTER}_defaults
|
||||
fi
|
||||
if is_function __r_${__REPORTER}_config; then
|
||||
__r_${__REPORTER}_config
|
||||
fi
|
||||
if is_function __r_${__REPORTER}_start; then
|
||||
__r_${__REPORTER}_start
|
||||
fi
|
||||
}
|
||||
|
||||
main_collect () {
|
||||
# register trap
|
||||
trap '
|
||||
trap "" 13
|
||||
|
@ -89,15 +75,31 @@ main_collect () {
|
|||
kill -13 -$$
|
||||
' 13 INT TERM EXIT
|
||||
|
||||
# init reporter
|
||||
local reporter_alias=$(resolve_reporter $__REPORTER)
|
||||
local reporter_file=$(resolve_reporter $__REPORTER)
|
||||
load_reporter_with_prefix __r_${reporter_alias}_ ./reporters/${reporter_file}.sh
|
||||
|
||||
if is_function __r_${reporter_alias}_defaults; then
|
||||
__r_${reporter_alias}_defaults
|
||||
fi
|
||||
if is_function __r_${reporter_alias}_config; then
|
||||
__r_${reporter_alias}_config
|
||||
fi
|
||||
if is_function __r_${reporter_alias}_start; then
|
||||
__r_${reporter_alias}_start
|
||||
fi
|
||||
|
||||
# collect metrics
|
||||
for metric in $__METRICS; do
|
||||
# run in subshell to isolate scope
|
||||
(
|
||||
local metric_name=$(get_name $metric)
|
||||
local metric_alias=$(get_alias $metric)
|
||||
local metric_file=$(resolve_metric $metric)
|
||||
|
||||
# init metric
|
||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_name}.sh
|
||||
load_metric_with_prefix __m_${metric_alias}_ ./metrics/${metric_file}.sh
|
||||
|
||||
if is_function __m_${metric_alias}_defaults; then
|
||||
__m_${metric_alias}_defaults
|
||||
|
@ -135,7 +137,7 @@ main_collect () {
|
|||
_r_result="$2"
|
||||
fi
|
||||
if is_number $_r_result; then
|
||||
__r_${__REPORTER}_report $_r_label $_r_result
|
||||
__r_${reporter_alias}_report $_r_label $_r_result
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -156,9 +158,10 @@ main_collect () {
|
|||
|
||||
main_terminate () {
|
||||
# stop reporter
|
||||
verbose "Stopping reporter '${__REPORTER}'"
|
||||
if is_function __r_${__REPORTER}_stop; then
|
||||
__r_${__REPORTER}_stop
|
||||
local reporter_alias=$(get_alias $__REPORTER)
|
||||
verbose "Stopping reporter '${reporter_alias}'"
|
||||
if is_function __r_${reporter_alias}_stop; then
|
||||
__r_${reporter_alias}_stop
|
||||
fi
|
||||
|
||||
verbose "Cleaning up..."
|
||||
|
|
|
@ -105,3 +105,27 @@ get_alias () {
|
|||
fi
|
||||
echo $_alias
|
||||
}
|
||||
|
||||
resolve_reporter () {
|
||||
local _alias=$(get_alias $1)
|
||||
for reporter in $__CFG_REPORTERS; do
|
||||
local __alias=$(get_alias $reporter)
|
||||
if [ $_alias = $__alias ]; then
|
||||
echo $(get_name $reporter)
|
||||
return
|
||||
fi
|
||||
done
|
||||
echo $_alias
|
||||
}
|
||||
|
||||
resolve_metric () {
|
||||
local _alias=$(get_alias $1)
|
||||
for metric in $__CFG_METRICS; do
|
||||
local __alias=$(get_alias $metric)
|
||||
if [ $_alias = $__alias ]; then
|
||||
echo $(get_name $metric)
|
||||
return
|
||||
fi
|
||||
done
|
||||
echo $_alias
|
||||
}
|
Loading…
Reference in New Issue