Skip to content

Commit 944e403

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

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

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

Lines changed: 8 additions & 15 deletions
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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,40 +173,30 @@ 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
},
187185
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",
186+
input: "This is a test.\n\nThis is another test.\n",
187+
expected: "This is a test.\n\nThis is another test.\n",
192188
}),
193189
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",
190+
input: "This is a test.\n\n\nThis is another test.\n",
191+
expected: "This is a test.\nThis is another test.\n",
198192
}),
199193
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",
194+
input: "\nThis is a test.\n\nThis is another test\n\n",
195+
expected: "This is a test.\n\nThis is another test",
204196
}),
205197
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",
198+
input: "\nThis is a test.\n\n\nThis is another test\n",
199+
expected: "This is a test.\nThis is another test",
210200
}),
211201
)
212202
})
@@ -217,7 +207,7 @@ var _ = Describe("Dedup", func() {
217207
expected string
218208
}
219209
DescribeTable("should", func(given testCase) {
220-
actual := string(dedupFn([]byte(given.input), '\n', '\n'))
210+
actual := string(dedup([]byte(given.input), '\n', '\n'))
221211
Expect(actual).To(Equal(given.expected))
222212
},
223213
Entry("should deduplicate newline characters", testCase{

0 commit comments

Comments
 (0)