private async getMetricFromMonitoringApi()

in projects/alloydb-autoscaler/src/autoscaler-core/poller/monitoring-metrics-reader.ts [107:145]


  private async getMetricFromMonitoringApi(
    metricDefinition: MonitoringMetricDefinition
  ): Promise<[number, string]> {
    this.logger.debug(
      `Getting metric ${metricDefinition.name} ` +
        `from ${this.instance.info.resourcePath}.`
    );

    const metricResponses =
      await this.metricsClient.listTimeSeries(metricDefinition);

    const resources = metricResponses[0];
    let maxValue = 0;
    let maxLocation = 'global';

    for (const resource of resources) {
      const dataPoints = resource?.points;
      if (!dataPoints || dataPoints.length === 0) {
        throw new Error(
          `No data points found for metric ${metricDefinition.name}`
        );
      }
      for (const point of dataPoints) {
        const value = this.getValueFromPoint(point);
        if (value === null) {
          throw new Error(
            `No value for point in metric ${metricDefinition.name}`
          );
        }
        if (value < maxValue) continue;
        maxValue = value;
        if (resource.resource?.labels?.location) {
          maxLocation = resource.resource.labels.location;
        }
      }
    }

    return [maxValue, maxLocation];
  }