fix(otel-nestjs-instrumentation): force RPC span kind when interceptor starts fallback transaction - #12
Merged
Conversation
…r starts fallback transaction When no guard runs (e.g. gRPC/microservice), the interceptor now passes effectiveType='rpc' to otelInstrumentation.create(), ensuring the fallback span is always SERVER/RPC instead of potentially being created as HTTP. The create() function signature was simplified: replaced forceSpanKind parameter with effectiveType?: 'http' | 'rpc'. When undefined, it falls back to context.getType(). This reduces cognitive complexity from 17 to below the SonarCloud limit of 15.
Farenheith
force-pushed
the
fix/otel-interceptor-rpc-span-kind
branch
from
August 11, 2026 02:38
49d8df2 to
b91eeb3
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
When the OtelInterceptor started a fallback OpenTelemetry transaction (no existing guard-started span), it passed the raw context to
otelInstrumentation.create()which checkedcontext.getType(). For non-HTTP transports (gRPC, microservices) where the guard didn't run, this could incorrectly create HTTP-span attributes instead of RPC-span attributes.Fix
Added an optional
forceSpanKindparameter through the call chain:SpanKind.SERVERas force when callingstartOtelInstrumentationIfAbsent(). Logic: if it were HTTP/HTTPS, the guard would have already started the transaction — any span created by the interceptor is inherently non-HTTP and should be RPC.create()rpc.method) instead of HTTP attributesChanges
src/internal/otel-instrumentation.ts— accept optionalforceSpanKind, use RPC attrs when forcedsrc/start-otel-instrumentation-if-absent.ts— pass through new parametersrc/otel.interceptor.ts— always force SERVER (RPC) span kind on fallbackVerification