Skip to content
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
12 changes: 8 additions & 4 deletions apps/collector-proxy/tests/foreign-traces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ describe("foreign traces", () => {

const schemaHash = faker.string.alpha(6);

const document = /* GraphQL */ `
const documentAst = parse(/* GraphQL */ `
query {
users {
name
}
}
`;
`);

const printedDocument = print(documentAst);

const jsonDocument = JSON.stringify(documentAst);

const payload: PostTraces["body"] = {
resourceSpans: [
Expand Down Expand Up @@ -64,7 +68,7 @@ describe("foreign traces", () => {
{
key: AttributeNames.DOCUMENT,
value: {
stringValue: document,
stringValue: jsonDocument,
},
},
{
Expand Down Expand Up @@ -231,7 +235,7 @@ describe("foreign traces", () => {
const rootSpan = traceGroup?.spans.find((span) => span.isGraphQLRootSpan);
expect(rootSpan).toBeDefined();
expect(rootSpan?.name).toEqual("query users");
expect(rootSpan?.graphqlDocument).toEqual(print(parse(document)));
expect(rootSpan?.graphqlDocument).toEqual(printedDocument);

const foreignSpans = traceGroup?.spans.filter(
(span) => span.isForeign === true,
Expand Down
12 changes: 8 additions & 4 deletions apps/collector-proxy/tests/plugin-express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ describe("plugin express", () => {

const schemaHash = faker.string.alpha(6);

const document = /* GraphQL */ `
const documentAst = parse(/* GraphQL */ `
query {
users {
name
}
}
`;
`);

const printedDocument = print(documentAst);

const jsonDocument = JSON.stringify(documentAst);

const expressScopeSpan: ResourceSpans["scopeSpans"][0] = {
scope: knownScope,
Expand Down Expand Up @@ -105,7 +109,7 @@ describe("plugin express", () => {
{
key: AttributeNames.DOCUMENT,
value: {
stringValue: document,
stringValue: jsonDocument,
},
},
{
Expand Down Expand Up @@ -202,7 +206,7 @@ describe("plugin express", () => {
const rootSpan = traceGroup?.spans.find((span) => span.isGraphQLRootSpan);
expect(rootSpan).toBeDefined();
expect(rootSpan?.name).toEqual("query users");
expect(rootSpan?.graphqlDocument).toEqual(print(parse(document)));
expect(rootSpan?.graphqlDocument).toEqual(printedDocument);

const nameSpan = traceGroup?.spans.find(
(span) => span.name === "User name",
Expand Down
12 changes: 8 additions & 4 deletions apps/collector-proxy/tests/post-traces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("POST /v1/traces", () => {

const schemaHash = faker.string.alpha(6);

const document = /* GraphQL */ `
const documentAst = /* GraphQL */ parse(`
query {
users {
name
Expand All @@ -80,7 +80,11 @@ describe("POST /v1/traces", () => {
}
}
}
`;
`);

const printedDocument = print(documentAst);

const jsonDocument = JSON.stringify(documentAst);

const payload: PostTraces["body"] = {
resourceSpans: [
Expand Down Expand Up @@ -111,7 +115,7 @@ describe("POST /v1/traces", () => {
{
key: AttributeNames.DOCUMENT,
value: {
stringValue: document,
stringValue: jsonDocument,
},
},
{
Expand Down Expand Up @@ -367,6 +371,6 @@ describe("POST /v1/traces", () => {
const rootSpan = traceGroup?.spans.find((span) => span.isGraphQLRootSpan);
expect(rootSpan).toBeDefined();
expect(rootSpan?.name).toEqual("query users");
expect(rootSpan?.graphqlDocument).toEqual(print(parse(document)));
expect(rootSpan?.graphqlDocument).toEqual(printedDocument);
});
});
7 changes: 4 additions & 3 deletions packages/opentelemetry/src/extract-spans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ResourceSpans,
} from "@graphql-debugger/types";

import { parse, print } from "graphql";
import { print } from "graphql";

import { attributesToObject } from "./attributes-to-object";
import { debug } from "./debug";
Expand Down Expand Up @@ -46,8 +46,9 @@ export function extractSpans({
const document = attributes[AttributeNames.DOCUMENT];
if (document) {
try {
const parsed = parse(document);
const printed = print(parsed);
const astString = document;
const ast = JSON.parse(astString);
const printed = print(ast);
graphqlDocument = printed;
} catch (error) {
debug("Error parsing document", error);
Expand Down
5 changes: 2 additions & 3 deletions packages/opentelemetry/src/info-to-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AttributeNames } from "@graphql-debugger/types";
import { isGraphQLInfoRoot } from "@graphql-debugger/utils";

import { GraphQLResolveInfo, print } from "graphql";
import { GraphQLResolveInfo } from "graphql";

export function infoToAttributes({
info,
Expand All @@ -21,8 +21,7 @@ export function infoToAttributes({
...(isRoot
? {
[AttributeNames.OPERATION_ROOT]: true,
// Note
[AttributeNames.DOCUMENT]: print(info.operation),
[AttributeNames.DOCUMENT]: JSON.stringify(info.operation),
[AttributeNames.OPERATION_TYPE]:
info.operation.operation.toLowerCase(),
...(info.operation.name?.value
Expand Down
46 changes: 25 additions & 21 deletions packages/opentelemetry/tests/extract-spans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ describe("extractSpans", () => {
test("should extract data from spans", () => {
const schemaHash = faker.string.alpha(6);

const document = print(
parse(/* GraphQL */ `
{
users(where: { name: "bob" }) {
name
errorField
}
const docAst = parse(/* GraphQL */ `
{
users(where: { name: "bob" }) {
name
errorField
}
`),
);
}
`);

const printedDocument = print(docAst);

const jsonDocument = JSON.stringify(docAst);

const error = new Error("something went wrong");

Expand Down Expand Up @@ -57,7 +59,7 @@ describe("extractSpans", () => {
{
key: AttributeNames.DOCUMENT,
value: {
stringValue: document,
stringValue: jsonDocument,
},
},
{
Expand Down Expand Up @@ -197,7 +199,7 @@ describe("extractSpans", () => {
resourceSpans[0].scopeSpans[0].spans[0].endTimeUnixNano,
).toString(),
graphqlSchemaHash: schemaHash,
graphqlDocument: document,
graphqlDocument: printedDocument,
errorMessage: undefined,
errorStack: undefined,
isForeign: false,
Expand Down Expand Up @@ -258,15 +260,17 @@ describe("extractSpans", () => {
test("should extract data from foreign spans", () => {
const schemaHash = faker.string.alpha(6);

const document = print(
parse(/* GraphQL */ `
{
users {
name
}
const docAst = parse(/* GraphQL */ `
{
users {
name
}
`),
);
}
`);

const printedDocument = print(docAst);

const jsonDocument = JSON.stringify(docAst);

const knownScope: ResourceSpans["scopeSpans"][0]["scope"] = {
name: TRACER_NAME,
Expand Down Expand Up @@ -305,7 +309,7 @@ describe("extractSpans", () => {
{
key: AttributeNames.DOCUMENT,
value: {
stringValue: document,
stringValue: jsonDocument,
},
},
{
Expand Down Expand Up @@ -456,7 +460,7 @@ describe("extractSpans", () => {
).toString(),
isForeign: false,
graphqlSchemaHash: schemaHash,
graphqlDocument: "{\n users {\n name\n }\n}",
graphqlDocument: printedDocument,
parentSpanId: undefined,
errorMessage: undefined,
errorStack: undefined,
Expand Down
93 changes: 72 additions & 21 deletions packages/trace-directive/tests/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,17 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query users");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

expect(spanTree.span.attributes[AttributeNames.OPERATION_NAME]).toMatch(
"users",
);
Expand Down Expand Up @@ -305,9 +313,16 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("mutation createUser");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

const postsSpan = spanTree.children.find(
(child) => child.span.name === "User posts",
Expand Down Expand Up @@ -372,9 +387,16 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query users");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

const events = spanTree.span.events;

Expand Down Expand Up @@ -478,9 +500,17 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query users");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

expect(
spanTree.span.attributes[AttributeNames.OPERATION_RETURN_TYPE],
).toMatch("[User]");
Expand Down Expand Up @@ -541,9 +571,16 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query user");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

const nameSpan = spanTree.children.find(
(child) => child.span.name === "User name",
Expand Down Expand Up @@ -604,9 +641,16 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query user");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));

const result = spanTree.span.attributes[AttributeNames.SCHEMA_HASH];

Expand Down Expand Up @@ -665,8 +709,15 @@ describe("@trace directive", () => {
const spanTree = buildSpanTree({ span: rootSpan, children: [] }, spans);

expect(spanTree.span.name).toEqual("query hello");
expect(spanTree.span.attributes[AttributeNames.DOCUMENT]).toMatch(
print(parse(query)),
);

const astString = spanTree.span.attributes[
AttributeNames.DOCUMENT
] as string;
if (!astString) {
throw new Error("astString is undefined");
}

const ast = JSON.parse(astString);
expect(print(ast)).toMatch(print(parse(query)));
});
});