@reactionary/provider-commercetools 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 +2 -2
package/index.js
CHANGED
|
@@ -152,12 +152,244 @@ var RedisCache = class {
|
|
|
152
152
|
}
|
|
153
153
|
};
|
|
154
154
|
|
|
155
|
+
// otel/src/trpc-middleware.ts
|
|
156
|
+
import { TRPCError } from "@trpc/server";
|
|
157
|
+
import {
|
|
158
|
+
SpanKind as SpanKind2,
|
|
159
|
+
SpanStatusCode as SpanStatusCode3
|
|
160
|
+
} from "@opentelemetry/api";
|
|
161
|
+
|
|
162
|
+
// otel/src/tracer.ts
|
|
163
|
+
import {
|
|
164
|
+
trace,
|
|
165
|
+
SpanStatusCode,
|
|
166
|
+
context as otelContext
|
|
167
|
+
} from "@opentelemetry/api";
|
|
168
|
+
|
|
169
|
+
// otel/src/sdk.ts
|
|
170
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
171
|
+
var sdk = null;
|
|
172
|
+
var isInitialized = false;
|
|
173
|
+
var initializationPromise = null;
|
|
174
|
+
function isBrowser() {
|
|
175
|
+
return typeof window !== "undefined" && typeof process === "undefined";
|
|
176
|
+
}
|
|
177
|
+
function ensureInitialized() {
|
|
178
|
+
if (isInitialized || initializationPromise) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (isBrowser()) {
|
|
182
|
+
isInitialized = true;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
initializationPromise = Promise.resolve().then(() => {
|
|
186
|
+
sdk = new NodeSDK();
|
|
187
|
+
sdk.start();
|
|
188
|
+
isInitialized = true;
|
|
189
|
+
process.on("SIGTERM", async () => {
|
|
190
|
+
try {
|
|
191
|
+
await shutdownOtel();
|
|
192
|
+
if (process.env["OTEL_LOG_LEVEL"] === "debug") {
|
|
193
|
+
console.log("OpenTelemetry terminated successfully");
|
|
194
|
+
}
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error("Error terminating OpenTelemetry", error);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
async function shutdownOtel() {
|
|
202
|
+
if (sdk) {
|
|
203
|
+
await sdk.shutdown();
|
|
204
|
+
sdk = null;
|
|
205
|
+
isInitialized = false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function isOtelInitialized() {
|
|
209
|
+
ensureInitialized();
|
|
210
|
+
return isInitialized;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// otel/src/tracer.ts
|
|
214
|
+
import { SpanKind, SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
|
|
215
|
+
var TRACER_NAME = "@reactionary/otel";
|
|
216
|
+
var TRACER_VERSION = "0.0.1";
|
|
217
|
+
var globalTracer = null;
|
|
218
|
+
function getTracer() {
|
|
219
|
+
if (!globalTracer) {
|
|
220
|
+
isOtelInitialized();
|
|
221
|
+
globalTracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
|
|
222
|
+
}
|
|
223
|
+
return globalTracer;
|
|
224
|
+
}
|
|
225
|
+
function withSpan(name, fn, options) {
|
|
226
|
+
const tracer = getTracer();
|
|
227
|
+
return tracer.startActiveSpan(name, options || {}, async (span) => {
|
|
228
|
+
try {
|
|
229
|
+
const result = await fn(span);
|
|
230
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
231
|
+
return result;
|
|
232
|
+
} catch (error) {
|
|
233
|
+
span.setStatus({
|
|
234
|
+
code: SpanStatusCode.ERROR,
|
|
235
|
+
message: error instanceof Error ? error.message : String(error)
|
|
236
|
+
});
|
|
237
|
+
if (error instanceof Error) {
|
|
238
|
+
span.recordException(error);
|
|
239
|
+
}
|
|
240
|
+
throw error;
|
|
241
|
+
} finally {
|
|
242
|
+
span.end();
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function setSpanAttributes(span, attributes) {
|
|
247
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
248
|
+
if (value !== void 0 && value !== null) {
|
|
249
|
+
span.setAttribute(key, value);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// otel/src/metrics.ts
|
|
255
|
+
import { metrics } from "@opentelemetry/api";
|
|
256
|
+
var METER_NAME = "@reactionary/otel";
|
|
257
|
+
var METER_VERSION = "0.0.1";
|
|
258
|
+
var globalMeter = null;
|
|
259
|
+
function getMeter() {
|
|
260
|
+
if (!globalMeter) {
|
|
261
|
+
isOtelInitialized();
|
|
262
|
+
globalMeter = metrics.getMeter(METER_NAME, METER_VERSION);
|
|
263
|
+
}
|
|
264
|
+
return globalMeter;
|
|
265
|
+
}
|
|
266
|
+
var metricsInstance = null;
|
|
267
|
+
function initializeMetrics() {
|
|
268
|
+
if (metricsInstance) {
|
|
269
|
+
return metricsInstance;
|
|
270
|
+
}
|
|
271
|
+
const meter = getMeter();
|
|
272
|
+
metricsInstance = {
|
|
273
|
+
requestCounter: meter.createCounter("reactionary.requests", {
|
|
274
|
+
description: "Total number of requests"
|
|
275
|
+
}),
|
|
276
|
+
requestDuration: meter.createHistogram("reactionary.request.duration", {
|
|
277
|
+
description: "Request duration in milliseconds",
|
|
278
|
+
unit: "ms"
|
|
279
|
+
}),
|
|
280
|
+
activeRequests: meter.createUpDownCounter("reactionary.requests.active", {
|
|
281
|
+
description: "Number of active requests"
|
|
282
|
+
}),
|
|
283
|
+
errorCounter: meter.createCounter("reactionary.errors", {
|
|
284
|
+
description: "Total number of errors"
|
|
285
|
+
}),
|
|
286
|
+
providerCallCounter: meter.createCounter("reactionary.provider.calls", {
|
|
287
|
+
description: "Total number of provider calls"
|
|
288
|
+
}),
|
|
289
|
+
providerCallDuration: meter.createHistogram("reactionary.provider.duration", {
|
|
290
|
+
description: "Provider call duration in milliseconds",
|
|
291
|
+
unit: "ms"
|
|
292
|
+
}),
|
|
293
|
+
cacheHitCounter: meter.createCounter("reactionary.cache.hits", {
|
|
294
|
+
description: "Total number of cache hits"
|
|
295
|
+
}),
|
|
296
|
+
cacheMissCounter: meter.createCounter("reactionary.cache.misses", {
|
|
297
|
+
description: "Total number of cache misses"
|
|
298
|
+
})
|
|
299
|
+
};
|
|
300
|
+
return metricsInstance;
|
|
301
|
+
}
|
|
302
|
+
function getMetrics() {
|
|
303
|
+
if (!metricsInstance) {
|
|
304
|
+
return initializeMetrics();
|
|
305
|
+
}
|
|
306
|
+
return metricsInstance;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// otel/src/provider-instrumentation.ts
|
|
310
|
+
import { SpanKind as SpanKind3 } from "@opentelemetry/api";
|
|
311
|
+
async function withProviderSpan(options, fn) {
|
|
312
|
+
const { providerName, operationType, operationName, attributes = {} } = options;
|
|
313
|
+
const metrics2 = getMetrics();
|
|
314
|
+
const spanName = `provider.${providerName}.${operationType}${operationName ? `.${operationName}` : ""}`;
|
|
315
|
+
const startTime = Date.now();
|
|
316
|
+
metrics2.providerCallCounter.add(1, {
|
|
317
|
+
"provider.name": providerName,
|
|
318
|
+
"provider.operation.type": operationType,
|
|
319
|
+
"provider.operation.name": operationName || "unknown"
|
|
320
|
+
});
|
|
321
|
+
return withSpan(
|
|
322
|
+
spanName,
|
|
323
|
+
async (span) => {
|
|
324
|
+
setSpanAttributes(span, {
|
|
325
|
+
"provider.name": providerName,
|
|
326
|
+
"provider.operation.type": operationType,
|
|
327
|
+
"provider.operation.name": operationName,
|
|
328
|
+
...attributes
|
|
329
|
+
});
|
|
330
|
+
try {
|
|
331
|
+
const result = await fn(span);
|
|
332
|
+
const duration = Date.now() - startTime;
|
|
333
|
+
metrics2.providerCallDuration.record(duration, {
|
|
334
|
+
"provider.name": providerName,
|
|
335
|
+
"provider.operation.type": operationType,
|
|
336
|
+
"provider.operation.name": operationName || "unknown",
|
|
337
|
+
"status": "success"
|
|
338
|
+
});
|
|
339
|
+
return result;
|
|
340
|
+
} catch (error) {
|
|
341
|
+
const duration = Date.now() - startTime;
|
|
342
|
+
metrics2.providerCallDuration.record(duration, {
|
|
343
|
+
"provider.name": providerName,
|
|
344
|
+
"provider.operation.type": operationType,
|
|
345
|
+
"provider.operation.name": operationName || "unknown",
|
|
346
|
+
"status": "error"
|
|
347
|
+
});
|
|
348
|
+
metrics2.errorCounter.add(1, {
|
|
349
|
+
"provider.name": providerName,
|
|
350
|
+
"provider.operation.type": operationType,
|
|
351
|
+
"provider.operation.name": operationName || "unknown"
|
|
352
|
+
});
|
|
353
|
+
throw error;
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
{ kind: SpanKind3.CLIENT }
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
function createProviderInstrumentation(providerName) {
|
|
360
|
+
return {
|
|
361
|
+
traceQuery: (operationName, fn, attributes) => {
|
|
362
|
+
return withProviderSpan(
|
|
363
|
+
{
|
|
364
|
+
providerName,
|
|
365
|
+
operationType: "query",
|
|
366
|
+
operationName,
|
|
367
|
+
attributes
|
|
368
|
+
},
|
|
369
|
+
fn
|
|
370
|
+
);
|
|
371
|
+
},
|
|
372
|
+
traceMutation: (operationName, fn, attributes) => {
|
|
373
|
+
return withProviderSpan(
|
|
374
|
+
{
|
|
375
|
+
providerName,
|
|
376
|
+
operationType: "mutation",
|
|
377
|
+
operationName,
|
|
378
|
+
attributes
|
|
379
|
+
},
|
|
380
|
+
fn
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
155
386
|
// core/src/providers/base.provider.ts
|
|
156
387
|
var BaseProvider = class {
|
|
157
388
|
constructor(schema, querySchema, mutationSchema) {
|
|
158
389
|
this.schema = schema;
|
|
159
390
|
this.querySchema = querySchema;
|
|
160
391
|
this.mutationSchema = mutationSchema;
|
|
392
|
+
this.instrumentation = createProviderInstrumentation(this.constructor.name);
|
|
161
393
|
}
|
|
162
394
|
/**
|
|
163
395
|
* Validates that the final domain model constructed by the provider
|
|
@@ -179,20 +411,35 @@ var BaseProvider = class {
|
|
|
179
411
|
* of the results will match the order of the queries.
|
|
180
412
|
*/
|
|
181
413
|
async query(queries, session) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
414
|
+
return this.instrumentation.traceQuery(
|
|
415
|
+
"query",
|
|
416
|
+
async (span) => {
|
|
417
|
+
span.setAttribute("provider.query.count", queries.length);
|
|
418
|
+
const results = await this.fetch(queries, session);
|
|
419
|
+
for (const result of results) {
|
|
420
|
+
this.assert(result);
|
|
421
|
+
}
|
|
422
|
+
span.setAttribute("provider.result.count", results.length);
|
|
423
|
+
return results;
|
|
424
|
+
},
|
|
425
|
+
{ queryCount: queries.length }
|
|
426
|
+
);
|
|
187
427
|
}
|
|
188
428
|
/**
|
|
189
429
|
* Executes the listed mutations in order and returns the final state
|
|
190
430
|
* resulting from that set of operations.
|
|
191
431
|
*/
|
|
192
432
|
async mutate(mutations, session) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
433
|
+
return this.instrumentation.traceMutation(
|
|
434
|
+
"mutate",
|
|
435
|
+
async (span) => {
|
|
436
|
+
span.setAttribute("provider.mutation.count", mutations.length);
|
|
437
|
+
const result = await this.process(mutations, session);
|
|
438
|
+
this.assert(result);
|
|
439
|
+
return result;
|
|
440
|
+
},
|
|
441
|
+
{ mutationCount: mutations.length }
|
|
442
|
+
);
|
|
196
443
|
}
|
|
197
444
|
};
|
|
198
445
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-commercetools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.30",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@reactionary/core": "0.0.
|
|
5
|
+
"@reactionary/core": "0.0.30",
|
|
6
6
|
"zod": "4.0.0-beta.20250430T185432",
|
|
7
7
|
"@commercetools/ts-client": "^3.2.2",
|
|
8
8
|
"@commercetools/platform-sdk": "^8.8.0"
|