Skip to content

Commit 58481fd

Browse files
author
jeho
committed
feat: add metrics endpoint (#409)
* feat: add metrics endpoint * feat: add metrics endpoint * feat: add metrics endpoint * feat: add metrics endpoint and use it for license limits * feat: add metrics endpoint and use it for license limits
1 parent 5ea59f8 commit 58481fd

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

src/api/license.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Debug from 'debug'
22
import { Operation, OperationHandlerArray } from 'express-openapi'
33
import { OpenApiRequestExt } from 'src/otomi-models'
44

5-
const debug = Debug('otomi:api:delete')
5+
const debug = Debug('otomi:api:license')
66

77
export default function (): OperationHandlerArray {
88
const del: Operation = [

src/api/metrics.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Debug from 'debug'
2+
import { Operation, OperationHandlerArray } from 'express-openapi'
3+
import { OpenApiRequestExt } from 'src/otomi-models'
4+
5+
const debug = Debug('otomi:api:metrics')
6+
7+
export default function (): OperationHandlerArray {
8+
const get: Operation = [
9+
({ otomi }: OpenApiRequestExt, res) => {
10+
debug('get')
11+
const data = otomi.getMetrics()
12+
res.json(data)
13+
},
14+
]
15+
const api = {
16+
get,
17+
}
18+
return api
19+
}

src/openapi/api.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ paths:
7474
application/json:
7575
schema:
7676
$ref: '#/components/schemas/License'
77+
/metrics:
78+
get:
79+
operationId: getMetrics
80+
description: Get otomi metrics
81+
x-aclSchema: Metrics
82+
responses:
83+
'200':
84+
description: Successfully obtained otomi metrics
85+
content:
86+
application/json:
87+
schema:
88+
$ref: '#/components/schemas/Metrics'
7789
/secrets:
7890
get:
7991
operationId: getAllSecrets
@@ -1092,6 +1104,8 @@ components:
10921104
$ref: license.yaml#/LicenseJwt
10931105
License:
10941106
$ref: license.yaml#/License
1107+
Metrics:
1108+
$ref: metrics.yaml#/Metrics
10951109
DockerConfig:
10961110
$ref: dockerconfig.yaml#/DockerConfig
10971111
OpenApiValidationError:

src/openapi/metrics.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Metrics:
2+
x-acl:
3+
admin: [read-any]
4+
team: [read-any]
5+
type: object
6+
properties:
7+
otomi_backups:
8+
type: integer
9+
minimum: 0
10+
default: 0
11+
otomi_builds:
12+
type: integer
13+
minimum: 0
14+
default: 0
15+
otomi_secrets:
16+
type: integer
17+
minimum: 0
18+
default: 0q
19+
otomi_services:
20+
type: integer
21+
minimum: 0
22+
default: 0
23+
otomi_teams:
24+
type: integer
25+
minimum: 0
26+
default: 0
27+
otomi_workloads:
28+
type: integer
29+
minimum: 0
30+
default: 0
31+
required:
32+
- otomi_backups
33+
- otomi_builds
34+
- otomi_secrets
35+
- otomi_services
36+
- otomi_teams
37+
- otomi_workloads

src/otomi-models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type Workload = components['schemas']['Workload']
2121
export type WorkloadValues = components['schemas']['WorkloadValues']
2222
export type Build = components['schemas']['Build']
2323
export type TeamAuthz = components['schemas']['TeamAuthz']
24-
24+
export type Metrics = components['schemas']['Metrics']
2525
// Derived setting models
2626
export type Alerts = Settings['alerts']
2727
export type Cluster = Settings['cluster']

src/otomi-stack.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import * as osPath from 'path'
1212
import { getAppList, getAppSchema, getSpec } from 'src/app'
1313
import Db from 'src/db'
1414
import { DeployLockError, PublicUrlExists, ValidationError } from 'src/error'
15-
import { cleanAllSessions, cleanSession, DbMessage, getIo, getSessionStack } from 'src/middleware'
15+
import { DbMessage, cleanAllSessions, cleanSession, getIo, getSessionStack } from 'src/middleware'
1616
import {
1717
App,
1818
Backup,
1919
Build,
2020
Core,
2121
K8sService,
2222
License,
23+
Metrics,
2324
Policies,
2425
Secret,
2526
Service,
@@ -34,7 +35,6 @@ import {
3435
import getRepo, { Repo } from 'src/repo'
3536
import { arrayToObject, getServiceUrl, objectToArray, removeBlankAttributes } from 'src/utils'
3637
import {
37-
cleanEnv,
3838
CUSTOM_ROOT_CA,
3939
EDITOR_INACTIVITY_TIMEOUT,
4040
GIT_BRANCH,
@@ -45,6 +45,7 @@ import {
4545
GIT_USER,
4646
TOOLS_HOST,
4747
VERSIONS,
48+
cleanEnv,
4849
} from 'src/validators'
4950
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml'
5051
import connect from './otomiCloud/connect'
@@ -133,6 +134,18 @@ export default class OtomiStack {
133134
return apps.concat(ingressApps)
134135
}
135136

137+
getMetrics(): Metrics {
138+
const metrics: Metrics = {
139+
otomi_backups: this.getAllBackups().length,
140+
otomi_builds: this.getAllBuilds().length,
141+
otomi_secrets: this.getAllSecrets().length,
142+
otomi_services: this.getAllServices().length,
143+
// We do not count team_admin as a regular team
144+
otomi_teams: this.getTeams().length - 1,
145+
otomi_workloads: this.getAllWorkloads().length,
146+
}
147+
return metrics
148+
}
136149
getRepoPath() {
137150
if (env.isTest || this.editor === undefined) return env.GIT_LOCAL_PATH
138151
const folder = `${rootPath}/${this.editor}`

0 commit comments

Comments
 (0)