expfmt.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2015 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 contains tools for reading and writing Prometheus metrics.
  14. package expfmt
  15. import (
  16. "strings"
  17. "github.com/prometheus/common/model"
  18. )
  19. // Format specifies the HTTP content type of the different wire protocols.
  20. type Format string
  21. // Constants to assemble the Content-Type values for the different wire
  22. // protocols. The Content-Type strings here are all for the legacy exposition
  23. // formats, where valid characters for metric names and label names are limited.
  24. // Support for arbitrary UTF-8 characters in those names is already partially
  25. // implemented in this module (see model.ValidationScheme), but to actually use
  26. // it on the wire, new content-type strings will have to be agreed upon and
  27. // added here.
  28. const (
  29. TextVersion = "0.0.4"
  30. ProtoType = `application/vnd.google.protobuf`
  31. ProtoProtocol = `io.prometheus.client.MetricFamily`
  32. protoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"
  33. OpenMetricsType = `application/openmetrics-text`
  34. OpenMetricsVersion_0_0_1 = "0.0.1"
  35. OpenMetricsVersion_1_0_0 = "1.0.0"
  36. // The Content-Type values for the different wire protocols. Note that these
  37. // values are now unexported. If code was relying on comparisons to these
  38. // constants, instead use FormatType().
  39. fmtUnknown Format = `<unknown>`
  40. fmtText Format = `text/plain; version=` + TextVersion + `; charset=utf-8`
  41. fmtProtoDelim Format = protoFmt + ` encoding=delimited`
  42. fmtProtoText Format = protoFmt + ` encoding=text`
  43. fmtProtoCompact Format = protoFmt + ` encoding=compact-text`
  44. fmtOpenMetrics_1_0_0 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_1_0_0 + `; charset=utf-8`
  45. fmtOpenMetrics_0_0_1 Format = OpenMetricsType + `; version=` + OpenMetricsVersion_0_0_1 + `; charset=utf-8`
  46. )
  47. const (
  48. hdrContentType = "Content-Type"
  49. hdrAccept = "Accept"
  50. )
  51. // FormatType is a Go enum representing the overall category for the given
  52. // Format. As the number of Format permutations increases, doing basic string
  53. // comparisons are not feasible, so this enum captures the most useful
  54. // high-level attribute of the Format string.
  55. type FormatType int
  56. const (
  57. TypeUnknown = iota
  58. TypeProtoCompact
  59. TypeProtoDelim
  60. TypeProtoText
  61. TypeTextPlain
  62. TypeOpenMetrics
  63. )
  64. // NewFormat generates a new Format from the type provided. Mostly used for
  65. // tests, most Formats should be generated as part of content negotiation in
  66. // encode.go.
  67. func NewFormat(t FormatType) Format {
  68. switch t {
  69. case TypeProtoCompact:
  70. return fmtProtoCompact
  71. case TypeProtoDelim:
  72. return fmtProtoDelim
  73. case TypeProtoText:
  74. return fmtProtoText
  75. case TypeTextPlain:
  76. return fmtText
  77. case TypeOpenMetrics:
  78. return fmtOpenMetrics_1_0_0
  79. default:
  80. return fmtUnknown
  81. }
  82. }
  83. // FormatType deduces an overall FormatType for the given format.
  84. func (f Format) FormatType() FormatType {
  85. toks := strings.Split(string(f), ";")
  86. if len(toks) < 2 {
  87. return TypeUnknown
  88. }
  89. params := make(map[string]string)
  90. for i, t := range toks {
  91. if i == 0 {
  92. continue
  93. }
  94. args := strings.Split(t, "=")
  95. if len(args) != 2 {
  96. continue
  97. }
  98. params[strings.TrimSpace(args[0])] = strings.TrimSpace(args[1])
  99. }
  100. switch strings.TrimSpace(toks[0]) {
  101. case ProtoType:
  102. if params["proto"] != ProtoProtocol {
  103. return TypeUnknown
  104. }
  105. switch params["encoding"] {
  106. case "delimited":
  107. return TypeProtoDelim
  108. case "text":
  109. return TypeProtoText
  110. case "compact-text":
  111. return TypeProtoCompact
  112. default:
  113. return TypeUnknown
  114. }
  115. case OpenMetricsType:
  116. if params["charset"] != "utf-8" {
  117. return TypeUnknown
  118. }
  119. return TypeOpenMetrics
  120. case "text/plain":
  121. v, ok := params["version"]
  122. if !ok {
  123. return TypeTextPlain
  124. }
  125. if v == TextVersion {
  126. return TypeTextPlain
  127. }
  128. return TypeUnknown
  129. default:
  130. return TypeUnknown
  131. }
  132. }
  133. // ToEscapingScheme returns an EscapingScheme depending on the Format. Iff the
  134. // Format contains a escaping=allow-utf-8 term, it will select NoEscaping. If a valid
  135. // "escaping" term exists, that will be used. Otherwise, the global default will
  136. // be returned.
  137. func (format Format) ToEscapingScheme() model.EscapingScheme {
  138. for _, p := range strings.Split(string(format), ";") {
  139. toks := strings.Split(p, "=")
  140. if len(toks) != 2 {
  141. continue
  142. }
  143. key, value := strings.TrimSpace(toks[0]), strings.TrimSpace(toks[1])
  144. if key == model.EscapingKey {
  145. scheme, err := model.ToEscapingScheme(value)
  146. if err != nil {
  147. return model.NameEscapingScheme
  148. }
  149. return scheme
  150. }
  151. }
  152. return model.NameEscapingScheme
  153. }