Skip to content

Commit a7bdb23

Browse files
authored
docs(supabase): expand tracePropagation tsdoc with examples (#2388)
1 parent 9623b01 commit a7bdb23

3 files changed

Lines changed: 113 additions & 34 deletions

File tree

packages/core/supabase-js/src/SupabaseClient.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ export default class SupabaseClient<
5050
| { PostgrestVersion: string } = 'public' extends keyof Omit<Database, '__InternalSupabase'>
5151
? 'public'
5252
: string & keyof Omit<Database, '__InternalSupabase'>,
53-
SchemaName extends string &
54-
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
55-
keyof Omit<Database, '__InternalSupabase'>
56-
? SchemaNameOrClientOptions
57-
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
58-
? 'public'
59-
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
53+
SchemaName extends string & keyof Omit<Database, '__InternalSupabase'> =
54+
SchemaNameOrClientOptions extends string & keyof Omit<Database, '__InternalSupabase'>
55+
? SchemaNameOrClientOptions
56+
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
57+
? 'public'
58+
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
6059
Schema extends Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
6160
? Omit<Database, '__InternalSupabase'>[SchemaName]
6261
: never = Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
@@ -282,6 +281,30 @@ export default class SupabaseClient<
282281
*
283282
* const { data } = await supabase.from('profiles').select('*')
284283
* ```
284+
*
285+
* @exampleDescription With OpenTelemetry tracing
286+
* Opt in to W3C trace context propagation so the `trace_id` from your
287+
* client-side spans is attached to Supabase requests and appears in API
288+
* Gateway and Edge Function logs. Requires `@opentelemetry/api` to be
289+
* installed in your application. See [Tracing with the JS SDK](https://supabase.com/docs/guides/telemetry/client-side-tracing).
290+
*
291+
* @example With OpenTelemetry tracing
292+
* ```ts
293+
* import { createClient } from '@supabase/supabase-js'
294+
* import { trace } from '@opentelemetry/api'
295+
*
296+
* const supabase = createClient('https://xyzcompany.supabase.co', 'your-publishable-key', {
297+
* tracePropagation: true,
298+
* })
299+
*
300+
* const tracer = trace.getTracer('my-app')
301+
*
302+
* await tracer.startActiveSpan('fetch-users', async (span) => {
303+
* // Outgoing request carries the active trace context.
304+
* const { data, error } = await supabase.from('users').select('*')
305+
* span.end()
306+
* })
307+
* ```
285308
*/
286309
constructor(
287310
protected supabaseUrl: string,
@@ -447,11 +470,8 @@ export default class SupabaseClient<
447470
rpc<
448471
FnName extends string & keyof Schema['Functions'],
449472
Args extends Schema['Functions'][FnName]['Args'] = never,
450-
FilterBuilder extends GetRpcFunctionFilterBuilderByArgs<
451-
Schema,
452-
FnName,
453-
Args
454-
> = GetRpcFunctionFilterBuilderByArgs<Schema, FnName, Args>,
473+
FilterBuilder extends GetRpcFunctionFilterBuilderByArgs<Schema, FnName, Args> =
474+
GetRpcFunctionFilterBuilderByArgs<Schema, FnName, Args>,
455475
>(
456476
fn: FnName,
457477
args: Args = {} as Args,

packages/core/supabase-js/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export * from '@supabase/realtime-js'
2626
export { default as SupabaseClient } from './SupabaseClient'
2727
export type {
2828
SupabaseClientOptions,
29+
TracePropagationOptions,
2930
QueryResult,
3031
QueryData,
3132
QueryError,
@@ -50,13 +51,12 @@ export const createClient = <
5051
| { PostgrestVersion: string } = 'public' extends keyof Omit<Database, '__InternalSupabase'>
5152
? 'public'
5253
: string & keyof Omit<Database, '__InternalSupabase'>,
53-
SchemaName extends string &
54-
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
55-
keyof Omit<Database, '__InternalSupabase'>
56-
? SchemaNameOrClientOptions
57-
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
58-
? 'public'
59-
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
54+
SchemaName extends string & keyof Omit<Database, '__InternalSupabase'> =
55+
SchemaNameOrClientOptions extends string & keyof Omit<Database, '__InternalSupabase'>
56+
? SchemaNameOrClientOptions
57+
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
58+
? 'public'
59+
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
6060
>(
6161
supabaseUrl: string,
6262
supabaseKey: string,

packages/core/supabase-js/src/lib/types.ts

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,65 @@ export type Fetch = typeof fetch
3030
*
3131
* Enables distributed tracing across Supabase services using W3C Trace Context
3232
* and OpenTelemetry standards. When enabled, the SDK automatically attaches
33-
* trace context headers (traceparent, tracestate, baggage) to outgoing requests
34-
* to Supabase domains.
33+
* trace context headers (`traceparent`, `tracestate`, `baggage`) to outgoing
34+
* requests to Supabase domains. The resulting `trace_id` appears in API
35+
* Gateway and Edge Function logs, so logs forwarded through Log Drains can
36+
* be correlated back to the originating client-side span.
37+
*
38+
* Requires `@opentelemetry/api` to be installed in the consuming application.
39+
* If it is not installed, or there is no active context at request time,
40+
* propagation silently no-ops.
41+
*
42+
* @example Enable with defaults
43+
* ```ts
44+
* const supabase = createClient(url, key, {
45+
* tracePropagation: { enabled: true },
46+
* })
47+
* ```
3548
*
3649
* @see https://www.w3.org/TR/trace-context/
3750
* @see https://opentelemetry.io/docs/concepts/context-propagation/
51+
* @see https://supabase.com/docs/guides/telemetry/client-side-tracing
3852
*/
3953
export interface TracePropagationOptions {
4054
/**
4155
* Enable trace propagation. Disabled by default.
4256
*
4357
* When enabled, automatically detects and propagates active trace context
4458
* from the OpenTelemetry API to outgoing Supabase requests. Trace context
45-
* is only propagated to Supabase domains (*.supabase.co, *.supabase.in,
46-
* localhost) for security.
59+
* is only propagated to Supabase domains (`*.supabase.co`, `*.supabase.in`,
60+
* `localhost`) for security — third-party hosts never receive trace headers.
4761
*
4862
* @default false
63+
*
64+
* @example
65+
* ```ts
66+
* const supabase = createClient(url, key, {
67+
* tracePropagation: { enabled: true },
68+
* })
69+
* ```
4970
*/
5071
enabled?: boolean
5172

5273
/**
5374
* Respect upstream sampling decisions.
5475
*
55-
* When true, trace context will not be propagated if the upstream trace
56-
* indicates non-sampling (sampled flag = 0 in traceparent header).
57-
* This helps reduce overhead when traces are not being collected.
76+
* When true (the default), trace context is not propagated if the upstream
77+
* trace indicates non-sampling (sampled flag = `0` in the `traceparent`
78+
* header). This avoids overhead when traces are being recorded but dropped.
79+
*
80+
* Set to `false` to always propagate, regardless of the sampling decision
81+
* — useful when you want every Supabase request tagged with a `trace_id`
82+
* for log correlation, even if the trace itself will not be exported.
5883
*
5984
* @default true
85+
*
86+
* @example Always propagate, ignore sampling
87+
* ```ts
88+
* const supabase = createClient(url, key, {
89+
* tracePropagation: { enabled: true, respectSamplingDecision: false },
90+
* })
91+
* ```
6092
*/
6193
respectSamplingDecision?: boolean
6294
}
@@ -215,22 +247,49 @@ export type SupabaseClientOptions<SchemaName> = {
215247
* Enable OpenTelemetry / W3C trace context propagation to Supabase services.
216248
*
217249
* Disabled by default. Pass `true` for the common case (auto-detect an
218-
* active OTel context and inject `traceparent` / `tracestate` / `baggage`
219-
* headers) or an object for fine-grained control.
250+
* active OpenTelemetry context and inject `traceparent` / `tracestate` /
251+
* `baggage` headers) or an object for fine-grained control.
220252
*
221253
* Requires `@opentelemetry/api` to be installed in your application; if
222-
* not present, the SDK silently no-ops.
254+
* not present, the SDK silently no-ops. Trace headers are only attached
255+
* to requests targeting Supabase domains, so third-party hosts called
256+
* through a custom `fetch` are never tagged.
223257
*
224-
* @example
258+
* The resulting `trace_id` appears in Supabase logs (API Gateway, Edge
259+
* Functions), letting you correlate client-side spans with server-side
260+
* log entries — including logs forwarded via Log Drains.
261+
*
262+
* @example Shorthand — opt in with defaults
263+
* ```ts
264+
* import { createClient } from '@supabase/supabase-js'
265+
*
266+
* const supabase = createClient(url, key, { tracePropagation: true })
267+
* ```
268+
*
269+
* @example With an active OpenTelemetry span
225270
* ```ts
226-
* // Shorthand — opt in with defaults.
227-
* createClient(url, key, { tracePropagation: true })
271+
* import { createClient } from '@supabase/supabase-js'
272+
* import { trace } from '@opentelemetry/api'
228273
*
229-
* // Advanced — always propagate, even for non-sampled traces.
230-
* createClient(url, key, {
274+
* const supabase = createClient(url, key, { tracePropagation: true })
275+
* const tracer = trace.getTracer('my-app')
276+
*
277+
* await tracer.startActiveSpan('fetch-users', async (span) => {
278+
* // Request carries the active trace context.
279+
* const { data, error } = await supabase.from('users').select('*')
280+
* span.end()
281+
* })
282+
* ```
283+
*
284+
* @example Advanced — always propagate, even for non-sampled traces
285+
* ```ts
286+
* const supabase = createClient(url, key, {
231287
* tracePropagation: { enabled: true, respectSamplingDecision: false },
232288
* })
233289
* ```
290+
*
291+
* @see https://supabase.com/docs/guides/telemetry/client-side-tracing
292+
* @see https://www.w3.org/TR/trace-context/
234293
*/
235294
tracePropagation?: TracePropagationOptions | boolean
236295
}

0 commit comments

Comments
 (0)