Просмотр исходного кода

Merge pull request #11789 from mtanda/prom_annotation_series_timestamp

(prometheus) add annotation option to treat series value as timestamp
David 7 лет назад
Родитель
Сommit
d67d890ccc

+ 8 - 2
public/app/plugins/datasource/prometheus/datasource.ts

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

+ 10 - 1
public/app/plugins/datasource/prometheus/partials/annotations.editor.html

@@ -10,7 +10,7 @@
 </div>
 </div>
 
 
 <div class="gf-form-group">
 <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-inline">
 		<div class="gf-form">
 		<div class="gf-form">
 			<span class="gf-form-label width-5">Title</span>
 			<span class="gf-form-label width-5">Title</span>
@@ -27,4 +27,13 @@
 			</div>
 			</div>
 		</div>
 		</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>
 </div>

+ 49 - 29
public/app/plugins/datasource/prometheus/specs/datasource.test.ts

@@ -581,7 +581,7 @@ describe('PrometheusDatasource', () => {
   describe('When performing annotationQuery', () => {
   describe('When performing annotationQuery', () => {
     let results;
     let results;
 
 
-    const options = {
+    const options: any = {
       annotation: {
       annotation: {
         expr: 'ALERTS{alertstate="firing"}',
         expr: 'ALERTS{alertstate="firing"}',
         tagKeys: 'job',
         tagKeys: 'job',
@@ -594,41 +594,61 @@ describe('PrometheusDatasource', () => {
       },
       },
     };
     };
 
 
-    beforeEach(async () => {
-      const response = {
-        status: 'success',
+    const response = {
+      status: 'success',
+      data: {
         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);
+      });
     });
     });
   });
   });