Skip to content

Commit

Permalink
add metrics (#158)
Browse files Browse the repository at this point in the history
* add metrics

* fix tests

* prettify
  • Loading branch information
pedrotambo authored Jun 2, 2023
1 parent 6a1ce5e commit cc47956
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@well-known-components/interfaces"
import { randomUUID } from "crypto"
import { setTimeout } from "timers/promises"
import { ISubgraphComponent, PostQueryResponse, SubgraphProvider, SubgraphResponse, Variables } from "./types"
import { ISubgraphComponent, PostQueryResponse, SubgraphResponse, Variables } from "./types"
import { UNKNOWN_SUBGRAPH_PROVIDER, withTimeout } from "./utils"

export * from "./types"
Expand All @@ -29,9 +29,8 @@ export async function createSubgraphComponent(
const TIMEOUT = (await config.getNumber("SUBGRAPH_COMPONENT_QUERY_TIMEOUT")) ?? 10000
const TIMEOUT_INCREMENT = (await config.getNumber("SUBGRAPH_COMPONENT_TIMEOUT_INCREMENT")) ?? 10000
const BACKOFF = (await config.getNumber("SUBGRAPH_COMPONENT_BACKOFF")) ?? 500
const USER_AGENT = `Subgraph component / ${
(await config.getString("SUBGRAPH_COMPONENT_AGENT_NAME")) ?? "Unknown sender"
}`
const USER_AGENT = `Subgraph component / ${(await config.getString("SUBGRAPH_COMPONENT_AGENT_NAME")) ?? "Unknown sender"
}`

async function executeQuery<T>(
query: string,
Expand All @@ -46,6 +45,7 @@ export async function createSubgraphComponent(
const queryId = randomUUID()
const logData = { queryId, currentAttempt, attempts, timeoutWait, url }

const { end } = metrics.startTimer('subgraph_query_duration_seconds', { url })
try {
const [provider, response] = await withTimeout(
(abortController) => postQuery<T>(query, variables, abortController),
Expand Down Expand Up @@ -83,6 +83,8 @@ export async function createSubgraphComponent(
} else {
throw error // bubble up
}
} finally {
end({ url })
}
}

Expand Down Expand Up @@ -137,6 +139,11 @@ export const metricDeclarations: IMetricsComponent.MetricsRecordDefinition<strin
subgraph_errors_total: {
help: "Subgraph error counter",
type: IMetricsComponent.CounterType,
labelNames: ["url", "errorMessage"],
labelNames: ["url"],
},
subgraph_query_duration_seconds: {
type: IMetricsComponent.HistogramType,
help: "Request duration in seconds.",
labelNames: ["url"],
},
}
27 changes: 16 additions & 11 deletions test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ jest.mock("crypto")
jest.mock("timers/promises")

test("subgraph component", function ({ components, stubComponents }) {

const randomUUIMock: jest.Mock = randomUUID as any
const setTimeoutMock: jest.Mock = setTimeout as any

beforeEach(() => {
;(setTimeout as jest.Mock).mockImplementation((_time: number, name: string) => {
setTimeoutMock.mockImplementation((_time: number, name: string) => {
if (name === "Timeout") {
return new Promise(() => {})
return new Promise(() => { })
} else {
return Promise.resolve()
}
})
jest.spyOn(stubComponents.metrics, 'startTimer').mockReturnValue({ end: jest.fn() })
})

afterEach(() => {
Expand Down Expand Up @@ -158,7 +163,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query", {}, 0) // no retires
} catch (error) {}
} catch (error) { }

expect(metrics.increment).toHaveBeenCalledWith("subgraph_errors_total", {
url: SUBGRAPH_URL,
Expand Down Expand Up @@ -191,7 +196,7 @@ test("subgraph component", function ({ components, stubComponents }) {

jest.spyOn(logger, "debug")
jest.spyOn(logs, "getLogger").mockImplementationOnce(() => logger)
;(randomUUID as jest.Mock).mockReturnValue(queryId)
randomUUIMock.mockReturnValue(queryId)

subgraph = await createSubgraphComponent(components, SUBGRAPH_URL)
})
Expand All @@ -201,7 +206,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query", {}, 0)
} catch (error) {}
} catch (error) { }

expect(logs.getLogger).toBeCalledWith("thegraph-port")
})
Expand Down Expand Up @@ -234,7 +239,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query", {}, 0) // no retires
} catch (error) {}
} catch (error) { }

expect(metrics.increment).toHaveBeenCalledWith("subgraph_errors_total", {
url: SUBGRAPH_URL,
Expand Down Expand Up @@ -316,7 +321,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query", {}, retries)
} catch (error) {}
} catch (error) { }

expect(fetchMock).toHaveBeenCalledTimes(retries + 1)
})
Expand All @@ -327,7 +332,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query", {}, retries)
} catch (error) {}
} catch (error) { }

expect(metrics.increment).toHaveBeenCalledTimes(retries + 1)
expect(metrics.increment).toHaveBeenCalledWith("subgraph_errors_total", {
Expand Down Expand Up @@ -363,7 +368,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query")
} catch (error) {}
} catch (error) { }

expect(fetchMock).toHaveBeenCalledTimes(retries + 1)
})
Expand All @@ -383,7 +388,7 @@ test("subgraph component", function ({ components, stubComponents }) {
reject = rej
})
fetchMock = jest.spyOn(fetch, "fetch").mockImplementation(() => fetchPromise as any)
;(setTimeout as jest.Mock).mockReset().mockImplementation(() => {
setTimeoutMock.mockReset().mockImplementation(() => {
reject(new Error(errorMessage))
return Promise.resolve()
})
Expand All @@ -400,7 +405,7 @@ test("subgraph component", function ({ components, stubComponents }) {

try {
await subgraph.query("query")
} catch (error) {}
} catch (error) { }

expect(metrics.increment).toHaveBeenCalledWith("subgraph_errors_total", {
url: SUBGRAPH_URL,
Expand Down

0 comments on commit cc47956

Please sign in to comment.