-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add GraphQL and MongoDB Prometheus metrics #544
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
71c0b81
Initial plan
Copilot e9ec203
feat: add GraphQL and MongoDB metrics
Copilot 638e67b
Bump version up to 1.1.43
github-actions[bot] 02e6cd0
fix: correctly extract collection name from MongoDB command events
Copilot e510da1
fix: add proper type annotations to GraphQL metrics plugin
Copilot ea345c5
fix: add null check for event.command to prevent undefined access
Copilot d09f34d
Update mongodb.ts
neSpecc 388df44
lint
neSpecc d380041
reduce cardinality for mongo metrics
neSpecc ac20180
decrease buckets number, handle getMore case
neSpecc d3f9bb9
Update package.json
neSpecc 095f446
Delete metrics.test.ts
neSpecc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import client from 'prom-client'; | ||
| import { GraphQLRequestContext } from 'apollo-server-plugin-base'; | ||
| import { GraphQLError } from 'graphql'; | ||
|
|
||
| /** | ||
| * GraphQL operation duration histogram | ||
| * Tracks GraphQL operation duration by operation name and type | ||
| */ | ||
| export const gqlOperationDuration = new client.Histogram({ | ||
| name: 'hawk_gql_operation_duration_seconds', | ||
| help: 'Histogram of total GraphQL operation duration by operation name and type', | ||
| labelNames: ['operation_name', 'operation_type'], | ||
| buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10], | ||
| }); | ||
|
|
||
| /** | ||
| * GraphQL operation errors counter | ||
| * Tracks failed GraphQL operations grouped by operation name and error class | ||
| */ | ||
| export const gqlOperationErrors = new client.Counter({ | ||
| name: 'hawk_gql_operation_errors_total', | ||
| help: 'Counter of failed GraphQL operations grouped by operation name and error class', | ||
| labelNames: ['operation_name', 'error_type'], | ||
| }); | ||
|
|
||
| /** | ||
| * GraphQL resolver duration histogram | ||
| * Tracks resolver execution time per type, field, and operation | ||
| */ | ||
| export const gqlResolverDuration = new client.Histogram({ | ||
| name: 'hawk_gql_resolver_duration_seconds', | ||
| help: 'Histogram of resolver execution time per type, field, and operation', | ||
| labelNames: ['type_name', 'field_name', 'operation_name'], | ||
| buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5], | ||
| }); | ||
|
|
||
| /** | ||
| * Apollo Server plugin to track GraphQL metrics | ||
| */ | ||
| export const graphqlMetricsPlugin = { | ||
| async requestDidStart(requestContext: GraphQLRequestContext) { | ||
| const startTime = Date.now(); | ||
| let operationName = 'unknown'; | ||
| let operationType = 'unknown'; | ||
|
|
||
| return { | ||
| async didResolveOperation(requestContext: GraphQLRequestContext) { | ||
| operationName = requestContext.operationName || 'anonymous'; | ||
| operationType = requestContext.operation?.operation || 'unknown'; | ||
| }, | ||
|
|
||
| async executionDidStart() { | ||
| return { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| willResolveField({ info }: any) { | ||
| const fieldStartTime = Date.now(); | ||
|
|
||
| return () => { | ||
| const duration = (Date.now() - fieldStartTime) / 1000; | ||
|
|
||
| gqlResolverDuration | ||
| .labels( | ||
| info.parentType.name, | ||
| info.fieldName, | ||
| operationName | ||
| ) | ||
| .observe(duration); | ||
| }; | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| async willSendResponse(requestContext: GraphQLRequestContext) { | ||
| const duration = (Date.now() - startTime) / 1000; | ||
|
|
||
| gqlOperationDuration | ||
| .labels(operationName, operationType) | ||
| .observe(duration); | ||
|
|
||
| // Track errors if any | ||
| if (requestContext.errors && requestContext.errors.length > 0) { | ||
| requestContext.errors.forEach((error: GraphQLError) => { | ||
| const errorType = error.extensions?.code || error.name || 'unknown'; | ||
|
|
||
| gqlOperationErrors | ||
| .labels(operationName, errorType as string) | ||
| .inc(); | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import client from 'prom-client'; | ||
| import { MongoClient, MongoClientOptions } from 'mongodb'; | ||
|
|
||
| /** | ||
| * MongoDB command duration histogram | ||
| * Tracks MongoDB command duration by command, collection, and database | ||
| */ | ||
| export const mongoCommandDuration = new client.Histogram({ | ||
| name: 'hawk_mongo_command_duration_seconds', | ||
| help: 'Histogram of MongoDB command duration by command, collection, and db', | ||
| labelNames: ['command', 'collection', 'db'], | ||
| buckets: [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10], | ||
| }); | ||
|
|
||
| /** | ||
| * MongoDB command errors counter | ||
| * Tracks failed MongoDB commands grouped by command and error code | ||
| */ | ||
| export const mongoCommandErrors = new client.Counter({ | ||
| name: 'hawk_mongo_command_errors_total', | ||
neSpecc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| help: 'Counter of failed MongoDB commands grouped by command and error code', | ||
| labelNames: ['command', 'error_code'], | ||
| }); | ||
|
|
||
| /** | ||
| * Enhance MongoClient options with monitoring | ||
| * @param options - Original MongoDB connection options | ||
| * @returns Enhanced options with monitoring enabled | ||
| */ | ||
| export function withMongoMetrics(options: MongoClientOptions = {}): MongoClientOptions { | ||
| return { | ||
| ...options, | ||
| monitorCommands: true, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Setup MongoDB metrics monitoring on a MongoClient | ||
| * @param client - MongoDB client to monitor | ||
| */ | ||
| export function setupMongoMetrics(client: MongoClient): void { | ||
| client.on('commandStarted', (event) => { | ||
| // Store start time for this command | ||
| const startTimeKey = `${event.requestId}`; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| (client as any)[startTimeKey] = Date.now(); | ||
| }); | ||
|
|
||
| client.on('commandSucceeded', (event) => { | ||
| const startTimeKey = `${event.requestId}`; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const startTime = (client as any)[startTimeKey]; | ||
|
|
||
| if (startTime) { | ||
| const duration = (Date.now() - startTime) / 1000; | ||
| const collection = event.command?.collection || event.command?.[event.commandName] || 'unknown'; | ||
neSpecc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const db = event.databaseName || 'unknown'; | ||
|
|
||
| mongoCommandDuration | ||
| .labels(event.commandName, collection, db) | ||
| .observe(duration); | ||
|
|
||
| // Clean up start time | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| delete (client as any)[startTimeKey]; | ||
| } | ||
| }); | ||
|
|
||
| client.on('commandFailed', (event) => { | ||
| const startTimeKey = `${event.requestId}`; | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const startTime = (client as any)[startTimeKey]; | ||
|
|
||
| if (startTime) { | ||
| const duration = (Date.now() - startTime) / 1000; | ||
| const collection = event.command?.collection || event.command?.[event.commandName] || 'unknown'; | ||
| const db = event.databaseName || 'unknown'; | ||
|
|
||
| mongoCommandDuration | ||
| .labels(event.commandName, collection, db) | ||
| .observe(duration); | ||
|
|
||
| // Track error | ||
| const errorCode = event.failure?.code?.toString() || 'unknown'; | ||
|
|
||
| mongoCommandErrors | ||
| .labels(event.commandName, errorCode) | ||
| .inc(); | ||
|
|
||
| // Clean up start time | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| delete (client as any)[startTimeKey]; | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.