Skip to content

Commit

Permalink
fix: further exclude common context values
Browse files Browse the repository at this point in the history
  • Loading branch information
danstarns committed Sep 1, 2023
1 parent 4fc898c commit 5821093
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 7 deletions.
4 changes: 2 additions & 2 deletions graphql-otel.com/package-lock.json

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

148 changes: 146 additions & 2 deletions package/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 package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-otel",
"version": "0.1.6",
"version": "0.1.7",
"description": "Opentelemetry GraphQL Schema Directives.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
13 changes: 12 additions & 1 deletion package/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import safeJsonStringify from "safe-json-stringify";

export const excludedKeys = [
"req",
"_req",
"request",
"_request",
"res",
"_res",
"params",
"_params",
];

export function safeJson(object: any) {
return safeJsonStringify(object || {}, (key, value) => {
if (typeof value === "bigint") {
return value.toString();
}

if (["req", "_req", "request", "_request"].includes(key)) {
if (excludedKeys.includes(key)) {
return undefined;
}

Expand Down
22 changes: 21 additions & 1 deletion package/tests/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { GraphQLOTELContext } from "../src/context";
import { SpanStatusCode } from "@opentelemetry/api";
import { AttributeName } from "../src/trace-directive";
import { describe, beforeEach, test, expect } from "@jest/globals";
import { excludedKeys } from "../src/utils";
import exp from "constants";

const util = require("util");
const sleep = util.promisify(setTimeout);
Expand Down Expand Up @@ -443,6 +445,24 @@ describe("@trace directive", () => {
_req: {
url: "http://localhost:3000/graphql",
},
request: {
url: "http://localhost:3000/graphql",
},
_request: {
url: "http://localhost:3000/graphql",
},
res: {
statusCode: 200,
},
_res: {
statusCode: 200,
},
params: {
statusCode: 200,
},
_params: {
statusCode: 200,
},
someFunction: () => {},
},
});
Expand Down Expand Up @@ -471,7 +491,7 @@ describe("@trace directive", () => {
const context = JSON.parse(
spanTree.span.attributes[AttributeName.OPERATION_CONTEXT] as string
);
expect(context).toMatchObject({
expect(context).toEqual({
name: randomName,
someFunction: "Function",
});
Expand Down

0 comments on commit 5821093

Please sign in to comment.