Skip to content

Commit

Permalink
[Monitoring] Add tests to ensure mappings exist on metric fields (ela…
Browse files Browse the repository at this point in the history
…stic#23958)

* Ensure mappings exist

* Add APM

* Remove unnecessary eslint disable comment

* Update snapshots
  • Loading branch information
chrisronline committed Oct 22, 2018
1 parent 8c6f1e6 commit 25f4843
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,7 @@ Object {
"description": "The number of operations the follower index is lagging behind the leader.",
"field": "",
"format": "0,0.[00]",
"getFields": [Function],
"label": "Ops delay",
"metricAgg": "sum",
"timestampField": "timestamp",
Expand Down Expand Up @@ -2277,7 +2278,7 @@ Object {
"app": "elasticsearch",
"derivative": true,
"description": "Time spent on Elasticsearch refresh for primary and replica shards.",
"field": "total.refresh.total_time_in_millis",
"field": "index_stats.total.refresh.total_time_in_millis",
"format": "0,0.[00]",
"label": "Total Refresh Time",
"metricAgg": "max",
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/monitoring/server/lib/metrics/classes/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ export class Metric {
};
}

getFields() {
return [this.field];
}

getDocType() {
return this.docType || this.getInferredDocType();
}

getInferredDocType() {
const fields = this.getFields();
return fields && fields.length ? fields[0].split('.')[0] : null;
}

static calculateLatency(timeInMillis, totalEvents) {
if (timeInMillis === null || totalEvents === null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class DifferenceMetric extends ElasticsearchMetric {
},
};

this.getFields = () => [`${fieldSource}.${metric}`, `${fieldSource}.${metric2}`];

this.calculation = (bucket) => {
return _.get(bucket, 'metric_max.value') - _.get(bucket, 'metric2_max.value');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ export const metrics = {
type: 'index'
}),
index_refresh_time: new ElasticsearchMetric({
field: 'total.refresh.total_time_in_millis',
field: 'index_stats.total.refresh.total_time_in_millis',
label: 'Total Refresh Time',
description:
'Time spent on Elasticsearch refresh for primary and replica shards.',
Expand Down
11 changes: 11 additions & 0 deletions x-pack/test/api_integration/apis/monitoring/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export default function ({ loadTestFile }) {
describe('common', () => {
loadTestFile(require.resolve('./mappings_exist'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from 'expect.js';
import { get } from 'lodash';
import * as esMetrics from '../../../../../plugins/monitoring/server/lib/metrics/elasticsearch/metrics';
import * as kibanaMetrics from '../../../../../plugins/monitoring/server/lib/metrics/kibana/metrics';
import * as logstashMetrics from '../../../../../plugins/monitoring/server/lib/metrics/logstash/metrics';
import * as beatsMetrics from '../../../../../plugins/monitoring/server/lib/metrics/beats/metrics';
import * as apmMetrics from '../../../../../plugins/monitoring/server/lib/metrics/apm/metrics';

export default function ({ getService }) {
const es = getService('es');

const metricSets = [
{
metrics: esMetrics.metrics,
name: 'es metrics',
indexTemplate: '.monitoring-es'
},
{
metrics: kibanaMetrics.metrics,
name: 'kibana metrics',
indexTemplate: '.monitoring-kibana'
},
{
metrics: logstashMetrics.metrics,
name: 'logstash metrics',
indexTemplate: '.monitoring-logstash'
},
{
metrics: beatsMetrics.metrics,
name: 'beats metrics',
indexTemplate: '.monitoring-beats'
},
{
metrics: apmMetrics.metrics,
name: 'apm metrics',
indexTemplate: '.monitoring-beats' // apm uses the same as beats
},
];

describe('mappings', () => {
for (const { indexTemplate, metrics, name } of metricSets) {
let mappings;

before('load mappings', async () => {
const template = await es.indices.getTemplate({ name: indexTemplate });
mappings = get(template, [indexTemplate, 'mappings', 'doc', 'properties']);
});

describe(`for ${name}`, () => { // eslint-disable-line no-loop-func
for (const metric of Object.values(metrics)) {
for (const field of metric.getFields()) {
it(`${field} should exist in the mappings`, () => { // eslint-disable-line no-loop-func
const propertyGetter = field.split('.').reduce((list, field) => {
list.push(field);
list.push('properties');
return list;
}, []).slice(0, -1); // Remove the trailing 'properties'

const foundMapping = get(mappings, propertyGetter, null);
expect(foundMapping).to.not.equal(null);
});
}
}
});
}
});
}
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/monitoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./elasticsearch_settings'));
loadTestFile(require.resolve('./kibana'));
loadTestFile(require.resolve('./logstash'));
loadTestFile(require.resolve('./common'));
});
}

0 comments on commit 25f4843

Please sign in to comment.