@contractspec/lib.observability 3.7.17 → 3.7.18

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/anomaly/alert-manager.js +1 -23
  3. package/dist/anomaly/anomaly-detector.js +1 -101
  4. package/dist/anomaly/baseline-calculator.js +1 -39
  5. package/dist/anomaly/root-cause-analyzer.js +1 -31
  6. package/dist/index.js +5 -1128
  7. package/dist/intent/aggregator.js +1 -109
  8. package/dist/intent/detector.js +1 -132
  9. package/dist/logging/index.js +1 -41
  10. package/dist/metrics/index.js +1 -30
  11. package/dist/node/anomaly/alert-manager.js +1 -23
  12. package/dist/node/anomaly/anomaly-detector.js +1 -101
  13. package/dist/node/anomaly/baseline-calculator.js +1 -39
  14. package/dist/node/anomaly/root-cause-analyzer.js +1 -31
  15. package/dist/node/index.js +5 -1128
  16. package/dist/node/intent/aggregator.js +1 -109
  17. package/dist/node/intent/detector.js +1 -132
  18. package/dist/node/logging/index.js +1 -41
  19. package/dist/node/metrics/index.js +1 -30
  20. package/dist/node/pipeline/evolution-pipeline.js +1 -299
  21. package/dist/node/pipeline/lifecycle-pipeline.js +1 -85
  22. package/dist/node/telemetry/model-selection-telemetry.js +1 -30
  23. package/dist/node/telemetry/posthog-baseline-reader.js +5 -308
  24. package/dist/node/telemetry/posthog-telemetry.js +1 -60
  25. package/dist/node/tracing/core.js +1 -52
  26. package/dist/node/tracing/index.js +1 -75
  27. package/dist/node/tracing/middleware.js +1 -171
  28. package/dist/node/tracing/model-selection.span.js +1 -72
  29. package/dist/pipeline/evolution-pipeline.js +1 -299
  30. package/dist/pipeline/lifecycle-pipeline.js +1 -85
  31. package/dist/telemetry/model-selection-telemetry.js +1 -30
  32. package/dist/telemetry/posthog-baseline-reader.js +5 -308
  33. package/dist/telemetry/posthog-telemetry.js +1 -60
  34. package/dist/tracing/core.js +1 -52
  35. package/dist/tracing/index.js +1 -75
  36. package/dist/tracing/middleware.js +1 -171
  37. package/dist/tracing/model-selection.span.js +1 -72
  38. package/package.json +7 -7
@@ -1,172 +1,2 @@
1
1
  // @bun
2
- // src/metrics/index.ts
3
- import {
4
- metrics
5
- } from "@opentelemetry/api";
6
- var DEFAULT_METER_NAME = "@contractspec/lib.observability";
7
- function getMeter(name = DEFAULT_METER_NAME) {
8
- return metrics.getMeter(name);
9
- }
10
- function createCounter(name, description, meterName) {
11
- return getMeter(meterName).createCounter(name, { description });
12
- }
13
- function createUpDownCounter(name, description, meterName) {
14
- return getMeter(meterName).createUpDownCounter(name, { description });
15
- }
16
- function createHistogram(name, description, meterName) {
17
- return getMeter(meterName).createHistogram(name, { description });
18
- }
19
- var standardMetrics = {
20
- httpRequests: createCounter("http_requests_total", "Total HTTP requests"),
21
- httpDuration: createHistogram("http_request_duration_seconds", "HTTP request duration"),
22
- operationErrors: createCounter("operation_errors_total", "Total operation errors"),
23
- workflowDuration: createHistogram("workflow_duration_seconds", "Workflow execution duration")
24
- };
25
-
26
- // src/tracing/core.ts
27
- import {
28
- SpanStatusCode,
29
- trace
30
- } from "@opentelemetry/api";
31
- var DEFAULT_TRACER_NAME = "@contractspec/lib.observability";
32
- function getTracer(name = DEFAULT_TRACER_NAME) {
33
- return trace.getTracer(name);
34
- }
35
- async function traceAsync(name, fn, tracerName) {
36
- const tracer = getTracer(tracerName);
37
- return tracer.startActiveSpan(name, async (span) => {
38
- try {
39
- const result = await fn(span);
40
- span.setStatus({ code: SpanStatusCode.OK });
41
- return result;
42
- } catch (error) {
43
- span.recordException(error);
44
- span.setStatus({
45
- code: SpanStatusCode.ERROR,
46
- message: error instanceof Error ? error.message : String(error)
47
- });
48
- throw error;
49
- } finally {
50
- span.end();
51
- }
52
- });
53
- }
54
- function traceSync(name, fn, tracerName) {
55
- const tracer = getTracer(tracerName);
56
- return tracer.startActiveSpan(name, (span) => {
57
- try {
58
- const result = fn(span);
59
- span.setStatus({ code: SpanStatusCode.OK });
60
- return result;
61
- } catch (error) {
62
- span.recordException(error);
63
- span.setStatus({
64
- code: SpanStatusCode.ERROR,
65
- message: error instanceof Error ? error.message : String(error)
66
- });
67
- throw error;
68
- } finally {
69
- span.end();
70
- }
71
- });
72
- }
73
-
74
- // src/tracing/model-selection.span.ts
75
- async function traceModelSelection(fn, input) {
76
- const startMs = performance.now();
77
- return traceAsync("model.selection", async (span) => {
78
- const result = await fn();
79
- const durationMs = performance.now() - startMs;
80
- span.setAttribute("model.selected", input.modelId);
81
- span.setAttribute("model.provider", input.providerKey);
82
- span.setAttribute("model.score", input.score);
83
- span.setAttribute("model.alternatives_count", input.alternativesCount);
84
- span.setAttribute("model.selection_duration_ms", durationMs);
85
- span.setAttribute("model.reason", input.reason);
86
- if (input.dimension) {
87
- span.setAttribute("model.dimension", input.dimension);
88
- }
89
- if (input.constraints) {
90
- span.setAttribute("model.constraints", JSON.stringify(input.constraints));
91
- }
92
- return result;
93
- });
94
- }
95
- // src/tracing/middleware.ts
96
- function createTracingMiddleware(options = {}) {
97
- return async (req, next) => {
98
- const method = req.method;
99
- const url = new URL(req.url);
100
- const path = url.pathname;
101
- standardMetrics.httpRequests.add(1, { method, path });
102
- const startTime = performance.now();
103
- return traceAsync(`HTTP ${method} ${path}`, async (span) => {
104
- span.setAttribute("http.method", method);
105
- span.setAttribute("http.url", req.url);
106
- try {
107
- const response = await next();
108
- span.setAttribute("http.status_code", response.status);
109
- const duration = (performance.now() - startTime) / 1000;
110
- standardMetrics.httpDuration.record(duration, {
111
- method,
112
- path,
113
- status: response.status.toString()
114
- });
115
- emitTelemetrySample({
116
- req,
117
- res: response,
118
- span,
119
- success: true,
120
- durationMs: duration * 1000,
121
- options
122
- });
123
- return response;
124
- } catch (error) {
125
- standardMetrics.operationErrors.add(1, { method, path });
126
- emitTelemetrySample({
127
- req,
128
- span,
129
- success: false,
130
- durationMs: performance.now() - startTime,
131
- error,
132
- options
133
- });
134
- throw error;
135
- }
136
- });
137
- };
138
- }
139
- function emitTelemetrySample({
140
- req,
141
- res,
142
- span,
143
- success,
144
- durationMs,
145
- error,
146
- options
147
- }) {
148
- if (!options.onSample || !options.resolveOperation)
149
- return;
150
- const operation = options.resolveOperation({ req, res });
151
- if (!operation)
152
- return;
153
- const sample = {
154
- operation,
155
- durationMs,
156
- success,
157
- timestamp: new Date,
158
- errorCode: !success && error instanceof Error ? error.name : success ? undefined : "unknown",
159
- tenantId: options.tenantResolver?.(req),
160
- actorId: options.actorResolver?.(req),
161
- traceId: span.spanContext().traceId,
162
- metadata: {
163
- method: req.method,
164
- path: new URL(req.url).pathname,
165
- status: res?.status
166
- }
167
- };
168
- options.onSample(sample);
169
- }
170
- export {
171
- createTracingMiddleware
172
- };
2
+ import{metrics as y}from"@opentelemetry/api";var R="@contractspec/lib.observability";function g(r=R){return y.getMeter(r)}function p(r,t,a){return g(a).createCounter(r,{description:t})}function A(r,t,a){return g(a).createUpDownCounter(r,{description:t})}function T(r,t,a){return g(a).createHistogram(r,{description:t})}var m={httpRequests:p("http_requests_total","Total HTTP requests"),httpDuration:T("http_request_duration_seconds","HTTP request duration"),operationErrors:p("operation_errors_total","Total operation errors"),workflowDuration:T("workflow_duration_seconds","Workflow execution duration")};import{SpanStatusCode as d,trace as S}from"@opentelemetry/api";var h="@contractspec/lib.observability";function l(r=h){return S.getTracer(r)}async function u(r,t,a){return l(a).startActiveSpan(r,async(n)=>{try{let e=await t(n);return n.setStatus({code:d.OK}),e}catch(e){throw n.recordException(e),n.setStatus({code:d.ERROR,message:e instanceof Error?e.message:String(e)}),e}finally{n.end()}})}function E(r,t,a){return l(a).startActiveSpan(r,(n)=>{try{let e=t(n);return n.setStatus({code:d.OK}),e}catch(e){throw n.recordException(e),n.setStatus({code:d.ERROR,message:e instanceof Error?e.message:String(e)}),e}finally{n.end()}})}async function D(r,t){let a=performance.now();return u("model.selection",async(o)=>{let n=await r(),e=performance.now()-a;if(o.setAttribute("model.selected",t.modelId),o.setAttribute("model.provider",t.providerKey),o.setAttribute("model.score",t.score),o.setAttribute("model.alternatives_count",t.alternativesCount),o.setAttribute("model.selection_duration_ms",e),o.setAttribute("model.reason",t.reason),t.dimension)o.setAttribute("model.dimension",t.dimension);if(t.constraints)o.setAttribute("model.constraints",JSON.stringify(t.constraints));return n})}function k(r={}){return async(t,a)=>{let o=t.method,e=new URL(t.url).pathname;m.httpRequests.add(1,{method:o,path:e});let s=performance.now();return u(`HTTP ${o} ${e}`,async(i)=>{i.setAttribute("http.method",o),i.setAttribute("http.url",t.url);try{let c=await a();i.setAttribute("http.status_code",c.status);let f=(performance.now()-s)/1000;return m.httpDuration.record(f,{method:o,path:e,status:c.status.toString()}),w({req:t,res:c,span:i,success:!0,durationMs:f*1000,options:r}),c}catch(c){throw m.operationErrors.add(1,{method:o,path:e}),w({req:t,span:i,success:!1,durationMs:performance.now()-s,error:c,options:r}),c}})}}function w({req:r,res:t,span:a,success:o,durationMs:n,error:e,options:s}){if(!s.onSample||!s.resolveOperation)return;let i=s.resolveOperation({req:r,res:t});if(!i)return;let c={operation:i,durationMs:n,success:o,timestamp:new Date,errorCode:!o&&e instanceof Error?e.name:o?void 0:"unknown",tenantId:s.tenantResolver?.(r),actorId:s.actorResolver?.(r),traceId:a.spanContext().traceId,metadata:{method:r.method,path:new URL(r.url).pathname,status:t?.status}};s.onSample(c)}export{k as createTracingMiddleware};
@@ -1,73 +1,2 @@
1
1
  // @bun
2
- // src/tracing/core.ts
3
- import {
4
- SpanStatusCode,
5
- trace
6
- } from "@opentelemetry/api";
7
- var DEFAULT_TRACER_NAME = "@contractspec/lib.observability";
8
- function getTracer(name = DEFAULT_TRACER_NAME) {
9
- return trace.getTracer(name);
10
- }
11
- async function traceAsync(name, fn, tracerName) {
12
- const tracer = getTracer(tracerName);
13
- return tracer.startActiveSpan(name, async (span) => {
14
- try {
15
- const result = await fn(span);
16
- span.setStatus({ code: SpanStatusCode.OK });
17
- return result;
18
- } catch (error) {
19
- span.recordException(error);
20
- span.setStatus({
21
- code: SpanStatusCode.ERROR,
22
- message: error instanceof Error ? error.message : String(error)
23
- });
24
- throw error;
25
- } finally {
26
- span.end();
27
- }
28
- });
29
- }
30
- function traceSync(name, fn, tracerName) {
31
- const tracer = getTracer(tracerName);
32
- return tracer.startActiveSpan(name, (span) => {
33
- try {
34
- const result = fn(span);
35
- span.setStatus({ code: SpanStatusCode.OK });
36
- return result;
37
- } catch (error) {
38
- span.recordException(error);
39
- span.setStatus({
40
- code: SpanStatusCode.ERROR,
41
- message: error instanceof Error ? error.message : String(error)
42
- });
43
- throw error;
44
- } finally {
45
- span.end();
46
- }
47
- });
48
- }
49
-
50
- // src/tracing/model-selection.span.ts
51
- async function traceModelSelection(fn, input) {
52
- const startMs = performance.now();
53
- return traceAsync("model.selection", async (span) => {
54
- const result = await fn();
55
- const durationMs = performance.now() - startMs;
56
- span.setAttribute("model.selected", input.modelId);
57
- span.setAttribute("model.provider", input.providerKey);
58
- span.setAttribute("model.score", input.score);
59
- span.setAttribute("model.alternatives_count", input.alternativesCount);
60
- span.setAttribute("model.selection_duration_ms", durationMs);
61
- span.setAttribute("model.reason", input.reason);
62
- if (input.dimension) {
63
- span.setAttribute("model.dimension", input.dimension);
64
- }
65
- if (input.constraints) {
66
- span.setAttribute("model.constraints", JSON.stringify(input.constraints));
67
- }
68
- return result;
69
- });
70
- }
71
- export {
72
- traceModelSelection
73
- };
2
+ import{SpanStatusCode as g,trace as R}from"@opentelemetry/api";var m="@contractspec/lib.observability";function E(o=m){return R.getTracer(o)}async function T(o,e,S){return E(S).startActiveSpan(o,async(t)=>{try{let r=await e(t);return t.setStatus({code:g.OK}),r}catch(r){throw t.recordException(r),t.setStatus({code:g.ERROR,message:r instanceof Error?r.message:String(r)}),r}finally{t.end()}})}function f(o,e,S){return E(S).startActiveSpan(o,(t)=>{try{let r=e(t);return t.setStatus({code:g.OK}),r}catch(r){throw t.recordException(r),t.setStatus({code:g.ERROR,message:r instanceof Error?r.message:String(r)}),r}finally{t.end()}})}async function v(o,e){let S=performance.now();return T("model.selection",async(c)=>{let t=await o(),r=performance.now()-S;if(c.setAttribute("model.selected",e.modelId),c.setAttribute("model.provider",e.providerKey),c.setAttribute("model.score",e.score),c.setAttribute("model.alternatives_count",e.alternativesCount),c.setAttribute("model.selection_duration_ms",r),c.setAttribute("model.reason",e.reason),e.dimension)c.setAttribute("model.dimension",e.dimension);if(e.constraints)c.setAttribute("model.constraints",JSON.stringify(e.constraints));return t})}export{v as traceModelSelection};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractspec/lib.observability",
3
- "version": "3.7.17",
3
+ "version": "3.7.18",
4
4
  "description": "OpenTelemetry-based observability primitives",
5
5
  "keywords": [
6
6
  "contractspec",
@@ -20,16 +20,16 @@
20
20
  "dev": "contractspec-bun-build dev",
21
21
  "clean": "rimraf dist .turbo",
22
22
  "lint": "bun lint:fix",
23
- "lint:fix": "biome check --write --unsafe --only=nursery/useSortedClasses . && biome check --write .",
24
- "lint:check": "biome check .",
23
+ "lint:fix": "node ../../../scripts/biome.cjs check --write --unsafe --only=nursery/useSortedClasses . && node ../../../scripts/biome.cjs check --write .",
24
+ "lint:check": "node ../../../scripts/biome.cjs check .",
25
25
  "test": "bun test --pass-with-no-tests",
26
26
  "prebuild": "contractspec-bun-build prebuild",
27
27
  "typecheck": "tsc --noEmit"
28
28
  },
29
29
  "dependencies": {
30
- "@contractspec/lib.lifecycle": "3.7.17",
31
- "@contractspec/lib.contracts-spec": "5.1.0",
32
- "@contractspec/lib.contracts-integrations": "3.8.9"
30
+ "@contractspec/lib.lifecycle": "3.7.18",
31
+ "@contractspec/lib.contracts-spec": "5.2.0",
32
+ "@contractspec/lib.contracts-integrations": "3.8.10"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@opentelemetry/api": "1.9.1"
@@ -37,7 +37,7 @@
37
37
  "devDependencies": {
38
38
  "@contractspec/tool.typescript": "3.7.13",
39
39
  "typescript": "^5.9.3",
40
- "@contractspec/tool.bun": "3.7.13"
40
+ "@contractspec/tool.bun": "3.7.14"
41
41
  },
42
42
  "exports": {
43
43
  ".": {