Repository: prometheus/prometheus
File: storage/remote/queue_manager_test.go (Go)

Code before:
			nSamples, nExemplars, nHistograms, _, _ := populateV2TimeSeries(
				&symbolTable,
				batch,
				pendingData,
				false, // sendExemplars
				false, // sendNativeHistograms
				true,  // enableTypeAndUnitLabels
			)

			require.Equal(t, 1, nSamples, "Should have 1 sample")
			require.Equal(t, 0, nExemplars, "Should have 0 exemplars")
			require.Equal(t, 0, nHistograms, "Should have 0 histograms")

			require.Equal(t, tc.expectedType, pendingData[0].Metadata.Type,
				"Type should match expected for %s", tc.description)

			unitRef := pendingData[0].Metadata.UnitRef

			symbols := symbolTable.Symbols()
			require.Equal(t, tc.unitLabel, symbols[unitRef], "Unit should match")
		})
	}
}

// TestPopulateV2TimeSeries_AgentMode verifies that type and unit labels are properly
// extracted from labels even when d.metadata is nil (agent mode scenario).
func TestPopulateV2TimeSeries_AgentMode(t *testing.T) {
	symbolTable := writev2.NewSymbolTable()

	testCases := []struct {
		name              string
		typeLabel         string
		unitLabel         string
		metadata          *metadata.Metadata // nil simulates agent mode.
		expectedType      writev2.Metadata_MetricType
		expectedUnit      string
		enableTypeAndUnit bool
	}{
		{
			name:              "agent_mode_with_type_and_unit_labels_enabled",
			typeLabel:         "gauge",
			unitLabel:         "bytes",
			metadata:          nil, // Agent mode - no metadata records.
			expectedType:      writev2.Metadata_METRIC_TYPE_GAUGE,
			expectedUnit:      "bytes",
			enableTypeAndUnit: true,
		},
		{
			name:              "agent_mode_with_type_no_unit",
			typeLabel:         "counter",
			unitLabel:         "",

Review comment (refactor):
```suggestion
// extracted from labels even when d.metadata is not nil (agent mode scenario).
// Regression test for https://github.com/prometheus/prometheus/issues/17381.
```

Code after:
			nSamples, nExemplars, nHistograms, _, _ := populateV2TimeSeries(
				&symbolTable,
				batch,
				pendingData,
				false, // sendExemplars
				false, // sendNativeHistograms
				true,  // enableTypeAndUnitLabels
			)

			require.Equal(t, 1, nSamples, "Should have 1 sample")
			require.Equal(t, 0, nExemplars, "Should have 0 exemplars")
			require.Equal(t, 0, nHistograms, "Should have 0 histograms")

			require.Equal(t, tc.expectedType, pendingData[0].Metadata.Type,
				"Type should match expected for %s", tc.description)

			unitRef := pendingData[0].Metadata.UnitRef

			symbols := symbolTable.Symbols()
			require.Equal(t, tc.unitLabel, symbols[unitRef], "Unit should match")
		})
	}
}

// TestPopulateV2TimeSeries_MetadataAndTypeAndUnit verifies that type and unit labels are properly
// extracted from labels even when d.metadata is not nil (agent mode scenario).
// Regression test for https://github.com/prometheus/prometheus/issues/17381.
func TestPopulateV2TimeSeries_MetadataAndTypeAndUnit(t *testing.T) {
	symbolTable := writev2.NewSymbolTable()

	testCases := []struct {
		name              string
		typeLabel         string
		unitLabel         string
		metadata          *metadata.Metadata
		expectedType      writev2.Metadata_MetricType
		expectedUnit      string
		enableTypeAndUnit bool
	}{
		{
			name:              "type_and_unit_no_meta",
			typeLabel:         "gauge",
			unitLabel:         "bytes",
			metadata:          nil,
			expectedType:      writev2.Metadata_METRIC_TYPE_GAUGE,
			expectedUnit:      "bytes",
			enableTypeAndUnit: true,
		},
		{
			name:              "type_no_unit_no_meta",
			typeLabel:         "counter",