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

[prometheus] improve documentation #7218

Merged
merged 19 commits into from
Jul 9, 2024
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
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_cli-7218-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/cli": patch
---
dependencies updates:
- Updated dependency [`rimraf@^6.0.0` ↗︎](https://www.npmjs.com/package/rimraf/v/6.0.0) (from `^5.0.0`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/fusion-composition": patch
---
dependencies updates:
- Updated dependency [`@theguild/federation-composition@^0.12.0` ↗︎](https://www.npmjs.com/package/@theguild/federation-composition/v/0.12.0) (from `^0.11.2`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_serve-runtime-7218-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/serve-runtime": patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/executor-http@^1.1.2` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor-http/v/1.1.2) (from `^1.0.6`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/transport-common": patch
---
dependencies updates:
- Added dependency [`@envelop/core@^5.0.1` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.0.1) (to `dependencies`)
10 changes: 10 additions & 0 deletions .changeset/@graphql-mesh_transport-http-7218-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@graphql-mesh/transport-http": patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/executor-http@^1.1.2` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor-http/v/1.1.2) (from `^1.0.6`, in `dependencies`)
- Added dependency [`@graphql-tools/executor-graphql-ws@^1.2.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor-graphql-ws/v/1.2.0) (to `dependencies`)
- Added dependency [`@graphql-tools/utils@^10.2.3` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.2.3) (to `dependencies`)
- Added dependency [`graphql-ws@^5.16.0` ↗︎](https://www.npmjs.com/package/graphql-ws/v/5.16.0) (to `dependencies`)
- Added dependency [`ws@^8.18.0` ↗︎](https://www.npmjs.com/package/ws/v/8.18.0) (to `dependencies`)
- Removed dependency [`@envelop/core@^5.0.0` ↗︎](https://www.npmjs.com/package/@envelop/core/v/5.0.0) (from `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-mesh_transport-rest-7218-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/transport-rest": patch
---
dependencies updates:
- Updated dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/qs/v/6.12.3) (from `6.12.2`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@omnigraph_json-schema-7218-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@omnigraph/json-schema": patch
---
dependencies updates:
- Updated dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/qs/v/6.12.3) (from `6.12.2`, in `dependencies`)
6 changes: 6 additions & 0 deletions .changeset/hungry-dolphins-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/plugin-prometheus': minor
---

Add missing labels and move their configuration to the `labels` options to match Yoga and Envelop
plugin API
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ newrelic_agent.log
!.yarn/releases
!.yarn/sdks
!.yarn/versions

.mise.toml
96 changes: 73 additions & 23 deletions packages/plugins/prometheus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ import {
usePrometheus,
} from '@graphql-yoga/plugin-prometheus';

export type { CounterAndLabels, FillLabelsFnParams, HistogramAndLabels, SummaryAndLabels };
export { createCounter, createHistogram, createSummary };
export type { CounterAndLabels, FillLabelsFnParams, HistogramAndLabels, SummaryAndLabels };

type MeshMetricsConfig = {
/**
* @deprecated Graph delegation is no longer a concept in Mesh v1, use `subgraphExecute` instead
*/
delegation?:
| boolean
| string
| HistogramAndLabels<string, Omit<OnDelegateHookPayload<unknown>, 'context'> | undefined>;
/**
* @deprecated Graph delegation is no longer a concept in Mesh v1
*/
delegationArgs?: boolean;
/**
* @deprecated Graph delegation is no longer a concept in Mesh v1
*/
delegationKey?: boolean;

subgraphExecute?:
Expand All @@ -58,8 +67,20 @@ type MeshMetricsConfig = {
string,
{ url: string; options: MeshFetchRequestInit; response: Response }
>;

/**
* @deprecated Use `labels.fetchRequestHeaders` instead
*/
fetchRequestHeaders?: boolean;
/**
* @deprecated Use `labels.fetchResponseHeaders` instead
*/
fetchResponseHeaders?: boolean;

labels?: {
fetchRequestHeaders?: boolean;
fetchResponseHeaders?: boolean;
};
};

type PrometheusPluginOptions = Omit<
Expand Down Expand Up @@ -105,10 +126,14 @@ export default function useMeshPrometheus(

if (pluginOptions.fetchMetrics) {
const labelNames = ['url', 'method', 'statusCode', 'statusText'];
if (pluginOptions.fetchRequestHeaders) {
const {
fetchRequestHeaders = pluginOptions.fetchRequestHeaders,
fetchResponseHeaders = pluginOptions.fetchResponseHeaders,
} = pluginOptions.labels || {};
if (fetchRequestHeaders) {
labelNames.push('requestHeaders');
}
if (pluginOptions.fetchResponseHeaders) {
if (fetchResponseHeaders) {
labelNames.push('responseHeaders');
}

Expand All @@ -133,10 +158,10 @@ export default function useMeshPrometheus(
statusText: response.statusText,
};

if (pluginOptions.fetchRequestHeaders) {
if (fetchRequestHeaders) {
labels.requestHeaders = JSON.stringify(options.headers);
}
if (pluginOptions.fetchResponseHeaders) {
if (fetchResponseHeaders) {
labels.responseHeaders = JSON.stringify(getHeadersObj(response.headers));
}
return labels;
Expand All @@ -151,10 +176,11 @@ export default function useMeshPrometheus(

if (pluginOptions.delegation) {
const delegationLabelNames = ['sourceName', 'typeName', 'fieldName'];
if (pluginOptions.delegationArgs) {
const { delegationArgs, delegationKey } = pluginOptions;
if (delegationArgs) {
delegationLabelNames.push('args');
}
if (pluginOptions.delegationKey) {
if (delegationKey) {
delegationLabelNames.push('key');
}
delegateHistogram =
Expand All @@ -175,19 +201,23 @@ export default function useMeshPrometheus(
sourceName,
typeName,
fieldName,
args: pluginOptions.delegationArgs ? JSON.stringify(args) : undefined,
key: pluginOptions.delegationKey ? JSON.stringify(key) : undefined,
args: delegationArgs ? JSON.stringify(args) : undefined,
key: delegationKey ? JSON.stringify(key) : undefined,
};
},
});
}

let subgraphExecuteHistogram: HistogramAndLabels<
'subgraphName' | 'operationType',
SubgraphMetricsLabelParams
>;
let subgraphExecuteHistogram: HistogramAndLabels<string, SubgraphMetricsLabelParams>;

if (pluginOptions.subgraphExecute !== false) {
const subgraphExecuteLabels = ['subgraphName'];
if (pluginOptions.labels?.operationName !== false) {
subgraphExecuteLabels.push('operationName');
}
if (pluginOptions.labels?.operationType !== false) {
subgraphExecuteLabels.push('operationType');
}
subgraphExecuteHistogram =
typeof pluginOptions.subgraphExecute === 'object'
? pluginOptions.subgraphExecute
Expand All @@ -199,20 +229,32 @@ export default function useMeshPrometheus(
? pluginOptions.subgraphExecute
: 'graphql_mesh_subgraph_execute_duration',
help: 'Time spent on subgraph execution',
labelNames: ['subgraphName', 'operationType'],
labelNames: subgraphExecuteLabels,
},
fillLabelsFn: ({ subgraphName, executionRequest: { operationType = 'query' } }) => ({
fillLabelsFn: ({
subgraphName,
executionRequest: { operationType = 'query', operationName },
}) => ({
subgraphName,
operationType,
operationType:
pluginOptions.labels?.operationType !== false ? operationType : undefined,
operationName:
pluginOptions.labels?.operationName !== false
? operationName || 'Anonymous'
: undefined,
}),
});
}

let subgraphExecuteErrorCounter: CounterAndLabels<
'subgraphName' | 'operationType',
SubgraphMetricsLabelParams
>;
let subgraphExecuteErrorCounter: CounterAndLabels<string, SubgraphMetricsLabelParams>;
if (pluginOptions.subgraphExecuteErrors !== false) {
const subgraphExecuteErrorLabels = ['subgraphName'];
if (pluginOptions.labels?.operationName !== false) {
subgraphExecuteErrorLabels.push('operationName');
}
if (pluginOptions.labels?.operationType !== false) {
subgraphExecuteErrorLabels.push('operationType');
}
subgraphExecuteErrorCounter =
typeof pluginOptions.subgraphExecuteErrors === 'object'
? pluginOptions.subgraphExecuteErrors
Expand All @@ -224,11 +266,19 @@ export default function useMeshPrometheus(
? pluginOptions.subgraphExecuteErrors
: `graphql_mesh_subgraph_execute_errors`,
help: 'Number of errors on subgraph execution',
labelNames: ['subgraphName', 'operationType'],
labelNames: subgraphExecuteErrorLabels,
},
fillLabelsFn: ({ subgraphName, executionRequest: { operationType = 'query' } }) => ({
fillLabelsFn: ({
subgraphName,
executionRequest: { operationType = 'query', operationName },
}) => ({
subgraphName,
operationType,
operationType:
pluginOptions.labels?.operationType !== false ? operationType : undefined,
operationName:
pluginOptions.labels?.operationName !== false
? operationName || 'Anonymous'
: undefined,
}),
});
}
Expand Down
Loading
Loading