@pydantic/logfire-cf-workers 0.12.4 → 1.0.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 +28 -6
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1 -1
- package/package.json +5 -11
- package/dist/index.cjs +0 -1
- package/dist/index.d.cts +0 -70
package/README.md
CHANGED
|
@@ -30,6 +30,28 @@ import { instrument as instrumentFunction } from 'logfire'
|
|
|
30
30
|
import { instrument as instrumentWorker } from '@pydantic/logfire-cf-workers'
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
Durable Object classes can be wrapped with `instrumentDO()` from this package so
|
|
34
|
+
they use the same Logfire token, base URL, environment, and scrubbing
|
|
35
|
+
configuration as Worker handlers.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { instrumentDO } from '@pydantic/logfire-cf-workers'
|
|
39
|
+
|
|
40
|
+
class CounterDurableObject implements DurableObject {
|
|
41
|
+
async fetch(): Promise<Response> {
|
|
42
|
+
return new Response('ok')
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const Counter = instrumentDO(CounterDurableObject, {
|
|
47
|
+
service: {
|
|
48
|
+
name: 'counter-do',
|
|
49
|
+
namespace: '',
|
|
50
|
+
version: '1.0.0',
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
33
55
|
## Runtime lifecycle
|
|
34
56
|
|
|
35
57
|
Cloudflare Workers do not have a process-style shutdown hook. Logfire relies on
|
|
@@ -42,12 +64,12 @@ This applies to both Logfire entrypoints:
|
|
|
42
64
|
export.
|
|
43
65
|
- `instrumentTail()` configures tail Worker export.
|
|
44
66
|
|
|
45
|
-
Both entrypoints
|
|
46
|
-
|
|
47
|
-
promises passed to `ctx.waitUntil()` inside the user handler,
|
|
48
|
-
export on the original context with `ctx.waitUntil()`.
|
|
49
|
-
scheduler tick and the tracked promises before force-flushing
|
|
50
|
-
processors.
|
|
67
|
+
Both entrypoints use this repository's Cloudflare Worker OpenTelemetry
|
|
68
|
+
instrumentation internally. The instrumentation proxies the Worker execution
|
|
69
|
+
context, tracks promises passed to `ctx.waitUntil()` inside the user handler,
|
|
70
|
+
then schedules span export on the original context with `ctx.waitUntil()`.
|
|
71
|
+
Export waits for a scheduler tick and the tracked promises before force-flushing
|
|
72
|
+
the configured span processors.
|
|
51
73
|
|
|
52
74
|
Because export is tied to each Worker event, there is no long-lived Logfire
|
|
53
75
|
runtime to shut down after deployment. Use `ctx.waitUntil()` for any asynchronous
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,8 @@ interface TraceItem {
|
|
|
11
11
|
declare function exportTailEventsToLogfire(events: TraceItem[], env: Record<string, string | undefined>): Promise<Response | undefined>;
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/index.d.ts
|
|
14
|
-
type Env =
|
|
14
|
+
type Env = unknown;
|
|
15
|
+
type DOClass<Env = unknown> = new (state: DurableObjectState, env: Env) => DurableObject;
|
|
15
16
|
type ConfigOptionsBase = Pick<TraceConfig, "environment" | "fetch" | "handlers" | "instrumentation" | "propagator" | "sampling" | "scope" | "service">;
|
|
16
17
|
interface InProcessConfigOptions extends ConfigOptionsBase {
|
|
17
18
|
/**
|
|
@@ -31,6 +32,7 @@ interface InProcessConfigOptions extends ConfigOptionsBase {
|
|
|
31
32
|
interface TailConfigOptions extends ConfigOptionsBase {}
|
|
32
33
|
declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
|
|
33
34
|
declare function instrumentInProcess<T>(handler: T, config: InProcessConfigOptions): T;
|
|
35
|
+
declare function instrumentDO<Env = unknown, T extends DOClass<Env> = DOClass<Env>>(doClass: T, config: InProcessConfigOptions): T;
|
|
34
36
|
declare function instrumentTail<T>(handler: T, config: TailConfigOptions): T;
|
|
35
37
|
/**
|
|
36
38
|
* Alias for `instrumentInProcess` to maintain compatibility with previous versions.
|
|
@@ -49,6 +51,7 @@ declare const defaultExport: {
|
|
|
49
51
|
getTailConfig: typeof getTailConfig;
|
|
50
52
|
info: typeof info;
|
|
51
53
|
instrument: typeof instrument;
|
|
54
|
+
instrumentDO: typeof instrumentDO;
|
|
52
55
|
instrumentInProcess: typeof instrumentInProcess;
|
|
53
56
|
instrumentTail: typeof instrumentTail;
|
|
54
57
|
log: typeof log;
|
|
@@ -67,4 +70,4 @@ declare const defaultExport: {
|
|
|
67
70
|
withTags: typeof withTags;
|
|
68
71
|
};
|
|
69
72
|
//#endregion
|
|
70
|
-
export { InProcessConfigOptions, TailConfigOptions, TraceItem, defaultExport as default, exportTailEventsToLogfire, getTailConfig, instrument, instrumentInProcess, instrumentTail };
|
|
73
|
+
export { InProcessConfigOptions, TailConfigOptions, TraceItem, defaultExport as default, exportTailEventsToLogfire, getTailConfig, instrument, instrumentDO, instrumentInProcess, instrumentTail };
|
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
|
|
1
|
+
import{SimpleSpanProcessor as e}from"@opentelemetry/sdk-trace-base";import{instrument as t,instrumentDO as n}from"@pydantic/otel-cf-workers";import{Level as r,LogfireAttributeScrubber as i,NoopAttributeScrubber as a,ULIDGenerator as o,configureLogfireApi as s,debug as c,error as l,fatal as u,info as d,log as f,logfireApiConfig as p,notice as m,reportError as h,resolveBaseUrl as g,resolveSendToLogfire as _,serializeAttributes as v,span as y,startPendingSpan as b,startSpan as x,trace as S,warning as C,withSettings as w,withTags as T}from"logfire";import{ExportResultCode as E,hrTimeToMicroseconds as D}from"@opentelemetry/core";import{JsonTraceSerializer as O}from"@opentelemetry/otlp-transformer";async function k(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=g(t,void 0,n),i=A(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 A(e){for(let t of e)for(let e of t.logs)if(Array.isArray(e.message)){for(let t of e.message)if(j(t))return t}return null}function j(e){return typeof e==`object`&&!!e&&`resourceSpans`in e}const M={1:`trace`,5:`debug`,9:`info`,10:`notice`,13:`warning`,17:`error`,21:`fatal`};var N=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:D(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:D(e.startTime),traceId:e.spanContext().traceId,traceState:e.spanContext().traceState?.serialize()}}sendSpans(e,t){for(let t of e){let e=M[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:E.SUCCESS})}},P=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){F(t.resource);for(let e of t.scopeSpans){e.scope&&F(e.scope);for(let t of e.spans??[])F(t)}}return e}sendSpans(e,t){let n=O.serializeRequest(e),r=new TextDecoder().decode(n),i=JSON.parse(r),a=this.cleanNullValues(i);return console.log(a),t?.({code:E.SUCCESS})}};function F(e){e?.attributes&&(e.attributes=e.attributes.filter(I))}function I(e){return Object.keys(e.value).length>0}function L(e,t){if(typeof e!=`object`||!e)return;let n=e[t];return typeof n==`string`?n:void 0}function R(e){e.scrubbing!==void 0&&s({scrubbing:e.scrubbing})}function z(t){return n=>{let r=L(n,`LOGFIRE_TOKEN`)??``,i=L(n,`LOGFIRE_ENVIRONMENT`),a=g(n,t.baseUrl,r),s=t.environment??i,c=t.additionalSpanProcessors??[];return t.console===!0&&c.push(new e(new N)),{...t,additionalSpanProcessors:c,exporter:{headers:{Authorization:r},url:`${a}/v1/traces`},idGenerator:new o,postProcessor:e=>G(e),...s===void 0?{}:{environment:s}}}}function B(e){return t=>({...e,exporter:new P,idGenerator:new o})}function V(e,n){return R(n),t(e,z(n))}function H(e,t){return R(t),n(e,z(t))}function U(e,n){return t(e,B(n))}const W=V;function G(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,v(t.attributes))}return e}const K={exportTailEventsToLogfire:k,configureLogfireApi:s,debug:c,error:l,fatal:u,getTailConfig:B,info:d,instrument:W,instrumentDO:H,instrumentInProcess:V,instrumentTail:U,Level:r,log:f,logfireApiConfig:p,LogfireAttributeScrubber:i,NoopAttributeScrubber:a,notice:m,reportError:h,resolveBaseUrl:g,resolveSendToLogfire:_,serializeAttributes:v,span:y,startPendingSpan:b,startSpan:x,trace:S,ULIDGenerator:o,warning:C,withSettings:w,withTags:T};export{K as default,k as exportTailEventsToLogfire,B as getTailConfig,W as instrument,H as instrumentDO,V as instrumentInProcess,U as instrumentTail};
|
package/package.json
CHANGED
|
@@ -26,25 +26,19 @@
|
|
|
26
26
|
"stats",
|
|
27
27
|
"monitoring"
|
|
28
28
|
],
|
|
29
|
-
"version": "0.
|
|
29
|
+
"version": "1.0.0",
|
|
30
30
|
"type": "module",
|
|
31
|
-
"main": "./dist/index.
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
32
|
"module": "./dist/index.js",
|
|
33
33
|
"types": "./dist/index.d.ts",
|
|
34
34
|
"exports": {
|
|
35
35
|
".": {
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
"default": "./dist/index.js"
|
|
39
|
-
},
|
|
40
|
-
"require": {
|
|
41
|
-
"types": "./dist/index.d.cts",
|
|
42
|
-
"default": "./dist/index.cjs"
|
|
43
|
-
}
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"default": "./dist/index.js"
|
|
44
38
|
}
|
|
45
39
|
},
|
|
46
40
|
"dependencies": {
|
|
47
|
-
"@pydantic/otel-cf-workers": "
|
|
41
|
+
"@pydantic/otel-cf-workers": "1.0.0",
|
|
48
42
|
"logfire": "0.20.1"
|
|
49
43
|
},
|
|
50
44
|
"devDependencies": {
|
package/dist/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,withSettings:n.withSettings,withTags:n.withTags};exports.default=y,exports.exportTailEventsToLogfire=a,exports.getTailConfig=m,exports.instrument=_,exports.instrumentInProcess=h,exports.instrumentTail=g;
|
package/dist/index.d.cts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
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, startPendingSpan, startSpan, trace, warning, withSettings, withTags } from "logfire";
|
|
4
|
-
|
|
5
|
-
//#region src/exportTailEventsToLogfire.d.ts
|
|
6
|
-
interface TraceItem {
|
|
7
|
-
logs: {
|
|
8
|
-
message: unknown[];
|
|
9
|
-
}[];
|
|
10
|
-
}
|
|
11
|
-
declare function exportTailEventsToLogfire(events: TraceItem[], env: Record<string, string | undefined>): Promise<Response | undefined>;
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/index.d.ts
|
|
14
|
-
type Env = Record<string, string | undefined>;
|
|
15
|
-
type ConfigOptionsBase = Pick<TraceConfig, "environment" | "fetch" | "handlers" | "instrumentation" | "propagator" | "sampling" | "scope" | "service">;
|
|
16
|
-
interface InProcessConfigOptions extends ConfigOptionsBase {
|
|
17
|
-
/**
|
|
18
|
-
* Additional span processors to add to the tracer provider.
|
|
19
|
-
*/
|
|
20
|
-
additionalSpanProcessors?: SpanProcessor[];
|
|
21
|
-
baseUrl?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Whether to log the spans to the console in addition to sending them to the Logfire API.
|
|
24
|
-
*/
|
|
25
|
-
console?: boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Options for scrubbing sensitive data. Set to False to disable.
|
|
28
|
-
*/
|
|
29
|
-
scrubbing?: false | ScrubbingOptions;
|
|
30
|
-
}
|
|
31
|
-
interface TailConfigOptions extends ConfigOptionsBase {}
|
|
32
|
-
declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
|
|
33
|
-
declare function instrumentInProcess<T>(handler: T, config: InProcessConfigOptions): T;
|
|
34
|
-
declare function instrumentTail<T>(handler: T, config: TailConfigOptions): T;
|
|
35
|
-
/**
|
|
36
|
-
* Alias for `instrumentInProcess` to maintain compatibility with previous versions.
|
|
37
|
-
*/
|
|
38
|
-
declare const instrument: typeof instrumentInProcess;
|
|
39
|
-
declare const defaultExport: {
|
|
40
|
-
exportTailEventsToLogfire: typeof exportTailEventsToLogfire;
|
|
41
|
-
Level: typeof Level;
|
|
42
|
-
LogfireAttributeScrubber: typeof LogfireAttributeScrubber;
|
|
43
|
-
NoopAttributeScrubber: typeof NoopAttributeScrubber;
|
|
44
|
-
ULIDGenerator: typeof ULIDGenerator;
|
|
45
|
-
configureLogfireApi: typeof configureLogfireApi;
|
|
46
|
-
debug: typeof debug;
|
|
47
|
-
error: typeof error;
|
|
48
|
-
fatal: typeof fatal;
|
|
49
|
-
getTailConfig: typeof getTailConfig;
|
|
50
|
-
info: typeof info;
|
|
51
|
-
instrument: typeof instrument;
|
|
52
|
-
instrumentInProcess: typeof instrumentInProcess;
|
|
53
|
-
instrumentTail: typeof instrumentTail;
|
|
54
|
-
log: typeof log;
|
|
55
|
-
logfireApiConfig: typeof logfireApiConfig;
|
|
56
|
-
notice: typeof notice;
|
|
57
|
-
reportError: typeof reportError;
|
|
58
|
-
resolveBaseUrl: typeof resolveBaseUrl;
|
|
59
|
-
resolveSendToLogfire: typeof resolveSendToLogfire;
|
|
60
|
-
serializeAttributes: typeof serializeAttributes;
|
|
61
|
-
span: typeof span;
|
|
62
|
-
startPendingSpan: typeof startPendingSpan;
|
|
63
|
-
startSpan: typeof startSpan;
|
|
64
|
-
trace: typeof trace;
|
|
65
|
-
warning: typeof warning;
|
|
66
|
-
withSettings: typeof withSettings;
|
|
67
|
-
withTags: typeof withTags;
|
|
68
|
-
};
|
|
69
|
-
//#endregion
|
|
70
|
-
export { InProcessConfigOptions, TailConfigOptions, TraceItem, defaultExport as default, exportTailEventsToLogfire, getTailConfig, instrument, instrumentInProcess, instrumentTail };
|