From 05842176a6080cbca76e917528714d2da95c8d92 Mon Sep 17 00:00:00 2001 From: gotjosh Date: Wed, 4 Dec 2019 15:18:27 +0000 Subject: [PATCH] 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 --- scrape/scrape.go | 6 +++--- scrape/scrape_test.go | 6 +++--- scrape/target.go | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/scrape/scrape.go b/scrape/scrape.go index 3c6ecf3566..303d7974d0 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -209,7 +209,7 @@ func newScrapePool(cfg *config.ScrapeConfig, app Appendable, jitterSeed uint64, sp.newLoop = func(opts scrapeLoopOptions) loop { // Update the targets retrieval function for metadata to a new scrape cache. cache := newScrapeCache() - opts.target.setMetadataStore(cache) + opts.target.SetMetadataStore(cache) return newScrapeLoop( ctx, @@ -794,7 +794,7 @@ func (c *scrapeCache) setUnit(metric, unit []byte) { c.metaMtx.Unlock() } -func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) { +func (c *scrapeCache) GetMetadata(metric string) (MetricMetadata, bool) { c.metaMtx.Lock() defer c.metaMtx.Unlock() @@ -810,7 +810,7 @@ func (c *scrapeCache) getMetadata(metric string) (MetricMetadata, bool) { }, true } -func (c *scrapeCache) listMetadata() []MetricMetadata { +func (c *scrapeCache) ListMetadata() []MetricMetadata { c.metaMtx.Lock() defer c.metaMtx.Unlock() diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 406dd8c4cd..01788bef88 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -615,19 +615,19 @@ test_metric 1 testutil.Ok(t, err) 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, textparse.MetricTypeCounter == md.Type, "unexpected metric type") testutil.Equals(t, "some help text", md.Help) 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, textparse.MetricTypeGauge == md.Type, "unexpected metric type") testutil.Equals(t, "", md.Help) 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, textparse.MetricTypeUnknown == md.Type, "unexpected metric type") testutil.Equals(t, "other help text", md.Help) diff --git a/scrape/target.go b/scrape/target.go index d3a6a379e5..a6955d3ffd 100644 --- a/scrape/target.go +++ b/scrape/target.go @@ -58,7 +58,7 @@ type Target struct { lastScrape time.Time lastScrapeDuration time.Duration health TargetHealth - metadata metricMetadataStore + metadata MetricMetadataStore } // NewTarget creates a reasonably configured target for querying. @@ -75,9 +75,9 @@ func (t *Target) String() string { return t.URL().String() } -type metricMetadataStore interface { - listMetadata() []MetricMetadata - getMetadata(metric string) (MetricMetadata, bool) +type MetricMetadataStore interface { + ListMetadata() []MetricMetadata + GetMetadata(metric string) (MetricMetadata, bool) } // MetricMetadata is a piece of metadata for a metric. @@ -95,7 +95,7 @@ func (t *Target) MetadataList() []MetricMetadata { if t.metadata == nil { return nil } - return t.metadata.listMetadata() + return t.metadata.ListMetadata() } // 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 { 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() defer t.mtx.Unlock() t.metadata = s