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

Code before:

	// 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        bool
	ConvertClassicHistogramsToNHCB bool

	// Whether to also parse a classic histogram that is also present as a
	// native histogram. (Proto parsing only).
	ParseClassicHistograms bool

	// Whether to skip _created timestamp series during OpenMetrics parsing.
	SkipOMCTSeries bool

	FallbackType 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) {
	if st == nil {
		st = labels.NewSymbolTable()
	}

	mediaType, err := extractMediaType(contentType, opts.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
		})

Review comment (suggestion):
```suggestion
	// FallbackContentType represents the content-type to use when the requested content-type is not supported.
	FallbackContentType string
```

Code after:

	// 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) {
	if st == nil {
		st = labels.NewSymbolTable()
	}

	mediaType, err := extractMediaType(contentType, opts.FallbackContentType)
	// err may be nil or something we want to warn about.