-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rebase upstream changes to
d78fab9
(#7)
Signed-off-by: Mathew Wicks <5735406+thesuperzapper@users.noreply.github.com>
- Loading branch information
1 parent
1cb2316
commit da9cb36
Showing
21 changed files
with
575 additions
and
1,758 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import {Interval, MetricsInfo, MetricsService, TimeSeriesPoint} from "./metrics_service"; | ||
import {PrometheusDriver, RangeVector, ResponseType} from 'prometheus-query'; | ||
|
||
export class PrometheusMetricsService implements MetricsService { | ||
private readonly prometheusDriver: PrometheusDriver; | ||
private readonly dashboardUrl: string | undefined; | ||
|
||
constructor(prometheusDriver: PrometheusDriver, dashboardUrl: string | undefined) { | ||
this.prometheusDriver = prometheusDriver; | ||
this.dashboardUrl = dashboardUrl; | ||
} | ||
|
||
async getNodeCpuUtilization(interval: Interval): Promise<TimeSeriesPoint[]> { | ||
const query = `sum(rate(node_cpu_seconds_total[5m])) by (instance)`; | ||
const result = await this.queryPrometheus(query, this.getCorrespondingTime(interval)); | ||
return this.convertToTimeSeriesPoints(result); | ||
} | ||
|
||
async getPodCpuUtilization(interval: Interval): Promise<TimeSeriesPoint[]> { | ||
const query = `sum(rate(container_cpu_usage_seconds_total[5m]))`; | ||
const result = await this.queryPrometheus(query, this.getCorrespondingTime(interval)); | ||
return this.convertToTimeSeriesPoints(result); | ||
} | ||
|
||
async getPodMemoryUsage(interval: Interval): Promise<TimeSeriesPoint[]> { | ||
const query = `sum(container_memory_usage_bytes)`; | ||
const result = await this.queryPrometheus(query, this.getCorrespondingTime(interval)); | ||
return this.convertToTimeSeriesPoints(result); | ||
} | ||
|
||
private async queryPrometheus(query: string, start: number, end: number = Date.now()): Promise<RangeVector[]> { | ||
const result = await this.prometheusDriver.rangeQuery(query, start, end, 10); | ||
if(result.resultType !== ResponseType.MATRIX) { | ||
console.warn(`The prometheus server returned invalid result type: ${result.resultType}`); | ||
return []; | ||
} | ||
return result.result as RangeVector[]; | ||
} | ||
|
||
private getCorrespondingTime(interval: Interval): number { | ||
let minutes = 0; | ||
switch (interval) { | ||
case Interval.Last5m: | ||
minutes = 5; | ||
break; | ||
case Interval.Last15m: | ||
minutes = 15; | ||
break; | ||
case Interval.Last30m: | ||
minutes = 30; | ||
break; | ||
case Interval.Last60m: | ||
minutes = 60; | ||
break; | ||
case Interval.Last180m: | ||
minutes = 180; | ||
break; | ||
default: | ||
console.warn("unknown interval."); | ||
} | ||
return Date.now() - minutes * 60 * 1000; | ||
} | ||
|
||
private convertToTimeSeriesPoints(series: RangeVector[]): TimeSeriesPoint[] { | ||
const timeSeriesPoints: TimeSeriesPoint[] = []; | ||
series.forEach(serie => { | ||
|
||
const label = Object.entries(serie.metric.labels).map((entry) => { | ||
return entry[0] + "=" + entry[1]; | ||
}).join(","); | ||
|
||
// The `public/components/resource-chart.js` is multiplying the timestamp by 1000 and the value by 100 | ||
serie.values.forEach(value => { | ||
timeSeriesPoints.push({ | ||
timestamp: value.time.getTime() / 1000, | ||
label, | ||
value: value.value / 100, | ||
}); | ||
}); | ||
}); | ||
return timeSeriesPoints; | ||
} | ||
|
||
getChartsLink(): MetricsInfo { | ||
return { | ||
resourceChartsLink: this.dashboardUrl, | ||
resourceChartsLinkText: 'View in dashboard' | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {Metric, PrometheusDriver, QueryResult, ResponseType} from "prometheus-query"; | ||
import {PrometheusMetricsService} from "./prometheus_metrics_service"; | ||
import {Interval, MetricsService, TimeSeriesPoint} from "./metrics_service"; | ||
import {SampleValue} from "prometheus-query/dist/types"; | ||
|
||
type MetricsServiceKeys = keyof MetricsService; | ||
const methods: MetricsServiceKeys[] = ["getNodeCpuUtilization", "getPodCpuUtilization", "getPodMemoryUsage"]; | ||
const queries: {[id: string]: string} = { | ||
"getNodeCpuUtilization": "sum(rate(node_cpu_seconds_total[5m])) by (instance)", | ||
"getPodCpuUtilization": "sum(rate(container_cpu_usage_seconds_total[5m]))", | ||
"getPodMemoryUsage": "sum(container_memory_usage_bytes)" | ||
}; | ||
|
||
const fixedDate = 1557705600000; | ||
|
||
const emptyDataSet: QueryResult = {"resultType": ResponseType.MATRIX,"result":[]}; | ||
const singleInstanceDataSet: QueryResult = { | ||
"resultType": ResponseType.MATRIX, | ||
"result":[ | ||
{ | ||
"metric": {"labels": {"instance":"one"}} as Metric, | ||
"values":[ | ||
{ | ||
time: new Date(fixedDate), | ||
value: 95.5, | ||
} as SampleValue | ||
] | ||
} | ||
] | ||
}; | ||
const multipleInstancesDataSet: QueryResult = { | ||
"resultType": ResponseType.MATRIX, | ||
"result":[ | ||
{ | ||
"metric": {"labels": {"instance":"one"}} as Metric, | ||
"values":[ | ||
{ | ||
time: new Date(fixedDate), | ||
value: 1.0, | ||
} as SampleValue | ||
] | ||
}, | ||
{ | ||
"metric": {"labels": {"instance":"two"}} as Metric, | ||
"values":[ | ||
{ | ||
time: new Date(fixedDate), | ||
value: 2.0, | ||
} as SampleValue | ||
] | ||
}, | ||
{ | ||
"metric": {"labels": {"instance":"three"}} as Metric, | ||
"values":[ | ||
{ | ||
time: new Date(fixedDate), | ||
value: 3.0, | ||
} as SampleValue | ||
] | ||
} | ||
] | ||
}; | ||
|
||
describe('PrometheusMetricsService', () => { | ||
let prometheusDriverClient: jasmine.SpyObj<PrometheusDriver>; | ||
let service: PrometheusMetricsService; | ||
|
||
beforeEach(() => { | ||
jasmine.clock().install(); | ||
jasmine.clock().mockDate(new Date(1557705600000)); | ||
prometheusDriverClient = jasmine.createSpyObj<PrometheusDriver>( | ||
'prometheusDriverClient', ['rangeQuery']); | ||
|
||
service = | ||
new PrometheusMetricsService(prometheusDriverClient, undefined); | ||
}); | ||
|
||
// Iterate over all methods since they have the same behavior | ||
methods.forEach((method) => { | ||
describe(method, async () => { | ||
it('Empty return', async () => { | ||
prometheusDriverClient.rangeQuery.withArgs( | ||
queries[method], | ||
Date.now() - 5 * 60 * 1000, | ||
Date.now(), | ||
10 | ||
).and.returnValue(Promise.resolve(emptyDataSet)); | ||
|
||
const emptyResult = await service[method](Interval.Last5m); | ||
expect(emptyResult).toEqual(Array.of<TimeSeriesPoint>()); | ||
}); | ||
|
||
it('One instance', async () => { | ||
prometheusDriverClient.rangeQuery.withArgs( | ||
queries[method], | ||
Date.now() - 5 * 60 * 1000, | ||
Date.now(), | ||
10 | ||
).and.returnValue(Promise.resolve(singleInstanceDataSet)); | ||
|
||
const singleInstanceResult = await service[method](Interval.Last5m); | ||
expect(singleInstanceResult).toEqual(Array.of<TimeSeriesPoint>({ | ||
timestamp: fixedDate / 1000, | ||
value: 0.955, | ||
label: "instance=one" | ||
})); | ||
}); | ||
|
||
it('Multiple instances', async () => { | ||
prometheusDriverClient.rangeQuery.withArgs( | ||
queries[method], | ||
Date.now() - 5 * 60 * 1000, | ||
Date.now(), | ||
10 | ||
).and.returnValue(Promise.resolve(multipleInstancesDataSet)); | ||
|
||
const singleInstanceResult = await service[method](Interval.Last5m); | ||
expect(singleInstanceResult).toEqual( | ||
Array.of<TimeSeriesPoint>({ | ||
timestamp: fixedDate / 1000, | ||
value: 0.010, | ||
label: "instance=one" | ||
}, | ||
{ | ||
timestamp: fixedDate / 1000, | ||
value: 0.020, | ||
label: "instance=two" | ||
}, | ||
{ | ||
timestamp: fixedDate / 1000, | ||
value: 0.030, | ||
label: "instance=three" | ||
}) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jasmine.clock().uninstall(); | ||
}); | ||
}); |
Oops, something went wrong.