Repository: prometheus/prometheus
File: model/textparse/interface.go (Go)

Code before:
			retErr := fmt.Errorf("cannot parse Content-Type %q and no fallback_scrape_protocol for target", contentType)
			return "", errors.Join(retErr, err)
		}
		retErr := fmt.Errorf("could not parse received Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType)
		return fallbackType, errors.Join(retErr, err)
	}

	// We have a valid media type, either we recognise it and can use it
	// or we have to error.
	switch mediaType {
	case "application/openmetrics-text", "application/vnd.google.protobuf", "text/plain":
		return mediaType, nil
	}
	// We're here because we have no recognised mediaType.
	if fallbackType == "" {
		return "", fmt.Errorf("received unsupported Content-Type %q and no fallback_scrape_protocol specified for target", contentType)
	}
	return fallbackType, fmt.Errorf("received unsupported Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType)
}

type ParserOptions struct {
	ParseClassicHistograms         bool
	ConvertClassicHistogramsToNHCB bool
	SkipOMCTSeries                 bool
	EnableTypeAndUnitLabels        bool
}

// New returns a new parser of the byte slice.
//
// This function no longer guarantees to return a valid parser.
//
// It only returns a valid parser if the supplied contentType and fallbackType allow.
// An error may also be returned if fallbackType had to be used or there was some
// other error parsing the supplied Content-Type.
// If the returned parser is nil then the scrape must fail.
func New(b []byte, contentType, fallbackType string, opts ParserOptions, st *labels.SymbolTable) (Parser, error) {
	mediaType, err := extractMediaType(contentType, fallbackType)
	// err may be nil or something we want to warn about.

	var baseParser Parser
	switch mediaType {
	case "application/openmetrics-text":
		baseParser = NewOpenMetricsParser(b, st, func(o *openMetricsParserOptions) {
			o.skipCTSeries = opts.SkipOMCTSeries
			o.enableTypeAndUnitLabels = opts.EnableTypeAndUnitLabels
		})
	case "application/vnd.google.protobuf":
		baseParser = NewProtobufParser(b, opts.ParseClassicHistograms, opts.EnableTypeAndUnitLabels, st)
	case "text/plain":
		baseParser = NewPromParser(b, st, opts.EnableTypeAndUnitLabels)
	default:

Review comment (suggestion):
We have opportunity those options a bit more clear.

```suggestion
type ParserOptions struct {
	EnableTypeAndUnitLabels        bool
	ConvertClassicHistogramsToNHCB bool
	
	// Proto parsing only.
	ParseClassicHistograms         bool
		
	// OpenMetrics only.
	SkipOMCTSeries                 bool
}
```

Code after:
			retErr := fmt.Errorf("cannot parse Content-Type %q and no fallback_scrape_protocol for target", contentType)
			return "", errors.Join(retErr, err)
		}
		retErr := fmt.Errorf("could not parse received Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType)
		return fallbackType, errors.Join(retErr, err)
	}

	// We have a valid media type, either we recognise it and can use it
	// or we have to error.
	switch mediaType {
	case "application/openmetrics-text", "application/vnd.google.protobuf", "text/plain":
		return mediaType, nil
	}
	// We're here because we have no recognised mediaType.
	if fallbackType == "" {
		return "", fmt.Errorf("received unsupported Content-Type %q and no fallback_scrape_protocol specified for target", contentType)
	}
	return fallbackType, fmt.Errorf("received unsupported Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType)
}

type ParserOptions struct {
	// EnableTypeAndUnitLabels enables parsing and inclusion of type and unit labels
	// in the parsed metrics.
	EnableTypeAndUnitLabels bool

	// ConvertClassicHistogramsToNHCB enables conversion of classic histograms
	// to native histogram custom buckets (NHCB) format.
	ConvertClassicHistogramsToNHCB bool

	// KeepClassicOnClassicAndNativeHistograms causes parser to output classic histogram
	// that is also present as a native histogram. (Proto parsing only).
	KeepClassicOnClassicAndNativeHistograms bool

	// OpenMetricsSkipCTSeries determines whether to skip `_created` timestamp series
	// during (OpenMetrics parsing only).
	OpenMetricsSkipCTSeries bool

	// FallbackContentType specifies the fallback content type to use when the provided
	// Content-Type header cannot be parsed or is not supported.
	FallbackContentType string
}

// New returns a new parser of the byte slice.
//
// This function no longer guarantees to return a valid parser.
//
// It only returns a valid parser if the supplied contentType and fallbackType allow.
// An error may also be returned if fallbackType had to be used or there was some
// other error parsing the supplied Content-Type.
// If the returned parser is nil then the scrape must fail.
func New(b []byte, contentType string, st *labels.SymbolTable, opts ParserOptions) (Parser, error) {