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

Open telemetry support #803

Merged
merged 1 commit into from
Jun 11, 2021
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
4 changes: 3 additions & 1 deletion federation-integration-testsuite-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"homepage": "https://github.com/apollographql/federation#readme",
"dependencies": {
"apollo-graphql": "^0.9.3",
"graphql-tag": "^2.10.4"
"graphql-tag": "^2.10.4",
"@opentelemetry/api": "^0.20.0",
"@opentelemetry/tracing": "^0.20.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { queryPlanSerializer } from '@apollo/query-planner';
export { default as selectionSetSerializer } from './selectionSetSerializer';
export { default as typeSerializer } from './typeSerializer';
export { default as graphqlErrorSerializer } from './graphqlErrorSerializer';
export { default as spanSerializer } from './readableSpanArraySerializer';

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Plugin } from 'pretty-format';
import { ReadableSpan } from '@opentelemetry/tracing'

export default {
test(value: any) {
return value && Array.isArray(value) && value.length > 0 && isReadableSpan(value[0]);
},

print(spans: ReadableSpan[]) {
// This method takes an array of spans and builds up a set of trees containing only the information we are interested in.
// Spans contain the ID of it's parent, unless it is a root span with no parent.
//
// The simplified data model is called a span mirror.
//
// The algorithm is:
// 1. create span mirrors for every span and place it in a map.
// For each span:
// 1. find the corresponding mirrors for itself and parent.
// 2. push the parent in to the parent's children array.

const root = {children:[]};
const spanMirrors = new Map<ReadableSpan | undefined, any>();
spanMirrors.set(undefined, root);
spans.forEach((s) => spanMirrors.set(s, {name: s.name, attributes:s.attributes, children: [], status: s.status}));
spans.forEach((s) => {
const spanMirror = spanMirrors.get(s);
const parentSpan = spans.find((s2) => s2.spanContext().spanId === s.parentSpanId);
const parentSpanMirror = spanMirrors.get(parentSpan);
parentSpanMirror.children.push(spanMirror);
});

return JSON.stringify(root.children, undefined, 2);
},
} as Plugin;

function isReadableSpan(arg: any): arg is ReadableSpan {
let isSpan = arg && 'kind' in arg && 'startTime' in arg && 'parentSpanId' in arg;
return isSpan;
}
3 changes: 2 additions & 1 deletion gateway-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.

- Take subtypes into account when matching type conditions to extract representations. [PR #804](https://github.com/apollographql/federation/pull/804)
- Take subtypes into account when matching type conditions to extract representations. [PR #804](https://github.com/apollographql/federation/pull/804)
- OpenTelemetry support. [803](https://github.com/apollographql/federation/pull/803)

## v0.28.3

Expand Down
3 changes: 2 additions & 1 deletion gateway-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"apollo-server-types": "^0.9.0",
"loglevel": "^1.6.1",
"make-fetch-happen": "^8.0.0",
"pretty-format": "^26.0.0"
"pretty-format": "^26.0.0",
"@opentelemetry/api": "^0.20.0"
},
"peerDependencies": {
"graphql": "^14.5.0 || ^15.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`opentelemetry receives spans on fetch failure 1`] = `
[
{
"name": "gateway.request",
"attributes": {},
"children": [
{
"name": "gateway.validate",
"attributes": {},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.plan",
"attributes": {},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.execute",
"attributes": {},
"children": [
{
"name": "gateway.fetch",
"attributes": {
"service": "product"
},
"children": [],
"status": {
"code": 2
}
},
{
"name": "gateway.postprocessing",
"attributes": {},
"children": [],
"status": {
"code": 0
}
}
],
"status": {
"code": 2
}
}
],
"status": {
"code": 2
}
}
]
`;

exports[`opentelemetry with local data receives spans on plan failure 1`] = `
[
{
"name": "gateway.request",
"attributes": {},
"children": [
{
"name": "gateway.validate",
"attributes": {},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.plan",
"attributes": {},
"children": [],
"status": {
"code": 2
}
}
],
"status": {
"code": 2
}
}
]
`;

exports[`opentelemetry with local data receives spans on success 1`] = `
[
{
"name": "gateway.request",
"attributes": {},
"children": [
{
"name": "gateway.validate",
"attributes": {},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.plan",
"attributes": {},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.execute",
"attributes": {},
"children": [
{
"name": "gateway.fetch",
"attributes": {
"service": "product"
},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.fetch",
"attributes": {
"service": "books"
},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.fetch",
"attributes": {
"service": "product"
},
"children": [],
"status": {
"code": 0
}
},
{
"name": "gateway.postprocessing",
"attributes": {},
"children": [],
"status": {
"code": 0
}
}
],
"status": {
"code": 0
}
}
],
"status": {
"code": 0
}
}
]
`;

exports[`opentelemetry with local data receives spans on validation failure 1`] = `
[
{
"name": "gateway.request",
"attributes": {},
"children": [
{
"name": "gateway.validate",
"attributes": {},
"children": [],
"status": {
"code": 2
}
}
],
"status": {
"code": 2
}
}
]
`;
121 changes: 121 additions & 0 deletions gateway-js/src/__tests__/gateway/opentelemetry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import gql from 'graphql-tag';
import {ApolloGateway, LocalGraphQLDataSource} from '../../';
import {fixtures, spanSerializer} from 'apollo-federation-integration-testsuite';
import {fetch} from '../../__mocks__/apollo-server-env';
import {InMemorySpanExporter, SimpleSpanProcessor} from '@opentelemetry/tracing'
import {NodeTracerProvider} from '@opentelemetry/node';
import { buildFederatedSchema } from '@apollo/federation';

expect.addSnapshotSerializer(spanSerializer);

const inMemorySpans = new InMemorySpanExporter();
const tracerProvider = new NodeTracerProvider();
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(inMemorySpans));
tracerProvider.register();

beforeEach(() => {
fetch.mockReset();
inMemorySpans.reset();
});

describe('opentelemetry', () => {
async function execute(executor: any, source: string, variables: any) {
await executor({
source,
document: gql(source),
request: {
variables: variables,
},
queryHash: 'hashed',
context: null,
cache: {} as any,
});
}

describe('with local data', () =>
{
async function gateway() {
const gateway = new ApolloGateway({
localServiceList: fixtures,
buildService: service => {
// @ts-ignore
return new LocalGraphQLDataSource(buildFederatedSchema([service]));
},
});

const {executor} = await gateway.load();
return executor;
}

it('receives spans on success', async () => {
const executor = await gateway();

const source = `#graphql
query GetProduct($upc: String!) {
product(upc: $upc) {
name
}
}
`;

await execute(executor, source, {upc: '1'});
expect(inMemorySpans.getFinishedSpans()).toMatchSnapshot();
});

it('receives spans on validation failure', async () => {
const executor = await gateway();
const source = `#graphql
query InvalidVariables($first: Int!) {
topReviews(first: $first) {
body
}
}
`;

await execute(executor, source, {upc: '1'});
expect(inMemorySpans.getFinishedSpans()).toMatchSnapshot();
});

it('receives spans on plan failure', async () => {
const executor = await gateway();
const source = `#graphql
subscription GetProduct($upc: String!) {
product(upc: $upc) {
name
}
}
`;

try {
await execute(executor, source, {upc: '1'});
}
catch(err) {}
expect(inMemorySpans.getFinishedSpans()).toMatchSnapshot();
});
});


it('receives spans on fetch failure', async () => {

fetch.mockImplementationOnce(async () => {
throw Error("Nooo");
});

const gateway = new ApolloGateway({
localServiceList: fixtures,
});

const { executor } = await gateway.load();

const source = `#graphql
query GetProduct($upc: String!) {
product(upc: $upc) {
name
}
}
`;

await execute(executor, source, {upc: '1'});
expect(inMemorySpans.getFinishedSpans()).toMatchSnapshot();
});
});
Loading