Skip to content
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

Add metrics endpoints #665

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add metrics endpoints #665

wants to merge 1 commit into from

Conversation

skoeva
Copy link
Contributor

@skoeva skoeva commented Aug 2, 2023

Pull request #656 adds the necessary instance_stats database table as well as functions for querying instance counts (by channel, version, and architecture) from the groups, instance, and instance_application tables and storing the results in the new table.

To make this data accessible outside Nebraska, we create the following:

  • Prometheus metrics endpoint: A new HTTP endpoint (instance-metrics/prometheus) that serves only the latest snapshot from the instance_stats table. As Prometheus is a time-series database, this serves instance counts with the latest timestamp only.
  • JSON metrics data endpoint: A new HTTP endpoint that emits all instance_stats data in JSON format. The difference here is that we query for all data, and we emit one JSON document per row.

@skoeva skoeva requested a review from yolossn August 3, 2023 14:05
@skoeva skoeva force-pushed the skoeva/endpoints branch 2 times, most recently from 7cb2fd7 to 274f205 Compare August 12, 2023 04:04
@skoeva skoeva marked this pull request as ready for review August 12, 2023 23:51
Pull request #656 adds the necessary instance_stats database table as well as functions for querying instance counts (by channel, version, and architecture) from the groups, instance, and instance_application tables and storing the results in the new table.

To make this data accessible outside Nebraska, we create the following:

- Prometheus metrics endpoint: A new HTTP endpoint (instance-metrics/prometheus) that serves only the latest snapshot from the instance_stats table. As Prometheus is a time-series database, this serves instance counts with the latest timestamp only.
- JSON metrics data endpoint: A new HTTP endpoint that emits all instance_stats data in JSON format. The difference here is that we query for all data, and we emit one JSON document per row.
Comment on lines +1197 to +1213
/instance-metrics/json:
get:
description: Get instance stats
operationId: getInstanceStats
security:
- oidcBearerAuth: []
- oidcCookieAuth: []
- githubCookieAuth: []
responses:
"200":
description: Get instance stats success response
content:
application/x-ndjson:
schema:
$ref: "#/components/schemas/instanceStats"
"500":
description: Get instance stats error response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a response stream? And do we want to return (via stream) all the instance stats in this endpoint? I would suggest to use some form pagination instead of stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the goal is to output all instance stats here, and I agree with the pagination suggestion

@@ -49,7 +49,7 @@ const (

const (
validityInterval postgresDuration = "1 days"
defaultInterval time.Duration = time.Hour
defaultInterval time.Duration = 2 * time.Hour
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intentional here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! This was just an issue with rebasing, see #666

@@ -651,7 +651,7 @@ func (api *API) GetDefaultInterval() time.Duration {
// that have been checked in during a given duration from a given time.
func (api *API) instanceStatsQuery(t *time.Time, duration *time.Duration) *goqu.SelectDataset {
if t == nil {
now := time.Now()
now := time.Now().UTC()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is done in #664, please rebase with main to avoid redoing this part.

Copy link
Contributor

@yolossn yolossn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left few comments, overall the prometheus metrics endpoint looks good.

latestInstanceStatsSQL string = `
SELECT channel_name, version, arch, timestamp, instances AS instances_count
FROM instance_stats
WHERE timestamp = (SELECT MAX(timestamp) FROM instance_stats)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing raw SQL here, could we add a new function GetInstanceStatsLatest in pkg/api/instances.go and use that? This would also allow to add a test for this new function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this logic initially in pkg/api/instances.go but decided to follow the convention above, though I agree with this suggestion

@skoeva
Copy link
Contributor Author

skoeva commented Aug 22, 2023

I have left an update in my project doc for you guys to go through regarding my progress as my internship has concluded and I have started the fall semester at my university

I'm unable to make changes with my current setup but happy to follow along with the work on Nebraska!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants