MCPcopy
hub / github.com/grafana/grafana / GetMetrics

Method GetMetrics

apps/dashvalidator/pkg/cache/cache.go:84–136  ·  view source on GitHub ↗

GetMetrics fetches metrics from cache or delegates to the appropriate provider. The provider is looked up from the registered providers by datasource type. On cache hit (non-expired entry), returns cached metrics immediately. On cache miss or expiration, fetches from provider and caches the result.

(ctx context.Context, orgID int64, dsType, datasourceUID, datasourceURL string,
	client *http.Client)

Source from the content-addressed store, hash-verified

82// On cache hit (non-expired entry), returns cached metrics immediately.
83// On cache miss or expiration, fetches from provider and caches the result.
84func (c *MetricsCache) GetMetrics(ctx context.Context, orgID int64, dsType, datasourceUID, datasourceURL string,
85 client *http.Client) ([]string, error) {
86 cacheKey := fmt.Sprintf("%d/%s", orgID, datasourceUID)
87
88 // Check cache first (read lock)
89 c.mu.RLock()
90 cached, exists := c.entries[cacheKey]
91 provider := c.providers[dsType]
92 c.mu.RUnlock()
93
94 if exists && time.Now().Before(cached.expiresAt) {
95 return cached.metrics, nil
96 }
97
98 // Verify provider exists
99 if provider == nil {
100 return nil, fmt.Errorf("no metrics provider registered for datasource type: %s", dsType)
101 }
102
103 // Cache miss or expired — use singleflight to coalesce concurrent fetches
104 v, err, _ := c.sf.Do(cacheKey, func() (any, error) {
105 // Re-check cache: another goroutine may have populated it while we waited
106 c.mu.RLock()
107 cached, exists := c.entries[cacheKey]
108 c.mu.RUnlock()
109 if exists && time.Now().Before(cached.expiresAt) {
110 return cached.metrics, nil
111 }
112
113 result, err := provider.GetMetrics(ctx, datasourceUID, datasourceURL, client)
114 if err != nil {
115 return nil, err
116 }
117
118 // Don't cache results with zero TTL
119 if result.TTL > 0 {
120 c.mu.Lock()
121 c.entries[cacheKey] = &cacheEntry{
122 metrics: result.Metrics,
123 metricsSet: toMetricsSet(result.Metrics),
124 expiresAt: time.Now().Add(result.TTL),
125 }
126 c.mu.Unlock()
127 }
128
129 return result.Metrics, nil
130 })
131 if err != nil {
132 return nil, err
133 }
134
135 return v.([]string), nil
136}
137
138// GetMetricsSet returns the cached metrics as a set (map[string]bool) for O(1) lookup.
139// This avoids rebuilding the set from []string on every call — critical when the

Callers 2

GetMetricsSetMethod · 0.95

Calls 9

toMetricsSetFunction · 0.85
BeforeMethod · 0.65
NowMethod · 0.65
ErrorfMethod · 0.65
DoMethod · 0.65
GetMetricsMethod · 0.65
LockMethod · 0.65
AddMethod · 0.65
UnlockMethod · 0.65

Tested by 1