keyvalues.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package serialize
  14. import (
  15. "bytes"
  16. "fmt"
  17. "strconv"
  18. "github.com/go-logr/logr"
  19. )
  20. type textWriter interface {
  21. WriteText(*bytes.Buffer)
  22. }
  23. // WithValues implements LogSink.WithValues. The old key/value pairs are
  24. // assumed to be well-formed, the new ones are checked and padded if
  25. // necessary. It returns a new slice.
  26. func WithValues(oldKV, newKV []interface{}) []interface{} {
  27. if len(newKV) == 0 {
  28. return oldKV
  29. }
  30. newLen := len(oldKV) + len(newKV)
  31. hasMissingValue := newLen%2 != 0
  32. if hasMissingValue {
  33. newLen++
  34. }
  35. // The new LogSink must have its own slice.
  36. kv := make([]interface{}, 0, newLen)
  37. kv = append(kv, oldKV...)
  38. kv = append(kv, newKV...)
  39. if hasMissingValue {
  40. kv = append(kv, missingValue)
  41. }
  42. return kv
  43. }
  44. // MergeKVs deduplicates elements provided in two key/value slices.
  45. //
  46. // Keys in each slice are expected to be unique, so duplicates can only occur
  47. // when the first and second slice contain the same key. When that happens, the
  48. // key/value pair from the second slice is used. The first slice must be well-formed
  49. // (= even key/value pairs). The second one may have a missing value, in which
  50. // case the special "missing value" is added to the result.
  51. func MergeKVs(first, second []interface{}) []interface{} {
  52. maxLength := len(first) + (len(second)+1)/2*2
  53. if maxLength == 0 {
  54. // Nothing to do at all.
  55. return nil
  56. }
  57. if len(first) == 0 && len(second)%2 == 0 {
  58. // Nothing to be overridden, second slice is well-formed
  59. // and can be used directly.
  60. return second
  61. }
  62. // Determine which keys are in the second slice so that we can skip
  63. // them when iterating over the first one. The code intentionally
  64. // favors performance over completeness: we assume that keys are string
  65. // constants and thus compare equal when the string values are equal. A
  66. // string constant being overridden by, for example, a fmt.Stringer is
  67. // not handled.
  68. overrides := map[interface{}]bool{}
  69. for i := 0; i < len(second); i += 2 {
  70. overrides[second[i]] = true
  71. }
  72. merged := make([]interface{}, 0, maxLength)
  73. for i := 0; i+1 < len(first); i += 2 {
  74. key := first[i]
  75. if overrides[key] {
  76. continue
  77. }
  78. merged = append(merged, key, first[i+1])
  79. }
  80. merged = append(merged, second...)
  81. if len(merged)%2 != 0 {
  82. merged = append(merged, missingValue)
  83. }
  84. return merged
  85. }
  86. type Formatter struct {
  87. AnyToStringHook AnyToStringFunc
  88. }
  89. type AnyToStringFunc func(v interface{}) string
  90. // MergeKVsInto is a variant of MergeKVs which directly formats the key/value
  91. // pairs into a buffer.
  92. func (f Formatter) MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
  93. if len(first) == 0 && len(second) == 0 {
  94. // Nothing to do at all.
  95. return
  96. }
  97. if len(first) == 0 && len(second)%2 == 0 {
  98. // Nothing to be overridden, second slice is well-formed
  99. // and can be used directly.
  100. for i := 0; i < len(second); i += 2 {
  101. f.KVFormat(b, second[i], second[i+1])
  102. }
  103. return
  104. }
  105. // Determine which keys are in the second slice so that we can skip
  106. // them when iterating over the first one. The code intentionally
  107. // favors performance over completeness: we assume that keys are string
  108. // constants and thus compare equal when the string values are equal. A
  109. // string constant being overridden by, for example, a fmt.Stringer is
  110. // not handled.
  111. overrides := map[interface{}]bool{}
  112. for i := 0; i < len(second); i += 2 {
  113. overrides[second[i]] = true
  114. }
  115. for i := 0; i < len(first); i += 2 {
  116. key := first[i]
  117. if overrides[key] {
  118. continue
  119. }
  120. f.KVFormat(b, key, first[i+1])
  121. }
  122. // Round down.
  123. l := len(second)
  124. l = l / 2 * 2
  125. for i := 1; i < l; i += 2 {
  126. f.KVFormat(b, second[i-1], second[i])
  127. }
  128. if len(second)%2 == 1 {
  129. f.KVFormat(b, second[len(second)-1], missingValue)
  130. }
  131. }
  132. func MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
  133. Formatter{}.MergeAndFormatKVs(b, first, second)
  134. }
  135. const missingValue = "(MISSING)"
  136. // KVListFormat serializes all key/value pairs into the provided buffer.
  137. // A space gets inserted before the first pair and between each pair.
  138. func (f Formatter) KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
  139. for i := 0; i < len(keysAndValues); i += 2 {
  140. var v interface{}
  141. k := keysAndValues[i]
  142. if i+1 < len(keysAndValues) {
  143. v = keysAndValues[i+1]
  144. } else {
  145. v = missingValue
  146. }
  147. f.KVFormat(b, k, v)
  148. }
  149. }
  150. func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
  151. Formatter{}.KVListFormat(b, keysAndValues...)
  152. }
  153. // KVFormat serializes one key/value pair into the provided buffer.
  154. // A space gets inserted before the pair.
  155. func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
  156. b.WriteByte(' ')
  157. // Keys are assumed to be well-formed according to
  158. // https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments
  159. // for the sake of performance. Keys with spaces,
  160. // special characters, etc. will break parsing.
  161. if sK, ok := k.(string); ok {
  162. // Avoid one allocation when the key is a string, which
  163. // normally it should be.
  164. b.WriteString(sK)
  165. } else {
  166. b.WriteString(fmt.Sprintf("%s", k))
  167. }
  168. // The type checks are sorted so that more frequently used ones
  169. // come first because that is then faster in the common
  170. // cases. In Kubernetes, ObjectRef (a Stringer) is more common
  171. // than plain strings
  172. // (https://github.com/kubernetes/kubernetes/pull/106594#issuecomment-975526235).
  173. switch v := v.(type) {
  174. case textWriter:
  175. writeTextWriterValue(b, v)
  176. case fmt.Stringer:
  177. writeStringValue(b, true, StringerToString(v))
  178. case string:
  179. writeStringValue(b, true, v)
  180. case error:
  181. writeStringValue(b, true, ErrorToString(v))
  182. case logr.Marshaler:
  183. value := MarshalerToValue(v)
  184. // A marshaler that returns a string is useful for
  185. // delayed formatting of complex values. We treat this
  186. // case like a normal string. This is useful for
  187. // multi-line support.
  188. //
  189. // We could do this by recursively formatting a value,
  190. // but that comes with the risk of infinite recursion
  191. // if a marshaler returns itself. Instead we call it
  192. // only once and rely on it returning the intended
  193. // value directly.
  194. switch value := value.(type) {
  195. case string:
  196. writeStringValue(b, true, value)
  197. default:
  198. writeStringValue(b, false, f.AnyToString(value))
  199. }
  200. case []byte:
  201. // In https://github.com/kubernetes/klog/pull/237 it was decided
  202. // to format byte slices with "%+q". The advantages of that are:
  203. // - readable output if the bytes happen to be printable
  204. // - non-printable bytes get represented as unicode escape
  205. // sequences (\uxxxx)
  206. //
  207. // The downsides are that we cannot use the faster
  208. // strconv.Quote here and that multi-line output is not
  209. // supported. If developers know that a byte array is
  210. // printable and they want multi-line output, they can
  211. // convert the value to string before logging it.
  212. b.WriteByte('=')
  213. b.WriteString(fmt.Sprintf("%+q", v))
  214. default:
  215. writeStringValue(b, false, f.AnyToString(v))
  216. }
  217. }
  218. func KVFormat(b *bytes.Buffer, k, v interface{}) {
  219. Formatter{}.KVFormat(b, k, v)
  220. }
  221. // AnyToString is the historic fallback formatter.
  222. func (f Formatter) AnyToString(v interface{}) string {
  223. if f.AnyToStringHook != nil {
  224. return f.AnyToStringHook(v)
  225. }
  226. return fmt.Sprintf("%+v", v)
  227. }
  228. // StringerToString converts a Stringer to a string,
  229. // handling panics if they occur.
  230. func StringerToString(s fmt.Stringer) (ret string) {
  231. defer func() {
  232. if err := recover(); err != nil {
  233. ret = fmt.Sprintf("<panic: %s>", err)
  234. }
  235. }()
  236. ret = s.String()
  237. return
  238. }
  239. // MarshalerToValue invokes a marshaler and catches
  240. // panics.
  241. func MarshalerToValue(m logr.Marshaler) (ret interface{}) {
  242. defer func() {
  243. if err := recover(); err != nil {
  244. ret = fmt.Sprintf("<panic: %s>", err)
  245. }
  246. }()
  247. ret = m.MarshalLog()
  248. return
  249. }
  250. // ErrorToString converts an error to a string,
  251. // handling panics if they occur.
  252. func ErrorToString(err error) (ret string) {
  253. defer func() {
  254. if err := recover(); err != nil {
  255. ret = fmt.Sprintf("<panic: %s>", err)
  256. }
  257. }()
  258. ret = err.Error()
  259. return
  260. }
  261. func writeTextWriterValue(b *bytes.Buffer, v textWriter) {
  262. b.WriteRune('=')
  263. defer func() {
  264. if err := recover(); err != nil {
  265. fmt.Fprintf(b, `"<panic: %s>"`, err)
  266. }
  267. }()
  268. v.WriteText(b)
  269. }
  270. func writeStringValue(b *bytes.Buffer, quote bool, v string) {
  271. data := []byte(v)
  272. index := bytes.IndexByte(data, '\n')
  273. if index == -1 {
  274. b.WriteByte('=')
  275. if quote {
  276. // Simple string, quote quotation marks and non-printable characters.
  277. b.WriteString(strconv.Quote(v))
  278. return
  279. }
  280. // Non-string with no line breaks.
  281. b.WriteString(v)
  282. return
  283. }
  284. // Complex multi-line string, show as-is with indention like this:
  285. // I... "hello world" key=<
  286. // <tab>line 1
  287. // <tab>line 2
  288. // >
  289. //
  290. // Tabs indent the lines of the value while the end of string delimiter
  291. // is indented with a space. That has two purposes:
  292. // - visual difference between the two for a human reader because indention
  293. // will be different
  294. // - no ambiguity when some value line starts with the end delimiter
  295. //
  296. // One downside is that the output cannot distinguish between strings that
  297. // end with a line break and those that don't because the end delimiter
  298. // will always be on the next line.
  299. b.WriteString("=<\n")
  300. for index != -1 {
  301. b.WriteByte('\t')
  302. b.Write(data[0 : index+1])
  303. data = data[index+1:]
  304. index = bytes.IndexByte(data, '\n')
  305. }
  306. if len(data) == 0 {
  307. // String ended with line break, don't add another.
  308. b.WriteString(" >")
  309. } else {
  310. // No line break at end of last line, write rest of string and
  311. // add one.
  312. b.WriteByte('\t')
  313. b.Write(data)
  314. b.WriteString("\n >")
  315. }
  316. }