Skip to content

Commit

Permalink
[otel] rely on context for parenting spans correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin committed Feb 10, 2025
1 parent 8c466f4 commit 8f14dd3
Show file tree
Hide file tree
Showing 61 changed files with 978 additions and 480 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-mesh/plugin-opentelemetry': patch
---

dependencies updates:

- Added dependency [`@opentelemetry/context-async-hooks@^1.30.1` ↗︎](https://www.npmjs.com/package/@opentelemetry/context-async-hooks/v/1.30.1) (to `dependencies`)
119 changes: 81 additions & 38 deletions e2e/opentelemetry/opentelemetry.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ beforeAll(async () => {
type JaegerTracesApiResponse = {
data: Array<{
traceID: string;
spans: Array<{
traceID: string;
spanID: string;
operationName: string;
tags: Array<{ key: string; value: string; type: string }>;
}>;
spans: JaegerTraceSpan[];
}>;
};

type JaegerTraceSpan = {
traceID: string;
spanID: string;
operationName: string;
tags: Array<{ key: string; value: string; type: string }>;
references: Array<{ refType: string; spanID: string; traceID: string }>;
};

describe('OpenTelemetry', () => {
(['grpc', 'http'] as const).forEach((OTLP_EXPORTER_TYPE) => {
describe(`exporter > ${OTLP_EXPORTER_TYPE}`, () => {
Expand Down Expand Up @@ -77,6 +80,9 @@ describe('OpenTelemetry', () => {
await checkFn(res);
return;
} catch (e) {
if (signal.aborted) {
throw err;
}
err = e;
}
}
Expand Down Expand Up @@ -559,40 +565,52 @@ describe('OpenTelemetry', () => {
expect(relevantTraces.length).toBe(1);
const relevantTrace = relevantTraces[0];
expect(relevantTrace).toBeDefined();
expect(relevantTrace?.spans.length).toBe(11);
expect(relevantTrace!.spans.length).toBe(18);

expect(relevantTrace?.spans).toContainEqual(
expect.objectContaining({ operationName: 'POST /graphql' }),
);
expect(relevantTrace?.spans).toContainEqual(
expect.objectContaining({ operationName: 'graphql.parse' }),
);
expect(relevantTrace?.spans).toContainEqual(
expect.objectContaining({ operationName: 'graphql.validate' }),
);
expect(relevantTrace?.spans).toContainEqual(
expect.objectContaining({ operationName: 'graphql.execute' }),
const spanTree = buildSpanTree(relevantTrace!.spans, 'POST /graphql');
expect(spanTree).toBeDefined();

const expectedHttpChildren = [
'graphql.parse',
'graphql.validate',
'graphql.execute',
];
expect(spanTree!.children).toHaveLength(3);
for (const operationName of expectedHttpChildren) {
expect(spanTree?.children).toContainEqual(
expect.objectContaining({
span: expect.objectContaining({ operationName }),
}),
);
}

const executeSpan = spanTree?.children.find(
({ span }) => span.operationName === 'graphql.execute',
);
expect(
relevantTrace?.spans.filter(
(r) => r.operationName === 'subgraph.execute (accounts)',
).length,
).toBe(2);
expect(
relevantTrace?.spans.filter(
(r) => r.operationName === 'subgraph.execute (products)',
).length,
).toBe(2);
expect(
relevantTrace?.spans.filter(
(r) => r.operationName === 'subgraph.execute (inventory)',
).length,
).toBe(1);
expect(
relevantTrace?.spans.filter(
(r) => r.operationName === 'subgraph.execute (reviews)',
).length,
).toBe(2);

const expectedExecuteChildren = [
['subgraph.execute (accounts)', 2],
['subgraph.execute (products)', 2],
['subgraph.execute (inventory)', 1],
['subgraph.execute (reviews)', 2],
] as const;

for (const [operationName, count] of expectedExecuteChildren) {
const matchingChildren = executeSpan!.children.filter(
({ span }) => span.operationName === operationName,
);
expect(matchingChildren).toHaveLength(count);
for (const child of matchingChildren) {
expect(child.children).toHaveLength(1);
expect(child.children).toContainEqual(
expect.objectContaining({
span: expect.objectContaining({
operationName: 'http.fetch',
}),
}),
);
}
}
});
});

Expand Down Expand Up @@ -1279,3 +1297,28 @@ describe('OpenTelemetry', () => {
});
});
});

type TraceTreeNode = {
span: JaegerTraceSpan;
children: TraceTreeNode[];
};
function buildSpanTree(
spans: JaegerTraceSpan[],
rootName: string,
): TraceTreeNode | undefined {
function buildNode(root: JaegerTraceSpan): TraceTreeNode {
return {
span: root,
children: spans
.filter((span) =>
span.references.find(
(ref) => ref.refType === 'CHILD_OF' && ref.spanID === root.spanID,
),
)
.map(buildNode),
};
}

const root = spans.find((span) => span.operationName === rootName);
return root && buildNode(root);
}
Binary file modified examples/apq-subgraphs/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/apq-subgraphs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/apq-subgraphs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@graphql-mesh/compose-cli": "^1.2.13",
"graphql": "^16.9.0",
"tslib": "^2.8.1",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"scripts": {
"service:greetings": "tsx services/greetings.ts",
Expand Down
Binary file modified examples/extra-fields/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/extra-fields/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/extra-fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"graphql": "^16.9.0",
"graphql-yoga": "^5.10.11",
"tslib": "^2.8.1",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"devDependencies": {
"tsx": "^4.19.2"
Expand Down
Binary file modified examples/federation-example/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/federation-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/federation-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@apollo/server": "^4.10.3",
"@apollo/subgraph": "^2.7.2",
"graphql": "^16.9.0",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"scripts": {
"service:accounts": "tsx services/accounts/index.ts",
Expand Down
Binary file modified examples/federation-mixed/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/federation-mixed/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/federation-mixed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@apollo/server": "^4.10.3",
"@apollo/subgraph": "^2.7.2",
"fets": "^0.8.4",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"devDependencies": {
"tsx": "^4.19.2",
Expand Down
Binary file modified examples/federation-subscriptions-passthrough/example.tar.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/federation-subscriptions-passthrough/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"graphql-sse": "^2.5.3",
"graphql-ws": "^5.16.0",
"ws": "^8.17.1",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"devDependencies": {
"tsx": "^4.19.2",
Expand Down
Binary file modified examples/file-upload/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/file-upload/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/file-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"@graphql-mesh/compose-cli": "^1.2.13",
"graphql": "^16.9.0",
"tslib": "^2.8.1",
"@graphql-hive/gateway": "^1.9.0"
"@graphql-hive/gateway": "^1.9.1"
},
"devDependencies": {
"tsx": "^4.19.2"
Expand Down
Binary file modified examples/hmac-auth-https/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/hmac-auth-https/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/hmac-auth-https/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@apollo/server": "^4.10.3",
"@apollo/subgraph": "^2.9.3",
"@graphql-hive/gateway": "^1.9.0",
"@graphql-hive/gateway": "^1.9.1",
"@graphql-mesh/compose-cli": "^1.3.3",
"@graphql-mesh/hmac-upstream-signature": "^1.2.19",
"@graphql-mesh/plugin-jwt-auth": "^1.4.0",
Expand Down
Binary file modified examples/interface-additional-resolvers/example.tar.gz
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/interface-additional-resolvers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8f14dd3

Please sign in to comment.