Repository: prometheus/prometheus
File: storage/remote/otlptranslator/prometheusremotewrite/combined_appender_test.go (Go)

Code before:
		app := &appenderRecorder{}
		capp := NewCombinedAppender(app, promslog.NewNopLogger(), true, false, NewCombinedAppenderMetrics(prometheus.NewRegistry()))

		ls := labels.FromStrings(
			model.MetricNameLabel, "test_bytes_total",
			"foo", "bar",
		)
		err := capp.AppendHistogram(ls, Metadata{}, 4, 2, nil, nil)
		require.Error(t, err)
	})

	for _, appendMetadata := range []bool{false, true} {
		t.Run(fmt.Sprintf("appendMetadata=%t", appendMetadata), func(t *testing.T) {
			app := &appenderRecorder{}
			capp := NewCombinedAppender(app, promslog.NewNopLogger(), true, appendMetadata, NewCombinedAppenderMetrics(prometheus.NewRegistry()))

			require.NoError(t, capp.AppendSample(seriesLabels.Copy(), floatMetadata, 1, 2, 42.0, nil))

			if appendMetadata {
				// Verify the operations: AppendCTZeroSample, Append, and UpdateMetadata
				require.Len(t, app.records, 3)
				requireEqualOp(t, "AppendCTZeroSample", app.records[0])
				requireEqualOp(t, "Append", app.records[1])
				requireEqualOp(t, "UpdateMetadata", app.records[2])
			} else {
				// Verify the operations: AppendCTZeroSample and Append, but NO UpdateMetadata
				require.Len(t, app.records, 2)
				requireEqualOp(t, "AppendCTZeroSample", app.records[0])
				requireEqualOp(t, "Append", app.records[1])
			}
		})
	}
}

// TestCombinedAppenderMetadataChanges verifies that UpdateMetadata is called
// when metadata fields change (help, unit, or type).
func TestCombinedAppenderMetadataChanges(t *testing.T) {
	seriesLabels := labels.FromStrings(
		model.MetricNameLabel, "test_metric",
		"foo", "bar",
	)

	baseMetadata := Metadata{
		Metadata: metadata.Metadata{
			Type: model.MetricTypeCounter,
			Unit: "bytes",
			Help: "original help",
		},
		MetricFamilyName: "test_metric",
	}


Review comment (suggestion):
It's superfluous to document implementation, it will quickly go stale.
```
```

Code after:
		app := &appenderRecorder{}
		capp := NewCombinedAppender(app, promslog.NewNopLogger(), true, false, NewCombinedAppenderMetrics(prometheus.NewRegistry()))

		ls := labels.FromStrings(
			model.MetricNameLabel, "test_bytes_total",
			"foo", "bar",
		)
		err := capp.AppendHistogram(ls, Metadata{}, 4, 2, nil, nil)
		require.Error(t, err)
	})

	for _, appendMetadata := range []bool{false, true} {
		t.Run(fmt.Sprintf("appendMetadata=%t", appendMetadata), func(t *testing.T) {
			app := &appenderRecorder{}
			capp := NewCombinedAppender(app, promslog.NewNopLogger(), true, appendMetadata, NewCombinedAppenderMetrics(prometheus.NewRegistry()))

			require.NoError(t, capp.AppendSample(seriesLabels.Copy(), floatMetadata, 1, 2, 42.0, nil))

			if appendMetadata {
				require.Len(t, app.records, 3)
				requireEqualOp(t, "AppendCTZeroSample", app.records[0])
				requireEqualOp(t, "Append", app.records[1])
				requireEqualOp(t, "UpdateMetadata", app.records[2])
			} else {
				require.Len(t, app.records, 2)
				requireEqualOp(t, "AppendCTZeroSample", app.records[0])
				requireEqualOp(t, "Append", app.records[1])
			}
		})
	}
}

// TestCombinedAppenderMetadataChanges verifies that UpdateMetadata is called
// when metadata fields change (help, unit, or type).
func TestCombinedAppenderMetadataChanges(t *testing.T) {
	seriesLabels := labels.FromStrings(
		model.MetricNameLabel, "test_metric",
		"foo", "bar",
	)

	baseMetadata := Metadata{
		Metadata: metadata.Metadata{
			Type: model.MetricTypeCounter,
			Unit: "bytes",
			Help: "original help",
		},
		MetricFamilyName: "test_metric",
	}

	tests := []struct {
		name           string