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: add graphql subscription metrics #1970

Merged
merged 1 commit into from
Jan 14, 2024
Merged
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
30 changes: 30 additions & 0 deletions packages/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ function buildApolloSubscriptionServer(
help: 'Number of currently connected clients'
})

prometheusClient.register.removeSingleMetric(
'speckle_server_apollo_graphql_total_subscription_operations'
)
const metricSubscriptionTotalOperations = new prometheusClient.Counter({
name: 'speckle_server_apollo_graphql_total_subscription_operations',
help: 'Number of total subscription operations served by this instance',
labelNames: ['subscriptionType'] as const
})

prometheusClient.register.removeSingleMetric(
'speckle_server_apollo_graphql_total_subscription_responses'
)
const metricSubscriptionTotalResponses = new prometheusClient.Counter({
name: 'speckle_server_apollo_graphql_total_subscription_responses',
help: 'Number of total subscription responses served by this instance',
labelNames: ['subscriptionType', 'status'] as const
})

return SubscriptionServer.create(
{
schema,
Expand Down Expand Up @@ -178,17 +196,29 @@ function buildApolloSubscriptionServer(
// kinda hacky, but we're using this as an "subscription event emitted"
// callback to clear subscription connection dataloaders to avoid stale cache
const baseParams = params[1]
metricSubscriptionTotalOperations.inc({
subscriptionType: baseParams.operationName
})
const ctx = baseParams.context as GraphQLContext

// eslint-disable-next-line @typescript-eslint/no-explicit-any
baseParams.formatResponse = (val: SubscriptionResponse) => {
ctx.loaders.clearAll()
logSubscriptionOperation({ ctx, execParams: baseParams, response: val })
metricSubscriptionTotalResponses.inc({
subscriptionType: baseParams.operationName,
status: 'success'
})
return val
}
baseParams.formatError = (e: Error) => {
ctx.loaders.clearAll()
logSubscriptionOperation({ ctx, execParams: baseParams, error: e })

metricSubscriptionTotalResponses.inc({
subscriptionType: baseParams.operationName,
status: 'error'
})
return e
}

Expand Down