Skip to content

Commit

Permalink
feat(extension/opentelemetry): add new extension (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 11, 2024
1 parent 24c1ae2 commit 84fe8be
Show file tree
Hide file tree
Showing 31 changed files with 854 additions and 45 deletions.
15 changes: 2 additions & 13 deletions examples/$/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import getPort from 'get-port'
import type { GraphQLSchema } from 'graphql'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { inspect } from 'node:util'

export * from './show.js'

export const documentQueryContinents = {
document: `query { continents { name } }`,
Expand All @@ -12,18 +13,6 @@ export const publicGraphQLSchemaEndpoints = {
Atlas: `https://countries.trevorblades.com/graphql`,
}

export const showPartition = `---------------------------------------- SHOW ----------------------------------------`

export const show = (value: unknown) => {
console.log(showPartition)
console.log(inspect(value, { depth: null, colors: true }))
}

export const showJson = (value: unknown) => {
console.log(showPartition)
console.log(JSON.stringify(value, null, 2))
}

export const serveSchema = async (input: { schema: GraphQLSchema }) => {
const { schema } = input
const yoga = createYoga({ schema, logging: false, maskedErrors: false })
Expand Down
39 changes: 39 additions & 0 deletions examples/$/show.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { inspect } from 'node:util'

const originalWrite = globalThis.process.stdout.write.bind(globalThis.process.stdout)

type Logger = typeof console.log | typeof globalThis.process.stdout.write

export const show = <$Logger extends Logger = typeof console.log>(
value: unknown,
logger?: $Logger,
): ReturnType<$Logger> => {
const write = logger ?? console.log
const inspected = inspect(value, { depth: null, colors: true })
const message = renderShow(inspected)
return write(message) as ReturnType<$Logger>
}

export const showJson = <$Logger extends Logger = typeof console.log>(
value: unknown,
logger?: $Logger,
): ReturnType<$Logger> => {
const write = logger ?? console.log
const encoded = JSON.stringify(value, null, 2)
const message = renderShow(encoded)
return write(message) as ReturnType<$Logger>
}

export const showPartition = `---------------------------------------- SHOW ----------------------------------------`

const renderShow = (value: string) => {
return showPartition + '\n' + value
}

export const interceptAndShowOutput = (): void => {
globalThis.process.stdout.write = (value) => {
if (typeof value !== `string`) return originalWrite(value)
if (value.includes(showPartition)) return originalWrite(value)
return originalWrite(renderShow(value))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const encode = (value: string) => {
return value
.replaceAll(/(id: )'.+'/g, `$1'...'`)
.replaceAll(/(traceId: )'.+'/g, `$1'...'`)
.replaceAll(/(parentId: )'.+'/g, `$1'...'`)
.replaceAll(/(timestamp: ).+,/g, `$10,`)
.replaceAll(/(duration: ).+,/g, `$10.0,`)
.replaceAll(/(service\.name': )'.+'/g, `$1'...'`)
.replaceAll(/('telemetry\.sdk\.version': )'.+'/g, `$1'...'`)
}
156 changes: 156 additions & 0 deletions examples/__outputs__/extension|extension_opentelemetry.output.test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: '...',
traceState: undefined,
name: 'encode',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: '...',
traceState: undefined,
name: 'pack',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: '...',
traceState: undefined,
name: 'exchange',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: '...',
traceState: undefined,
name: 'unpack',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: '...',
traceState: undefined,
name: 'decode',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': '...',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '...'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '...',
parentId: undefined,
traceState: undefined,
name: 'request',
id: '...',
kind: 0,
timestamp: 0,
duration: 0.0,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
continents: [
{ name: 'Africa' },
{ name: 'Antarctica' },
{ name: 'Asia' },
{ name: 'Europe' },
{ name: 'North America' },
{ name: 'Oceania' },
{ name: 'South America' }
]
}
156 changes: 156 additions & 0 deletions examples/__outputs__/extension|extension_opentelemetry.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: 'e3b3fd57f531674b',
traceState: undefined,
name: 'encode',
id: '0f8e4e2d2fa7a1d1',
kind: 0,
timestamp: 1726068743834000,
duration: 442.792,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: 'e3b3fd57f531674b',
traceState: undefined,
name: 'pack',
id: '37766e2e0fa6ea2e',
kind: 0,
timestamp: 1726068743836000,
duration: 808.5,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: 'e3b3fd57f531674b',
traceState: undefined,
name: 'exchange',
id: 'ed9ae7aad6fd1e69',
kind: 0,
timestamp: 1726068743837000,
duration: 329989.458,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: 'e3b3fd57f531674b',
traceState: undefined,
name: 'unpack',
id: 'd0d9cbd74e358490',
kind: 0,
timestamp: 1726068744168000,
duration: 1907.291,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: 'e3b3fd57f531674b',
traceState: undefined,
name: 'decode',
id: '51a5859eae82dd62',
kind: 0,
timestamp: 1726068744170000,
duration: 97.958,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
resource: {
attributes: {
'service.name': 'unknown_service:/Users/jasonkuhrt/Library/pnpm/nodejs/22.7.0/bin/node',
'telemetry.sdk.language': 'nodejs',
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': '1.26.0'
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '93ece70aa164958fd1b3d3c6c89e9f70',
parentId: undefined,
traceState: undefined,
name: 'request',
id: 'e3b3fd57f531674b',
kind: 0,
timestamp: 1726068743834000,
duration: 336135.125,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
---------------------------------------- SHOW ----------------------------------------
{
continents: [
{ name: 'Africa' },
{ name: 'Antarctica' },
{ name: 'Asia' },
{ name: 'Europe' },
{ name: 'North America' },
{ name: 'Oceania' },
{ name: 'South America' }
]
}
Loading

0 comments on commit 84fe8be

Please sign in to comment.