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

add annotation option to treat series value as timestamp

Mitsuhiro Tanda 7 лет назад
Родитель
Сommit
2926725bab

+ 8 - 2
public/app/plugins/datasource/prometheus/datasource.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 - 1
public/app/plugins/datasource/prometheus/partials/annotations.editor.html

@@ -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>

+ 39 - 0
public/app/plugins/datasource/prometheus/specs/datasource.test.ts

@@ -630,6 +630,45 @@ describe('PrometheusDatasource', () => {
       expect(results[0].text).toBe('testinstance');
       expect(results[0].time).toBe(123 * 1000);
     });
+
+    it('should return annotation list with seriesValueAsTiemstamp', () => {
+      const options = {
+        annotation: {
+          expr: 'timestamp_seconds',
+          tagKeys: 'job',
+          titleFormat: '{{job}}',
+          textFormat: '{{instance}}',
+          useValueForTime: true,
+        },
+        range: {
+          from: new Date('2014-04-10T05:20:10Z'),
+          to: new Date('2014-05-20T03:10:22Z'),
+        },
+      };
+      ctx.backendSrvMock.datasourceRequest.mockReturnValue(
+        Promise.resolve({
+          status: 'success',
+          data: {
+            resultType: 'matrix',
+            result: [
+              {
+                metric: {
+                  __name__: 'timestamp_milliseconds',
+                  instance: 'testinstance',
+                  job: 'testjob',
+                },
+                values: [[1443454528, '1500000000000']],
+              },
+            ],
+          },
+        })
+      );
+      ctx.ds = new PrometheusDatasource(instanceSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
+      ctx.ds.annotationQuery(options).then(function (results) {
+        expect(results[0].time).toEqual(1500000000000);
+        ctx.backendSrvMock.datasourceRequest.mockReset();
+      });
+    });
   });
 
   describe('When resultFormat is table and instant = true', () => {