func()

in custom-metrics-stackdriver-adapter/pkg/adapter/provider/provider.go [326:375]


func (p *StackdriverProvider) GetExternalMetric(ctx context.Context, namespace string, metricSelector labels.Selector, info provider.ExternalMetricInfo) (*external_metrics.ExternalMetricValueList, error) {
	if p.externalMetricsCache != nil {
		key := cacheKey{
			namespace:      namespace,
			metricSelector: metricSelector.String(),
			info:           info,
		}
		if cachedValue, ok := p.externalMetricsCache.get(key); ok {
			return cachedValue, nil
		}
	}

	// Proceed to do a fresh fetch for metrics since one of the following is true at this point
	// a) externalMetricCache is disabled
	// b) the key was never added to the cache
	// c) the key was in the cache, but its corrupt or its TTL has expired
	metricNameEscaped := info.Metric
	metricName := getExternalMetricName(metricNameEscaped)
	metricKind, metricValueType, err := p.translator.GetMetricKind(metricName, metricSelector)
	if err != nil {
		return nil, err
	}
	stackdriverRequest, err := p.translator.GetExternalMetricRequest(metricName, metricKind, metricValueType, metricSelector)
	if err != nil {
		return nil, err
	}
	stackdriverResponse, err := stackdriverRequest.Do()
	if err != nil {
		return nil, err
	}
	externalMetricItems, err := p.translator.GetRespForExternalMetric(stackdriverResponse, metricNameEscaped)
	if err != nil {
		return nil, err
	}

	resp := &external_metrics.ExternalMetricValueList{
		Items: externalMetricItems,
	}

	if p.externalMetricsCache != nil {
		key := cacheKey{
			namespace:      namespace,
			metricSelector: metricSelector.String(),
			info:           info,
		}
		p.externalMetricsCache.add(key, resp)
	}

	return resp, nil
}