grpc-dc 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,5 +30,6 @@ https://browser-grpc.github.io/grpc-over-datachannels/app/
30
30
  - Runtime: this directory (see its generated sources).
31
31
  - Worked example: `examples/` (Echo in every method shape).
32
32
  - Tests: `tests/` — the same BDD scenarios every language runs.
33
+ - Test and example index: [`TESTS.md`](TESTS.md) — every suite and example here, plus shared matrix coverage.
33
34
 
34
35
  Full project quickstart: [`../QUICKSTART.md`](../QUICKSTART.md).
package/index.js CHANGED
@@ -11,8 +11,11 @@ export {
11
11
  decodeFrame,
12
12
  encodeTimeout,
13
13
  parseTimeout,
14
+ asTraceParent,
15
+ newTraceContext,
14
16
  setTracer,
15
17
  setTraceFilter,
18
+ traceEnd,
16
19
  toHex,
17
20
  fromHex,
18
21
  } from "./src/codec.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grpc-dc",
3
- "version": "0.38.0",
3
+ "version": "0.40.0",
4
4
  "description": "gRPC in the browser over WebRTC DataChannels: dependency-free generated gRPC clients & servers for JavaScript \u2014 no grpc-web proxy, no external runtime",
5
5
  "keywords": [
6
6
  "grpc",
package/src/channel.js CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  formatTraceparent,
13
13
  getTracer,
14
14
  headersFrame,
15
+ asTraceParent,
15
16
  parseTraceparent,
16
17
  statusFrame,
17
18
  traceEnd,
@@ -88,6 +89,18 @@ function responsesIterable(inner, cancel) {
88
89
  export class Channel {
89
90
  #endpoint;
90
91
  #traces = new Map();
92
+ #traceContext = null;
93
+
94
+ // A Channel view sharing this endpoint whose outbound spans are
95
+ // parented to traceContext (a server span context from
96
+ // ctx.traceContext, or a root from newTraceContext). Explicit
97
+ // call metadata still wins. This is the browser-safe propagation
98
+ // path: no ambient async context is assumed.
99
+ withTraceContext(traceContext) {
100
+ const ch = new Channel(this.#endpoint);
101
+ ch.#traceContext = traceContext ?? null;
102
+ return ch;
103
+ }
91
104
 
92
105
  /** Accepts an Endpoint or a raw RTCDataChannel-like object. */
93
106
  constructor(dcOrEndpoint, options) {
@@ -107,11 +120,15 @@ export class Channel {
107
120
  }
108
121
  let traceCtx = null;
109
122
  if (getTracer() !== null) {
110
- traceCtx = traceStart("client", path, parseTraceparent(metadata?.traceparent));
123
+ traceCtx = traceStart(
124
+ "client",
125
+ path,
126
+ parseTraceparent(metadata?.traceparent) ?? asTraceParent(this.#traceContext),
127
+ );
111
128
  if (traceCtx !== null) {
112
129
  metadata = {
113
130
  ...metadata,
114
- traceparent: formatTraceparent(traceCtx.traceId, traceCtx.spanId),
131
+ traceparent: formatTraceparent(traceCtx.traceId, traceCtx.spanId, traceCtx.sampled),
115
132
  };
116
133
  }
117
134
  }
package/src/codec.js CHANGED
@@ -153,7 +153,7 @@ export function fromHex(hex) {
153
153
  return out;
154
154
  }
155
155
 
156
- // --- Homegrown tracing (W3C traceparent propagation) -------------
156
+ // --- Homegrown tracing (beta) (W3C traceparent propagation) -------------
157
157
  // Dormant until setTracer() registers an observer with
158
158
  // onStart(kind, method, traceId, spanId, parentSpanId) and
159
159
  // onEnd(kind, method, traceId, spanId, statusName). Generated code
@@ -194,17 +194,18 @@ function randomHex(bytes) {
194
194
  return toHex(buf);
195
195
  }
196
196
 
197
- export function formatTraceparent(traceId, spanId) {
198
- return `00-${traceId}-${spanId}-01`;
197
+ export function formatTraceparent(traceId, spanId, sampled = true) {
198
+ return `00-${traceId}-${spanId}-${sampled ? "01" : "00"}`;
199
199
  }
200
200
 
201
201
  export function parseTraceparent(value) {
202
202
  if (typeof value !== "string") return null;
203
203
  const parts = value.split("-");
204
204
  if (parts.length !== 4) return null;
205
- const [, traceId, spanId] = parts;
205
+ const [, traceId, spanId, flags] = parts;
206
206
  if (!/^[0-9a-f]{32}$/.test(traceId) || !/^[0-9a-f]{16}$/.test(spanId)) return null;
207
- return { traceId, spanId };
207
+ if (!/^[0-9a-f]{2}$/.test(flags)) return null;
208
+ return { traceId, spanId, sampled: (parseInt(flags, 16) & 1) === 1 };
208
209
  }
209
210
 
210
211
  export function traceStart(kind, method, parent) {
@@ -213,7 +214,12 @@ export function traceStart(kind, method, parent) {
213
214
  traceId: parent ? parent.traceId : randomHex(16),
214
215
  spanId: randomHex(8),
215
216
  parentSpanId: parent ? parent.spanId : "",
217
+ sampled: parent ? parent.sampled : true,
218
+ startMs: Date.now(),
216
219
  };
220
+ // An upstream not-sampled decision is honored: the context
221
+ // still propagates (with flags 00) but no span is recorded.
222
+ if (!ctx.sampled) return ctx;
217
223
  try {
218
224
  tracer.onStart?.(kind, method, ctx.traceId, ctx.spanId, ctx.parentSpanId);
219
225
  } catch {
@@ -223,10 +229,23 @@ export function traceStart(kind, method, parent) {
223
229
  }
224
230
 
225
231
  export function traceEnd(kind, method, ctx, status) {
226
- if (ctx === null || tracer === null) return;
232
+ if (ctx === null || tracer === null || ctx.sampled === false) return;
227
233
  try {
228
- tracer.onEnd?.(kind, method, ctx.traceId, ctx.spanId, status);
234
+ tracer.onEnd?.(kind, method, ctx.traceId, ctx.spanId, status, Date.now() - ctx.startMs);
229
235
  } catch {
230
236
  // observers must never break the call
231
237
  }
232
238
  }
239
+
240
+ // Start a root span context (e.g. for a client workflow grouping
241
+ // several outbound calls under one trace). Pair with traceEnd.
242
+ // Returns null when tracing is dormant.
243
+ export function newTraceContext(kind = "internal", method = "") {
244
+ return traceStart(kind, method, null);
245
+ }
246
+
247
+ // View a span context as a traceparent parent for an outbound call.
248
+ export function asTraceParent(ctx) {
249
+ if (!ctx) return null;
250
+ return { traceId: ctx.traceId, spanId: ctx.spanId, sampled: ctx.sampled !== false };
251
+ }
package/src/server.js CHANGED
@@ -99,6 +99,7 @@ export class Server {
99
99
  setTrailers: (md) => Object.assign(responseTrailers, md),
100
100
  };
101
101
  const traceCtx = traceStart("server", path, parseTraceparent(metadata.traceparent));
102
+ ctx.traceContext = traceCtx;
102
103
  let traceStatus = "OK";
103
104
  try {
104
105
  await this.#dispatchTraced(path, method, ctx, input, callSink);