@pydantic/logfire-cf-workers 0.4.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 +1 -1
- package/dist/index.d.cts +23 -5
- package/dist/index.d.ts +23 -5
- package/dist/index.js +104 -20
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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,15 +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
|
-
|
|
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>;
|
|
7
13
|
|
|
8
|
-
export declare
|
|
14
|
+
export declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
|
|
15
|
+
|
|
16
|
+
export declare interface InProcessConfigOptions extends ConfigOptionsBase {
|
|
9
17
|
baseUrl?: string;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
+
}
|
|
26
|
+
|
|
27
|
+
declare interface TraceItem {
|
|
28
|
+
logs: {
|
|
29
|
+
message: any[];
|
|
30
|
+
}[];
|
|
13
31
|
}
|
|
14
32
|
|
|
15
33
|
export { }
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +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
|
-
|
|
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>;
|
|
7
13
|
|
|
8
|
-
export declare
|
|
14
|
+
export declare function getTailConfig(config: TailConfigOptions): (env: Env) => TraceConfig;
|
|
15
|
+
|
|
16
|
+
export declare interface InProcessConfigOptions extends ConfigOptionsBase {
|
|
9
17
|
baseUrl?: string;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
+
}
|
|
26
|
+
|
|
27
|
+
declare interface TraceItem {
|
|
28
|
+
logs: {
|
|
29
|
+
message: any[];
|
|
30
|
+
}[];
|
|
13
31
|
}
|
|
14
32
|
|
|
15
33
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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) {
|
|
4
80
|
return (t) => {
|
|
5
|
-
const { LOGFIRE_TOKEN: r = "" } = t,
|
|
6
|
-
return {
|
|
81
|
+
const { LOGFIRE_TOKEN: r = "" } = t, n = u(t, e.baseUrl, r);
|
|
82
|
+
return Object.assign({}, e, {
|
|
7
83
|
exporter: {
|
|
8
84
|
headers: { Authorization: r },
|
|
9
|
-
url: `${
|
|
85
|
+
url: `${n}/v1/traces`
|
|
10
86
|
},
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
namespace: e.serviceNamespace ?? "",
|
|
15
|
-
version: e.serviceVersion ?? "0.0.0"
|
|
16
|
-
}
|
|
17
|
-
};
|
|
87
|
+
idGenerator: new l(),
|
|
88
|
+
postProcessor: (o) => E(o)
|
|
89
|
+
});
|
|
18
90
|
};
|
|
19
91
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
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));
|
|
22
103
|
}
|
|
23
|
-
function
|
|
104
|
+
function E(e) {
|
|
24
105
|
for (const t of e) {
|
|
25
106
|
for (const r of Object.keys(t.attributes)) {
|
|
26
|
-
const
|
|
27
|
-
|
|
107
|
+
const n = t.attributes[r];
|
|
108
|
+
n == null && delete t.attributes[r];
|
|
28
109
|
}
|
|
29
|
-
Object.assign(t.attributes,
|
|
110
|
+
Object.assign(t.attributes, m(t.attributes));
|
|
30
111
|
}
|
|
31
112
|
return e;
|
|
32
113
|
}
|
|
33
114
|
export {
|
|
34
|
-
|
|
115
|
+
C as exportTailEventsToLogfire,
|
|
116
|
+
x as getTailConfig,
|
|
117
|
+
G as instrumentInProcess,
|
|
118
|
+
P as instrumentTail
|
|
35
119
|
};
|
package/package.json
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"stats",
|
|
23
23
|
"monitoring"
|
|
24
24
|
],
|
|
25
|
-
"version": "0.4.
|
|
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
|
-
"@
|
|
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": "^
|
|
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",
|