Skip to content

Commit

Permalink
Add OTEL tests (#47149)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankaifer authored Mar 16, 2023
1 parent 67aceea commit 53f29cd
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@next/polyfill-module": "workspace:*",
"@next/polyfill-nomodule": "workspace:*",
"@next/swc": "workspace:*",
"@opentelemetry/api": "1.4.1",
"@svgr/webpack": "5.5.0",
"@swc/cli": "0.1.55",
"@swc/core": "1.2.203",
Expand Down
4 changes: 2 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"styled-jsx": "5.1.1"
},
"peerDependencies": {
"@opentelemetry/api": "^1.4.0",
"@opentelemetry/api": "^1.4.1",
"fibers": ">= 3.1.0",
"node-sass": "^6.0.0 || ^7.0.0",
"react": "^18.2.0",
Expand Down Expand Up @@ -140,7 +140,7 @@
"@next/react-dev-overlay": "13.2.5-canary.4",
"@next/react-refresh-utils": "13.2.5-canary.4",
"@next/swc": "13.2.5-canary.4",
"@opentelemetry/api": "1.4.0",
"@opentelemetry/api": "1.4.1",
"@segment/ajv-human-errors": "2.1.2",
"@taskr/clear": "1.1.0",
"@taskr/esnext": "1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/compiled/@opentelemetry/api/index.js

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions test/e2e/opentelemetry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# files generated by next.js
tsconfig.json
node_modules/
3 changes: 3 additions & 0 deletions test/e2e/opentelemetry/app/app/api/data/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function GET() {
return new Response(JSON.stringify({ test: 'data' }))
}
7 changes: 7 additions & 0 deletions test/e2e/opentelemetry/app/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
7 changes: 7 additions & 0 deletions test/e2e/opentelemetry/app/app/rsc-fetch/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// We are fetching our own API
export const dynamic = 'force-dynamic'

export default async function Page() {
const data = await fetch('https://user:pass@vercel.com')
return <pre>{await data.text()}</pre>
}
18 changes: 18 additions & 0 deletions test/e2e/opentelemetry/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { SpanKind } from '@opentelemetry/api'

export const traceFile = 'otel-trace.txt'

export type SavedSpan = {
traceId: string
parentId?: string
traceState: any
name: string
id: string
kind: SpanKind
timestamp: number
duration: number
attributes: Record<string, any>
status: any
events: any[]
links: any[]
}
70 changes: 70 additions & 0 deletions test/e2e/opentelemetry/instrumentation-node-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Resource } from '@opentelemetry/resources'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import {
NodeTracerProvider,
SimpleSpanProcessor,
SpanExporter,
ReadableSpan,
} from '@opentelemetry/sdk-trace-node'
import {
ExportResult,
ExportResultCode,
hrTimeToMicroseconds,
} from '@opentelemetry/core'
import fs from 'fs-extra'

import { SavedSpan, traceFile } from './constants'
import path from 'path'
import url from 'url'

const serializeSpan = (span: ReadableSpan): SavedSpan => ({
traceId: span.spanContext().traceId,
parentId: span.parentSpanId,
traceState: span.spanContext().traceState?.serialize(),
name: span.name,
id: span.spanContext().spanId,
kind: span.kind,
timestamp: hrTimeToMicroseconds(span.startTime),
duration: hrTimeToMicroseconds(span.duration),
attributes: span.attributes,
status: span.status,
events: span.events,
links: span.links,
})

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

class TestExporter implements SpanExporter {
async export(
spans: ReadableSpan[],
resultCallback: (result: ExportResult) => void
) {
const traceFullPath = path.join(__dirname, traceFile)
for (const span of spans) {
await fs.appendFile(
traceFullPath,
JSON.stringify(serializeSpan(span)) + '\n'
)
}
resultCallback({ code: ExportResultCode.SUCCESS })
}
shutdown(): Promise<void> {
return Promise.resolve()
}
}

export const register = () => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'test-next-app',
}),
})

provider.addSpanProcessor(new SimpleSpanProcessor(new TestExporter()))

// Make sure to register you provider
provider.register()

// Creating this file will let our tests know that initialization is done
fs.createFileSync('./' + traceFile)
}
24 changes: 24 additions & 0 deletions test/e2e/opentelemetry/instrumentation-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Resource } from '@opentelemetry/resources'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import {
NodeTracerProvider,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-node'

// You can use gRPC exporter instead
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'

export function register() {
// Next.js expects you to use to register TraceProvider. It won't work if you use NodeSDK.
// We use registered provider to create traces inside of Next.js internals.
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'next-app',
}),
})

provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter({})))

// Make sure to register you provider
provider.register()
}
11 changes: 11 additions & 0 deletions test/e2e/opentelemetry/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
if (process.env.__NEXT_TEST_MODE) {
require('./instrumentation-node-test').register()
} else {
// We use this instrumentation for easier debugging with this test.
// We want this test to be executable with `pnpm next-with-deps`.
require('./instrumentation-node').register()
}
}
}
6 changes: 6 additions & 0 deletions test/e2e/opentelemetry/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
experimental: {
instrumentationHook: true,
appDir: true,
},
}
Loading

0 comments on commit 53f29cd

Please sign in to comment.