Skip to content

Commit 30726d0

Browse files
committed
refactor(metrics): remove unnecessary params
Signed-off-by: AyushSenapati <a.p.senapati008@gmail.com>
1 parent ffb7ab1 commit 30726d0

File tree

2 files changed

+17
-58
lines changed

2 files changed

+17
-58
lines changed

app/kuma-dp/pkg/dataplane/metrics/server.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ func (s *Hijacker) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
212212
}
213213
}
214214

215-
func processMetrics(metrices <-chan []byte, contentType expfmt.Format) []byte {
215+
func processMetrics(contents <-chan []byte, contentType expfmt.Format) []byte {
216216
buf := new(bytes.Buffer)
217217

218-
for metrics := range metrices {
218+
for metrics := range contents {
219219
// remove the EOF marker from the metrics, because we are
220220
// merging multiple metrics into one response.
221221
metrics = bytes.ReplaceAll(metrics, []byte("# EOF"), []byte(""))
@@ -228,7 +228,7 @@ func processMetrics(metrices <-chan []byte, contentType expfmt.Format) []byte {
228228
}
229229
}
230230

231-
processedMetrics := processNewlineChars(buf.Bytes(), true, true)
231+
processedMetrics := processNewlineChars(buf.Bytes())
232232
processedMetrics = append(processedMetrics, '\n')
233233
buf.Reset()
234234
_, err := buf.Write(processedMetrics)
@@ -245,20 +245,13 @@ func processMetrics(metrices <-chan []byte, contentType expfmt.Format) []byte {
245245
return buf.Bytes()
246246
}
247247

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.
250-
// If trim is true, it trims the leading and trailing newline characters.
251-
func processNewlineChars(byteData []byte, dedup, trim bool) []byte {
252-
if dedup {
253-
byteData = dedupFn(byteData, '\n', '\n')
254-
}
255-
if trim {
256-
byteData = bytes.TrimSpace(byteData)
257-
}
258-
return byteData
248+
// processNewlineChars takes byte data and returns a new byte slice
249+
// after trimming and deduplicating the newline characters.
250+
func processNewlineChars(byteData []byte) []byte {
251+
return bytes.TrimSpace(dedup(byteData, '\n', '\n'))
259252
}
260253

261-
func dedupFn(input []byte, old, new byte) []byte {
254+
func dedup(input []byte, old, new byte) []byte {
262255
var deduped []byte
263256

264257
var last byte

app/kuma-dp/pkg/dataplane/metrics/server_test.go

+9-43
Original file line numberDiff line numberDiff line change
@@ -173,56 +173,22 @@ var _ = Describe("Process Metrics", func() {
173173

174174
var _ = Describe("ProcessNewlineChars", func() {
175175
type testCase struct {
176-
input string
177-
deduplicate bool
178-
trim bool
179-
expected string
176+
input string
177+
expected string
180178
}
181179

182180
DescribeTable("should",
183181
func(given testCase) {
184-
actual := string(processNewlineChars([]byte(given.input), given.deduplicate, given.trim))
182+
actual := string(processNewlineChars([]byte(given.input)))
185183
Expect(actual).To(Equal(given.expected))
186184
},
187-
Entry("should not deduplicate or trim newline characters", testCase{
188-
input: "This is a test.\n\nThis is another test.\n",
189-
deduplicate: false,
190-
trim: false,
191-
expected: "This is a test.\n\nThis is another test.\n",
192-
}),
193-
Entry("should deduplicate newline characters", testCase{
194-
input: "This is a test.\n\n\nThis is another test.\n",
195-
deduplicate: true,
196-
trim: false,
197-
expected: "This is a test.\nThis is another test.\n",
185+
Entry("should dedup newline characters and trim spaces", testCase{
186+
input: "\n This is a test.\n\n\n\nThis is another test\n\n ",
187+
expected: "This is a test.\nThis is another test",
198188
}),
199-
Entry("should trim leading and trailing newline characters", testCase{
200-
input: "\nThis is a test.\n\nThis is another test\n\n",
201-
deduplicate: false,
202-
trim: true,
203-
expected: "This is a test.\n\nThis is another test",
204-
}),
205-
Entry("should deduplicate and trim newline characters", testCase{
206-
input: "\nThis is a test.\n\n\nThis is another test\n",
207-
deduplicate: true,
208-
trim: true,
209-
expected: "This is a test.\nThis is another test",
210-
}),
211-
)
212-
})
213-
214-
var _ = Describe("Dedup", func() {
215-
type testCase struct {
216-
input string
217-
expected string
218-
}
219-
DescribeTable("should", func(given testCase) {
220-
actual := string(dedupFn([]byte(given.input), '\n', '\n'))
221-
Expect(actual).To(Equal(given.expected))
222-
},
223-
Entry("should deduplicate newline characters", testCase{
224-
input: "This is a test.\n\n\n\nThis is another test.\n",
225-
expected: "This is a test.\nThis is another test.\n",
189+
Entry("should dedup newline characters and trim spaces", testCase{
190+
input: "\nThis is a test.\n \nThis is another test\n",
191+
expected: "This is a test.\n \nThis is another test",
226192
}),
227193
)
228194
})

0 commit comments

Comments
 (0)