-
Notifications
You must be signed in to change notification settings - Fork 51
Add metrics endpoints #665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1177,6 +1177,53 @@ paths: | |||||
| description: Activity not found response | ||||||
| "500": | ||||||
| description: List activity error response | ||||||
| /instance-metrics/prometheus: | ||||||
| get: | ||||||
| description: Get latest instance stats | ||||||
| operationId: getLatestInstanceStats | ||||||
| security: | ||||||
| - oidcBearerAuth: [] | ||||||
| - oidcCookieAuth: [] | ||||||
| - githubCookieAuth: [] | ||||||
| responses: | ||||||
| "200": | ||||||
| description: Get latest instance stats success response | ||||||
| content: | ||||||
| text/plain: | ||||||
| schema: | ||||||
| type: string | ||||||
| "500": | ||||||
| description: Get latest instance stats error response | ||||||
| /instance-metrics/json: | ||||||
| get: | ||||||
| description: Get instance stats | ||||||
| operationId: getInstanceStats | ||||||
| parameters: | ||||||
| - in: query | ||||||
| name: page | ||||||
| required: false | ||||||
| schema: | ||||||
| type: integer | ||||||
| minimum: 0 | ||||||
| - in: query | ||||||
| name: perpage | ||||||
| required: false | ||||||
| schema: | ||||||
| type: integer | ||||||
| minimum: 10 | ||||||
| security: | ||||||
| - oidcBearerAuth: [] | ||||||
| - oidcCookieAuth: [] | ||||||
| - githubCookieAuth: [] | ||||||
| responses: | ||||||
| "200": | ||||||
| description: List instance stats response | ||||||
| content: | ||||||
| application/x-ndjson: | ||||||
| schema: | ||||||
| $ref: "#/components/schemas/instanceStats" | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not this rather be referencing
Suggested change
|
||||||
| "500": | ||||||
| description: Get instance stats error response | ||||||
| components: | ||||||
| schemas: | ||||||
| ## request Body | ||||||
|
|
@@ -1339,6 +1386,46 @@ components: | |||||
| package_id: | ||||||
| type: string | ||||||
|
|
||||||
| instanceStatsPage: | ||||||
| type: object | ||||||
| required: | ||||||
| - totalCount | ||||||
| - count | ||||||
| - applications | ||||||
| properties: | ||||||
| totalCount: | ||||||
| type: integer | ||||||
| count: | ||||||
| type: integer | ||||||
| applications: | ||||||
| type: array | ||||||
| items: | ||||||
| $ref: "#/components/schemas/instanceStats" | ||||||
|
|
||||||
| instanceStats: | ||||||
| type: object | ||||||
| required: | ||||||
| - type | ||||||
| - channel | ||||||
| - version | ||||||
| - arch | ||||||
| - timestamp | ||||||
| - instances | ||||||
| properties: | ||||||
| type: | ||||||
| type: string | ||||||
| enum: ["instance_count"] | ||||||
| channel: | ||||||
| type: string | ||||||
| version: | ||||||
| type: string | ||||||
| arch: | ||||||
| type: string | ||||||
| timestamp: | ||||||
| type: string | ||||||
| count: | ||||||
| type: integer | ||||||
|
|
||||||
|
|
||||||
| ## response | ||||||
| config: | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -723,11 +723,24 @@ func (api *API) instanceStatsQuery(t *time.Time, duration *time.Duration) *goqu. | |
| return query | ||
| } | ||
|
|
||
| // GetInstanceStatsCount returns the total number of InstanceStats. | ||
| func (api *API) GetInstanceStatsCount() (int, error) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Let's stick to |
||
| query := goqu.From("instance_stats").Select(goqu.L("count(*)")) | ||
| return api.GetCountQuery(query) | ||
| } | ||
|
|
||
| // GetInstanceStats returns an InstanceStats table with all instances that have | ||
| // been previously been checked in. | ||
| func (api *API) GetInstanceStats() ([]InstanceStats, error) { | ||
| query, _, err := goqu.From("instance_stats"). | ||
| Order(goqu.C("timestamp").Asc()).ToSQL() | ||
| func (api *API) GetInstanceStats(page, perPage uint64) ([]InstanceStats, error) { | ||
| page, perPage = validatePaginationParams(page, perPage) | ||
| limit, offset := sqlPaginate(page, perPage) | ||
|
|
||
| query, _, err := goqu. | ||
| From("instance_stats"). | ||
| Limit(limit). | ||
| Offset(offset). | ||
| Order(goqu.C("timestamp").Asc()). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it matters or not, but is the ordering correct here? shouldn't we get the latest stats first? |
||
| ToSQL() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,12 @@ WHERE a.id = e.application_id AND e.event_type_id = et.id AND et.result = 0 AND | |
| GROUP BY app_name | ||
| ORDER BY app_name | ||
| `, ignoreFakeInstanceCondition("e.instance_id")) | ||
|
|
||
| latestInstanceStatsSQL = ` | ||
| SELECT channel_name, version, arch, timestamp, instances AS instances_count | ||
| FROM instance_stats | ||
| WHERE timestamp = (SELECT MAX(timestamp) FROM instance_stats) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing raw SQL here, could we add a new function
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had this logic initially in
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a index on timestamp? |
||
| ` | ||
| ) | ||
|
|
||
| type AppInstancesPerChannelMetric struct { | ||
|
|
@@ -77,6 +83,38 @@ func (api *API) GetFailedUpdatesMetrics() ([]FailedUpdatesMetric, error) { | |
| return metrics, nil | ||
| } | ||
|
|
||
| type LatestInstanceStatsMetric struct { | ||
| ChannelName string `db:"channel_name" json:"channel_name"` | ||
| Version string `db:"version" json:"version"` | ||
| Arch string `db:"arch" json:"arch"` | ||
| Timestamp string `db:"timestamp" json:"timestamp"` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it doesn't matter as we are just forwarding the value as text, but a go |
||
| InstancesCount int `db:"instances_count" json:"instances_count"` | ||
| } | ||
|
|
||
| func (api *API) GetLatestInstanceStatsMetrics() ([]LatestInstanceStatsMetric, error) { | ||
| var metrics []LatestInstanceStatsMetric | ||
| rows, err := api.db.Queryx(latestInstanceStatsSQL) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("querying latest instance stats from SQL: %w", err) | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| for rows.Next() { | ||
| var metric LatestInstanceStatsMetric | ||
| if err := rows.StructScan(&metric); err != nil { | ||
| return nil, fmt.Errorf("scanning instance stat metric: %w", err) | ||
| } | ||
|
|
||
| metrics = append(metrics, metric) | ||
| } | ||
|
|
||
| if err := rows.Err(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return metrics, nil | ||
| } | ||
|
|
||
| func (api *API) DbStats() sql.DBStats { | ||
| return api.db.Stats() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we need the json sub-path, especially if we also define the response content type as json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I would propose to keep
/instance-metrics/jsonand use/instance-metricsfor the Prometheus endpoint to get closer from the default/metricsendpoint.