@pydantic/logfire-cf-workers 0.11.10 → 0.11.11
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 +23 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,6 +21,29 @@ If you need to instrument your browser application, see the [Logfire Browser pac
|
|
|
21
21
|
|
|
22
22
|
See the [cf-worker example](https://github.com/pydantic/logfire-js/tree/main/examples/cf-worker) for a primer.
|
|
23
23
|
|
|
24
|
+
## Runtime lifecycle
|
|
25
|
+
|
|
26
|
+
Cloudflare Workers do not have a process-style shutdown hook. Logfire relies on
|
|
27
|
+
the Worker request lifetime instead: each instrumented handler schedules span
|
|
28
|
+
export with `ctx.waitUntil()` after the handler finishes.
|
|
29
|
+
|
|
30
|
+
This applies to both Logfire entrypoints:
|
|
31
|
+
|
|
32
|
+
- `instrumentInProcess()` and its `instrument()` alias configure in-process OTLP
|
|
33
|
+
export.
|
|
34
|
+
- `instrumentTail()` configures tail Worker export.
|
|
35
|
+
|
|
36
|
+
Both entrypoints delegate to the underlying `@pydantic/otel-cf-workers`
|
|
37
|
+
`instrument()` helper. That helper proxies the Worker execution context, tracks
|
|
38
|
+
promises passed to `ctx.waitUntil()` inside the user handler, then schedules span
|
|
39
|
+
export on the original context with `ctx.waitUntil()`. Export waits for a
|
|
40
|
+
scheduler tick and the tracked promises before force-flushing the configured span
|
|
41
|
+
processors.
|
|
42
|
+
|
|
43
|
+
Because export is tied to each Worker event, there is no long-lived Logfire
|
|
44
|
+
runtime to shut down after deployment. Use `ctx.waitUntil()` for any asynchronous
|
|
45
|
+
work that should be included in request-lifetime telemetry.
|
|
46
|
+
|
|
24
47
|
## Managed Variables
|
|
25
48
|
|
|
26
49
|
Cloudflare Workers can use local managed variables from `logfire/vars` when the
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});let e=require(`@opentelemetry/sdk-trace-base`),t=require(`@pydantic/otel-cf-workers`),n=require(`logfire`),r=require(`@opentelemetry/core`),i=require(`@opentelemetry/otlp-transformer`);async function a(e,t){let r=t.LOGFIRE_TOKEN;if(r===void 0||r===``){console.warn(`No token provided, not sending payload to Logfire`);return}let i=(0,n.resolveBaseUrl)(t,void 0,r),a=o(e);if(a!==null)try{return await fetch(`${i}/v1/traces`,{body:JSON.stringify(a),headers:{Authorization:r,"Content-Type":`application/json`},method:`POST`})}catch(e){console.error(e);return}}function o(e){for(let t of e)for(let e of t.logs)if(Array.isArray(e.message)){for(let t of e.message)if(s(t))return t}return null}function s(e){return typeof e==`object`&&!!e&&`resourceSpans`in e}const c={1:`trace`,5:`debug`,9:`info`,10:`notice`,13:`warning`,17:`error`,21:`fatal`};var l=class{export(e,t){this.sendSpans(e,t)}async forceFlush(){return Promise.resolve()}async shutdown(){return this.sendSpans([]),this.forceFlush()}exportInfo(e){return{attributes:e.attributes,duration:(0,r.hrTimeToMicroseconds)(e.duration),events:e.events,id:e.spanContext().spanId,instrumentationScope:e.instrumentationScope,kind:e.kind,links:e.links,name:e.name,parentSpanContext:e.parentSpanContext,resource:{attributes:e.resource.attributes},status:e.status,timestamp:(0,r.hrTimeToMicroseconds)(e.startTime),traceId:e.spanContext().traceId,traceState:e.spanContext().traceState?.serialize()}}sendSpans(e,t){for(let t of e){let e=c[t.attributes[`logfire.level_num`]]??`info`,{attributes:n,name:r,...i}=this.exportInfo(t);console.log(`Logfire: ${e} >> ${r}`),console.log(`Attributes:`),console.log(JSON.stringify(n,null,2)),console.log(`---`),console.log(`Span details:`),console.log(JSON.stringify(i,null,2)),console.log(`---`)}t&&t({code:r.ExportResultCode.SUCCESS})}},u=class{export(e,t){this.sendSpans(e,t)}async shutdown(){return this.sendSpans([]),Promise.resolve()}cleanNullValues(e){if(!e.resourceSpans)return e;for(let t of e.resourceSpans){d(t.resource);for(let e of t.scopeSpans){e.scope&&d(e.scope);for(let t of e.spans??[])d(t)}}return e}sendSpans(e,t){let n=i.JsonTraceSerializer.serializeRequest(e),a=new TextDecoder().decode(n),o=JSON.parse(a),s=this.cleanNullValues(o);return console.log(s),t?.({code:r.ExportResultCode.SUCCESS})}};function d(e){e?.attributes&&(e.attributes=e.attributes.filter(f))}function f(e){return Object.keys(e.value).length>0}function p(t){return r=>{let{LOGFIRE_ENVIRONMENT:i,LOGFIRE_TOKEN:a=``}=r,o=(0,n.resolveBaseUrl)(r,t.baseUrl,a),s=t.environment??i,c=t.additionalSpanProcessors??[];return t.console===!0&&c.push(new e.SimpleSpanProcessor(new l)),{...t,additionalSpanProcessors:c,exporter:{headers:{Authorization:a},url:`${o}/v1/traces`},idGenerator:new n.ULIDGenerator,postProcessor:e=>v(e),...s===void 0?{}:{environment:s}}}}function m(e){return t=>({...e,exporter:new u,idGenerator:new n.ULIDGenerator})}function h(e,r){return r.scrubbing!==void 0&&(0,n.configureLogfireApi)({scrubbing:r.scrubbing}),(0,t.instrument)(e,p(r))}function g(e,n){return(0,t.instrument)(e,m(n))}const _=h;function v(e){for(let t of e){for(let e of Object.keys(t.attributes))t.attributes[e]??delete t.attributes[e];Object.assign(t.attributes,(0,n.serializeAttributes)(t.attributes))}return e}const y={exportTailEventsToLogfire:a,configureLogfireApi:n.configureLogfireApi,debug:n.debug,error:n.error,fatal:n.fatal,getTailConfig:m,info:n.info,instrument:_,instrumentInProcess:h,instrumentTail:g,Level:n.Level,log:n.log,logfireApiConfig:n.logfireApiConfig,LogfireAttributeScrubber:n.LogfireAttributeScrubber,NoopAttributeScrubber:n.NoopAttributeScrubber,notice:n.notice,reportError:n.reportError,resolveBaseUrl:n.resolveBaseUrl,resolveSendToLogfire:n.resolveSendToLogfire,serializeAttributes:n.serializeAttributes,span:n.span,startSpan:n.startSpan,trace:n.trace,ULIDGenerator:n.ULIDGenerator,warning:n.warning};exports.default=y,exports.exportTailEventsToLogfire=a,exports.getTailConfig=m,exports.instrument=_,exports.instrumentInProcess=h,exports.instrumentTail=g;
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});let e=require(`@opentelemetry/sdk-trace-base`),t=require(`@pydantic/otel-cf-workers`),n=require(`logfire`),r=require(`@opentelemetry/core`),i=require(`@opentelemetry/otlp-transformer`);async function a(e,t){let r=t.LOGFIRE_TOKEN;if(r===void 0||r===``){console.warn(`No token provided, not sending payload to Logfire`);return}let i=(0,n.resolveBaseUrl)(t,void 0,r),a=o(e);if(a!==null)try{return await fetch(`${i}/v1/traces`,{body:JSON.stringify(a),headers:{Authorization:r,"Content-Type":`application/json`},method:`POST`})}catch(e){console.error(e);return}}function o(e){for(let t of e)for(let e of t.logs)if(Array.isArray(e.message)){for(let t of e.message)if(s(t))return t}return null}function s(e){return typeof e==`object`&&!!e&&`resourceSpans`in e}const c={1:`trace`,5:`debug`,9:`info`,10:`notice`,13:`warning`,17:`error`,21:`fatal`};var l=class{export(e,t){this.sendSpans(e,t)}async forceFlush(){return Promise.resolve()}async shutdown(){return this.sendSpans([]),this.forceFlush()}exportInfo(e){return{attributes:e.attributes,duration:(0,r.hrTimeToMicroseconds)(e.duration),events:e.events,id:e.spanContext().spanId,instrumentationScope:e.instrumentationScope,kind:e.kind,links:e.links,name:e.name,parentSpanContext:e.parentSpanContext,resource:{attributes:e.resource.attributes},status:e.status,timestamp:(0,r.hrTimeToMicroseconds)(e.startTime),traceId:e.spanContext().traceId,traceState:e.spanContext().traceState?.serialize()}}sendSpans(e,t){for(let t of e){let e=c[t.attributes[`logfire.level_num`]]??`info`,{attributes:n,name:r,...i}=this.exportInfo(t);console.log(`Logfire: ${e} >> ${r}`),console.log(`Attributes:`),console.log(JSON.stringify(n,null,2)),console.log(`---`),console.log(`Span details:`),console.log(JSON.stringify(i,null,2)),console.log(`---`)}t&&t({code:r.ExportResultCode.SUCCESS})}},u=class{export(e,t){this.sendSpans(e,t)}async shutdown(){return this.sendSpans([]),Promise.resolve()}cleanNullValues(e){if(!e.resourceSpans)return e;for(let t of e.resourceSpans){d(t.resource);for(let e of t.scopeSpans){e.scope&&d(e.scope);for(let t of e.spans??[])d(t)}}return e}sendSpans(e,t){let n=i.JsonTraceSerializer.serializeRequest(e),a=new TextDecoder().decode(n),o=JSON.parse(a),s=this.cleanNullValues(o);return console.log(s),t?.({code:r.ExportResultCode.SUCCESS})}};function d(e){e?.attributes&&(e.attributes=e.attributes.filter(f))}function f(e){return Object.keys(e.value).length>0}function p(t){return r=>{let{LOGFIRE_ENVIRONMENT:i,LOGFIRE_TOKEN:a=``}=r,o=(0,n.resolveBaseUrl)(r,t.baseUrl,a),s=t.environment??i,c=t.additionalSpanProcessors??[];return t.console===!0&&c.push(new e.SimpleSpanProcessor(new l)),{...t,additionalSpanProcessors:c,exporter:{headers:{Authorization:a},url:`${o}/v1/traces`},idGenerator:new n.ULIDGenerator,postProcessor:e=>v(e),...s===void 0?{}:{environment:s}}}}function m(e){return t=>({...e,exporter:new u,idGenerator:new n.ULIDGenerator})}function h(e,r){return r.scrubbing!==void 0&&(0,n.configureLogfireApi)({scrubbing:r.scrubbing}),(0,t.instrument)(e,p(r))}function g(e,n){return(0,t.instrument)(e,m(n))}const _=h;function v(e){for(let t of e){for(let e of Object.keys(t.attributes))t.attributes[e]??delete t.attributes[e];Object.assign(t.attributes,(0,n.serializeAttributes)(t.attributes))}return e}const y={exportTailEventsToLogfire:a,configureLogfireApi:n.configureLogfireApi,debug:n.debug,error:n.error,fatal:n.fatal,getTailConfig:m,info:n.info,instrument:_,instrumentInProcess:h,instrumentTail:g,Level:n.Level,log:n.log,logfireApiConfig:n.logfireApiConfig,LogfireAttributeScrubber:n.LogfireAttributeScrubber,NoopAttributeScrubber:n.NoopAttributeScrubber,notice:n.notice,reportError:n.reportError,resolveBaseUrl:n.resolveBaseUrl,resolveSendToLogfire:n.resolveSendToLogfire,serializeAttributes:n.serializeAttributes,span:n.span,startPendingSpan:n.startPendingSpan,startSpan:n.startSpan,trace:n.trace,ULIDGenerator:n.ULIDGenerator,warning:n.warning};exports.default=y,exports.exportTailEventsToLogfire=a,exports.getTailConfig=m,exports.instrument=_,exports.instrumentInProcess=h,exports.instrumentTail=g;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
2
|
import { TraceConfig } from "@pydantic/otel-cf-workers";
|
|
3
|
-
import { Level, LogfireAttributeScrubber, NoopAttributeScrubber, ScrubbingOptions, ULIDGenerator, configureLogfireApi, debug, error, fatal, info, log, logfireApiConfig, notice, reportError, resolveBaseUrl, resolveSendToLogfire, serializeAttributes, span, startSpan, trace, warning } from "logfire";
|
|
3
|
+
import { Level, LogfireAttributeScrubber, NoopAttributeScrubber, ScrubbingOptions, ULIDGenerator, configureLogfireApi, debug, error, fatal, info, log, logfireApiConfig, notice, reportError, resolveBaseUrl, resolveSendToLogfire, serializeAttributes, span, startPendingSpan, startSpan, trace, warning } from "logfire";
|
|
4
4
|
|
|
5
5
|
//#region src/exportTailEventsToLogfire.d.ts
|
|
6
6
|
interface TraceItem {
|
|
@@ -59,6 +59,7 @@ declare const defaultExport: {
|
|
|
59
59
|
resolveSendToLogfire: typeof resolveSendToLogfire;
|
|
60
60
|
serializeAttributes: typeof serializeAttributes;
|
|
61
61
|
span: typeof span;
|
|
62
|
+
startPendingSpan: typeof startPendingSpan;
|
|
62
63
|
startSpan: typeof startSpan;
|
|
63
64
|
trace: typeof trace;
|
|
64
65
|
warning: typeof warning;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
2
2
|
import { TraceConfig } from "@pydantic/otel-cf-workers";
|
|
3
|
-
import { Level, LogfireAttributeScrubber, NoopAttributeScrubber, ScrubbingOptions, ULIDGenerator, configureLogfireApi, debug, error, fatal, info, log, logfireApiConfig, notice, reportError, resolveBaseUrl, resolveSendToLogfire, serializeAttributes, span, startSpan, trace, warning } from "logfire";
|
|
3
|
+
import { Level, LogfireAttributeScrubber, NoopAttributeScrubber, ScrubbingOptions, ULIDGenerator, configureLogfireApi, debug, error, fatal, info, log, logfireApiConfig, notice, reportError, resolveBaseUrl, resolveSendToLogfire, serializeAttributes, span, startPendingSpan, startSpan, trace, warning } from "logfire";
|
|
4
4
|
|
|
5
5
|
//#region src/exportTailEventsToLogfire.d.ts
|
|
6
6
|
interface TraceItem {
|
|
@@ -59,6 +59,7 @@ declare const defaultExport: {
|
|
|
59
59
|
resolveSendToLogfire: typeof resolveSendToLogfire;
|
|
60
60
|
serializeAttributes: typeof serializeAttributes;
|
|
61
61
|
span: typeof span;
|
|
62
|
+
startPendingSpan: typeof startPendingSpan;
|
|
62
63
|
startSpan: typeof startSpan;
|
|
63
64
|
trace: typeof trace;
|
|
64
65
|
warning: typeof warning;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{SimpleSpanProcessor as e}from"@opentelemetry/sdk-trace-base";import{instrument as t}from"@pydantic/otel-cf-workers";import{Level as n,LogfireAttributeScrubber as r,NoopAttributeScrubber as i,ULIDGenerator as a,configureLogfireApi as o,debug as s,error as c,fatal as l,info as u,log as d,logfireApiConfig as f,notice as p,reportError as m,resolveBaseUrl as h,resolveSendToLogfire as g,serializeAttributes as _,span as v,
|
|
1
|
+
import{SimpleSpanProcessor as e}from"@opentelemetry/sdk-trace-base";import{instrument as t}from"@pydantic/otel-cf-workers";import{Level as n,LogfireAttributeScrubber as r,NoopAttributeScrubber as i,ULIDGenerator as a,configureLogfireApi as o,debug as s,error as c,fatal as l,info as u,log as d,logfireApiConfig as f,notice as p,reportError as m,resolveBaseUrl as h,resolveSendToLogfire as g,serializeAttributes as _,span as v,startPendingSpan as y,startSpan as b,trace as x,warning as S}from"logfire";import{ExportResultCode as C,hrTimeToMicroseconds as w}from"@opentelemetry/core";import{JsonTraceSerializer as T}from"@opentelemetry/otlp-transformer";async function E(e,t){let n=t.LOGFIRE_TOKEN;if(n===void 0||n===``){console.warn(`No token provided, not sending payload to Logfire`);return}let r=h(t,void 0,n),i=D(e);if(i!==null)try{return await fetch(`${r}/v1/traces`,{body:JSON.stringify(i),headers:{Authorization:n,"Content-Type":`application/json`},method:`POST`})}catch(e){console.error(e);return}}function D(e){for(let t of e)for(let e of t.logs)if(Array.isArray(e.message)){for(let t of e.message)if(O(t))return t}return null}function O(e){return typeof e==`object`&&!!e&&`resourceSpans`in e}const k={1:`trace`,5:`debug`,9:`info`,10:`notice`,13:`warning`,17:`error`,21:`fatal`};var A=class{export(e,t){this.sendSpans(e,t)}async forceFlush(){return Promise.resolve()}async shutdown(){return this.sendSpans([]),this.forceFlush()}exportInfo(e){return{attributes:e.attributes,duration:w(e.duration),events:e.events,id:e.spanContext().spanId,instrumentationScope:e.instrumentationScope,kind:e.kind,links:e.links,name:e.name,parentSpanContext:e.parentSpanContext,resource:{attributes:e.resource.attributes},status:e.status,timestamp:w(e.startTime),traceId:e.spanContext().traceId,traceState:e.spanContext().traceState?.serialize()}}sendSpans(e,t){for(let t of e){let e=k[t.attributes[`logfire.level_num`]]??`info`,{attributes:n,name:r,...i}=this.exportInfo(t);console.log(`Logfire: ${e} >> ${r}`),console.log(`Attributes:`),console.log(JSON.stringify(n,null,2)),console.log(`---`),console.log(`Span details:`),console.log(JSON.stringify(i,null,2)),console.log(`---`)}t&&t({code:C.SUCCESS})}},j=class{export(e,t){this.sendSpans(e,t)}async shutdown(){return this.sendSpans([]),Promise.resolve()}cleanNullValues(e){if(!e.resourceSpans)return e;for(let t of e.resourceSpans){M(t.resource);for(let e of t.scopeSpans){e.scope&&M(e.scope);for(let t of e.spans??[])M(t)}}return e}sendSpans(e,t){let n=T.serializeRequest(e),r=new TextDecoder().decode(n),i=JSON.parse(r),a=this.cleanNullValues(i);return console.log(a),t?.({code:C.SUCCESS})}};function M(e){e?.attributes&&(e.attributes=e.attributes.filter(N))}function N(e){return Object.keys(e.value).length>0}function P(t){return n=>{let{LOGFIRE_ENVIRONMENT:r,LOGFIRE_TOKEN:i=``}=n,o=h(n,t.baseUrl,i),s=t.environment??r,c=t.additionalSpanProcessors??[];return t.console===!0&&c.push(new e(new A)),{...t,additionalSpanProcessors:c,exporter:{headers:{Authorization:i},url:`${o}/v1/traces`},idGenerator:new a,postProcessor:e=>z(e),...s===void 0?{}:{environment:s}}}}function F(e){return t=>({...e,exporter:new j,idGenerator:new a})}function I(e,n){return n.scrubbing!==void 0&&o({scrubbing:n.scrubbing}),t(e,P(n))}function L(e,n){return t(e,F(n))}const R=I;function z(e){for(let t of e){for(let e of Object.keys(t.attributes))t.attributes[e]??delete t.attributes[e];Object.assign(t.attributes,_(t.attributes))}return e}const B={exportTailEventsToLogfire:E,configureLogfireApi:o,debug:s,error:c,fatal:l,getTailConfig:F,info:u,instrument:R,instrumentInProcess:I,instrumentTail:L,Level:n,log:d,logfireApiConfig:f,LogfireAttributeScrubber:r,NoopAttributeScrubber:i,notice:p,reportError:m,resolveBaseUrl:h,resolveSendToLogfire:g,serializeAttributes:_,span:v,startPendingSpan:y,startSpan:b,trace:x,ULIDGenerator:a,warning:S};export{B as default,E as exportTailEventsToLogfire,F as getTailConfig,R as instrument,I as instrumentInProcess,L as instrumentTail};
|
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"stats",
|
|
27
27
|
"monitoring"
|
|
28
28
|
],
|
|
29
|
-
"version": "0.11.
|
|
29
|
+
"version": "0.11.11",
|
|
30
30
|
"type": "module",
|
|
31
31
|
"main": "./dist/index.cjs",
|
|
32
32
|
"module": "./dist/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@pydantic/otel-cf-workers": "^1.0.0-rc.56",
|
|
48
|
-
"logfire": "0.
|
|
48
|
+
"logfire": "0.16.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@cloudflare/workers-types": "4.20260528.1",
|