Terminate output with a linefeed
This commit is contained in:
parent
8935a2402c
commit
33a7c9d0cf
3 changed files with 30 additions and 17 deletions
|
@ -13,14 +13,16 @@ export class MetricsRenderer {
|
||||||
}
|
}
|
||||||
|
|
||||||
render(metrics: Metric[]): string {
|
render(metrics: Metric[]): string {
|
||||||
return Object.entries(metrics.sort().group((metric) => this.metricName(metric.name)))
|
return (
|
||||||
.map(([name, metrics]) => {
|
Object.entries(metrics.sort().group((metric) => this.metricName(metric.name)))
|
||||||
return [
|
.map(([name, metrics]) => {
|
||||||
`# TYPE ${name} ${name.endsWith('_total') ? 'counter' : 'gauge'}`,
|
return [
|
||||||
metrics.map((metric) => this.formatMetric(metric)).join('\n'),
|
`# TYPE ${name} ${name.endsWith('_total') ? 'counter' : 'gauge'}`,
|
||||||
].join('\n')
|
metrics.map((metric) => this.formatMetric(metric)).join('\n'),
|
||||||
})
|
].join('\n')
|
||||||
.join('\n\n')
|
})
|
||||||
|
.join('\n\n') + '\n'
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private formatMetric(metric: Metric): string {
|
private formatMetric(metric: Metric): string {
|
||||||
|
|
|
@ -74,6 +74,7 @@ describe('Fastify HTTP adapter', () => {
|
||||||
'',
|
'',
|
||||||
'# TYPE homebridge_something_total counter',
|
'# TYPE homebridge_something_total counter',
|
||||||
'homebridge_something_total{name="counter"} 100 1577836800000',
|
'homebridge_something_total{name="counter"} 100 1577836800000',
|
||||||
|
'',
|
||||||
].join('\n'),
|
].join('\n'),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -121,6 +122,7 @@ describe('Fastify HTTP adapter', () => {
|
||||||
'',
|
'',
|
||||||
'# TYPE homebridge_something_total counter',
|
'# TYPE homebridge_something_total counter',
|
||||||
'homebridge_something_total{name="counter"} 100 1577836800000',
|
'homebridge_something_total{name="counter"} 100 1577836800000',
|
||||||
|
'',
|
||||||
].join('\n'),
|
].join('\n'),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,14 +8,16 @@ describe('Render prometheus metrics', () => {
|
||||||
test('Renders simple metric', () => {
|
test('Renders simple metric', () => {
|
||||||
expect(renderer.render([new Metric('metric', 0.000001)])).toEqual(
|
expect(renderer.render([new Metric('metric', 0.000001)])).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric 0.000001`,
|
prefix_metric 0.000001
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Renders simple metric with timestamp', () => {
|
test('Renders simple metric with timestamp', () => {
|
||||||
expect(renderer.render([new Metric('metric', 0.000001, new Date('2000-01-01 00:00:00 UTC'))])).toEqual(
|
expect(renderer.render([new Metric('metric', 0.000001, new Date('2000-01-01 00:00:00 UTC'))])).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric 0.000001 946684800000`,
|
prefix_metric 0.000001 946684800000
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -26,7 +28,8 @@ prefix_metric 0.000001 946684800000`,
|
||||||
]),
|
]),
|
||||||
).toEqual(
|
).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric{label="Some Label"} 0.000001 946684800000`,
|
prefix_metric{label="Some Label"} 0.000001 946684800000
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -38,7 +41,8 @@ prefix_metric{label="Some Label"} 0.000001 946684800000`,
|
||||||
]),
|
]),
|
||||||
).toEqual(
|
).toEqual(
|
||||||
`# TYPE prefix_some_metric_total counter
|
`# TYPE prefix_some_metric_total counter
|
||||||
prefix_some_metric_total{label="Some Label"} 42 946684800000`,
|
prefix_some_metric_total{label="Some Label"} 42 946684800000
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -56,35 +60,40 @@ prefix_some_gauge 10 946684800000
|
||||||
prefix_some_gauge 20 946684800000
|
prefix_some_gauge 20 946684800000
|
||||||
|
|
||||||
# TYPE prefix_another_gauge gauge
|
# TYPE prefix_another_gauge gauge
|
||||||
prefix_another_gauge 30 946684800000`,
|
prefix_another_gauge 30 946684800000
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Sanitizes metric names', () => {
|
test('Sanitizes metric names', () => {
|
||||||
expect(renderer.render([new Metric('mätric name', 0)])).toEqual(
|
expect(renderer.render([new Metric('mätric name', 0)])).toEqual(
|
||||||
`# TYPE prefix_m_tric_name gauge
|
`# TYPE prefix_m_tric_name gauge
|
||||||
prefix_m_tric_name 0`,
|
prefix_m_tric_name 0
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Sanitizes label names', () => {
|
test('Sanitizes label names', () => {
|
||||||
expect(renderer.render([new Metric('metric', 0, null, { 'yet another label': 'foo' })])).toEqual(
|
expect(renderer.render([new Metric('metric', 0, null, { 'yet another label': 'foo' })])).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric{yet_another_label="foo"} 0`,
|
prefix_metric{yet_another_label="foo"} 0
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Escapes newlines in attribute value', () => {
|
test('Escapes newlines in attribute value', () => {
|
||||||
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo\nbar' })])).toEqual(
|
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo\nbar' })])).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric{label="foo\\nbar"} 0`,
|
prefix_metric{label="foo\\nbar"} 0
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Escapes quotes in attribute value', () => {
|
test('Escapes quotes in attribute value', () => {
|
||||||
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo"bar' })])).toEqual(
|
expect(renderer.render([new Metric('metric', 0, null, { label: 'foo"bar' })])).toEqual(
|
||||||
`# TYPE prefix_metric gauge
|
`# TYPE prefix_metric gauge
|
||||||
prefix_metric{label="foo\\"bar"} 0`,
|
prefix_metric{label="foo\\"bar"} 0
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue