Skip to content

Commit

Permalink
Terminate output with a linefeed
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jan 8, 2023
1 parent 8935a24 commit 33a7c9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ export class MetricsRenderer {
}

render(metrics: Metric[]): string {
return Object.entries(metrics.sort().group((metric) => this.metricName(metric.name)))
.map(([name, metrics]) => {
return [
`# TYPE ${name} ${name.endsWith('_total') ? 'counter' : 'gauge'}`,
metrics.map((metric) => this.formatMetric(metric)).join('\n'),
].join('\n')
})
.join('\n\n')
return (
Object.entries(metrics.sort().group((metric) => this.metricName(metric.name)))
.map(([name, metrics]) => {
return [
`# TYPE ${name} ${name.endsWith('_total') ? 'counter' : 'gauge'}`,
metrics.map((metric) => this.formatMetric(metric)).join('\n'),
].join('\n')
})
.join('\n\n') + '\n'
)
}

private formatMetric(metric: Metric): string {
Expand Down
2 changes: 2 additions & 0 deletions tests/adapters/http/fastify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Fastify HTTP adapter', () => {
'',
'# TYPE homebridge_something_total counter',
'homebridge_something_total{name="counter"} 100 1577836800000',
'',
].join('\n'),
)
})
Expand Down Expand Up @@ -121,6 +122,7 @@ describe('Fastify HTTP adapter', () => {
'',
'# TYPE homebridge_something_total counter',
'homebridge_something_total{name="counter"} 100 1577836800000',
'',
].join('\n'),
)
})
Expand Down
27 changes: 18 additions & 9 deletions tests/prometheus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ describe('Render prometheus metrics', () => {
test('Renders simple metric', () => {
expect(renderer.render([new Metric('metric', 0.000001)])).toEqual(
`# TYPE prefix_metric gauge
prefix_metric 0.000001`,
prefix_metric 0.000001
`,
)
})

test('Renders simple metric with timestamp', () => {
expect(renderer.render([new Metric('metric', 0.000001, new Date('2000-01-01 00:00:00 UTC'))])).toEqual(
`# TYPE prefix_metric gauge
prefix_metric 0.000001 946684800000`,
prefix_metric 0.000001 946684800000
`,
)
})

Expand All @@ -26,7 +28,8 @@ prefix_metric 0.000001 946684800000`,
]),
).toEqual(
`# TYPE prefix_metric gauge
prefix_metric{label="Some Label"} 0.000001 946684800000`,
prefix_metric{label="Some Label"} 0.000001 946684800000
`,
)
})

Expand All @@ -38,7 +41,8 @@ prefix_metric{label="Some Label"} 0.000001 946684800000`,
]),
).toEqual(
`# TYPE prefix_some_metric_total counter
prefix_some_metric_total{label="Some Label"} 42 946684800000`,
prefix_some_metric_total{label="Some Label"} 42 946684800000
`,
)
}
})
Expand All @@ -56,35 +60,40 @@ prefix_some_gauge 10 946684800000
prefix_some_gauge 20 946684800000
# TYPE prefix_another_gauge gauge
prefix_another_gauge 30 946684800000`,
prefix_another_gauge 30 946684800000
`,
)
})

test('Sanitizes metric names', () => {
expect(renderer.render([new Metric('mätric name', 0)])).toEqual(
`# TYPE prefix_m_tric_name gauge
prefix_m_tric_name 0`,
prefix_m_tric_name 0
`,
)
})

test('Sanitizes label names', () => {
expect(renderer.render([new Metric('metric', 0, null, { 'yet another label': 'foo' })])).toEqual(
`# TYPE prefix_metric gauge
prefix_metric{yet_another_label="foo"} 0`,
prefix_metric{yet_another_label="foo"} 0
`,
)
})

test('Escapes newlines in attribute value', () => {
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo\nbar' })])).toEqual(
`# TYPE prefix_metric gauge
prefix_metric{label="foo\\nbar"} 0`,
prefix_metric{label="foo\\nbar"} 0
`,
)
})

test('Escapes quotes in attribute value', () => {
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo"bar' })])).toEqual(
`# TYPE prefix_metric gauge
prefix_metric{label="foo\\"bar"} 0`,
prefix_metric{label="foo\\"bar"} 0
`,
)
})
})

0 comments on commit 33a7c9d

Please sign in to comment.