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

feat(stats): track gh calls #859

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/middleware/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ export class StatsMiddleware {

countDbUsers = wrapAsRequestHandler(() => this.statsService.countDbUsers())

countGithubSites = wrapAsRequestHandler(() =>
this.statsService.countGithubSites()
)

countMigratedSites = wrapAsRequestHandler(() =>
this.statsService.countMigratedSites()
)
Expand Down
1 change: 0 additions & 1 deletion src/routes/v2/authenticated/__tests__/Sites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe("Sites Router", () => {
}

const mockStatsMiddleware = {
countGithubSites: jest.fn(),
countMigratedSites: jest.fn(),
}

Expand Down
45 changes: 40 additions & 5 deletions src/services/api/AxiosInstance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosRequestConfig, AxiosResponse } from "axios"
import { setupCache } from "axios-cache-interceptor"
import { err } from "neverthrow"
import _ from "lodash"

import { config } from "@config/config"

Expand All @@ -11,6 +11,27 @@ import tracer from "@utils/tracer"
import { customHeaderInterpreter } from "@root/utils/headerInterpreter"
import { tokenServiceInstance } from "@services/db/TokenService"

import { statsService } from "../infra/StatsService"

const GITHUB_EXPERIMENTAL_TRIAL_SITES = ["pa-corp"]

const REPOS_SUBSTRING = "repos/isomerpages"
const extractRepoNameFromGithubUrl = (url: string): string => {
const idx = url.search(REPOS_SUBSTRING)
// NOTE: Should not hit here because we check that the url contains the site already
if (idx === -1) return ""
const ignoredLength = REPOS_SUBSTRING.length
return _.takeWhile(
url.slice(idx + ignoredLength + 1),
(char) => char !== "/"
).join("")
}

const getIsEmailUserFromAuthMessage = (
authMessage?: string | number | boolean
): boolean =>
!authMessage || authMessage === "token " || authMessage === "token undefined"

// Env vars
const GITHUB_ORG_NAME = config.get("github.orgName")

Expand All @@ -22,10 +43,7 @@ const requestFormatter = async (axiosConfig: AxiosRequestConfig) => {
// If accessToken is missing, authMessage is `token `
// NOTE: This also implies that the user has not provided
// their own github token and hence, are email login users.
const isEmailLoginUser =
!authMessage ||
authMessage === "token " ||
authMessage === "token undefined"
const isEmailLoginUser = getIsEmailUserFromAuthMessage(authMessage)

if (isEmailLoginUser) {
const accessToken = await tokenServiceInstance.getAccessToken()
Expand Down Expand Up @@ -73,6 +91,21 @@ const respHandler = (response: AxiosResponse) => {
return response
}

const githubApiInterceptor = (resp: AxiosResponse) => {
const fullUrl = `${resp.config.baseURL || ""}${resp.config.url || ""}`
if (
resp.status !== 304 &&
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
_.some(GITHUB_EXPERIMENTAL_TRIAL_SITES, (site) => fullUrl.includes(site)) &&
resp.config.method
) {
statsService.incrementGithubApiCall(
resp.config.method,
extractRepoNameFromGithubUrl(fullUrl)
)
}
return resp
}

const isomerRepoAxiosInstance = setupCache(
axios.create({
baseURL: `https://api.github.com/repos/${GITHUB_ORG_NAME}/`,
Expand All @@ -85,9 +118,11 @@ const isomerRepoAxiosInstance = setupCache(
)
isomerRepoAxiosInstance.interceptors.request.use(requestFormatter)
isomerRepoAxiosInstance.interceptors.response.use(respHandler)
isomerRepoAxiosInstance.interceptors.response.use(githubApiInterceptor)

const genericGitHubAxiosInstance = axios.create()
genericGitHubAxiosInstance.interceptors.request.use(requestFormatter)
genericGitHubAxiosInstance.interceptors.response.use(respHandler)
genericGitHubAxiosInstance.interceptors.response.use(githubApiInterceptor)

export { isomerRepoAxiosInstance, genericGitHubAxiosInstance }
45 changes: 11 additions & 34 deletions src/services/infra/StatsService.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
/* eslint-disable import/prefer-default-export */
import { Method } from "axios"
import StatsDClient, { StatsD } from "hot-shots"
import _ from "lodash"
import { ModelStatic } from "sequelize"

import { config } from "@config/config"

import {
Versions,
GH_MAX_REPO_COUNT,
GITHUB_ORG_REPOS_ENDPOINT,
ISOMERPAGES_REPO_PAGE_COUNT,
VersionNumber,
} from "@constants/index"
import { Versions, VersionNumber } from "@constants/index"

import { AccessToken, Site, User } from "@root/database/models"

import { genericGitHubAxiosInstance } from "../api/AxiosInstance"

export class StatsService {
private readonly statsD: StatsD

Expand Down Expand Up @@ -60,30 +52,6 @@ export class StatsService {
})
}

countGithubSites = async () => {
const accessToken = await this.accessTokenRepo.findOne()
// NOTE: Cannot submit metrics if we are unable to get said metric
if (!accessToken) return

const sitesArr = await Promise.all(
_.fill(Array(ISOMERPAGES_REPO_PAGE_COUNT), null)
.map((__, idx) => ({
per_page: GH_MAX_REPO_COUNT,
sort: "full_name",
page: idx + 1,
}))
.map((params) =>
genericGitHubAxiosInstance
.get<unknown[]>(GITHUB_ORG_REPOS_ENDPOINT, {
params,
})
.then(({ data }) => data)
)
)

this.statsD.distribution("sites.github.all", sitesArr.flat().length, 1)
}

countMigratedSites = async () => {
const numMigratedSites = await this.sitesRepo.count({
where: {
Expand All @@ -105,6 +73,15 @@ export class StatsService {
version: Versions.V2,
})
}

incrementGithubApiCall = (method: Method, site: string) => {
this.statsD.increment("users.github.api", {
site,
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: Allowed to pass in lowercase,
// standardised to uppercase for consistency
method: method.toUpperCase(),
})
}
}

const statsDClient = new StatsDClient({
Expand Down