@@ -11,9 +11,7 @@ import (
11
11
"net/http"
12
12
"net/url"
13
13
"os"
14
- "regexp"
15
14
"strconv"
16
- "strings"
17
15
"sync"
18
16
"time"
19
17
@@ -230,38 +228,53 @@ func processMetrics(metrices <-chan []byte, contentType expfmt.Format) []byte {
230
228
}
231
229
}
232
230
233
- str := processNewlineChars (buf .String (), true , true )
234
- str = fmt .Sprintf ("%s\n " , str )
231
+ processedMetrics := processNewlineChars (buf .Bytes (), true , true )
232
+ processedMetrics = append (processedMetrics , '\n' )
233
+ buf .Reset ()
234
+ _ , err := buf .Write (processedMetrics )
235
+ if err != nil {
236
+ panic (err )
237
+ }
235
238
236
- // if the content type is not OpenMetrics, we don't need to add EOF marker
237
- if ! (contentType == expfmt .FmtOpenMetrics_1_0_0 || contentType == expfmt .FmtOpenMetrics_0_0_1 ) {
238
- return []byte (str )
239
+ if contentType == expfmt .FmtOpenMetrics_1_0_0 || contentType == expfmt .FmtOpenMetrics_0_0_1 {
240
+ // make metrics OpenMetrics compliant
241
+ buf .Write ([]byte ("# EOF\n " ))
242
+ return buf .Bytes ()
239
243
}
240
244
241
- // make metrics OpenMetrics compliant
242
- return []byte (fmt .Sprintf ("%s# EOF\n " , str ))
245
+ return buf .Bytes ()
243
246
}
244
247
245
- // processNewlineChars processes the newline characters in the text .
246
- // If dedup is true, it replaces multiple newline characters with a single newline character.
248
+ // processNewlineChars processes the newline characters in the byteData .
249
+ // If dedup is true, it replaces multiple consecutive newline characters with a single newline character.
247
250
// If trim is true, it trims the leading and trailing newline characters.
248
- func processNewlineChars (text string , dedup , trim bool ) string {
251
+ func processNewlineChars (byteData [] byte , dedup , trim bool ) [] byte {
249
252
if dedup {
250
- // Create a regular expression to match multiple newline characters.
251
- reg := regexp .MustCompile (`(\r\n?|\n){2,}` )
252
-
253
- // Replace all the matches with a single newline character.
254
- text = reg .ReplaceAllString (text , "\n " )
253
+ byteData = dedupFn (byteData , '\n' , '\n' )
255
254
}
256
-
257
255
if trim {
258
- // Trim the leading and trailing newline characters.
259
- text = strings .TrimFunc (text , func (r rune ) bool {
260
- return r == '\n'
261
- })
256
+ byteData = bytes .TrimSpace (byteData )
262
257
}
258
+ return byteData
259
+ }
263
260
264
- return text
261
+ func dedupFn (input []byte , old , new byte ) []byte {
262
+ var deduped []byte
263
+
264
+ var last byte
265
+ if len (input ) > 0 {
266
+ last = input [0 ]
267
+ }
268
+ for i := 1 ; i < len (input ); i ++ {
269
+ if last == old && input [i ] == last {
270
+ last = new
271
+ continue
272
+ }
273
+ deduped = append (deduped , last )
274
+ last = input [i ]
275
+ }
276
+ deduped = append (deduped , last )
277
+ return deduped
265
278
}
266
279
267
280
// selectContentType selects the highest priority content type supported by the applications.
0 commit comments