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

Code before:
		require.NotEqual(t, ref, newRef, "the second AppendCTZeroSample returned a different reference")
		requireEqualOpAndRef(t, "Append", newRef, app.records[3])
		requireEqualOpAndRef(t, "AppendExemplar", newRef, app.records[4])
	})

	t.Run("check that invoking AppendHistogram returns an error for nil histogram", func(t *testing.T) {
		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,

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

Code after:
		require.NotEqual(t, ref, newRef, "the second AppendCTZeroSample returned a different reference")
		requireEqualOpAndRef(t, "Append", newRef, app.records[3])
		requireEqualOpAndRef(t, "AppendExemplar", newRef, app.records[4])
	})

	t.Run("check that invoking AppendHistogram returns an error for nil histogram", func(t *testing.T) {
		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",