text_parse.go 25 KB

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