Skip to content

Commit

Permalink
make handler spans more accurate, re-add connection span (#276)
Browse files Browse the repository at this point in the history
## Why

- a lot of proc handler specific log messages didnt get picked up by the
right tracer
- move proc stream <> handler proxy messages into the handler span
- move the invoked message into the client handler span
- re-add connection telemetry
- span linking between procs/handlers and their originating sessions
- we use
https://www.npmjs.com/package/@opentelemetry/context-async-hooks so
context is tracked safely in an async context so we should leverage that

## 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 21, 2024
1 parent 258fe9f commit b32d9f7
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 176 deletions.
13 changes: 13 additions & 0 deletions logging/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ValueError } from '@sinclair/typebox/value';
import { OpaqueTransportMessage, ProtocolVersion } from '../transport/message';
import { context, trace } from '@opentelemetry/api';

const LoggingLevels = {
debug: -1,
Expand Down Expand Up @@ -27,6 +28,17 @@ export type Tags =

const cleanedLogFn = (log: LogFn) => {
return (msg: string, metadata?: MessageMetadata) => {
// try to infer telemetry
if (metadata && !metadata.telemetry) {
const span = trace.getSpan(context.active());
if (span) {
metadata.telemetry = {
traceId: span.spanContext().traceId,
spanId: span.spanContext().spanId,
};
}
}

// skip cloning object if metadata has no transportMessage
if (!metadata?.transportMessage) {
log(msg, metadata);
Expand All @@ -37,6 +49,7 @@ const cleanedLogFn = (log: LogFn) => {
// clone metadata and clean transportMessage
const { payload, ...rest } = metadata.transportMessage;
metadata.transportMessage = rest;

log(msg, metadata);
};
};
Expand Down
53 changes: 22 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@replit/river",
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
"version": "0.203.0",
"version": "0.203.1",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -57,9 +57,9 @@
"@sinclair/typebox": "~0.32.8"
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^1.26.0",
"@opentelemetry/core": "^1.7.0",
"@opentelemetry/sdk-trace-base": "^1.24.1",
"@opentelemetry/sdk-trace-web": "^1.24.1",
"@stylistic/eslint-plugin": "^2.6.4",
"@types/ws": "^8.5.5",
"@typescript-eslint/eslint-plugin": "^7.8.0",
Expand Down
4 changes: 2 additions & 2 deletions router/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function handleProc(
const procClosesWithInit = procType === 'rpc' || procType === 'subscription';
const streamId = generateId();
const { span, ctx } = createProcTelemetryInfo(
transport,
session,
procType,
serviceName,
procedureName,
Expand Down Expand Up @@ -535,7 +535,7 @@ function handleProc(

/**
* Waits for a message in the response AND the server to close.
* Logs an error if we receive multiple messages.
* Logs an error if we receive multiple messages.
* Used in RPC and Upload.
*/
async function getSingleMessage(
Expand Down
Loading

0 comments on commit b32d9f7

Please sign in to comment.