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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:v4": "cross-env FASTIFY_VERSION=fastifyv4 npm run test:unit",
"test:v5": "cross-env FASTIFY_VERSION=fastify npm run test:unit",
"test:coverage": "c8 node --test && c8 report --reporter=html",
"test:typescript": "tsd"
"test:typescript": "tstyche"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -60,7 +60,7 @@
"fastify-plugin": "^5.1.0",
"fastifyv4": "npm:fastify@^4.0.0",
"neostandard": "^0.13.0",
"tsd": "^0.33.0"
"tstyche": "^7.0.0"
},
"dependencies": {
"@opentelemetry/core": "^2.0.0",
Expand All @@ -74,4 +74,4 @@
"tsd": {
"directory": "types"
}
}
}
17 changes: 0 additions & 17 deletions types/env-vars.test-d.ts

This file was deleted.

17 changes: 17 additions & 0 deletions types/env-vars.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from 'tstyche'
import { InstrumentationBase } from '@opentelemetry/instrumentation'
import { fastify as Fastify } from 'fastify'

import { FastifyOtelInstrumentation, FastifyOtelInstrumentationOpts } from '.'

expect(new FastifyOtelInstrumentation()).type.toBeAssignableTo<InstrumentationBase>()

expect<FastifyOtelInstrumentationOpts>().type.toBeAssignableFrom({ enabled: true })
expect<FastifyOtelInstrumentationOpts>().type.toBeAssignableFrom({})

const app = Fastify()
app.register(new FastifyOtelInstrumentation().plugin())
app.register((nested, _opts, done) => {
nested.register(new FastifyOtelInstrumentation().plugin())
done()
})
72 changes: 0 additions & 72 deletions types/index.test-d.ts

This file was deleted.

84 changes: 84 additions & 0 deletions types/index.tst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from 'tstyche'
import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instrumentation'
import { Context, Span, TextMapGetter, TextMapSetter, Tracer } from '@opentelemetry/api'
import { fastify as Fastify, FastifyInstance, FastifyPluginCallback, FastifyRequest } from 'fastify'

import FastifyInstrumentation, { FastifyOtelInstrumentation } from '.'
import { FastifyOtelInstrumentationOpts } from './types'

expect(
new FastifyOtelInstrumentation()
).type.toBeAssignableTo<InstrumentationBase>()
expect(
new FastifyInstrumentation()
).type.toBeAssignableTo<InstrumentationBase>()

const complexOpts = {
enabled: true,
requestHook (span, request) {
expect(span).type.toBeAssignableTo<Span>()
expect(request).type.toBeAssignableTo<FastifyRequest>()
},
lifecycleHook (span, info) {
expect(span).type.toBeAssignableTo<Span>()
expect(info.hookName).type.toBeAssignableTo<string>()
expect(info.request).type.toBeAssignableTo<FastifyRequest>()
expect(info.handler).type.toBeAssignableTo<string | undefined>()
},
recordExceptions: false
} as FastifyOtelInstrumentationOpts

expect(complexOpts).type.toBeAssignableTo<InstrumentationConfig>()
expect(
{} as FastifyOtelInstrumentationOpts
).type.toBeAssignableTo<InstrumentationConfig>()
const app = Fastify()
const plugin = new FastifyOtelInstrumentation().plugin()

expect(app).type.toBeAssignableTo<FastifyInstance>()
expect(plugin).type.toBeAssignableTo<FastifyPluginCallback>()
expect(app.register(plugin)).type.toBeAssignableTo<FastifyInstance>()

app.register(new FastifyOtelInstrumentation().plugin())
app.register((nested, _opts, done) => {
nested.register(new FastifyOtelInstrumentation().plugin())
done()
})

app.get('/', async function (request, reply) {
const otel = request.opentelemetry()

expect<(carrier: any, setter?: TextMapSetter) => void>().type.toBeAssignableTo<
(carrier: any, setter?: TextMapSetter) => void
>()

expect<
(carrier: any, getter?: TextMapGetter) => Context
>().type.toBeAssignableTo<(carrier: any, getter?: TextMapGetter) => Context>()
expect(otel.tracer).type.toBeAssignableTo<Tracer>()

if (otel.enabled) {
expect(otel.span).type.toBeAssignableTo<Span>()
expect(otel.context).type.toBeAssignableTo<Context>()
} else {
expect(otel.span).type.toBe<null>()
expect(otel.context).type.toBe<null>()
}
})

// Test that otel field in FastifyContextConfig is optional
app.get('/with-config', { config: { } }, async function (_request, _reply) {
return { hello: 'world' }
})

app.get('/with-otel-true', { config: { otel: true } }, async function (_request, _reply) {
return { hello: 'world' }
})

app.get('/with-otel-false', { config: { otel: false } }, async function (_request, _reply) {
return { hello: 'world' }
})

app.get('/with-other-config', { config: { customField: 'value' } }, async function (_request, _reply) {
return { hello: 'world' }
})
Loading