Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fastify v4 support #11

Merged
merged 5 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ jobs:
with:
lint: true
license-check: true
node-versions: '["18", "20", "22"]'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @fastify/otel

[![CI](https://github.com/fastify/otel/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/otel/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/otel.svg?style=flat)](https://www.npmjs.com/package/@fastify/otel)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

OpenTelemetry auto-instrumentation library.

## Install
Expand Down
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ const {
} = require('@opentelemetry/semantic-conventions')
const { InstrumentationBase } = require('@opentelemetry/instrumentation')

const fp = require('fastify-plugin')

const {
version: PACKAGE_VERSION,
name: PACKAGE_NAME
} = require('./package.json')

// Constants
const SUPPORTED_VERSIONS = '>=5.0.0 <6'
const SUPPORTED_VERSIONS = '>=4.0.0 <6'
const FASTIFY_HOOKS = [
'onRequest',
'preParsing',
Expand Down Expand Up @@ -64,10 +62,14 @@ class FastifyOtelInstrumentation extends InstrumentationBase {
plugin () {
const instrumentation = this

return fp(FastifyInstrumentationPlugin, {
FastifyInstrumentationPlugin[Symbol.for('skip-override')] = true
FastifyInstrumentationPlugin[Symbol.for('fastify.display-name')] = '@fastify/otel'
FastifyInstrumentationPlugin[Symbol.for('plugin-meta')] = {
fastify: SUPPORTED_VERSIONS,
name: '@fastify/otel',
})
}

return FastifyInstrumentationPlugin

function FastifyInstrumentationPlugin (instance, opts, done) {
const addHookOriginal = instance.addHook.bind(instance)
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
"scripts": {
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test": "npm run test:all && npm run test:typescript",
"test:unit": "c8 --100 node --test",
"test:all": "npm run test:v4 && npm run test:v5",
"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"
},
Expand Down Expand Up @@ -51,16 +54,17 @@
"@opentelemetry/sdk-trace-node": "^1.29.0",
"@types/node": "^22.0.0",
"c8": "^10.1.2",
"cross-env": "^7.0.3",
"eslint": "^9.16.0",
"fastify": "^5.1.0",
"fastifyv4": "npm:fastify@^4.0.0",
"neostandard": "^0.12.0",
"tsd": "^0.31.0"
},
"dependencies": {
"@opentelemetry/core": "^1.29.0",
"@opentelemetry/instrumentation": "^0.57.0",
"@opentelemetry/semantic-conventions": "^1.28.0",
"fastify-plugin": "^5.0.1"
"@opentelemetry/semantic-conventions": "^1.28.0"
},
"peerDependencies": {
"@opentelemetry/api": "^1.9.0"
Expand Down
41 changes: 41 additions & 0 deletions test/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const { test, describe } = require('node:test')
const assert = require('assert')
const Fastify = require(process.env.FASTIFY_VERSION || 'fastify')

const { InstrumentationBase } = require('@opentelemetry/instrumentation')

const FastifyInstrumentation = require('..')

describe('Interface', () => {
test('should exports support', t => {
assert.equal(FastifyInstrumentation.name, 'FastifyOtelInstrumentation')
assert.equal(
FastifyInstrumentation.default.name,
'FastifyOtelInstrumentation'
)
assert.equal(
FastifyInstrumentation.FastifyOtelInstrumentation.name,
'FastifyOtelInstrumentation'
)
assert.strictEqual(
Object.getPrototypeOf(FastifyInstrumentation),
InstrumentationBase
)
assert.strictEqual(new FastifyInstrumentation({ servername: 'test' }).servername, 'test')
})

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

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

app.register(plugin)

await app.ready()
})
})
Loading
Loading