Merge pull request #11789 from mtanda/prom_annotation_series_timestamp

(prometheus) add annotation option to treat series value as timestamp
pull/13269/head
David 7 years ago committed by GitHub
commit d67d890ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      public/app/plugins/datasource/prometheus/datasource.ts
  2. 11
      public/app/plugins/datasource/prometheus/partials/annotations.editor.html
  3. 78
      public/app/plugins/datasource/prometheus/specs/datasource.test.ts

@ -487,15 +487,21 @@ export class PrometheusDatasource {
.value();
for (const value of series.values) {
if (value[1] === '1') {
const valueIsTrue = value[1] === '1'; // e.g. ALERTS
if (valueIsTrue || annotation.useValueForTime) {
const event = {
annotation: annotation,
time: Math.floor(parseFloat(value[0])) * 1000,
title: self.resultTransformer.renderTemplate(titleFormat, series.metric),
tags: tags,
text: self.resultTransformer.renderTemplate(textFormat, series.metric),
};
if (annotation.useValueForTime) {
event['time'] = Math.floor(parseFloat(value[1]));
} else {
event['time'] = Math.floor(parseFloat(value[0])) * 1000;
}
eventList.push(event);
}
}

@ -10,7 +10,7 @@
</div>
<div class="gf-form-group">
<h5 class="section-heading">Field formats</h6>
<h5 class="section-heading">Field formats</h5>
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-5">Title</span>
@ -27,4 +27,13 @@
</div>
</div>
</div>
<h5 class="section-heading">Other options</h5>
<div class="gf-form-inline">
<div class="gf-form">
<gf-form-switch class="gf-form" label="Series value as timestamp" label-class="width-14" checked="ctrl.annotation.useValueForTime"
tooltip="The unit of timestamp is milliseconds. If the unit of the series value is seconds, multiply its range vector by 1000.">
</gf-form-switch>
</div>
</div>
</div>

@ -581,7 +581,7 @@ describe('PrometheusDatasource', () => {
describe('When performing annotationQuery', () => {
let results;
const options = {
const options: any = {
annotation: {
expr: 'ALERTS{alertstate="firing"}',
tagKeys: 'job',
@ -594,41 +594,61 @@ describe('PrometheusDatasource', () => {
},
};
beforeEach(async () => {
const response = {
status: 'success',
const response = {
status: 'success',
data: {
data: {
data: {
resultType: 'matrix',
result: [
{
metric: {
__name__: 'ALERTS',
alertname: 'InstanceDown',
alertstate: 'firing',
instance: 'testinstance',
job: 'testjob',
},
values: [[123, '1']],
resultType: 'matrix',
result: [
{
metric: {
__name__: 'ALERTS',
alertname: 'InstanceDown',
alertstate: 'firing',
instance: 'testinstance',
job: 'testjob',
},
],
},
values: [[123, '1']],
},
],
},
};
},
};
backendSrv.datasourceRequest = jest.fn(() => Promise.resolve(response));
ctx.ds = new PrometheusDatasource(instanceSettings, q, backendSrv as any, templateSrv, timeSrv);
describe('not use useValueForTime', () => {
beforeEach(async () => {
options.annotation.useValueForTime = false;
backendSrv.datasourceRequest = jest.fn(() => Promise.resolve(response));
ctx.ds = new PrometheusDatasource(instanceSettings, q, backendSrv as any, templateSrv, timeSrv);
await ctx.ds.annotationQuery(options).then(data => {
results = data;
await ctx.ds.annotationQuery(options).then(data => {
results = data;
});
});
it('should return annotation list', () => {
expect(results.length).toBe(1);
expect(results[0].tags).toContain('testjob');
expect(results[0].title).toBe('InstanceDown');
expect(results[0].text).toBe('testinstance');
expect(results[0].time).toBe(123 * 1000);
});
});
it('should return annotation list', () => {
expect(results.length).toBe(1);
expect(results[0].tags).toContain('testjob');
expect(results[0].title).toBe('InstanceDown');
expect(results[0].text).toBe('testinstance');
expect(results[0].time).toBe(123 * 1000);
describe('use useValueForTime', () => {
beforeEach(async () => {
options.annotation.useValueForTime = true;
backendSrv.datasourceRequest = jest.fn(() => Promise.resolve(response));
ctx.ds = new PrometheusDatasource(instanceSettings, q, backendSrv as any, templateSrv, timeSrv);
await ctx.ds.annotationQuery(options).then(data => {
results = data;
});
});
it('should return annotation list', () => {
expect(results[0].time).toEqual(1);
});
});
});

Loading…
Cancel
Save