2020-10-24 23:57:45 +05:30
|
|
|
import { getSeriesLabel, makeDataSeries } from '~/helpers/monitor_helper';
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
describe('monitor helper', () => {
|
|
|
|
const defaultConfig = { default: true, name: 'default name' };
|
|
|
|
const name = 'data name';
|
2021-03-08 18:12:59 +05:30
|
|
|
const series = [
|
|
|
|
[1, 1],
|
|
|
|
[2, 2],
|
|
|
|
[3, 3],
|
|
|
|
];
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
describe('getSeriesLabel', () => {
|
|
|
|
const metricAttributes = { __name__: 'up', app: 'prometheus' };
|
|
|
|
|
|
|
|
it('gets a single attribute label', () => {
|
|
|
|
expect(getSeriesLabel('app', metricAttributes)).toBe('app: prometheus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a templated label', () => {
|
|
|
|
expect(getSeriesLabel('{{__name__}}', metricAttributes)).toBe('up');
|
|
|
|
expect(getSeriesLabel('{{app}}', metricAttributes)).toBe('prometheus');
|
|
|
|
expect(getSeriesLabel('{{missing}}', metricAttributes)).toBe('{{missing}}');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a multiple label', () => {
|
|
|
|
expect(getSeriesLabel(null, metricAttributes)).toBe('__name__: up, app: prometheus');
|
|
|
|
expect(getSeriesLabel('', metricAttributes)).toBe('__name__: up, app: prometheus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a simple label', () => {
|
|
|
|
expect(getSeriesLabel('A label', {})).toBe('A label');
|
|
|
|
});
|
|
|
|
});
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
describe('makeDataSeries', () => {
|
2020-10-24 23:57:45 +05:30
|
|
|
const data = ({ metric = { default_name: name }, values = series } = {}) => [
|
|
|
|
{ metric, values },
|
|
|
|
];
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
const expectedDataSeries = [
|
|
|
|
{
|
|
|
|
...defaultConfig,
|
|
|
|
data: series,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
it('converts query results to data series', () => {
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(makeDataSeries(data({ metric: {} }), defaultConfig)).toEqual(expectedDataSeries);
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty array if no query results exist', () => {
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(makeDataSeries([], defaultConfig)).toEqual([]);
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('handles multi-series query results', () => {
|
|
|
|
const expectedData = { ...expectedDataSeries[0], name: 'default name: data name' };
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(makeDataSeries([...data(), ...data()], defaultConfig)).toEqual([
|
2019-07-07 11:18:12 +05:30
|
|
|
expectedData,
|
|
|
|
expectedData,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
it('updates series name from templates', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries([{ metric: { cmd: 'brpop' }, values: series }], config);
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
expect(result.name).toEqual('brpop');
|
|
|
|
});
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it('supports a multi metric label template expression', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '',
|
|
|
|
};
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries(
|
2020-04-22 19:07:51 +05:30
|
|
|
[
|
|
|
|
{
|
|
|
|
metric: {
|
|
|
|
backend: 'HA Server',
|
|
|
|
frontend: 'BA Server',
|
|
|
|
app: 'prometheus',
|
|
|
|
instance: 'k8 cluster 1',
|
|
|
|
},
|
|
|
|
values: series,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toBe(
|
|
|
|
'backend: HA Server, frontend: BA Server, app: prometheus, instance: k8 cluster 1',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
it('supports space-padded template expressions', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: 'backend: {{ backend }}',
|
|
|
|
};
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries(
|
2019-12-26 22:10:19 +05:30
|
|
|
[{ metric: { backend: 'HA Server' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('backend: HA Server');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports repeated template variables', () => {
|
|
|
|
const config = { ...defaultConfig, name: '{{cmd}}, {{cmd}}' };
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries([{ metric: { cmd: 'brpop' }, values: series }], config);
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
expect(result.name).toEqual('brpop, brpop');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports hyphenated template variables', () => {
|
|
|
|
const config = { ...defaultConfig, name: 'expired - {{ test-attribute }}' };
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries(
|
2019-12-26 22:10:19 +05:30
|
|
|
[{ metric: { 'test-attribute': 'test-attribute-value' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('expired - test-attribute-value');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates multiple series names from templates', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{job}}: {{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [result] = makeDataSeries(
|
2019-12-26 22:10:19 +05:30
|
|
|
[{ metric: { cmd: 'brpop', job: 'redis' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('redis: brpop');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates name for each series', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
const [firstSeries, secondSeries] = makeDataSeries(
|
2019-12-26 22:10:19 +05:30
|
|
|
[
|
|
|
|
{ metric: { cmd: 'brpop' }, values: series },
|
|
|
|
{ metric: { cmd: 'zrangebyscore' }, values: series },
|
|
|
|
],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(firstSeries.name).toEqual('brpop');
|
|
|
|
expect(secondSeries.name).toEqual('zrangebyscore');
|
|
|
|
});
|
2019-07-07 11:18:12 +05:30
|
|
|
});
|
|
|
|
});
|