Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions receiver/prometheusreceiver/internal/metadata/fakemetadata.go

This file was deleted.

47 changes: 41 additions & 6 deletions receiver/prometheusreceiver/internal/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/scrape"

fakemetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal/metadata"
)

func TestMetadataForMetric_Internal(t *testing.T) {
Expand All @@ -27,7 +25,7 @@ func TestMetadataForMetric_Internal(t *testing.T) {
}

func TestMetadataForMetric_ExternalExactHit(t *testing.T) {
store := fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
store := newFakeMetadataStore(map[string]scrape.MetricMetadata{
"http_requests_total": {
MetricFamily: "http_requests_total",
Type: model.MetricTypeCounter,
Expand All @@ -46,7 +44,7 @@ func TestMetadataForMetric_ExternalExactHit(t *testing.T) {

func TestMetadataForMetric_NormalizedFallback_Gauge(t *testing.T) {
// Simulate a merged metric like "histogram_count" where store only knows the base name.
store := fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
store := newFakeMetadataStore(map[string]scrape.MetricMetadata{
"histogram": {
MetricFamily: "histogram",
Type: model.MetricTypeGauge,
Expand All @@ -68,7 +66,7 @@ func TestMetadataForMetric_NormalizedFallback_Gauge(t *testing.T) {

func TestMetadataForMetric_NormalizedFallback_CounterKeepsOriginal(t *testing.T) {
// If normalized metadata type is Counter, resolved should stay the original name.
store := fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
store := newFakeMetadataStore(map[string]scrape.MetricMetadata{
"requests": {
MetricFamily: "requests",
Type: model.MetricTypeCounter,
Expand Down Expand Up @@ -106,7 +104,7 @@ func TestMetadataForMetric_Unknown(t *testing.T) {

func TestMetadataForMetric_PrefersInternalOverExternal(t *testing.T) {
// Ensure internal metric metadata is used even if external store provides a conflicting entry.
store := fakemetadata.NewFakeMetadataStore(map[string]scrape.MetricMetadata{
store := newFakeMetadataStore(map[string]scrape.MetricMetadata{
scrapeUpMetricName: {
MetricFamily: "up_external",
Type: model.MetricTypeCounter,
Expand All @@ -126,3 +124,40 @@ func TestMetadataForMetric_PrefersInternalOverExternal(t *testing.T) {
t.Fatalf("expected family %q from internal map, got %q", scrapeUpMetricName, m.MetricFamily)
}
}

// fakeMetadataStore implements scrape.MetricMetadataStore for tests.
// It is safe to use from other packages' tests
type fakeMetadataStore struct {
data map[string]scrape.MetricMetadata
}

// newFakeMetadataStore creates a FakeMetadataStore initialized with the given metadata.
func newFakeMetadataStore(init map[string]scrape.MetricMetadata) *fakeMetadataStore {
// copy defensively to avoid external mutation
cp := make(map[string]scrape.MetricMetadata, len(init))
for k, v := range init {
cp[k] = v
}
return &fakeMetadataStore{data: cp}
}

func (f fakeMetadataStore) ListMetadata() []scrape.MetricMetadata {
out := make([]scrape.MetricMetadata, 0, len(f.data))
for _, m := range f.data {
out = append(out, m)
}
return out
}

func (f fakeMetadataStore) GetMetadata(name string) (scrape.MetricMetadata, bool) {
m, ok := f.data[name]
return m, ok
}

func (f fakeMetadataStore) SizeMetadata() int {
return len(f.data)
}

func (f fakeMetadataStore) LengthMetadata() int {
return len(f.data)
}
7 changes: 3 additions & 4 deletions receiver/prometheusreceiver/internal/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"go.uber.org/zap/zaptest/observer"

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

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

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

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