Make the scrape.metricMetadataStore interface public

To test the implementation of our metric metadata API, we need to represent various states of metadata in the scrape metadata store. That is currently not possible as the interface and method to set the store are private.

This changes the interface, list and get methods, and the SetMetadaStore function to be public.

Incidentally, the scrapeCache implementation needs to be renamed to match the new signature.

Signed-off-by: gotjosh <josue@grafana.com>
This commit is contained in:
gotjosh 2019-12-04 15:18:27 +00:00
parent 4bf9c6bb82
commit 05842176a6
3 changed files with 13 additions and 13 deletions

View file

@ -209,7 +209,7 @@ func newScrapePool(cfg *config.ScrapeConfig, app Appendable, jitterSeed uint64,
sp.newLoop = func(opts scrapeLoopOptions) loop { sp.newLoop = func(opts scrapeLoopOptions) loop {
// Update the targets retrieval function for metadata to a new scrape cache. // Update the targets retrieval function for metadata to a new scrape cache.
cache := newScrapeCache() cache := newScrapeCache()
opts.target.setMetadataStore(cache) opts.target.SetMetadataStore(cache)
return newScrapeLoop( return newScrapeLoop(
ctx, ctx,
@ -794,7 +794,7 @@ func (c *scrapeCache) setUnit(metric, unit []byte) {
c.metaMtx.Unlock() c.metaMtx.Unlock()
} }
func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) { func (c *scrapeCache) GetMetadata(metric string) (MetricMetadata, bool) {
c.metaMtx.Lock() c.metaMtx.Lock()
defer c.metaMtx.Unlock() defer c.metaMtx.Unlock()
@ -810,7 +810,7 @@ func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) {
}, true }, true
} }
func (c *scrapeCache) listMetadata() []MetricMetadata { func (c *scrapeCache) ListMetadata() []MetricMetadata {
c.metaMtx.Lock() c.metaMtx.Lock()
defer c.metaMtx.Unlock() defer c.metaMtx.Unlock()

View file

@ -615,19 +615,19 @@ test_metric 1
testutil.Ok(t, err) testutil.Ok(t, err)
testutil.Equals(t, 1, total) testutil.Equals(t, 1, total)
md, ok := cache.getMetadata("test_metric") md, ok := cache.GetMetadata("test_metric")
testutil.Assert(t, ok, "expected metadata to be present") testutil.Assert(t, ok, "expected metadata to be present")
testutil.Assert(t, textparse.MetricTypeCounter == md.Type, "unexpected metric type") testutil.Assert(t, textparse.MetricTypeCounter == md.Type, "unexpected metric type")
testutil.Equals(t, "some help text", md.Help) testutil.Equals(t, "some help text", md.Help)
testutil.Equals(t, "metric", md.Unit) testutil.Equals(t, "metric", md.Unit)
md, ok = cache.getMetadata("test_metric_no_help") md, ok = cache.GetMetadata("test_metric_no_help")
testutil.Assert(t, ok, "expected metadata to be present") testutil.Assert(t, ok, "expected metadata to be present")
testutil.Assert(t, textparse.MetricTypeGauge == md.Type, "unexpected metric type") testutil.Assert(t, textparse.MetricTypeGauge == md.Type, "unexpected metric type")
testutil.Equals(t, "", md.Help) testutil.Equals(t, "", md.Help)
testutil.Equals(t, "", md.Unit) testutil.Equals(t, "", md.Unit)
md, ok = cache.getMetadata("test_metric_no_type") md, ok = cache.GetMetadata("test_metric_no_type")
testutil.Assert(t, ok, "expected metadata to be present") testutil.Assert(t, ok, "expected metadata to be present")
testutil.Assert(t, textparse.MetricTypeUnknown == md.Type, "unexpected metric type") testutil.Assert(t, textparse.MetricTypeUnknown == md.Type, "unexpected metric type")
testutil.Equals(t, "other help text", md.Help) testutil.Equals(t, "other help text", md.Help)

View file

@ -58,7 +58,7 @@ type Target struct {
lastScrape time.Time lastScrape time.Time
lastScrapeDuration time.Duration lastScrapeDuration time.Duration
health TargetHealth health TargetHealth
metadata metricMetadataStore metadata MetricMetadataStore
} }
// NewTarget creates a reasonably configured target for querying. // NewTarget creates a reasonably configured target for querying.
@ -75,9 +75,9 @@ func (t *Target) String() string {
return t.URL().String() return t.URL().String()
} }
type metricMetadataStore interface { type MetricMetadataStore interface {
listMetadata() []MetricMetadata ListMetadata() []MetricMetadata
getMetadata(metric string) (MetricMetadata, bool) GetMetadata(metric string) (MetricMetadata, bool)
} }
// MetricMetadata is a piece of metadata for a metric. // MetricMetadata is a piece of metadata for a metric.
@ -95,7 +95,7 @@ func (t *Target) MetadataList() []MetricMetadata {
if t.metadata == nil { if t.metadata == nil {
return nil return nil
} }
return t.metadata.listMetadata() return t.metadata.ListMetadata()
} }
// Metadata returns type and help metadata for the given metric. // Metadata returns type and help metadata for the given metric.
@ -106,10 +106,10 @@ func (t *Target) Metadata(metric string) (MetricMetadata, bool) {
if t.metadata == nil { if t.metadata == nil {
return MetricMetadata{}, false return MetricMetadata{}, false
} }
return t.metadata.getMetadata(metric) return t.metadata.GetMetadata(metric)
} }
func (t *Target) setMetadataStore(s metricMetadataStore) { func (t *Target) SetMetadataStore(s MetricMetadataStore) {
t.mtx.Lock() t.mtx.Lock()
defer t.mtx.Unlock() defer t.mtx.Unlock()
t.metadata = s t.metadata = s