Repository: prometheus/prometheus
File: config/config_test.go (Go)

Code before:
		var got Config
		require.NoError(t, yaml.UnmarshalStrict(out, &got))
		require.True(t, got.OTLPConfig.PromoteAllResourceAttributes)
		require.Len(t, got.OTLPConfig.PromoteResourceAttributes, 0)
		require.Equal(t, []string{"k8s.cluster.name", "k8s.job.name", "k8s.namespace.name"}, got.OTLPConfig.IgnoreResourceAttributes)
	})

	t.Run("bad config - ignore resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_sanitize_ignore_resource_attributes.bad.yml"), false, promslog.NewNopLogger())
		require.Contains(t, err.Error(), `invalid 'ignore_resource_attributes'`)
		require.ErrorContains(t, err, `duplicated ignored OTel resource attribute "k8s.job.name"`)
		require.ErrorContains(t, err, `empty ignored OTel resource attribute`)
	})

	t.Run("bad config - conflict promote all and promote specific resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_promote_all_resource_attributes.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `'promote_all_resource_attributes' and 'promote_resource_attributes' cannot be configured simultaneously`)
	})

	t.Run("bad config - conflict promote all, promote specific resource attributes and ignore resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_promote_all_resource_attributes2.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `'promote_all_resource_attributes' and 'promote_resource_attributes' cannot be configured simultaneously`)
	})

	t.Run("bad config - conflict promote specific resource attributes and ignore resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_promote_and_ignore_resource_attributes.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `'ignore_resource_attributes' cannot be configured unless 'promote_all_resource_attributes' is true`)
	})
}

func TestOTLPAllowServiceNameInTargetInfo(t *testing.T) {
	t.Run("good config", func(t *testing.T) {
		want, err := LoadFile(filepath.Join("testdata", "otlp_allow_keep_identifying_resource_attributes.good.yml"), false, promslog.NewNopLogger())
		require.NoError(t, err)

		out, err := yaml.Marshal(want)
		require.NoError(t, err)
		var got Config
		require.NoError(t, yaml.UnmarshalStrict(out, &got))

		require.True(t, got.OTLPConfig.KeepIdentifyingResourceAttributes)
	})
}

func TestOTLPConvertHistogramsToNHCB(t *testing.T) {
	t.Run("good config", func(t *testing.T) {
		want, err := LoadFile(filepath.Join("testdata", "otlp_convert_histograms_to_nhcb.good.yml"), false, promslog.NewNopLogger())
		require.NoError(t, err)

		out, err := yaml.Marshal(want)
		require.NoError(t, err)

Review comment (suggestion):
```suggestion
	t.Run("The actual problem is configuring ignoring of resource attributes without also enabling promotion of all resource attributes.", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_ignore_resource_attributes_without_promote_all.bad.yml"), false, promslog.NewNopLogger())
```

Code after:
		var got Config
		require.NoError(t, yaml.UnmarshalStrict(out, &got))
		require.True(t, got.OTLPConfig.PromoteAllResourceAttributes)
		require.Empty(t, got.OTLPConfig.PromoteResourceAttributes)
		require.Equal(t, []string{"k8s.cluster.name", "k8s.job.name", "k8s.namespace.name"}, got.OTLPConfig.IgnoreResourceAttributes)
	})

	t.Run("bad config - ignore resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_sanitize_ignore_resource_attributes.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `invalid 'ignore_resource_attributes'`)
		require.ErrorContains(t, err, `duplicated ignored OTel resource attribute "k8s.job.name"`)
		require.ErrorContains(t, err, `empty ignored OTel resource attribute`)
	})

	t.Run("bad config - conflict between promote all and promote specific resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_promote_all_resource_attributes.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `'promote_all_resource_attributes' and 'promote_resource_attributes' cannot be configured simultaneously`)
	})

	t.Run("bad config - configuring ignoring of resource attributes without also enabling promotion of all resource attributes", func(t *testing.T) {
		_, err := LoadFile(filepath.Join("testdata", "otlp_ignore_resource_attributes_without_promote_all.bad.yml"), false, promslog.NewNopLogger())
		require.ErrorContains(t, err, `'ignore_resource_attributes' cannot be configured unless 'promote_all_resource_attributes' is true`)
	})
}

func TestOTLPAllowServiceNameInTargetInfo(t *testing.T) {
	t.Run("good config", func(t *testing.T) {
		want, err := LoadFile(filepath.Join("testdata", "otlp_allow_keep_identifying_resource_attributes.good.yml"), false, promslog.NewNopLogger())
		require.NoError(t, err)

		out, err := yaml.Marshal(want)
		require.NoError(t, err)
		var got Config
		require.NoError(t, yaml.UnmarshalStrict(out, &got))

		require.True(t, got.OTLPConfig.KeepIdentifyingResourceAttributes)
	})
}

func TestOTLPConvertHistogramsToNHCB(t *testing.T) {
	t.Run("good config", func(t *testing.T) {
		want, err := LoadFile(filepath.Join("testdata", "otlp_convert_histograms_to_nhcb.good.yml"), false, promslog.NewNopLogger())
		require.NoError(t, err)

		out, err := yaml.Marshal(want)
		require.NoError(t, err)
		var got Config
		require.NoError(t, yaml.UnmarshalStrict(out, &got))

		require.True(t, got.OTLPConfig.ConvertHistogramsToNHCB)
	})