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
36 changes: 18 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
/// <reference types="node" />

import { Context, Span, TextMapGetter, TextMapSetter, Tracer } from '@opentelemetry/api'
import { InstrumentationBase, InstrumentationConfig, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'
import { FastifyPluginCallback } from 'fastify'

export interface FastifyOtelOptions {}
export interface FastifyOtelInstrumentationOpts extends InstrumentationConfig {
servername?: string
registerOnInitialization?: boolean
}
export type FastifyOtelRequestContext = {
span: Span,
tracer: Tracer,
context: Context,
inject: (carrier: {}, setter?: TextMapSetter) => void;
extract: (carrier: {}, getter?: TextMapGetter) => Context
}
import {
FastifyOtelInstrumentationOpts,
FastifyOtelOptions,
FastifyOtelRequestContext
} from './types'

declare module 'fastify' {
interface FastifyRequest {
opentelemetry(): FastifyOtelRequestContext
}
}

declare class FastifyOtelInstrumentation<Config extends FastifyOtelInstrumentationOpts = FastifyOtelInstrumentationOpts> extends InstrumentationBase<Config> {
static FastifyInstrumentation: FastifyOtelInstrumentation
// eslint-disable-next-line @typescript-eslint/no-unused-vars
declare class FastifyOtelInstrumentationClass<Config extends FastifyOtelInstrumentationOpts = FastifyOtelInstrumentationOpts> extends InstrumentationBase<Config> {
servername: string
constructor (config?: FastifyOtelInstrumentationOpts)
init (): InstrumentationNodeModuleDefinition[]
plugin (): FastifyPluginCallback<FastifyOtelOptions>
}

export default FastifyOtelInstrumentation
export { FastifyOtelInstrumentation }
type FastifyOtelInstrumentationClassType = typeof FastifyOtelInstrumentationClass

interface FastifyOtelInstrumentationExport extends FastifyOtelInstrumentationClassType {
FastifyOtelInstrumentation: FastifyOtelInstrumentationClassType
}

declare const exported: FastifyOtelInstrumentationExport

export = exported
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ const kAddHookOriginal = Symbol('fastify otel addhook original')
const kSetNotFoundOriginal = Symbol('fastify otel setnotfound original')

class FastifyOtelInstrumentation extends InstrumentationBase {
static FastifyOtelInstrumentation = FastifyOtelInstrumentation
static default = FastifyOtelInstrumentation
servername = ''

constructor (config) {
Expand Down Expand Up @@ -433,3 +431,4 @@ class FastifyOtelInstrumentation extends InstrumentationBase {
}

module.exports = FastifyOtelInstrumentation
module.exports.FastifyOtelInstrumentation = FastifyOtelInstrumentation
16 changes: 15 additions & 1 deletion test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const Fastify = require(process.env.FASTIFY_VERSION || 'fastify')
const { InstrumentationBase } = require('@opentelemetry/instrumentation')

const FastifyInstrumentation = require('..')
const { FastifyOtelInstrumentation } = require('..')

describe('Interface', () => {
test('should exports support', t => {
assert.equal(FastifyInstrumentation.name, 'FastifyOtelInstrumentation')
assert.equal(
FastifyInstrumentation.default.name,
FastifyOtelInstrumentation.name,
'FastifyOtelInstrumentation'
)
assert.equal(
Expand All @@ -39,6 +40,19 @@ describe('Interface', () => {
await app.ready()
})

test('NamedFastifyInstrumentation#plugin should return a valid Fastify Plugin', async t => {
const app = Fastify()
const instrumentation = new FastifyOtelInstrumentation()
const plugin = instrumentation.plugin()

assert.equal(typeof plugin, 'function')
assert.equal(plugin.length, 3)

app.register(plugin)

await app.ready()
})

test('FastifyInstrumentation#plugin should expose the right set of APIs', async t => {
/** @type {import('fastify').FastifyInstance} */
const app = Fastify()
Expand Down
11 changes: 11 additions & 0 deletions test/api.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test, describe } from 'node:test'
import assert from 'node:assert'

import DefaultFastifyOtelInstrumentation, { FastifyOtelInstrumentation } from '../index.js'

describe('Interface', () => {
test('should Have a default export', t => {
assert.equal(DefaultFastifyOtelInstrumentation.name, 'FastifyOtelInstrumentation', 'Default export works')
assert.equal(FastifyOtelInstrumentation.name, 'FastifyOtelInstrumentation', 'Named export works')
})
})
3 changes: 2 additions & 1 deletion test/env-vars.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { expectAssignable } from 'tsd'
import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instrumentation'
import { fastify as Fastify } from 'fastify'

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

expectAssignable<InstrumentationBase>(new FastifyOtelInstrumentation())
expectAssignable<InstrumentationConfig>({ servername: 'server', enabled: true } as FastifyOtelInstrumentationOpts)
Expand Down
3 changes: 2 additions & 1 deletion test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instr
import { Context, Span, TextMapGetter, TextMapSetter, Tracer } from '@opentelemetry/api'
import { fastify as Fastify, FastifyInstance, FastifyPluginCallback } from 'fastify'

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

expectAssignable<InstrumentationBase>(new FastifyOtelInstrumentation())
expectAssignable<InstrumentationConfig>({ servername: 'server', enabled: true } as FastifyOtelInstrumentationOpts)
Expand Down
17 changes: 17 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// types.d.ts
import { InstrumentationConfig } from '@opentelemetry/instrumentation'
import { Context, Span, TextMapGetter, TextMapSetter, Tracer } from '@opentelemetry/api'

export interface FastifyOtelOptions {}
export interface FastifyOtelInstrumentationOpts extends InstrumentationConfig {
servername?: string
registerOnInitialization?: boolean
}

export type FastifyOtelRequestContext = {
span: Span,
tracer: Tracer,
context: Context,
inject: (carrier: {}, setter?: TextMapSetter) => void;
extract: (carrier: {}, getter?: TextMapGetter) => Context
}