88
99 "github.com/prometheus/common/model"
1010 "github.com/prometheus/prometheus/scrape"
11-
12- fakemetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal/metadata"
1311)
1412
1513func TestMetadataForMetric_Internal (t * testing.T ) {
@@ -27,7 +25,7 @@ func TestMetadataForMetric_Internal(t *testing.T) {
2725}
2826
2927func TestMetadataForMetric_ExternalExactHit (t * testing.T ) {
30- store := fakemetadata . NewFakeMetadataStore (map [string ]scrape.MetricMetadata {
28+ store := newFakeMetadataStore (map [string ]scrape.MetricMetadata {
3129 "http_requests_total" : {
3230 MetricFamily : "http_requests_total" ,
3331 Type : model .MetricTypeCounter ,
@@ -46,7 +44,7 @@ func TestMetadataForMetric_ExternalExactHit(t *testing.T) {
4644
4745func TestMetadataForMetric_NormalizedFallback_Gauge (t * testing.T ) {
4846 // Simulate a merged metric like "histogram_count" where store only knows the base name.
49- store := fakemetadata . NewFakeMetadataStore (map [string ]scrape.MetricMetadata {
47+ store := newFakeMetadataStore (map [string ]scrape.MetricMetadata {
5048 "histogram" : {
5149 MetricFamily : "histogram" ,
5250 Type : model .MetricTypeGauge ,
@@ -68,7 +66,7 @@ func TestMetadataForMetric_NormalizedFallback_Gauge(t *testing.T) {
6866
6967func TestMetadataForMetric_NormalizedFallback_CounterKeepsOriginal (t * testing.T ) {
7068 // If normalized metadata type is Counter, resolved should stay the original name.
71- store := fakemetadata . NewFakeMetadataStore (map [string ]scrape.MetricMetadata {
69+ store := newFakeMetadataStore (map [string ]scrape.MetricMetadata {
7270 "requests" : {
7371 MetricFamily : "requests" ,
7472 Type : model .MetricTypeCounter ,
@@ -106,7 +104,7 @@ func TestMetadataForMetric_Unknown(t *testing.T) {
106104
107105func TestMetadataForMetric_PrefersInternalOverExternal (t * testing.T ) {
108106 // Ensure internal metric metadata is used even if external store provides a conflicting entry.
109- store := fakemetadata . NewFakeMetadataStore (map [string ]scrape.MetricMetadata {
107+ store := newFakeMetadataStore (map [string ]scrape.MetricMetadata {
110108 scrapeUpMetricName : {
111109 MetricFamily : "up_external" ,
112110 Type : model .MetricTypeCounter ,
@@ -126,3 +124,40 @@ func TestMetadataForMetric_PrefersInternalOverExternal(t *testing.T) {
126124 t .Fatalf ("expected family %q from internal map, got %q" , scrapeUpMetricName , m .MetricFamily )
127125 }
128126}
127+
128+ // fakeMetadataStore implements scrape.MetricMetadataStore for tests.
129+ // It is safe to use from other packages' tests
130+ type fakeMetadataStore struct {
131+ data map [string ]scrape.MetricMetadata
132+ }
133+
134+ // newFakeMetadataStore creates a FakeMetadataStore initialized with the given metadata.
135+ func newFakeMetadataStore (init map [string ]scrape.MetricMetadata ) * fakeMetadataStore {
136+ // copy defensively to avoid external mutation
137+ cp := make (map [string ]scrape.MetricMetadata , len (init ))
138+ for k , v := range init {
139+ cp [k ] = v
140+ }
141+ return & fakeMetadataStore {data : cp }
142+ }
143+
144+ func (f fakeMetadataStore ) ListMetadata () []scrape.MetricMetadata {
145+ out := make ([]scrape.MetricMetadata , 0 , len (f .data ))
146+ for _ , m := range f .data {
147+ out = append (out , m )
148+ }
149+ return out
150+ }
151+
152+ func (f fakeMetadataStore ) GetMetadata (name string ) (scrape.MetricMetadata , bool ) {
153+ m , ok := f .data [name ]
154+ return m , ok
155+ }
156+
157+ func (f fakeMetadataStore ) SizeMetadata () int {
158+ return len (f .data )
159+ }
160+
161+ func (f fakeMetadataStore ) LengthMetadata () int {
162+ return len (f .data )
163+ }
0 commit comments