Skip to content

Commit

Permalink
refactor(server): use prometheus record rules to enhance billing perf…
Browse files Browse the repository at this point in the history
…ormance (#1671)
  • Loading branch information
0fatal committed Nov 10, 2023
1 parent 67b3cd6 commit 619988e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
19 changes: 18 additions & 1 deletion build/charts/laf-server/templates/rumtime-exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
namespace: {{ .Release.Namespace }}
{{- include "runtime-exporter.labels" . | nindent 4 }}
release: prometheus
name: runtime-exporter
spec:
Expand All @@ -75,3 +75,20 @@ spec:
selector:
matchLabels:
{{- include "runtime-exporter.labels" . | nindent 6 }}
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
{{- include "runtime-exporter.labels" . | nindent 4 }}
release: prometheus
name: prometheus-laf-billing.rules
spec:
groups:
- name: prometheus-laf-billing.rules
interval: 60s
rules:
- record: laf:billing:cpu
expr: max_over_time(sum by (appid) (laf_runtime_cpu_limit{container!=""})[1h:])
- record: laf:billing:memory
expr: max_over_time(sum by (appid) (laf_runtime_memory_limit{container!=""})[1h:])
10 changes: 2 additions & 8 deletions server/src/billing/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,12 @@ export class BillingService {
})

const cpuTask = prom
.instantQuery(
`sum(max_over_time(laf_runtime_cpu_limit{container!="",appid="${app.appid}"}[1h])) by (appid)`,
time,
)
.instantQuery(`laf:billing:cpu{appid=${app.appid}}`, time)
.then((res) => res.result[0])
.then((res) => Number(res.value.value))

const memoryTask = prom
.instantQuery(
`sum(max_over_time(laf_runtime_memory_limit{container!="",appid="${app.appid}"}[1h])) by (appid)`,
time,
)
.instantQuery(`laf:billing:memory{appid=${app.appid}`, time)
.then((res) => res.result[0])
.then((res) => Number(res.value.value))

Expand Down

0 comments on commit 619988e

Please sign in to comment.