Skip to content

Commit 10c1e40

Browse files
authored
[CHORE] Move testing code to test file (#44579)
In #44447 we covered more lines of code in our tests, but we did so by adding more untested code 😅 Since the new fakeMetadataStore is only used for tests, I'm moving it to a test file Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent caa9a53 commit 10c1e40

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

receiver/prometheusreceiver/internal/metadata/fakemetadata.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

receiver/prometheusreceiver/internal/metadata_test.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
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

1513
func TestMetadataForMetric_Internal(t *testing.T) {
@@ -27,7 +25,7 @@ func TestMetadataForMetric_Internal(t *testing.T) {
2725
}
2826

2927
func 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

4745
func 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

6967
func 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

107105
func 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+
}

receiver/prometheusreceiver/internal/transaction_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"go.uber.org/zap/zaptest/observer"
3232

3333
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
34-
fakemetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal/metadata"
3534
)
3635

3736
const (
@@ -2227,7 +2226,7 @@ func newObs(t *testing.T) *receiverhelper.ObsReport {
22272226
func TestDetectAndStoreNativeHistogramStaleness_NonHistogramReturnsFalse(t *testing.T) {
22282227
tr := newTxn(t, true, true)
22292228
// metadata says "gauge" → should not be considered native histogram staleness
2230-
tr.mc = fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
2229+
tr.mc = newFakeMetadataStore(map[string]scrape.MetricMetadata{
22312230
"foo": {MetricFamily: "foo", Type: model.MetricTypeGauge},
22322231
})
22332232

@@ -2241,7 +2240,7 @@ func TestDetectAndStoreNativeHistogramStaleness_NonHistogramReturnsFalse(t *test
22412240
func TestGetOrCreateMetricFamily_DistinctFamiliesForNativeVsClassic(t *testing.T) {
22422241
tr := newTxn(t, true, true)
22432242
// Provide metadata so normalization doesn't kick in; name is the same family
2244-
tr.mc = fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
2243+
tr.mc = newFakeMetadataStore(map[string]scrape.MetricMetadata{
22452244
"same_family": {MetricFamily: "same_family", Type: model.MetricTypeHistogram},
22462245
})
22472246

@@ -2329,7 +2328,7 @@ func newTxn(t *testing.T, useMetadata, enableNative bool) *transaction {
23292328
)
23302329
ctx = scrape.ContextWithTarget(ctx, target)
23312330
if useMetadata {
2332-
ctx = scrape.ContextWithMetricMetadataStore(ctx, fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{}))
2331+
ctx = scrape.ContextWithMetricMetadataStore(ctx, newFakeMetadataStore(map[string]scrape.MetricMetadata{}))
23332332
}
23342333
sink := &consumertest.MetricsSink{}
23352334
settings := receivertest.NewNopSettings(receivertest.NopType)

0 commit comments

Comments
 (0)