Skip to content

Commit 4c22f71

Browse files
committed
Externalize zone.js in the browser package
1 parent e89901f commit 4c22f71

File tree

6 files changed

+43
-45
lines changed

6 files changed

+43
-45
lines changed

.changeset/fast-bushes-wonder.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pydantic/logfire-browser": patch
3+
"@pydantic/logfire-api": patch
4+
---
5+
6+
Externalize the context manager, to avoid zone.js patching

examples/browser/src/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getWebAutoInstrumentations } from "@opentelemetry/auto-instrumentations-web";
1+
import { getWebAutoInstrumentations } from '@opentelemetry/auto-instrumentations-web';
22
import * as logfire from '@pydantic/logfire-browser';
33

44

@@ -25,9 +25,18 @@ logfire.configure({
2525

2626

2727
document.querySelector('button')?.addEventListener('click', () => {
28+
logfire.info('Button clicked!')
2829
logfire.span('fetch wrapper',
2930
{
3031
callback: async () => { return fetch('https://jsonplaceholder.typicode.com/posts/1') }
3132
}
3233
)
34+
35+
logfire.span('test something', {
36+
callback: async () => {
37+
const promise = await new Promise((resolve) => setTimeout(resolve, 1000))
38+
logfire.info('something!')
39+
return promise
40+
},
41+
})
3342
})

package-lock.json

Lines changed: 1 addition & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/logfire-api/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function startSpan(
6464
): Span {
6565
const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
6666

67-
const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : logfireApiConfig.context
67+
const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active()
6868
const span = logfireApiConfig.tracer.startSpan(
6969
formattedMessage,
7070
{
@@ -84,7 +84,9 @@ export function startSpan(
8484

8585
type SpanCallback<R> = (activeSpan: Span) => R
8686
type SpanArgsVariant1<R> = [Record<string, unknown>, LogOptions, SpanCallback<R>]
87-
type SpanArgsVariant2<R> = [{ attributes?: Record<string, unknown>; callback: SpanCallback<R>; level?: LogFireLevel; tags?: string[] }]
87+
type SpanArgsVariant2<R> = [
88+
{ attributes?: Record<string, unknown>; callback: SpanCallback<R>; level?: LogFireLevel; parentSpan?: Span; tags?: string[] },
89+
]
8890

8991
/**
9092
* Starts a new Span and calls the given function passing it the
@@ -101,20 +103,24 @@ export function span<R>(msgTemplate: string, ...args: SpanArgsVariant1<R> | Span
101103
let level: LogFireLevel = Level.Info
102104
let tags: string[] = []
103105
let callback!: SpanCallback<R>
106+
let parentSpan: Span | undefined
104107
if (args.length === 1) {
105108
attributes = args[0].attributes ?? {}
106109
level = args[0].level ?? Level.Info
107110
tags = args[0].tags ?? []
108111
callback = args[0].callback
112+
parentSpan = args[0].parentSpan
109113
} else {
110114
attributes = args[0]
111115
level = args[1].level ?? Level.Info
112116
tags = args[1].tags ?? []
117+
parentSpan = args[1].parentSpan
113118
callback = args[2]
114119
}
115120

116121
const [formattedMessage, extraAttributes, newTemplate] = logfireFormatWithExtras(msgTemplate, attributes, logfireApiConfig.scrubber)
117122

123+
const context = parentSpan ? TheTraceAPI.setSpan(TheContextAPI.active(), parentSpan) : TheContextAPI.active()
118124
return logfireApiConfig.tracer.startActiveSpan(
119125
formattedMessage,
120126
{
@@ -125,6 +131,7 @@ export function span<R>(msgTemplate: string, ...args: SpanArgsVariant1<R> | Span
125131
[ATTRIBUTES_TAGS_KEY]: Array.from(new Set(tags).values()),
126132
},
127133
},
134+
context,
128135
(span: Span) => {
129136
const result = callback(span)
130137

packages/logfire-browser/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
},
5151
"dependencies": {
5252
"@opentelemetry/api": "^1.9.0",
53-
"@opentelemetry/context-zone": "^2.0.1",
5453
"@opentelemetry/core": "^2.0.1",
5554
"@opentelemetry/exporter-trace-otlp-http": "^0.202.0",
5655
"@opentelemetry/instrumentation": "^0.202.0",

packages/logfire-browser/src/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/* eslint-disable @typescript-eslint/no-deprecated */
2-
import { Context, diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api'
3-
import { ZoneContextManager } from '@opentelemetry/context-zone'
2+
import { Context, ContextManager, diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api'
43
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
54
import { Instrumentation, registerInstrumentations } from '@opentelemetry/instrumentation'
65
import { resourceFromAttributes } from '@opentelemetry/resources'
7-
import { BatchSpanProcessor, BufferConfig, ReadableSpan, Span, SpanProcessor, WebTracerProvider } from '@opentelemetry/sdk-trace-web'
6+
import {
7+
BatchSpanProcessor,
8+
BufferConfig,
9+
ReadableSpan,
10+
Span,
11+
SpanProcessor,
12+
StackContextManager,
13+
WebTracerProvider,
14+
} from '@opentelemetry/sdk-trace-web'
815
import {
916
ATTR_SERVICE_NAME,
1017
ATTR_SERVICE_VERSION,
@@ -41,14 +48,14 @@ export interface LogfireConfigOptions {
4148
*/
4249
batchSpanProcessorConfig?: BufferConfig
4350
/**
44-
* Defines the available internal logging levels for the diagnostic logger.
51+
* Pass a context manager (e.g. ZoneContextManager) to use.
4552
*/
46-
diagLogLevel?: DiagLogLevel
53+
contextManager?: ContextManager
4754

4855
/**
49-
* Set to `false` to disable the [zone context manager](https://www.npmjs.com/package/@opentelemetry/context-zone) usage.
56+
* Defines the available internal logging levels for the diagnostic logger.
5057
*/
51-
enableZoneContextManager?: boolean
58+
diagLogLevel?: DiagLogLevel
5259

5360
/**
5461
* The environment this service is running in, e.g. `staging` or `prod`. Sets the deployment.environment.name resource attribute. Useful for filtering within projects in the Logfire UI.
@@ -141,12 +148,9 @@ export function configure(options: LogfireConfigOptions) {
141148
],
142149
})
143150

144-
if (options.enableZoneContextManager !== false) {
145-
diag.info('logfire-browser: enable zone context manager')
146-
tracerProvider.register({
147-
contextManager: new ZoneContextManager(),
148-
})
149-
}
151+
tracerProvider.register({
152+
contextManager: options.contextManager ?? new StackContextManager(),
153+
})
150154

151155
const unregister = registerInstrumentations({
152156
instrumentations: options.instrumentations ?? [],

0 commit comments

Comments
 (0)