Skip to content

Commit

Permalink
thread spans through (#274)
Browse files Browse the repository at this point in the history
## Why

<!-- Describe what you are trying to accomplish with this pull request
-->

- being able to add things to the proc span directly from a handler is
nice

## What changed

<!-- Describe the changes you made in this pull request or pointers for
the reviewer -->

## Versioning

- [ ] Breaking protocol change
- [ ] Breaking ts/js API change

<!-- Kind reminder to add tests and updated documentation if needed -->
  • Loading branch information
jackyzha0 authored Oct 14, 2024
1 parent 1326bc5 commit 258fe9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions router/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Span } from '@opentelemetry/api';
import { TransportClientId } from '../transport/message';
import { SessionId } from '../transport/sessionStateMachine/common';

Expand Down Expand Up @@ -50,6 +51,11 @@ export type ProcedureHandlerContext<State> = ServiceContext & {
* State for this service as defined by the service definition.
*/
state: State;
/**
* The span for this procedure call. You can use this to add attributes, events, and
* links to the span.
*/
span: Span;
/**
* Metadata parsed on the server. See {@link ParsedMetadata}
*/
Expand Down
15 changes: 9 additions & 6 deletions router/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,22 @@ class RiverServer<Services extends AnyServiceSchemaMap>
closeReadable();
}

const handlerContext: ProcedureHandlerContext<object> = {
const handlerContextWithSpan: (
span: Span,
) => ProcedureHandlerContext<object> = (span) => ({
...serviceContext,
from: from,
sessionId,
metadata: sessionMetadata,
span,
cancel: () => {
onServerCancel({
code: CANCEL_CODE,
message: 'cancelled by server procedure handler',
});
},
signal: finishedController.signal,
};
});

switch (procedure.type) {
case 'rpc':
Expand All @@ -564,7 +567,7 @@ class RiverServer<Services extends AnyServiceSchemaMap>
async (span): ProcHandlerReturn => {
try {
const responsePayload = await procedure.handler({
ctx: handlerContext,
ctx: handlerContextWithSpan(span),
reqInit: initPayload,
});

Expand Down Expand Up @@ -592,7 +595,7 @@ class RiverServer<Services extends AnyServiceSchemaMap>
async (span): ProcHandlerReturn => {
try {
await procedure.handler({
ctx: handlerContext,
ctx: handlerContextWithSpan(span),
reqInit: initPayload,
reqReadable,
resWritable,
Expand All @@ -616,7 +619,7 @@ class RiverServer<Services extends AnyServiceSchemaMap>
async (span): ProcHandlerReturn => {
try {
await procedure.handler({
ctx: handlerContext,
ctx: handlerContextWithSpan(span),
reqInit: initPayload,
resWritable: resWritable,
});
Expand All @@ -638,7 +641,7 @@ class RiverServer<Services extends AnyServiceSchemaMap>
async (span): ProcHandlerReturn => {
try {
const responsePayload = await procedure.handler({
ctx: handlerContext,
ctx: handlerContextWithSpan(span),
reqInit: initPayload,
reqReadable: reqReadable,
});
Expand Down

0 comments on commit 258fe9f

Please sign in to comment.