Skip to content

feat(opentelemetry): create otel instrumentation for typed-express-router - #1044

Merged
ericcrosson-bitgo merged 1 commit into
betafrom
DX-1473-create-otel-wrapper-for-typed-express-router
Jul 18, 2025
Merged

feat(opentelemetry): create otel instrumentation for typed-express-router#1044
ericcrosson-bitgo merged 1 commit into
betafrom
DX-1473-create-otel-wrapper-for-typed-express-router

Conversation

@starfy84

@starfy84 starfy84 commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

Ticket: DX-1473

This PR implements opentelemetry instrumentation for the decode and sendEncoded calls in wrapRouter.

Changes:

  • typed-express-wrapper
    • @opentelemetry/api@^1.0.0 added as an optional peer dependency
    • @opentelemetry/sdk-trace-base@1.30.1 , @opentelemetry/sdk-trace-node@1.30.1, and @opentelemetry/api@1.9.0 added as dev dependencies
    • wrapRouter modified to create decode and encode spans if @opentelemetry/api is installed
      • if @opentelemetry/api is not installed, spans are not created
    • onDecodeError option removed. Please use decodeErrorFormatter and getDecodeErrorStatusCode
      • decodeErrorFormatter takes in an array of ValidationErrors and a WrappedRequest, returning a Json object.
      • getDecodeErrorStatusCode takes in an array of ValidationErrors and a WrappedRequest, returning a number.
    • onEncodeError option removed. Please use encodeErrorFormatter and getEncodeErrorStatusCode
      • encodeErrorFormatter takes in an error and a WrappedRequest, returning a Json object.
      • getEncodeErrorStatusCode takes in an error and a WrappedRequest, returning a number.
    • typed-express-router now handles the sending of the http response when there is a decode or encode error
  • express-wrapper
    • routerForApiSpec modified to pass in new parameters decodeErrorFormatter, getDecodeErrorStatusCode, encodeErrorFormatter, and getEncodeErrorStatusCode
      • These new parameters can also be customized by modifying the props passed into the function (see CreateRouterProps)

BREAKING CHANGE: onDecodeError and onEncodeError have been removed. Please use decodeErrorFormatter, getDecodeErrorStatusCode, encodeErrorFormatter, and getEncodeErrorStatusCode

@starfy84 starfy84 self-assigned this Jun 25, 2025
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch 2 times, most recently from 9c177cd to 0bff27e Compare June 26, 2025 15:07
@starfy84
starfy84 marked this pull request as ready for review June 26, 2025 15:15
@starfy84
starfy84 requested a review from a team as a code owner June 26, 2025 15:15
Comment thread packages/opentelemetry-instrumentation-typed-express-router/src/version.ts Outdated
Comment thread packages/opentelemetry-instrumentation-typed-express-router/src/utils.ts Outdated
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch from 43daf20 to 3781856 Compare June 30, 2025 15:38
@ericcrosson-bitgo
ericcrosson-bitgo marked this pull request as draft June 30, 2025 20:15
Comment thread packages/opentelemetry-instrumentation-express/test/utils.ts Fixed
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch 4 times, most recently from 6cd264b to 11fc7f0 Compare July 9, 2025 16:24
Comment thread packages/typed-express-router/src/index.ts Outdated
Comment thread packages/typed-express-router/src/index.ts Outdated
Comment thread packages/typed-express-router/src/telemetry.ts
Comment thread packages/typed-express-router/src/telemetry.ts
Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch from f804fb4 to 0158ca3 Compare July 9, 2025 16:28
@starfy84
starfy84 marked this pull request as ready for review July 9, 2025 16:30
@starfy84
starfy84 changed the base branch from master to beta July 9, 2025 19:55
@ericcrosson-bitgo
ericcrosson-bitgo marked this pull request as draft July 10, 2025 21:38
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch from a224511 to ab8a123 Compare July 15, 2025 15:19
Comment thread packages/express-wrapper/src/request.ts Outdated
Comment thread packages/typed-express-router/src/errors.ts Outdated
Comment thread packages/typed-express-router/src/types.ts Outdated
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch 2 times, most recently from 3cfad4e to 4248ffd Compare July 17, 2025 18:52
@starfy84
starfy84 marked this pull request as ready for review July 17, 2025 18:59

@ericcrosson-bitgo ericcrosson-bitgo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this text box can't be empty.

Comment thread packages/express-wrapper/src/request.ts
Comment thread packages/typed-express-router/src/errors.ts
onEncodeError: (_err, _req, res) => {
res.status(500).json('Custom encode error').end();
encodeErrorFormatter: (_err, _req) => {
return 'Custom encode error';

@ericcrosson-bitgo ericcrosson-bitgo Jul 17, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Look at all that data instead of state! 😍

Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
body?: any,
): Promise<{ statusCode: number; data: string }> => {
return new Promise((resolve, reject) => {
const url = `http://localhost:${(server.address() as any).port}${path}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: LSP says server.address() is of type string | AddressInfo | null -- how do you always know it is of type AddressInfo here? I wonder if there's a way to clean up the type cast.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation: https://github.com/nodejs/node/blob/main/doc/api/net.md#serveraddress

For a server listening on a pipe or Unix domain socket, the name is returned

and

server.address() returns null before the 'listening' event has been emitted or after calling server.close().

Since we're listening on a port instead of a pipe or a Unix domain socket, we can at least be sure that it'll at least be of type AddressInfo | null

IDEALLY, makeRequest should only be called when the server is listening. The server starts listening before every test (as defined in beforeEach).

So I think the best option here would just be to reject the promise if it's a string or null. (This would be an impossible case for the test)

Something like:

    const address = server.address();
    if (typeof address === 'string' || address === null) {
      reject('Unexpected address value');
      return;
    }
    const url = `http://localhost:${address.port}${path}`;

Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
Comment thread packages/typed-express-router/test/telemetry.test.ts Outdated
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch from 090eaed to 08e1817 Compare July 18, 2025 16:20
…uter

This commit implements `opentelemetry` instrumentation for the `decode` and `sendEncoded` calls in `wrapRouter`.

Changes:
- `typed-express-wrapper`
  - `@opentelemetry/api@^1.0.0` added as an optional peer dependency
  - `@opentelemetry/sdk-trace-base@1.30.1` , `@opentelemetry/sdk-trace-node@1.30.1`, and `@opentelemetry/api@1.9.0` added as dev dependencies
  - `wrapRouter` modified to create decode and encode spans if `@opentelemetry/api` is installed
    - if `@opentelemetry/api` is not installed, spans are not created
  - `onDecodeError` option removed. Please use `decodeErrorFormatter` and `getDecodeErrorStatusCode`
    - `decodeErrorFormatter` takes in an array of `ValidationError`s and a `WrappedRequest`, returning a `Json` object.
    - `getDecodeErrorStatusCode` takes in an array of `ValidationError`s and a `WrappedRequest`, returning a number.
  - `onEncodeError` option removed. Please use `encodeErrorFormatter` and `getEncodeErrorStatusCode`
    - `encodeErrorFormatter` takes in an error and a `WrappedRequest`, returning a `Json` object.
    - `getEncodeErrorStatusCode` takes in an error and a `WrappedRequest`, returning a number.
  - `typed-express-router` now handles the sending of the http response when there is a decode or encode error
- `express-wrapper`
  - `routerForApiSpec` modified to pass in new parameters `decodeErrorFormatter`, `getDecodeErrorStatusCode`, `encodeErrorFormatter`, and `getEncodeErrorStatusCode`
    - These new parameters can also be customized by modifying the props passed into the function (see `CreateRouterProps`)

BREAKING CHANGE: `onDecodeError` and `onEncodeError` have been removed. Please use `decodeErrorFormatter`, `getDecodeErrorStatusCode`, `encodeErrorFormatter`, and `getEncodeErrorStatusCode`
@starfy84
starfy84 force-pushed the DX-1473-create-otel-wrapper-for-typed-express-router branch from 08e1817 to 296a92e Compare July 18, 2025 16:44

@ericcrosson-bitgo ericcrosson-bitgo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds opentelemetry support for typed-express-router

Exemplary work, @starfy84!! 🚀

@ericcrosson-bitgo

Copy link
Copy Markdown
Contributor

Double-checked that beta and master still point to the same commit:

$ git log
*   a203328 (HEAD -> beta, origin/master, origin/beta, origin/HEAD, master) Merge pull request #1043 from ericcrosson-bitgo/DX-1472-chore-transfer-code-ownership-to-devex - Eric Crosson, 4 weeks ago

@ericcrosson-bitgo
ericcrosson-bitgo merged commit 872e93c into beta Jul 18, 2025
4 checks passed
@ericcrosson-bitgo
ericcrosson-bitgo deleted the DX-1473-create-otel-wrapper-for-typed-express-router branch July 18, 2025 17:21
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version @api-ts/typed-express-router@2.0.0-beta.1 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version @api-ts/express-wrapper@2.0.0-beta.1 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

@kaustubhbitgo

Copy link
Copy Markdown

@starfy84 Can we update README.md on deprecation of onDecodeError methods. I was adding typed routes to express but didn't realize they are deprecated when I was following README?

@starfy84

Copy link
Copy Markdown
Contributor Author

@starfy84 Can we update README.md on deprecation of onDecodeError methods. I was adding typed routes to express but didn't realize they are deprecated when I was following README?

Will do! Thanks for letting me know.

@github-actions

github-actions Bot commented Aug 1, 2025

Copy link
Copy Markdown

🎉 This PR is included in version @api-ts/typed-express-router@2.0.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions

github-actions Bot commented Aug 1, 2025

Copy link
Copy Markdown

🎉 This PR is included in version @api-ts/express-wrapper@2.0.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version @api-ts/openapi-generator@5.10.2 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants