Skip to content

feat(otel-nestjs-instrumentation): extract shared startOtelInstrumentationIfAbsent function - #9

Merged
Farenheith merged 1 commit into
mainfrom
feat/otel-shared-instrumentation-fn
Aug 10, 2026
Merged

feat(otel-nestjs-instrumentation): extract shared startOtelInstrumentationIfAbsent function#9
Farenheith merged 1 commit into
mainfrom
feat/otel-shared-instrumentation-fn

Conversation

@Farenheith

Copy link
Copy Markdown
Member

Summary

Mirrors the newrelic-nestjs-instrumentation pattern by extracting a shared startOtelInstrumentationIfAbsent function that both OtelContextGuard and OtelInterceptor delegate to.

Changes

  • New file: src/start-otel-instrumentation-if-absent.ts — checks for an existing span via InternalContext.getSpan() before creating a new one, preventing duplicate spans
  • Updated OtelContextGuard — now delegates to the shared function instead of inline span-check logic
  • Updated OtelInterceptor — calls the shared function as a fallback for RPC contexts where no HTTP guard has run (guard-only works for HTTP requests; interceptor is the RPC fallback)
  • Exports from src/index.ts and src/internal/index.ts
  • Tests updated to match the new architecture

Why this pattern?

The guard only runs for HTTP requests, while the interceptor also handles RPC. Both need to start instrumentation if none exists — a shared function avoids duplication and ensures consistent behavior across request types.

Verification

pnpm --filter otel-nestjs-instrumentation test   # 12 suites, 113 tests passed
pnpm --filter otel-nestjs-instrumentation lint   # Prettier + ESLint clean
pnpm --filter otel-nestjs-instrumentation build  # compiles without errors

…ationIfAbsent function

- Create src/start-otel-instrumentation-if-absent.ts mirroring the
  newrelic-nestjs-instrumentation pattern (startNewRelicTransactionIfAbsent)
- Update OtelContextGuard to delegate to the shared function instead of
  inline span-check logic
- Update OtelInterceptor to call startOtelInstrumentationIfAbsent as a
  fallback for RPC contexts where no HTTP guard has run
- Export from src/index.ts and internal/index.ts
- Update all affected tests (guard, interceptor, integration) to match
  the new architecture
@sonarqubecloud

Copy link
Copy Markdown

@Farenheith
Farenheith merged commit fb27ef1 into main Aug 10, 2026
3 checks passed
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.

1 participant