Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / prometheus / common / expfmt / text_parse.go
1 // Copyright 2014 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package expfmt
15
16 import (
17         "bufio"
18         "bytes"
19         "fmt"
20         "io"
21         "math"
22         "strconv"
23         "strings"
24
25         dto "github.com/prometheus/client_model/go"
26
27         "github.com/golang/protobuf/proto"
28         "github.com/prometheus/common/model"
29 )
30
31 // A stateFn is a function that represents a state in a state machine. By
32 // executing it, the state is progressed to the next state. The stateFn returns
33 // another stateFn, which represents the new state. The end state is represented
34 // by nil.
35 type stateFn func() stateFn
36
37 // ParseError signals errors while parsing the simple and flat text-based
38 // exchange format.
39 type ParseError struct {
40         Line int
41         Msg  string
42 }
43
44 // Error implements the error interface.
45 func (e ParseError) Error() string {
46         return fmt.Sprintf("text format parsing error in line %d: %s", e.Line, e.Msg)
47 }
48
49 // TextParser is used to parse the simple and flat text-based exchange format. Its
50 // nil value is ready to use.
51 type TextParser struct {
52         metricFamiliesByName map[string]*dto.MetricFamily
53         buf                  *bufio.Reader // Where the parsed input is read through.
54         err                  error         // Most recent error.
55         lineCount            int           // Tracks the line count for error messages.
56         currentByte          byte          // The most recent byte read.
57         currentToken         bytes.Buffer  // Re-used each time a token has to be gathered from multiple bytes.
58         currentMF            *dto.MetricFamily
59         currentMetric        *dto.Metric
60         currentLabelPair     *dto.LabelPair
61
62         // The remaining member variables are only used for summaries/histograms.
63         currentLabels map[string]string // All labels including '__name__' but excluding 'quantile'/'le'
64         // Summary specific.
65         summaries       map[uint64]*dto.Metric // Key is created with LabelsToSignature.
66         currentQuantile float64
67         // Histogram specific.
68         histograms    map[uint64]*dto.Metric // Key is created with LabelsToSignature.
69         currentBucket float64
70         // These tell us if the currently processed line ends on '_count' or
71         // '_sum' respectively and belong to a summary/histogram, representing the sample
72         // count and sum of that summary/histogram.
73         currentIsSummaryCount, currentIsSummarySum     bool
74         currentIsHistogramCount, currentIsHistogramSum bool
75 }
76
77 // TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
78 // format and creates MetricFamily proto messages. It returns the MetricFamily
79 // proto messages in a map where the metric names are the keys, along with any
80 // error encountered.
81 //
82 // If the input contains duplicate metrics (i.e. lines with the same metric name
83 // and exactly the same label set), the resulting MetricFamily will contain
84 // duplicate Metric proto messages. Similar is true for duplicate label
85 // names. Checks for duplicates have to be performed separately, if required.
86 // Also note that neither the metrics within each MetricFamily are sorted nor
87 // the label pairs within each Metric. Sorting is not required for the most
88 // frequent use of this method, which is sample ingestion in the Prometheus
89 // server. However, for presentation purposes, you might want to sort the
90 // metrics, and in some cases, you must sort the labels, e.g. for consumption by
91 // the metric family injection hook of the Prometheus registry.
92 //
93 // Summaries and histograms are rather special beasts. You would probably not
94 // use them in the simple text format anyway. This method can deal with
95 // summaries and histograms if they are presented in exactly the way the
96 // text.Create function creates them.
97 //
98 // This method must not be called concurrently. If you want to parse different
99 // input concurrently, instantiate a separate Parser for each goroutine.
100 func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricFamily, error) {
101         p.reset(in)
102         for nextState := p.startOfLine; nextState != nil; nextState = nextState() {
103                 // Magic happens here...
104         }
105         // Get rid of empty metric families.
106         for k, mf := range p.metricFamiliesByName {
107                 if len(mf.GetMetric()) == 0 {
108                         delete(p.metricFamiliesByName, k)
109                 }
110         }
111         // If p.err is io.EOF now, we have run into a premature end of the input
112         // stream. Turn this error into something nicer and more
113         // meaningful. (io.EOF is often used as a signal for the legitimate end
114         // of an input stream.)
115         if p.err == io.EOF {
116                 p.parseError("unexpected end of input stream")
117         }
118         return p.metricFamiliesByName, p.err
119 }
120
121 func (p *TextParser) reset(in io.Reader) {
122         p.metricFamiliesByName = map[string]*dto.MetricFamily{}
123         if p.buf == nil {
124                 p.buf = bufio.NewReader(in)
125         } else {
126                 p.buf.Reset(in)
127         }
128         p.err = nil
129         p.lineCount = 0
130         if p.summaries == nil || len(p.summaries) > 0 {
131                 p.summaries = map[uint64]*dto.Metric{}
132         }
133         if p.histograms == nil || len(p.histograms) > 0 {
134                 p.histograms = map[uint64]*dto.Metric{}
135         }
136         p.currentQuantile = math.NaN()
137         p.currentBucket = math.NaN()
138 }
139
140 // startOfLine represents the state where the next byte read from p.buf is the
141 // start of a line (or whitespace leading up to it).
142 func (p *TextParser) startOfLine() stateFn {
143         p.lineCount++
144         if p.skipBlankTab(); p.err != nil {
145                 // End of input reached. This is the only case where
146                 // that is not an error but a signal that we are done.
147                 p.err = nil
148                 return nil
149         }
150         switch p.currentByte {
151         case '#':
152                 return p.startComment
153         case '\n':
154                 return p.startOfLine // Empty line, start the next one.
155         }
156         return p.readingMetricName
157 }
158
159 // startComment represents the state where the next byte read from p.buf is the
160 // start of a comment (or whitespace leading up to it).
161 func (p *TextParser) startComment() stateFn {
162         if p.skipBlankTab(); p.err != nil {
163                 return nil // Unexpected end of input.
164         }
165         if p.currentByte == '\n' {
166                 return p.startOfLine
167         }
168         if p.readTokenUntilWhitespace(); p.err != nil {
169                 return nil // Unexpected end of input.
170         }
171         // If we have hit the end of line already, there is nothing left
172         // to do. This is not considered a syntax error.
173         if p.currentByte == '\n' {
174                 return p.startOfLine
175         }
176         keyword := p.currentToken.String()
177         if keyword != "HELP" && keyword != "TYPE" {
178                 // Generic comment, ignore by fast forwarding to end of line.
179                 for p.currentByte != '\n' {
180                         if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil {
181                                 return nil // Unexpected end of input.
182                         }
183                 }
184                 return p.startOfLine
185         }
186         // There is something. Next has to be a metric name.
187         if p.skipBlankTab(); p.err != nil {
188                 return nil // Unexpected end of input.
189         }
190         if p.readTokenAsMetricName(); p.err != nil {
191                 return nil // Unexpected end of input.
192         }
193         if p.currentByte == '\n' {
194                 // At the end of the line already.
195                 // Again, this is not considered a syntax error.
196                 return p.startOfLine
197         }
198         if !isBlankOrTab(p.currentByte) {
199                 p.parseError("invalid metric name in comment")
200                 return nil
201         }
202         p.setOrCreateCurrentMF()
203         if p.skipBlankTab(); p.err != nil {
204                 return nil // Unexpected end of input.
205         }
206         if p.currentByte == '\n' {
207                 // At the end of the line already.
208                 // Again, this is not considered a syntax error.
209                 return p.startOfLine
210         }
211         switch keyword {
212         case "HELP":
213                 return p.readingHelp
214         case "TYPE":
215                 return p.readingType
216         }
217         panic(fmt.Sprintf("code error: unexpected keyword %q", keyword))
218 }
219
220 // readingMetricName represents the state where the last byte read (now in
221 // p.currentByte) is the first byte of a metric name.
222 func (p *TextParser) readingMetricName() stateFn {
223         if p.readTokenAsMetricName(); p.err != nil {
224                 return nil
225         }
226         if p.currentToken.Len() == 0 {
227                 p.parseError("invalid metric name")
228                 return nil
229         }
230         p.setOrCreateCurrentMF()
231         // Now is the time to fix the type if it hasn't happened yet.
232         if p.currentMF.Type == nil {
233                 p.currentMF.Type = dto.MetricType_UNTYPED.Enum()
234         }
235         p.currentMetric = &dto.Metric{}
236         // Do not append the newly created currentMetric to
237         // currentMF.Metric right now. First wait if this is a summary,
238         // and the metric exists already, which we can only know after
239         // having read all the labels.
240         if p.skipBlankTabIfCurrentBlankTab(); p.err != nil {
241                 return nil // Unexpected end of input.
242         }
243         return p.readingLabels
244 }
245
246 // readingLabels represents the state where the last byte read (now in
247 // p.currentByte) is either the first byte of the label set (i.e. a '{'), or the
248 // first byte of the value (otherwise).
249 func (p *TextParser) readingLabels() stateFn {
250         // Summaries/histograms are special. We have to reset the
251         // currentLabels map, currentQuantile and currentBucket before starting to
252         // read labels.
253         if p.currentMF.GetType() == dto.MetricType_SUMMARY || p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
254                 p.currentLabels = map[string]string{}
255                 p.currentLabels[string(model.MetricNameLabel)] = p.currentMF.GetName()
256                 p.currentQuantile = math.NaN()
257                 p.currentBucket = math.NaN()
258         }
259         if p.currentByte != '{' {
260                 return p.readingValue
261         }
262         return p.startLabelName
263 }
264
265 // startLabelName represents the state where the next byte read from p.buf is
266 // the start of a label name (or whitespace leading up to it).
267 func (p *TextParser) startLabelName() stateFn {
268         if p.skipBlankTab(); p.err != nil {
269                 return nil // Unexpected end of input.
270         }
271         if p.currentByte == '}' {
272                 if p.skipBlankTab(); p.err != nil {
273                         return nil // Unexpected end of input.
274                 }
275                 return p.readingValue
276         }
277         if p.readTokenAsLabelName(); p.err != nil {
278                 return nil // Unexpected end of input.
279         }
280         if p.currentToken.Len() == 0 {
281                 p.parseError(fmt.Sprintf("invalid label name for metric %q", p.currentMF.GetName()))
282                 return nil
283         }
284         p.currentLabelPair = &dto.LabelPair{Name: proto.String(p.currentToken.String())}
285         if p.currentLabelPair.GetName() == string(model.MetricNameLabel) {
286                 p.parseError(fmt.Sprintf("label name %q is reserved", model.MetricNameLabel))
287                 return nil
288         }
289         // Special summary/histogram treatment. Don't add 'quantile' and 'le'
290         // labels to 'real' labels.
291         if !(p.currentMF.GetType() == dto.MetricType_SUMMARY && p.currentLabelPair.GetName() == model.QuantileLabel) &&
292                 !(p.currentMF.GetType() == dto.MetricType_HISTOGRAM && p.currentLabelPair.GetName() == model.BucketLabel) {
293                 p.currentMetric.Label = append(p.currentMetric.Label, p.currentLabelPair)
294         }
295         if p.skipBlankTabIfCurrentBlankTab(); p.err != nil {
296                 return nil // Unexpected end of input.
297         }
298         if p.currentByte != '=' {
299                 p.parseError(fmt.Sprintf("expected '=' after label name, found %q", p.currentByte))
300                 return nil
301         }
302         return p.startLabelValue
303 }
304
305 // startLabelValue represents the state where the next byte read from p.buf is
306 // the start of a (quoted) label value (or whitespace leading up to it).
307 func (p *TextParser) startLabelValue() stateFn {
308         if p.skipBlankTab(); p.err != nil {
309                 return nil // Unexpected end of input.
310         }
311         if p.currentByte != '"' {
312                 p.parseError(fmt.Sprintf("expected '\"' at start of label value, found %q", p.currentByte))
313                 return nil
314         }
315         if p.readTokenAsLabelValue(); p.err != nil {
316                 return nil
317         }
318         p.currentLabelPair.Value = proto.String(p.currentToken.String())
319         // Special treatment of summaries:
320         // - Quantile labels are special, will result in dto.Quantile later.
321         // - Other labels have to be added to currentLabels for signature calculation.
322         if p.currentMF.GetType() == dto.MetricType_SUMMARY {
323                 if p.currentLabelPair.GetName() == model.QuantileLabel {
324                         if p.currentQuantile, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
325                                 // Create a more helpful error message.
326                                 p.parseError(fmt.Sprintf("expected float as value for 'quantile' label, got %q", p.currentLabelPair.GetValue()))
327                                 return nil
328                         }
329                 } else {
330                         p.currentLabels[p.currentLabelPair.GetName()] = p.currentLabelPair.GetValue()
331                 }
332         }
333         // Similar special treatment of histograms.
334         if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
335                 if p.currentLabelPair.GetName() == model.BucketLabel {
336                         if p.currentBucket, p.err = strconv.ParseFloat(p.currentLabelPair.GetValue(), 64); p.err != nil {
337                                 // Create a more helpful error message.
338                                 p.parseError(fmt.Sprintf("expected float as value for 'le' label, got %q", p.currentLabelPair.GetValue()))
339                                 return nil
340                         }
341                 } else {
342                         p.currentLabels[p.currentLabelPair.GetName()] = p.currentLabelPair.GetValue()
343                 }
344         }
345         if p.skipBlankTab(); p.err != nil {
346                 return nil // Unexpected end of input.
347         }
348         switch p.currentByte {
349         case ',':
350                 return p.startLabelName
351
352         case '}':
353                 if p.skipBlankTab(); p.err != nil {
354                         return nil // Unexpected end of input.
355                 }
356                 return p.readingValue
357         default:
358                 p.parseError(fmt.Sprintf("unexpected end of label value %q", p.currentLabelPair.Value))
359                 return nil
360         }
361 }
362
363 // readingValue represents the state where the last byte read (now in
364 // p.currentByte) is the first byte of the sample value (i.e. a float).
365 func (p *TextParser) readingValue() stateFn {
366         // When we are here, we have read all the labels, so for the
367         // special case of a summary/histogram, we can finally find out
368         // if the metric already exists.
369         if p.currentMF.GetType() == dto.MetricType_SUMMARY {
370                 signature := model.LabelsToSignature(p.currentLabels)
371                 if summary := p.summaries[signature]; summary != nil {
372                         p.currentMetric = summary
373                 } else {
374                         p.summaries[signature] = p.currentMetric
375                         p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
376                 }
377         } else if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
378                 signature := model.LabelsToSignature(p.currentLabels)
379                 if histogram := p.histograms[signature]; histogram != nil {
380                         p.currentMetric = histogram
381                 } else {
382                         p.histograms[signature] = p.currentMetric
383                         p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
384                 }
385         } else {
386                 p.currentMF.Metric = append(p.currentMF.Metric, p.currentMetric)
387         }
388         if p.readTokenUntilWhitespace(); p.err != nil {
389                 return nil // Unexpected end of input.
390         }
391         value, err := strconv.ParseFloat(p.currentToken.String(), 64)
392         if err != nil {
393                 // Create a more helpful error message.
394                 p.parseError(fmt.Sprintf("expected float as value, got %q", p.currentToken.String()))
395                 return nil
396         }
397         switch p.currentMF.GetType() {
398         case dto.MetricType_COUNTER:
399                 p.currentMetric.Counter = &dto.Counter{Value: proto.Float64(value)}
400         case dto.MetricType_GAUGE:
401                 p.currentMetric.Gauge = &dto.Gauge{Value: proto.Float64(value)}
402         case dto.MetricType_UNTYPED:
403                 p.currentMetric.Untyped = &dto.Untyped{Value: proto.Float64(value)}
404         case dto.MetricType_SUMMARY:
405                 // *sigh*
406                 if p.currentMetric.Summary == nil {
407                         p.currentMetric.Summary = &dto.Summary{}
408                 }
409                 switch {
410                 case p.currentIsSummaryCount:
411                         p.currentMetric.Summary.SampleCount = proto.Uint64(uint64(value))
412                 case p.currentIsSummarySum:
413                         p.currentMetric.Summary.SampleSum = proto.Float64(value)
414                 case !math.IsNaN(p.currentQuantile):
415                         p.currentMetric.Summary.Quantile = append(
416                                 p.currentMetric.Summary.Quantile,
417                                 &dto.Quantile{
418                                         Quantile: proto.Float64(p.currentQuantile),
419                                         Value:    proto.Float64(value),
420                                 },
421                         )
422                 }
423         case dto.MetricType_HISTOGRAM:
424                 // *sigh*
425                 if p.currentMetric.Histogram == nil {
426                         p.currentMetric.Histogram = &dto.Histogram{}
427                 }
428                 switch {
429                 case p.currentIsHistogramCount:
430                         p.currentMetric.Histogram.SampleCount = proto.Uint64(uint64(value))
431                 case p.currentIsHistogramSum:
432                         p.currentMetric.Histogram.SampleSum = proto.Float64(value)
433                 case !math.IsNaN(p.currentBucket):
434                         p.currentMetric.Histogram.Bucket = append(
435                                 p.currentMetric.Histogram.Bucket,
436                                 &dto.Bucket{
437                                         UpperBound:      proto.Float64(p.currentBucket),
438                                         CumulativeCount: proto.Uint64(uint64(value)),
439                                 },
440                         )
441                 }
442         default:
443                 p.err = fmt.Errorf("unexpected type for metric name %q", p.currentMF.GetName())
444         }
445         if p.currentByte == '\n' {
446                 return p.startOfLine
447         }
448         return p.startTimestamp
449 }
450
451 // startTimestamp represents the state where the next byte read from p.buf is
452 // the start of the timestamp (or whitespace leading up to it).
453 func (p *TextParser) startTimestamp() stateFn {
454         if p.skipBlankTab(); p.err != nil {
455                 return nil // Unexpected end of input.
456         }
457         if p.readTokenUntilWhitespace(); p.err != nil {
458                 return nil // Unexpected end of input.
459         }
460         timestamp, err := strconv.ParseInt(p.currentToken.String(), 10, 64)
461         if err != nil {
462                 // Create a more helpful error message.
463                 p.parseError(fmt.Sprintf("expected integer as timestamp, got %q", p.currentToken.String()))
464                 return nil
465         }
466         p.currentMetric.TimestampMs = proto.Int64(timestamp)
467         if p.readTokenUntilNewline(false); p.err != nil {
468                 return nil // Unexpected end of input.
469         }
470         if p.currentToken.Len() > 0 {
471                 p.parseError(fmt.Sprintf("spurious string after timestamp: %q", p.currentToken.String()))
472                 return nil
473         }
474         return p.startOfLine
475 }
476
477 // readingHelp represents the state where the last byte read (now in
478 // p.currentByte) is the first byte of the docstring after 'HELP'.
479 func (p *TextParser) readingHelp() stateFn {
480         if p.currentMF.Help != nil {
481                 p.parseError(fmt.Sprintf("second HELP line for metric name %q", p.currentMF.GetName()))
482                 return nil
483         }
484         // Rest of line is the docstring.
485         if p.readTokenUntilNewline(true); p.err != nil {
486                 return nil // Unexpected end of input.
487         }
488         p.currentMF.Help = proto.String(p.currentToken.String())
489         return p.startOfLine
490 }
491
492 // readingType represents the state where the last byte read (now in
493 // p.currentByte) is the first byte of the type hint after 'HELP'.
494 func (p *TextParser) readingType() stateFn {
495         if p.currentMF.Type != nil {
496                 p.parseError(fmt.Sprintf("second TYPE line for metric name %q, or TYPE reported after samples", p.currentMF.GetName()))
497                 return nil
498         }
499         // Rest of line is the type.
500         if p.readTokenUntilNewline(false); p.err != nil {
501                 return nil // Unexpected end of input.
502         }
503         metricType, ok := dto.MetricType_value[strings.ToUpper(p.currentToken.String())]
504         if !ok {
505                 p.parseError(fmt.Sprintf("unknown metric type %q", p.currentToken.String()))
506                 return nil
507         }
508         p.currentMF.Type = dto.MetricType(metricType).Enum()
509         return p.startOfLine
510 }
511
512 // parseError sets p.err to a ParseError at the current line with the given
513 // message.
514 func (p *TextParser) parseError(msg string) {
515         p.err = ParseError{
516                 Line: p.lineCount,
517                 Msg:  msg,
518         }
519 }
520
521 // skipBlankTab reads (and discards) bytes from p.buf until it encounters a byte
522 // that is neither ' ' nor '\t'. That byte is left in p.currentByte.
523 func (p *TextParser) skipBlankTab() {
524         for {
525                 if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil || !isBlankOrTab(p.currentByte) {
526                         return
527                 }
528         }
529 }
530
531 // skipBlankTabIfCurrentBlankTab works exactly as skipBlankTab but doesn't do
532 // anything if p.currentByte is neither ' ' nor '\t'.
533 func (p *TextParser) skipBlankTabIfCurrentBlankTab() {
534         if isBlankOrTab(p.currentByte) {
535                 p.skipBlankTab()
536         }
537 }
538
539 // readTokenUntilWhitespace copies bytes from p.buf into p.currentToken.  The
540 // first byte considered is the byte already read (now in p.currentByte).  The
541 // first whitespace byte encountered is still copied into p.currentByte, but not
542 // into p.currentToken.
543 func (p *TextParser) readTokenUntilWhitespace() {
544         p.currentToken.Reset()
545         for p.err == nil && !isBlankOrTab(p.currentByte) && p.currentByte != '\n' {
546                 p.currentToken.WriteByte(p.currentByte)
547                 p.currentByte, p.err = p.buf.ReadByte()
548         }
549 }
550
551 // readTokenUntilNewline copies bytes from p.buf into p.currentToken.  The first
552 // byte considered is the byte already read (now in p.currentByte).  The first
553 // newline byte encountered is still copied into p.currentByte, but not into
554 // p.currentToken. If recognizeEscapeSequence is true, two escape sequences are
555 // recognized: '\\' tranlates into '\', and '\n' into a line-feed character. All
556 // other escape sequences are invalid and cause an error.
557 func (p *TextParser) readTokenUntilNewline(recognizeEscapeSequence bool) {
558         p.currentToken.Reset()
559         escaped := false
560         for p.err == nil {
561                 if recognizeEscapeSequence && escaped {
562                         switch p.currentByte {
563                         case '\\':
564                                 p.currentToken.WriteByte(p.currentByte)
565                         case 'n':
566                                 p.currentToken.WriteByte('\n')
567                         default:
568                                 p.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", p.currentByte))
569                                 return
570                         }
571                         escaped = false
572                 } else {
573                         switch p.currentByte {
574                         case '\n':
575                                 return
576                         case '\\':
577                                 escaped = true
578                         default:
579                                 p.currentToken.WriteByte(p.currentByte)
580                         }
581                 }
582                 p.currentByte, p.err = p.buf.ReadByte()
583         }
584 }
585
586 // readTokenAsMetricName copies a metric name from p.buf into p.currentToken.
587 // The first byte considered is the byte already read (now in p.currentByte).
588 // The first byte not part of a metric name is still copied into p.currentByte,
589 // but not into p.currentToken.
590 func (p *TextParser) readTokenAsMetricName() {
591         p.currentToken.Reset()
592         if !isValidMetricNameStart(p.currentByte) {
593                 return
594         }
595         for {
596                 p.currentToken.WriteByte(p.currentByte)
597                 p.currentByte, p.err = p.buf.ReadByte()
598                 if p.err != nil || !isValidMetricNameContinuation(p.currentByte) {
599                         return
600                 }
601         }
602 }
603
604 // readTokenAsLabelName copies a label name from p.buf into p.currentToken.
605 // The first byte considered is the byte already read (now in p.currentByte).
606 // The first byte not part of a label name is still copied into p.currentByte,
607 // but not into p.currentToken.
608 func (p *TextParser) readTokenAsLabelName() {
609         p.currentToken.Reset()
610         if !isValidLabelNameStart(p.currentByte) {
611                 return
612         }
613         for {
614                 p.currentToken.WriteByte(p.currentByte)
615                 p.currentByte, p.err = p.buf.ReadByte()
616                 if p.err != nil || !isValidLabelNameContinuation(p.currentByte) {
617                         return
618                 }
619         }
620 }
621
622 // readTokenAsLabelValue copies a label value from p.buf into p.currentToken.
623 // In contrast to the other 'readTokenAs...' functions, which start with the
624 // last read byte in p.currentByte, this method ignores p.currentByte and starts
625 // with reading a new byte from p.buf. The first byte not part of a label value
626 // is still copied into p.currentByte, but not into p.currentToken.
627 func (p *TextParser) readTokenAsLabelValue() {
628         p.currentToken.Reset()
629         escaped := false
630         for {
631                 if p.currentByte, p.err = p.buf.ReadByte(); p.err != nil {
632                         return
633                 }
634                 if escaped {
635                         switch p.currentByte {
636                         case '"', '\\':
637                                 p.currentToken.WriteByte(p.currentByte)
638                         case 'n':
639                                 p.currentToken.WriteByte('\n')
640                         default:
641                                 p.parseError(fmt.Sprintf("invalid escape sequence '\\%c'", p.currentByte))
642                                 return
643                         }
644                         escaped = false
645                         continue
646                 }
647                 switch p.currentByte {
648                 case '"':
649                         return
650                 case '\n':
651                         p.parseError(fmt.Sprintf("label value %q contains unescaped new-line", p.currentToken.String()))
652                         return
653                 case '\\':
654                         escaped = true
655                 default:
656                         p.currentToken.WriteByte(p.currentByte)
657                 }
658         }
659 }
660
661 func (p *TextParser) setOrCreateCurrentMF() {
662         p.currentIsSummaryCount = false
663         p.currentIsSummarySum = false
664         p.currentIsHistogramCount = false
665         p.currentIsHistogramSum = false
666         name := p.currentToken.String()
667         if p.currentMF = p.metricFamiliesByName[name]; p.currentMF != nil {
668                 return
669         }
670         // Try out if this is a _sum or _count for a summary/histogram.
671         summaryName := summaryMetricName(name)
672         if p.currentMF = p.metricFamiliesByName[summaryName]; p.currentMF != nil {
673                 if p.currentMF.GetType() == dto.MetricType_SUMMARY {
674                         if isCount(name) {
675                                 p.currentIsSummaryCount = true
676                         }
677                         if isSum(name) {
678                                 p.currentIsSummarySum = true
679                         }
680                         return
681                 }
682         }
683         histogramName := histogramMetricName(name)
684         if p.currentMF = p.metricFamiliesByName[histogramName]; p.currentMF != nil {
685                 if p.currentMF.GetType() == dto.MetricType_HISTOGRAM {
686                         if isCount(name) {
687                                 p.currentIsHistogramCount = true
688                         }
689                         if isSum(name) {
690                                 p.currentIsHistogramSum = true
691                         }
692                         return
693                 }
694         }
695         p.currentMF = &dto.MetricFamily{Name: proto.String(name)}
696         p.metricFamiliesByName[name] = p.currentMF
697 }
698
699 func isValidLabelNameStart(b byte) bool {
700         return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_'
701 }
702
703 func isValidLabelNameContinuation(b byte) bool {
704         return isValidLabelNameStart(b) || (b >= '0' && b <= '9')
705 }
706
707 func isValidMetricNameStart(b byte) bool {
708         return isValidLabelNameStart(b) || b == ':'
709 }
710
711 func isValidMetricNameContinuation(b byte) bool {
712         return isValidLabelNameContinuation(b) || b == ':'
713 }
714
715 func isBlankOrTab(b byte) bool {
716         return b == ' ' || b == '\t'
717 }
718
719 func isCount(name string) bool {
720         return len(name) > 6 && name[len(name)-6:] == "_count"
721 }
722
723 func isSum(name string) bool {
724         return len(name) > 4 && name[len(name)-4:] == "_sum"
725 }
726
727 func isBucket(name string) bool {
728         return len(name) > 7 && name[len(name)-7:] == "_bucket"
729 }
730
731 func summaryMetricName(name string) string {
732         switch {
733         case isCount(name):
734                 return name[:len(name)-6]
735         case isSum(name):
736                 return name[:len(name)-4]
737         default:
738                 return name
739         }
740 }
741
742 func histogramMetricName(name string) string {
743         switch {
744         case isCount(name):
745                 return name[:len(name)-6]
746         case isSum(name):
747                 return name[:len(name)-4]
748         case isBucket(name):
749                 return name[:len(name)-7]
750         default:
751                 return name
752         }
753 }