@reactionary/core 0.0.28 → 0.0.30
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/index.js +255 -8
- package/package.json +3 -2
- package/src/providers/base.provider.d.ts +1 -0
package/index.js
CHANGED
|
@@ -54,12 +54,244 @@ function buildClient(providers) {
|
|
|
54
54
|
return client;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// otel/src/trpc-middleware.ts
|
|
58
|
+
import { TRPCError } from "@trpc/server";
|
|
59
|
+
import {
|
|
60
|
+
SpanKind as SpanKind2,
|
|
61
|
+
SpanStatusCode as SpanStatusCode3
|
|
62
|
+
} from "@opentelemetry/api";
|
|
63
|
+
|
|
64
|
+
// otel/src/tracer.ts
|
|
65
|
+
import {
|
|
66
|
+
trace,
|
|
67
|
+
SpanStatusCode,
|
|
68
|
+
context as otelContext
|
|
69
|
+
} from "@opentelemetry/api";
|
|
70
|
+
|
|
71
|
+
// otel/src/sdk.ts
|
|
72
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
73
|
+
var sdk = null;
|
|
74
|
+
var isInitialized = false;
|
|
75
|
+
var initializationPromise = null;
|
|
76
|
+
function isBrowser() {
|
|
77
|
+
return typeof window !== "undefined" && typeof process === "undefined";
|
|
78
|
+
}
|
|
79
|
+
function ensureInitialized() {
|
|
80
|
+
if (isInitialized || initializationPromise) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (isBrowser()) {
|
|
84
|
+
isInitialized = true;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
initializationPromise = Promise.resolve().then(() => {
|
|
88
|
+
sdk = new NodeSDK();
|
|
89
|
+
sdk.start();
|
|
90
|
+
isInitialized = true;
|
|
91
|
+
process.on("SIGTERM", async () => {
|
|
92
|
+
try {
|
|
93
|
+
await shutdownOtel();
|
|
94
|
+
if (process.env["OTEL_LOG_LEVEL"] === "debug") {
|
|
95
|
+
console.log("OpenTelemetry terminated successfully");
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error("Error terminating OpenTelemetry", error);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async function shutdownOtel() {
|
|
104
|
+
if (sdk) {
|
|
105
|
+
await sdk.shutdown();
|
|
106
|
+
sdk = null;
|
|
107
|
+
isInitialized = false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function isOtelInitialized() {
|
|
111
|
+
ensureInitialized();
|
|
112
|
+
return isInitialized;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// otel/src/tracer.ts
|
|
116
|
+
import { SpanKind, SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
|
|
117
|
+
var TRACER_NAME = "@reactionary/otel";
|
|
118
|
+
var TRACER_VERSION = "0.0.1";
|
|
119
|
+
var globalTracer = null;
|
|
120
|
+
function getTracer() {
|
|
121
|
+
if (!globalTracer) {
|
|
122
|
+
isOtelInitialized();
|
|
123
|
+
globalTracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
|
|
124
|
+
}
|
|
125
|
+
return globalTracer;
|
|
126
|
+
}
|
|
127
|
+
function withSpan(name, fn, options) {
|
|
128
|
+
const tracer = getTracer();
|
|
129
|
+
return tracer.startActiveSpan(name, options || {}, async (span) => {
|
|
130
|
+
try {
|
|
131
|
+
const result = await fn(span);
|
|
132
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
133
|
+
return result;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
span.setStatus({
|
|
136
|
+
code: SpanStatusCode.ERROR,
|
|
137
|
+
message: error instanceof Error ? error.message : String(error)
|
|
138
|
+
});
|
|
139
|
+
if (error instanceof Error) {
|
|
140
|
+
span.recordException(error);
|
|
141
|
+
}
|
|
142
|
+
throw error;
|
|
143
|
+
} finally {
|
|
144
|
+
span.end();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function setSpanAttributes(span, attributes) {
|
|
149
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
150
|
+
if (value !== void 0 && value !== null) {
|
|
151
|
+
span.setAttribute(key, value);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// otel/src/metrics.ts
|
|
157
|
+
import { metrics } from "@opentelemetry/api";
|
|
158
|
+
var METER_NAME = "@reactionary/otel";
|
|
159
|
+
var METER_VERSION = "0.0.1";
|
|
160
|
+
var globalMeter = null;
|
|
161
|
+
function getMeter() {
|
|
162
|
+
if (!globalMeter) {
|
|
163
|
+
isOtelInitialized();
|
|
164
|
+
globalMeter = metrics.getMeter(METER_NAME, METER_VERSION);
|
|
165
|
+
}
|
|
166
|
+
return globalMeter;
|
|
167
|
+
}
|
|
168
|
+
var metricsInstance = null;
|
|
169
|
+
function initializeMetrics() {
|
|
170
|
+
if (metricsInstance) {
|
|
171
|
+
return metricsInstance;
|
|
172
|
+
}
|
|
173
|
+
const meter = getMeter();
|
|
174
|
+
metricsInstance = {
|
|
175
|
+
requestCounter: meter.createCounter("reactionary.requests", {
|
|
176
|
+
description: "Total number of requests"
|
|
177
|
+
}),
|
|
178
|
+
requestDuration: meter.createHistogram("reactionary.request.duration", {
|
|
179
|
+
description: "Request duration in milliseconds",
|
|
180
|
+
unit: "ms"
|
|
181
|
+
}),
|
|
182
|
+
activeRequests: meter.createUpDownCounter("reactionary.requests.active", {
|
|
183
|
+
description: "Number of active requests"
|
|
184
|
+
}),
|
|
185
|
+
errorCounter: meter.createCounter("reactionary.errors", {
|
|
186
|
+
description: "Total number of errors"
|
|
187
|
+
}),
|
|
188
|
+
providerCallCounter: meter.createCounter("reactionary.provider.calls", {
|
|
189
|
+
description: "Total number of provider calls"
|
|
190
|
+
}),
|
|
191
|
+
providerCallDuration: meter.createHistogram("reactionary.provider.duration", {
|
|
192
|
+
description: "Provider call duration in milliseconds",
|
|
193
|
+
unit: "ms"
|
|
194
|
+
}),
|
|
195
|
+
cacheHitCounter: meter.createCounter("reactionary.cache.hits", {
|
|
196
|
+
description: "Total number of cache hits"
|
|
197
|
+
}),
|
|
198
|
+
cacheMissCounter: meter.createCounter("reactionary.cache.misses", {
|
|
199
|
+
description: "Total number of cache misses"
|
|
200
|
+
})
|
|
201
|
+
};
|
|
202
|
+
return metricsInstance;
|
|
203
|
+
}
|
|
204
|
+
function getMetrics() {
|
|
205
|
+
if (!metricsInstance) {
|
|
206
|
+
return initializeMetrics();
|
|
207
|
+
}
|
|
208
|
+
return metricsInstance;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// otel/src/provider-instrumentation.ts
|
|
212
|
+
import { SpanKind as SpanKind3 } from "@opentelemetry/api";
|
|
213
|
+
async function withProviderSpan(options, fn) {
|
|
214
|
+
const { providerName, operationType, operationName, attributes = {} } = options;
|
|
215
|
+
const metrics2 = getMetrics();
|
|
216
|
+
const spanName = `provider.${providerName}.${operationType}${operationName ? `.${operationName}` : ""}`;
|
|
217
|
+
const startTime = Date.now();
|
|
218
|
+
metrics2.providerCallCounter.add(1, {
|
|
219
|
+
"provider.name": providerName,
|
|
220
|
+
"provider.operation.type": operationType,
|
|
221
|
+
"provider.operation.name": operationName || "unknown"
|
|
222
|
+
});
|
|
223
|
+
return withSpan(
|
|
224
|
+
spanName,
|
|
225
|
+
async (span) => {
|
|
226
|
+
setSpanAttributes(span, {
|
|
227
|
+
"provider.name": providerName,
|
|
228
|
+
"provider.operation.type": operationType,
|
|
229
|
+
"provider.operation.name": operationName,
|
|
230
|
+
...attributes
|
|
231
|
+
});
|
|
232
|
+
try {
|
|
233
|
+
const result = await fn(span);
|
|
234
|
+
const duration = Date.now() - startTime;
|
|
235
|
+
metrics2.providerCallDuration.record(duration, {
|
|
236
|
+
"provider.name": providerName,
|
|
237
|
+
"provider.operation.type": operationType,
|
|
238
|
+
"provider.operation.name": operationName || "unknown",
|
|
239
|
+
"status": "success"
|
|
240
|
+
});
|
|
241
|
+
return result;
|
|
242
|
+
} catch (error) {
|
|
243
|
+
const duration = Date.now() - startTime;
|
|
244
|
+
metrics2.providerCallDuration.record(duration, {
|
|
245
|
+
"provider.name": providerName,
|
|
246
|
+
"provider.operation.type": operationType,
|
|
247
|
+
"provider.operation.name": operationName || "unknown",
|
|
248
|
+
"status": "error"
|
|
249
|
+
});
|
|
250
|
+
metrics2.errorCounter.add(1, {
|
|
251
|
+
"provider.name": providerName,
|
|
252
|
+
"provider.operation.type": operationType,
|
|
253
|
+
"provider.operation.name": operationName || "unknown"
|
|
254
|
+
});
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{ kind: SpanKind3.CLIENT }
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
function createProviderInstrumentation(providerName) {
|
|
262
|
+
return {
|
|
263
|
+
traceQuery: (operationName, fn, attributes) => {
|
|
264
|
+
return withProviderSpan(
|
|
265
|
+
{
|
|
266
|
+
providerName,
|
|
267
|
+
operationType: "query",
|
|
268
|
+
operationName,
|
|
269
|
+
attributes
|
|
270
|
+
},
|
|
271
|
+
fn
|
|
272
|
+
);
|
|
273
|
+
},
|
|
274
|
+
traceMutation: (operationName, fn, attributes) => {
|
|
275
|
+
return withProviderSpan(
|
|
276
|
+
{
|
|
277
|
+
providerName,
|
|
278
|
+
operationType: "mutation",
|
|
279
|
+
operationName,
|
|
280
|
+
attributes
|
|
281
|
+
},
|
|
282
|
+
fn
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
57
288
|
// core/src/providers/base.provider.ts
|
|
58
289
|
var BaseProvider = class {
|
|
59
290
|
constructor(schema, querySchema, mutationSchema) {
|
|
60
291
|
this.schema = schema;
|
|
61
292
|
this.querySchema = querySchema;
|
|
62
293
|
this.mutationSchema = mutationSchema;
|
|
294
|
+
this.instrumentation = createProviderInstrumentation(this.constructor.name);
|
|
63
295
|
}
|
|
64
296
|
/**
|
|
65
297
|
* Validates that the final domain model constructed by the provider
|
|
@@ -81,20 +313,35 @@ var BaseProvider = class {
|
|
|
81
313
|
* of the results will match the order of the queries.
|
|
82
314
|
*/
|
|
83
315
|
async query(queries, session) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
316
|
+
return this.instrumentation.traceQuery(
|
|
317
|
+
"query",
|
|
318
|
+
async (span) => {
|
|
319
|
+
span.setAttribute("provider.query.count", queries.length);
|
|
320
|
+
const results = await this.fetch(queries, session);
|
|
321
|
+
for (const result of results) {
|
|
322
|
+
this.assert(result);
|
|
323
|
+
}
|
|
324
|
+
span.setAttribute("provider.result.count", results.length);
|
|
325
|
+
return results;
|
|
326
|
+
},
|
|
327
|
+
{ queryCount: queries.length }
|
|
328
|
+
);
|
|
89
329
|
}
|
|
90
330
|
/**
|
|
91
331
|
* Executes the listed mutations in order and returns the final state
|
|
92
332
|
* resulting from that set of operations.
|
|
93
333
|
*/
|
|
94
334
|
async mutate(mutations, session) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
335
|
+
return this.instrumentation.traceMutation(
|
|
336
|
+
"mutate",
|
|
337
|
+
async (span) => {
|
|
338
|
+
span.setAttribute("provider.mutation.count", mutations.length);
|
|
339
|
+
const result = await this.process(mutations, session);
|
|
340
|
+
this.assert(result);
|
|
341
|
+
return result;
|
|
342
|
+
},
|
|
343
|
+
{ mutationCount: mutations.length }
|
|
344
|
+
);
|
|
98
345
|
}
|
|
99
346
|
};
|
|
100
347
|
|
package/package.json
CHANGED
|
@@ -11,6 +11,7 @@ export declare abstract class BaseProvider<T extends BaseModel = BaseModel, Q ex
|
|
|
11
11
|
readonly schema: z.ZodType<T>;
|
|
12
12
|
readonly querySchema: z.ZodType<Q, Q>;
|
|
13
13
|
readonly mutationSchema: z.ZodType<M, M>;
|
|
14
|
+
private instrumentation;
|
|
14
15
|
constructor(schema: z.ZodType<T>, querySchema: z.ZodType<Q, Q>, mutationSchema: z.ZodType<M, M>);
|
|
15
16
|
/**
|
|
16
17
|
* Validates that the final domain model constructed by the provider
|