Copyright 2014 The Prometheus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package expfmt

import (
	
	
	
	
	
	
	

	dto 

	
	
)
A stateFn is a function that represents a state in a state machine. By executing it, the state is progressed to the next state. The stateFn returns another stateFn, which represents the new state. The end state is represented by nil.
type stateFn func() stateFn
ParseError signals errors while parsing the simple and flat text-based exchange format.
type ParseError struct {
	Line int
	Msg  string
}
Error implements the error interface.
func ( ParseError) () string {
	return fmt.Sprintf("text format parsing error in line %d: %s", .Line, .Msg)
}
TextParser is used to parse the simple and flat text-based exchange format. Its zero value is ready to use.
type TextParser struct {
	metricFamiliesByName map[string]*dto.MetricFamily
	buf                  *bufio.Reader // Where the parsed input is read through.
	err                  error         // Most recent error.
	lineCount            int           // Tracks the line count for error messages.
	currentByte          byte          // The most recent byte read.
	currentToken         bytes.Buffer  // Re-used each time a token has to be gathered from multiple bytes.
	currentMF            *dto.MetricFamily
	currentMetric        *dto.Metric
	currentLabelPair     *dto.LabelPair
The remaining member variables are only used for summaries/histograms.
Summary specific.
	summaries       map[uint64]*dto.Metric // Key is created with LabelsToSignature.
Histogram specific.
	histograms    map[uint64]*dto.Metric // Key is created with LabelsToSignature.
These tell us if the currently processed line ends on '_count' or '_sum' respectively and belong to a summary/histogram, representing the sample count and sum of that summary/histogram.
TextToMetricFamilies reads 'in' as the simple and flat text-based exchange format and creates MetricFamily proto messages. It returns the MetricFamily proto messages in a map where the metric names are the keys, along with any error encountered. If the input contains duplicate metrics (i.e. lines with the same metric name and exactly the same label set), the resulting MetricFamily will contain duplicate Metric proto messages. Similar is true for duplicate label names. Checks for duplicates have to be performed separately, if required. Also note that neither the metrics within each MetricFamily are sorted nor the label pairs within each Metric. Sorting is not required for the most frequent use of this method, which is sample ingestion in the Prometheus server. However, for presentation purposes, you might want to sort the metrics, and in some cases, you must sort the labels, e.g. for consumption by the metric family injection hook of the Prometheus registry. Summaries and histograms are rather special beasts. You would probably not use them in the simple text format anyway. This method can deal with summaries and histograms if they are presented in exactly the way the text.Create function creates them. This method must not be called concurrently. If you want to parse different input concurrently, instantiate a separate Parser for each goroutine.
Magic happens here...
Get rid of empty metric families.
	for ,  := range .metricFamiliesByName {
		if len(.GetMetric()) == 0 {
			delete(.metricFamiliesByName, )
		}
If p.err is io.EOF now, we have run into a premature end of the input stream. Turn this error into something nicer and more meaningful. (io.EOF is often used as a signal for the legitimate end of an input stream.)
	if .err == io.EOF {
		.parseError("unexpected end of input stream")
	}
	return .metricFamiliesByName, .err
}

func ( *TextParser) ( io.Reader) {
	.metricFamiliesByName = map[string]*dto.MetricFamily{}
	if .buf == nil {
		.buf = bufio.NewReader()
	} else {
		.buf.Reset()
	}
	.err = nil
	.lineCount = 0
	if .summaries == nil || len(.summaries) > 0 {
		.summaries = map[uint64]*dto.Metric{}
	}
	if .histograms == nil || len(.histograms) > 0 {
		.histograms = map[uint64]*dto.Metric{}
	}
	.currentQuantile = math.NaN()
	.currentBucket = math.NaN()
}
startOfLine represents the state where the next byte read from p.buf is the start of a line (or whitespace leading up to it).
End of input reached. This is the only case where that is not an error but a signal that we are done.
		.err = nil
		return nil
	}
	switch .currentByte {
	case '#':
		return .startComment
	case '\n':
		return . // Empty line, start the next one.
	}
	return .readingMetricName
}
startComment represents the state where the next byte read from p.buf is the start of a comment (or whitespace leading up to it).
func ( *TextParser) () stateFn {
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentByte == '\n' {
		return .startOfLine
	}
	if .readTokenUntilWhitespace(); .err != nil {
		return nil // Unexpected end of input.
If we have hit the end of line already, there is nothing left to do. This is not considered a syntax error.
	if .currentByte == '\n' {
		return .startOfLine
	}
	 := .currentToken.String()
Generic comment, ignore by fast forwarding to end of line.
		for .currentByte != '\n' {
			if .currentByte, .err = .buf.ReadByte(); .err != nil {
				return nil // Unexpected end of input.
			}
		}
		return .startOfLine
There is something. Next has to be a metric name.
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .readTokenAsMetricName(); .err != nil {
		return nil // Unexpected end of input.
	}
At the end of the line already. Again, this is not considered a syntax error.
		return .startOfLine
	}
	if !isBlankOrTab(.currentByte) {
		.parseError("invalid metric name in comment")
		return nil
	}
	.setOrCreateCurrentMF()
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
At the end of the line already. Again, this is not considered a syntax error.
		return .startOfLine
	}
	switch  {
	case "HELP":
		return .readingHelp
	case "TYPE":
		return .readingType
	}
	panic(fmt.Sprintf("code error: unexpected keyword %q", ))
}
readingMetricName represents the state where the last byte read (now in p.currentByte) is the first byte of a metric name.
func ( *TextParser) () stateFn {
	if .readTokenAsMetricName(); .err != nil {
		return nil
	}
	if .currentToken.Len() == 0 {
		.parseError("invalid metric name")
		return nil
	}
Now is the time to fix the type if it hasn't happened yet.
Do not append the newly created currentMetric to currentMF.Metric right now. First wait if this is a summary, and the metric exists already, which we can only know after having read all the labels.
	if .skipBlankTabIfCurrentBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	return .readingLabels
}
readingLabels represents the state where the last byte read (now in p.currentByte) is either the first byte of the label set (i.e. a '{'), or the first byte of the value (otherwise).
Summaries/histograms are special. We have to reset the currentLabels map, currentQuantile and currentBucket before starting to read labels.
startLabelName represents the state where the next byte read from p.buf is the start of a label name (or whitespace leading up to it).
func ( *TextParser) () stateFn {
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentByte == '}' {
		if .skipBlankTab(); .err != nil {
			return nil // Unexpected end of input.
		}
		return .readingValue
	}
	if .readTokenAsLabelName(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentToken.Len() == 0 {
		.parseError(fmt.Sprintf("invalid label name for metric %q", .currentMF.GetName()))
		return nil
	}
	.currentLabelPair = &dto.LabelPair{Name: proto.String(.currentToken.String())}
	if .currentLabelPair.GetName() == string(model.MetricNameLabel) {
		.parseError(fmt.Sprintf("label name %q is reserved", model.MetricNameLabel))
		return nil
Special summary/histogram treatment. Don't add 'quantile' and 'le' labels to 'real' labels.
	if !(.currentMF.GetType() == dto.MetricType_SUMMARY && .currentLabelPair.GetName() == model.QuantileLabel) &&
		!(.currentMF.GetType() == dto.MetricType_HISTOGRAM && .currentLabelPair.GetName() == model.BucketLabel) {
		.currentMetric.Label = append(.currentMetric.Label, .currentLabelPair)
	}
	if .skipBlankTabIfCurrentBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentByte != '=' {
		.parseError(fmt.Sprintf("expected '=' after label name, found %q", .currentByte))
		return nil
	}
	return .startLabelValue
}
startLabelValue represents the state where the next byte read from p.buf is the start of a (quoted) label value (or whitespace leading up to it).
func ( *TextParser) () stateFn {
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentByte != '"' {
		.parseError(fmt.Sprintf("expected '\"' at start of label value, found %q", .currentByte))
		return nil
	}
	if .readTokenAsLabelValue(); .err != nil {
		return nil
	}
	if !model.LabelValue(.currentToken.String()).IsValid() {
		.parseError(fmt.Sprintf("invalid label value %q", .currentToken.String()))
		return nil
	}
Special treatment of summaries: - Quantile labels are special, will result in dto.Quantile later. - Other labels have to be added to currentLabels for signature calculation.
Create a more helpful error message.
				.parseError(fmt.Sprintf("expected float as value for 'quantile' label, got %q", .currentLabelPair.GetValue()))
				return nil
			}
		} else {
			.currentLabels[.currentLabelPair.GetName()] = .currentLabelPair.GetValue()
		}
Similar special treatment of histograms.
Create a more helpful error message.
				.parseError(fmt.Sprintf("expected float as value for 'le' label, got %q", .currentLabelPair.GetValue()))
				return nil
			}
		} else {
			.currentLabels[.currentLabelPair.GetName()] = .currentLabelPair.GetValue()
		}
	}
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	switch .currentByte {
	case ',':
		return .startLabelName

	case '}':
		if .skipBlankTab(); .err != nil {
			return nil // Unexpected end of input.
		}
		return .readingValue
	default:
		.parseError(fmt.Sprintf("unexpected end of label value %q", .currentLabelPair.GetValue()))
		return nil
	}
}
readingValue represents the state where the last byte read (now in p.currentByte) is the first byte of the sample value (i.e. a float).
When we are here, we have read all the labels, so for the special case of a summary/histogram, we can finally find out if the metric already exists.
	if .currentMF.GetType() == dto.MetricType_SUMMARY {
		 := model.LabelsToSignature(.currentLabels)
		if  := .summaries[];  != nil {
			.currentMetric = 
		} else {
			.summaries[] = .currentMetric
			.currentMF.Metric = append(.currentMF.Metric, .currentMetric)
		}
	} else if .currentMF.GetType() == dto.MetricType_HISTOGRAM {
		 := model.LabelsToSignature(.currentLabels)
		if  := .histograms[];  != nil {
			.currentMetric = 
		} else {
			.histograms[] = .currentMetric
			.currentMF.Metric = append(.currentMF.Metric, .currentMetric)
		}
	} else {
		.currentMF.Metric = append(.currentMF.Metric, .currentMetric)
	}
	if .readTokenUntilWhitespace(); .err != nil {
		return nil // Unexpected end of input.
	}
	,  := parseFloat(.currentToken.String())
Create a more helpful error message.
startTimestamp represents the state where the next byte read from p.buf is the start of the timestamp (or whitespace leading up to it).
func ( *TextParser) () stateFn {
	if .skipBlankTab(); .err != nil {
		return nil // Unexpected end of input.
	}
	if .readTokenUntilWhitespace(); .err != nil {
		return nil // Unexpected end of input.
	}
	,  := strconv.ParseInt(.currentToken.String(), 10, 64)
Create a more helpful error message.
		.parseError(fmt.Sprintf("expected integer as timestamp, got %q", .currentToken.String()))
		return nil
	}
	.currentMetric.TimestampMs = proto.Int64()
	if .readTokenUntilNewline(false); .err != nil {
		return nil // Unexpected end of input.
	}
	if .currentToken.Len() > 0 {
		.parseError(fmt.Sprintf("spurious string after timestamp: %q", .currentToken.String()))
		return nil
	}
	return .startOfLine
}
readingHelp represents the state where the last byte read (now in p.currentByte) is the first byte of the docstring after 'HELP'.
func ( *TextParser) () stateFn {
	if .currentMF.Help != nil {
		.parseError(fmt.Sprintf("second HELP line for metric name %q", .currentMF.GetName()))
		return nil
Rest of line is the docstring.
	if .readTokenUntilNewline(true); .err != nil {
		return nil // Unexpected end of input.
	}
	.currentMF.Help = proto.String(.currentToken.String())
	return .startOfLine
}
readingType represents the state where the last byte read (now in p.currentByte) is the first byte of the type hint after 'HELP'.
func ( *TextParser) () stateFn {
	if .currentMF.Type != nil {
		.parseError(fmt.Sprintf("second TYPE line for metric name %q, or TYPE reported after samples", .currentMF.GetName()))
		return nil
Rest of line is the type.
	if .readTokenUntilNewline(false); .err != nil {
		return nil // Unexpected end of input.
	}
	,  := dto.MetricType_value[strings.ToUpper(.currentToken.String())]
	if ! {
		.parseError(fmt.Sprintf("unknown metric type %q", .currentToken.String()))
		return nil
	}
	.currentMF.Type = dto.MetricType().Enum()
	return .startOfLine
}
parseError sets p.err to a ParseError at the current line with the given message.
func ( *TextParser) ( string) {
	.err = ParseError{
		Line: .lineCount,
		Msg:  ,
	}
}
skipBlankTab reads (and discards) bytes from p.buf until it encounters a byte that is neither ' ' nor '\t'. That byte is left in p.currentByte.
func ( *TextParser) () {
	for {
		if .currentByte, .err = .buf.ReadByte(); .err != nil || !isBlankOrTab(.currentByte) {
			return
		}
	}
}
skipBlankTabIfCurrentBlankTab works exactly as skipBlankTab but doesn't do anything if p.currentByte is neither ' ' nor '\t'.
readTokenUntilWhitespace copies bytes from p.buf into p.currentToken. The first byte considered is the byte already read (now in p.currentByte). The first whitespace byte encountered is still copied into p.currentByte, but not into p.currentToken.
readTokenUntilNewline copies bytes from p.buf into p.currentToken. The first byte considered is the byte already read (now in p.currentByte). The first newline byte encountered is still copied into p.currentByte, but not into p.currentToken. If recognizeEscapeSequence is true, two escape sequences are recognized: '\\' translates into '\', and '\n' into a line-feed character. All other escape sequences are invalid and cause an error.
func ( *TextParser) ( bool) {
	.currentToken.Reset()
	 := false
	for .err == nil {
		if  &&  {
			switch .currentByte {
			case '\\':
				.currentToken.WriteByte(.currentByte)
			case 'n':
				.currentToken.WriteByte('\n')
			default:
				.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", .currentByte))
				return
			}
			 = false
		} else {
			switch .currentByte {
			case '\n':
				return
			case '\\':
				 = true
			default:
				.currentToken.WriteByte(.currentByte)
			}
		}
		.currentByte, .err = .buf.ReadByte()
	}
}
readTokenAsMetricName copies a metric name from p.buf into p.currentToken. The first byte considered is the byte already read (now in p.currentByte). The first byte not part of a metric name is still copied into p.currentByte, but not into p.currentToken.
readTokenAsLabelName copies a label name from p.buf into p.currentToken. The first byte considered is the byte already read (now in p.currentByte). The first byte not part of a label name is still copied into p.currentByte, but not into p.currentToken.
readTokenAsLabelValue copies a label value from p.buf into p.currentToken. In contrast to the other 'readTokenAs...' functions, which start with the last read byte in p.currentByte, this method ignores p.currentByte and starts with reading a new byte from p.buf. The first byte not part of a label value is still copied into p.currentByte, but not into p.currentToken.
func ( *TextParser) () {
	.currentToken.Reset()
	 := false
	for {
		if .currentByte, .err = .buf.ReadByte(); .err != nil {
			return
		}
		if  {
			switch .currentByte {
			case '"', '\\':
				.currentToken.WriteByte(.currentByte)
			case 'n':
				.currentToken.WriteByte('\n')
			default:
				.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", .currentByte))
				return
			}
			 = false
			continue
		}
		switch .currentByte {
		case '"':
			return
		case '\n':
			.parseError(fmt.Sprintf("label value %q contains unescaped new-line", .currentToken.String()))
			return
		case '\\':
			 = true
		default:
			.currentToken.WriteByte(.currentByte)
		}
	}
}

func ( *TextParser) () {
	.currentIsSummaryCount = false
	.currentIsSummarySum = false
	.currentIsHistogramCount = false
	.currentIsHistogramSum = false
	 := .currentToken.String()
	if .currentMF = .metricFamiliesByName[]; .currentMF != nil {
		return
Try out if this is a _sum or _count for a summary/histogram.
	 := summaryMetricName()
	if .currentMF = .metricFamiliesByName[]; .currentMF != nil {
		if .currentMF.GetType() == dto.MetricType_SUMMARY {
			if isCount() {
				.currentIsSummaryCount = true
			}
			if isSum() {
				.currentIsSummarySum = true
			}
			return
		}
	}
	 := histogramMetricName()
	if .currentMF = .metricFamiliesByName[]; .currentMF != nil {
		if .currentMF.GetType() == dto.MetricType_HISTOGRAM {
			if isCount() {
				.currentIsHistogramCount = true
			}
			if isSum() {
				.currentIsHistogramSum = true
			}
			return
		}
	}
	.currentMF = &dto.MetricFamily{Name: proto.String()}
	.metricFamiliesByName[] = .currentMF
}

func ( byte) bool {
	return ( >= 'a' &&  <= 'z') || ( >= 'A' &&  <= 'Z') ||  == '_'
}

func ( byte) bool {
	return isValidLabelNameStart() || ( >= '0' &&  <= '9')
}

func ( byte) bool {
	return isValidLabelNameStart() ||  == ':'
}

func ( byte) bool {
	return isValidLabelNameContinuation() ||  == ':'
}

func ( byte) bool {
	return  == ' ' ||  == '\t'
}

func ( string) bool {
	return len() > 6 && [len()-6:] == "_count"
}

func ( string) bool {
	return len() > 4 && [len()-4:] == "_sum"
}

func ( string) bool {
	return len() > 7 && [len()-7:] == "_bucket"
}

func ( string) string {
	switch {
	case isCount():
		return [:len()-6]
	case isSum():
		return [:len()-4]
	default:
		return 
	}
}

func ( string) string {
	switch {
	case isCount():
		return [:len()-6]
	case isSum():
		return [:len()-4]
	case isBucket():
		return [:len()-7]
	default:
		return 
	}
}

func ( string) (float64, error) {
	if strings.ContainsAny(, "pP_") {
		return 0, fmt.Errorf("unsupported character in float")
	}
	return strconv.ParseFloat(, 64)