private getMetricsForScaling()

in projects/alloydb-autoscaler/src/autoscaler-core/scaler/scaling-methods/linear-method.ts [76:148]


  private getMetricsForScaling(
    scalingDirection: AutoscalerScalingDirection,
    engineAnalysis: RulesEngineAnalysis | null,
    instanceLogger: pino.Logger
  ): ScalingMetric[] {
    if (!engineAnalysis) return [];

    const matchedConditions =
      engineAnalysis.matchedConditions[scalingDirection];
    if (!matchedConditions) return [];

    const scalingMetrics = engineAnalysis.scalingMetrics[scalingDirection];
    if (!scalingMetrics) return [];

    return matchedConditions
      .filter(matchedCondition => {
        const metricName = matchedCondition.fact;
        return scalingMetrics.has(metricName);
      })
      .map(matchedCondition => {
        return {
          name: matchedCondition.fact,
          value: matchedCondition.factResult,
          threshold: matchedCondition.value,
        } as ScalingMetric;
      })
      .filter(scalingMetric => {
        // This should not happen since the rules engine won't be able to
        // trigger this rule if the metric (fact) is not defined. Adding for
        // safety nonetheless.
        if (scalingMetric.value === null || scalingMetric.value === undefined) {
          instanceLogger.error({
            message:
              'Unable to use this metric for linear scaling. ' +
              `No value for metric ${scalingMetric.name} on the cluster. ` +
              'Consider removing this metric from scalingMetrics or adding a ' +
              'value to the condition for the fact with this name.',
          });
          return false;
        }

        // This should not happen since the rules engine won't be able to
        // trigger this rule if there is not threshold (condition.value).
        if (typeof scalingMetric.threshold !== 'number') {
          instanceLogger.error({
            message:
              'Unable to use this metric for linear scaling. ' +
              `No numeric threshold value for ${scalingMetric.name}. ` +
              'Consider removing this metric from scalingMetrics or adding a ' +
              'numeric value to the condition for the fact with this name. ' +
              'If a value is already added, ensure it is a number (numeric ' +
              'type).',
          });
          return false;
        }

        if (scalingMetric.threshold === 0) {
          instanceLogger.error({
            message:
              'Unable to use this metric for linear scaling. ' +
              `The threshold value for ${scalingMetric.name} is 0. Linear ` +
              'scaling uses threshold value as part of the cross ' +
              'multiplication to calculate the size and it is not possible ' +
              'to divide by 0. Consider removing this metric from ' +
              'scalingMetrics or adding a value other than 0 to the ' +
              'condition for the fact with this name.',
          });
          return false;
        }

        return true;
      });
  }