extractMediaType returns the mediaType of a required parser. It tries first to extract a valid and supported mediaType from contentType. If that fails, the provided fallbackType (possibly an empty string) is returned, together with an error. fallbackType is used as-is without further validation.
(contentType, fallbackType string)
| 91 | // the provided fallbackType (possibly an empty string) is returned, together with |
| 92 | // an error. fallbackType is used as-is without further validation. |
| 93 | func extractMediaType(contentType, fallbackType string) (string, error) { |
| 94 | if contentType == "" { |
| 95 | if fallbackType == "" { |
| 96 | return "", errors.New("non-compliant scrape target sending blank Content-Type and no fallback_scrape_protocol specified for target") |
| 97 | } |
| 98 | return fallbackType, fmt.Errorf("non-compliant scrape target sending blank Content-Type, using fallback_scrape_protocol %q", fallbackType) |
| 99 | } |
| 100 | |
| 101 | // We have a contentType, parse it. |
| 102 | mediaType, _, err := mime.ParseMediaType(contentType) |
| 103 | if err != nil { |
| 104 | if fallbackType == "" { |
| 105 | retErr := fmt.Errorf("cannot parse Content-Type %q and no fallback_scrape_protocol for target", contentType) |
| 106 | return "", errors.Join(retErr, err) |
| 107 | } |
| 108 | retErr := fmt.Errorf("could not parse received Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType) |
| 109 | return fallbackType, errors.Join(retErr, err) |
| 110 | } |
| 111 | |
| 112 | // We have a valid media type, either we recognise it and can use it |
| 113 | // or we have to error. |
| 114 | switch mediaType { |
| 115 | case "application/openmetrics-text", "application/vnd.google.protobuf", "text/plain": |
| 116 | return mediaType, nil |
| 117 | } |
| 118 | // We're here because we have no recognised mediaType. |
| 119 | if fallbackType == "" { |
| 120 | return "", fmt.Errorf("received unsupported Content-Type %q and no fallback_scrape_protocol specified for target", contentType) |
| 121 | } |
| 122 | return fallbackType, fmt.Errorf("received unsupported Content-Type %q, using fallback_scrape_protocol %q", contentType, fallbackType) |
| 123 | } |
| 124 | |
| 125 | type ParserOptions struct { |
| 126 | // EnableTypeAndUnitLabels enables parsing and inclusion of type and unit labels |
no outgoing calls
no test coverage detected
searching dependent graphs…