@pydantic/logfire-cf-workers 0.3.0 → 0.4.1

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/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@microlabs/otel-cf-workers"),o=require("@pydantic/logfire-api"),a="https://logfire-api.pydantic.dev/";function c(e){const s=new o.LogfireAttributeScrubber;return t=>{let{LOGFIRE_BASE_URL:r=a,LOGFIRE_TOKEN:i}=t;return r.endsWith("/")||(r+="/"),{exporter:{headers:{Authorization:i},url:`${r}v1/traces`},postProcessor:n=>l(n,s),service:{name:e.serviceName??"cloudflare-worker",namespace:e.serviceNamespace??"",version:e.serviceVersion??"0.0.0"}}}}function b(e,s){return u.instrument(e,c(s))}function l(e,s){for(const t of e){for(const r of Object.keys(t.attributes)){const i=t.attributes[r];i==null&&delete t.attributes[r]}Object.assign(t.attributes,o.serializeAttributes(t.attributes,s))}return e}exports.instrument=b;
1
+ "use strict";var g=Object.defineProperty;var S=(t,e,r)=>e in t?g(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var u=(t,e,r)=>S(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@pydantic/logfire-api"),l=require("@pydantic/otel-cf-workers"),y=require("@opentelemetry/core"),T=require("@opentelemetry/otlp-transformer"),b=require("@opentelemetry/sdk-trace-base");class m{export(e,r){this._sendSpans(e,r)}shutdown(){return this._sendSpans([]),Promise.resolve()}_cleanNullValues(e){if(!e.resourceSpans)return e;for(const r of e.resourceSpans){a(r.resource);for(const n of r.scopeSpans){n.scope&&a(n.scope);for(const o of n.spans??[])a(o)}}return e}_sendSpans(e,r){const n=T.JsonTraceSerializer.serializeRequest(e),o=new TextDecoder().decode(n),s=JSON.parse(o),i=this._cleanNullValues(s);return console.log(i),r==null?void 0:r({code:y.ExportResultCode.SUCCESS})}}function a(t){t!=null&&t.attributes&&(t.attributes=t.attributes.filter(v))}function v(t){return Object.keys(t.value).length>0}class f extends b.RandomIdGenerator{constructor(){super(...arguments);u(this,"generateTraceId",()=>O().toString(16).padStart(32,"0"))}}function O(){let t=BigInt(Date.now());const e=crypto.getRandomValues(new Uint8Array(10));for(const r of e)t<<=BigInt(8),t|=BigInt(r);return t}async function h(t,e){const r=e.LOGFIRE_TOKEN;if(!r){console.warn("No token provided, not sending payload to Logfire");return}const n=c.resolveBaseUrl(e,void 0,r);for(const o of t)for(const s of o.logs)if(Array.isArray(s.message)){for(const i of s.message)if("resourceSpans"in i)try{return await fetch(`${n}/v1/traces`,{body:JSON.stringify(i),headers:{Authorization:r,"Content-Type":"application/json"},method:"POST"})}catch(d){console.error(d)}}}function E(t){return e=>{const{LOGFIRE_TOKEN:r=""}=e,n=c.resolveBaseUrl(e,t.baseUrl,r);return Object.assign({},t,{exporter:{headers:{Authorization:r},url:`${n}/v1/traces`},idGenerator:new f,postProcessor:o=>A(o)})}}function p(t){return e=>Object.assign({},t,{exporter:new m,idGenerator:new f})}function I(t,e){return l.instrument(t,E(e))}function x(t,e){return l.instrument(t,p(e))}function A(t){for(const e of t){for(const r of Object.keys(e.attributes)){const n=e.attributes[r];n==null&&delete e.attributes[r]}Object.assign(e.attributes,c.serializeAttributes(e.attributes))}return t}exports.exportTailEventsToLogfire=h;exports.getTailConfig=p;exports.instrumentInProcess=I;exports.instrumentTail=x;
package/dist/index.d.cts CHANGED
@@ -1,14 +1,33 @@
1
+ import { TraceConfig } from '@pydantic/otel-cf-workers';
2
+
1
3
  export declare interface CloudflareConfigOptions {
2
4
  baseUrl?: string;
3
5
  token: string;
4
6
  }
5
7
 
6
- export declare function instrument<T>(handler: T, config: LogfireCloudflareConfigOptions): T;
8
+ declare type ConfigOptionsBase = Pick<TraceConfig, 'fetch' | 'handlers' | 'instrumentation' | 'propagator' | 'sampling' | 'scope' | 'service'>;
9
+
10
+ declare type Env = Record<string, string | undefined>;
11
+
12
+ export declare function exportTailEventsToLogfire(events: TraceItem[], env: Record<string, string | undefined>): Promise<Response | undefined>;
13
+
14
+ export declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
15
+
16
+ export declare interface InProcessConfigOptions extends ConfigOptionsBase {
17
+ baseUrl?: string;
18
+ }
19
+
20
+ export declare function instrumentInProcess<T>(handler: T, config: InProcessConfigOptions): T;
21
+
22
+ export declare function instrumentTail<T>(handler: T, config: TailConfigOptions): T;
23
+
24
+ export declare interface TailConfigOptions extends ConfigOptionsBase {
25
+ }
7
26
 
8
- export declare interface LogfireCloudflareConfigOptions {
9
- serviceName?: string;
10
- serviceNamespace?: string;
11
- serviceVersion?: string;
27
+ declare interface TraceItem {
28
+ logs: {
29
+ message: any[];
30
+ }[];
12
31
  }
13
32
 
14
33
  export { }
package/dist/index.d.ts CHANGED
@@ -1,14 +1,33 @@
1
+ import { TraceConfig } from '@pydantic/otel-cf-workers';
2
+
1
3
  export declare interface CloudflareConfigOptions {
2
4
  baseUrl?: string;
3
5
  token: string;
4
6
  }
5
7
 
6
- export declare function instrument<T>(handler: T, config: LogfireCloudflareConfigOptions): T;
8
+ declare type ConfigOptionsBase = Pick<TraceConfig, 'fetch' | 'handlers' | 'instrumentation' | 'propagator' | 'sampling' | 'scope' | 'service'>;
9
+
10
+ declare type Env = Record<string, string | undefined>;
11
+
12
+ export declare function exportTailEventsToLogfire(events: TraceItem[], env: Record<string, string | undefined>): Promise<Response | undefined>;
13
+
14
+ export declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
15
+
16
+ export declare interface InProcessConfigOptions extends ConfigOptionsBase {
17
+ baseUrl?: string;
18
+ }
19
+
20
+ export declare function instrumentInProcess<T>(handler: T, config: InProcessConfigOptions): T;
21
+
22
+ export declare function instrumentTail<T>(handler: T, config: TailConfigOptions): T;
23
+
24
+ export declare interface TailConfigOptions extends ConfigOptionsBase {
25
+ }
7
26
 
8
- export declare interface LogfireCloudflareConfigOptions {
9
- serviceName?: string;
10
- serviceNamespace?: string;
11
- serviceVersion?: string;
27
+ declare interface TraceItem {
28
+ logs: {
29
+ message: any[];
30
+ }[];
12
31
  }
13
32
 
14
33
  export { }
package/dist/index.js CHANGED
@@ -1,37 +1,119 @@
1
- import { instrument as n } from "@microlabs/otel-cf-workers";
2
- import { LogfireAttributeScrubber as a, serializeAttributes as u } from "@pydantic/logfire-api";
3
- const c = "https://logfire-api.pydantic.dev/";
4
- function b(t) {
5
- const s = new a();
6
- return (e) => {
7
- let { LOGFIRE_BASE_URL: r = c, LOGFIRE_TOKEN: i } = e;
8
- return r.endsWith("/") || (r += "/"), {
1
+ var d = Object.defineProperty;
2
+ var g = (e, t, r) => t in e ? d(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var c = (e, t, r) => g(e, typeof t != "symbol" ? t + "" : t, r);
4
+ import { resolveBaseUrl as u, serializeAttributes as m } from "@pydantic/logfire-api";
5
+ import { instrument as f } from "@pydantic/otel-cf-workers";
6
+ import { ExportResultCode as S } from "@opentelemetry/core";
7
+ import { JsonTraceSerializer as y } from "@opentelemetry/otlp-transformer";
8
+ import { RandomIdGenerator as b } from "@opentelemetry/sdk-trace-base";
9
+ class h {
10
+ export(t, r) {
11
+ this._sendSpans(t, r);
12
+ }
13
+ shutdown() {
14
+ return this._sendSpans([]), Promise.resolve();
15
+ }
16
+ _cleanNullValues(t) {
17
+ if (!t.resourceSpans)
18
+ return t;
19
+ for (const r of t.resourceSpans) {
20
+ a(r.resource);
21
+ for (const n of r.scopeSpans) {
22
+ n.scope && a(n.scope);
23
+ for (const o of n.spans ?? [])
24
+ a(o);
25
+ }
26
+ }
27
+ return t;
28
+ }
29
+ _sendSpans(t, r) {
30
+ const n = y.serializeRequest(t), o = new TextDecoder().decode(n), s = JSON.parse(o), i = this._cleanNullValues(s);
31
+ return console.log(i), r == null ? void 0 : r({ code: S.SUCCESS });
32
+ }
33
+ }
34
+ function a(e) {
35
+ e != null && e.attributes && (e.attributes = e.attributes.filter(O));
36
+ }
37
+ function O(e) {
38
+ return Object.keys(e.value).length > 0;
39
+ }
40
+ class l extends b {
41
+ constructor() {
42
+ super(...arguments);
43
+ c(this, "generateTraceId", () => T().toString(16).padStart(32, "0"));
44
+ }
45
+ }
46
+ function T() {
47
+ let e = BigInt(Date.now());
48
+ const t = crypto.getRandomValues(new Uint8Array(10));
49
+ for (const r of t)
50
+ e <<= BigInt(8), e |= BigInt(r);
51
+ return e;
52
+ }
53
+ async function C(e, t) {
54
+ const r = t.LOGFIRE_TOKEN;
55
+ if (!r) {
56
+ console.warn("No token provided, not sending payload to Logfire");
57
+ return;
58
+ }
59
+ const n = u(t, void 0, r);
60
+ for (const o of e)
61
+ for (const s of o.logs)
62
+ if (Array.isArray(s.message)) {
63
+ for (const i of s.message)
64
+ if ("resourceSpans" in i)
65
+ try {
66
+ return await fetch(`${n}/v1/traces`, {
67
+ body: JSON.stringify(i),
68
+ headers: {
69
+ Authorization: r,
70
+ "Content-Type": "application/json"
71
+ },
72
+ method: "POST"
73
+ });
74
+ } catch (p) {
75
+ console.error(p);
76
+ }
77
+ }
78
+ }
79
+ function v(e) {
80
+ return (t) => {
81
+ const { LOGFIRE_TOKEN: r = "" } = t, n = u(t, e.baseUrl, r);
82
+ return Object.assign({}, e, {
9
83
  exporter: {
10
- headers: { Authorization: i },
11
- url: `${r}v1/traces`
84
+ headers: { Authorization: r },
85
+ url: `${n}/v1/traces`
12
86
  },
13
- postProcessor: (o) => f(o, s),
14
- service: {
15
- name: t.serviceName ?? "cloudflare-worker",
16
- namespace: t.serviceNamespace ?? "",
17
- version: t.serviceVersion ?? "0.0.0"
18
- }
19
- };
87
+ idGenerator: new l(),
88
+ postProcessor: (o) => E(o)
89
+ });
20
90
  };
21
91
  }
22
- function p(t, s) {
23
- return n(t, b(s));
92
+ function x(e) {
93
+ return (t) => Object.assign({}, e, {
94
+ exporter: new h(),
95
+ idGenerator: new l()
96
+ });
97
+ }
98
+ function G(e, t) {
99
+ return f(e, v(t));
100
+ }
101
+ function P(e, t) {
102
+ return f(e, x(t));
24
103
  }
25
- function f(t, s) {
26
- for (const e of t) {
27
- for (const r of Object.keys(e.attributes)) {
28
- const i = e.attributes[r];
29
- i == null && delete e.attributes[r];
104
+ function E(e) {
105
+ for (const t of e) {
106
+ for (const r of Object.keys(t.attributes)) {
107
+ const n = t.attributes[r];
108
+ n == null && delete t.attributes[r];
30
109
  }
31
- Object.assign(e.attributes, u(e.attributes, s));
110
+ Object.assign(t.attributes, m(t.attributes));
32
111
  }
33
- return t;
112
+ return e;
34
113
  }
35
114
  export {
36
- p as instrument
115
+ C as exportTailEventsToLogfire,
116
+ x as getTailConfig,
117
+ G as instrumentInProcess,
118
+ P as instrumentTail
37
119
  };
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "stats",
23
23
  "monitoring"
24
24
  ],
25
- "version": "0.3.0",
25
+ "version": "0.4.1",
26
26
  "type": "module",
27
27
  "main": "./dist/index.cjs",
28
28
  "module": "./dist/index.js",
@@ -50,11 +50,11 @@
50
50
  },
51
51
  "dependencies": {
52
52
  "@pydantic/logfire-api": "*",
53
- "@microlabs/otel-cf-workers": "^1.0.0-rc.49"
53
+ "@pydantic/otel-cf-workers": "^1.0.0-rc.50"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@cloudflare/workers-types": "4.20250311.0",
57
- "@opentelemetry/sdk-trace-base": "^1.30.1",
57
+ "@opentelemetry/sdk-trace-base": "^2.0.0",
58
58
  "@pydantic/logfire-tooling-config": "*",
59
59
  "eslint": "^9.22.0",
60
60
  "prettier": "3.5.3",