Skip to content

Commit

Permalink
enhance(prom): add operationType label
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Mar 14, 2024
1 parent e2fb7ed commit 3d59e68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-grapes-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-mesh/plugin-prometheus": patch
---

Add `operationType` to metrics
10 changes: 6 additions & 4 deletions packages/plugins/prometheus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function useMeshPrometheus(
typeof pluginOptions.subgraphExecute === 'string'
? pluginOptions.subgraphExecute
: 'graphql_mesh_subgraph_execute_duration';
const subgraphExecuteLabelNames = ['subgraphName'];
const subgraphExecuteLabelNames = ['subgraphName', 'operationType'];
subgraphExecuteHistogram = new Histogram({
name,
help: 'Time spent on subgraph execution',
Expand Down Expand Up @@ -140,7 +140,7 @@ export default function useMeshPrometheus(
}
return undefined;
},
onSubgraphExecute({ subgraphName }) {
onSubgraphExecute({ subgraphName, executionRequest: { operationType = 'query' } }) {
if (subgraphExecuteHistogram) {
const start = Date.now();
return ({ result }) => {
Expand All @@ -149,7 +149,7 @@ export default function useMeshPrometheus(
onNext({ result }) {
if (result.errors) {
result.errors.forEach(() => {
subgraphExecuteErrorCounter?.inc({ subgraphName });
subgraphExecuteErrorCounter?.inc({ subgraphName, operationType });
});
}
},
Expand All @@ -159,6 +159,7 @@ export default function useMeshPrometheus(
subgraphExecuteHistogram.observe(
{
subgraphName,
operationType,
},
duration,
);
Expand All @@ -167,14 +168,15 @@ export default function useMeshPrometheus(
}
if (result.errors) {
result.errors.forEach(() => {
subgraphExecuteErrorCounter?.inc({ subgraphName });
subgraphExecuteErrorCounter?.inc({ subgraphName, operationType });
});
}
const end = Date.now();
const duration = end - start;
subgraphExecuteHistogram.observe(
{
subgraphName,
operationType,
},
duration,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/prometheus/tests/prometheus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Prometheus', () => {
const metrics = await registry.metrics();
expect(metrics).toContain('graphql_mesh_subgraph_execute_duration');
expect(metrics).toContain('subgraphName="TestSubgraph"');
expect(metrics).toContain('operationType="query"');
});
it('should track subgraph request errors', async () => {
const res = await serveRuntime.fetch('http://localhost:4000/graphql', {
Expand All @@ -87,5 +88,6 @@ describe('Prometheus', () => {
const metrics = await registry.metrics();
expect(metrics).toContain('graphql_mesh_subgraph_execute_errors');
expect(metrics).toContain('subgraphName="TestSubgraph"');
expect(metrics).toContain('operationType="query"');
});
});

0 comments on commit 3d59e68

Please sign in to comment.