Skip to content

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

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

Draft
wants to merge 4 commits into
base: beta
Choose a base branch
from

Conversation

starfy84
Copy link
Contributor

@starfy84 starfy84 commented Jun 25, 2025

Ticket: DX-1473

This PR implements a manual opentelemetry instrumentation for @api-ts/typed-express-router.

It modifies the library's wrapRouter and createRouter functions to create encode and decode spans.

For the telemetry to work, @opentelemetry/api needs to be installed

@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
@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
@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
@@ -20,6 +29,8 @@ import {
WrappedRouterOptions,
} from './types';

import type { Span } from '@opentelemetry/api';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is allowed. What happens when @opentelemetry/api isn't installed?

}
};
next();
};

const endDecodeSpanMiddleware: UncheckedRequestHandler = (req, _, next) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This middleware ends the decode span.

My only concern with this is that we're adding middleware to "check" (mainly just record, we don't throw/send any error status codes) for validation errors even though makeAddUncheckedRoute implies that the validation shouldn't be checked.

import type { Attributes, Span, Tracer } from '@opentelemetry/api';
import * as PathReporter from 'io-ts/lib/PathReporter';

let otelApi: any;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are we able to give this a concrete type definition? I wasn't able to figure out a way


import { Errors } from 'io-ts';
import type { SpanMetadata } from './types';
import type { Attributes, Span, Tracer } from '@opentelemetry/api';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same concern here as the type imports in index.ts

Comment on lines 443 to 447
if (require.cache[resolvedPath]) {
require.cache[resolvedPath] = {
...require.cache[resolvedPath],
exports: undefined,
};
}
delete require.cache[require.resolve('../src/telemetry')];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hack to pretend that package is not available

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants