@m1212e/rumble 0.23.2 → 0.23.4

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/out/index.cjs CHANGED
@@ -27,7 +27,6 @@ let graphql = require("graphql");
27
27
  let _escape_tech_graphql_armor = require("@escape.tech/graphql-armor");
28
28
  let _graphql_yoga_plugin_disable_introspection = require("@graphql-yoga/plugin-disable-introspection");
29
29
  let _opentelemetry_api = require("@opentelemetry/api");
30
- let _pothos_tracing_opentelemetry = require("@pothos/tracing-opentelemetry");
31
30
  let es_toolkit = require("es-toolkit");
32
31
  let graphql_yoga = require("graphql-yoga");
33
32
  let drizzle_orm = require("drizzle-orm");
@@ -36,6 +35,7 @@ let drizzle_orm_pg_core = require("drizzle-orm/pg-core");
36
35
  let drizzle_orm_mysql_core = require("drizzle-orm/mysql-core");
37
36
  let pluralize = require("pluralize");
38
37
  pluralize = __toESM(pluralize, 1);
38
+ let _pothos_tracing_opentelemetry = require("@pothos/tracing-opentelemetry");
39
39
  let drizzle_orm_sqlite_core = require("drizzle-orm/sqlite-core");
40
40
  let _pothos_core = require("@pothos/core");
41
41
  _pothos_core = __toESM(_pothos_core, 1);
@@ -208,7 +208,7 @@ function mapNullFieldsToUndefined(obj) {
208
208
  }
209
209
  //#endregion
210
210
  //#region package.json
211
- var version = "0.23.2";
211
+ var version = "0.23.4";
212
212
  //#endregion
213
213
  //#region lib/helpers/mergeFilters.ts
214
214
  function mergeFilters(filterA, filterB, mode = "AND") {
@@ -1300,6 +1300,165 @@ const sofaOpenAPIWebhookDocs = {
1300
1300
  } }
1301
1301
  };
1302
1302
  //#endregion
1303
+ //#region lib/helpers/tracing.ts
1304
+ function isAsyncIterable(value) {
1305
+ return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
1306
+ }
1307
+ async function* wrapSubscriptionIterator(iterator, log, operationName) {
1308
+ let eventCount = 0;
1309
+ try {
1310
+ for await (const event of iterator) {
1311
+ eventCount++;
1312
+ if (event.errors?.length) log.error({
1313
+ "graphql.operation.name": operationName,
1314
+ eventCount,
1315
+ errors: event.errors.map((e) => e.message)
1316
+ }, "graphql subscription event error");
1317
+ yield event;
1318
+ }
1319
+ log.info({
1320
+ "graphql.operation.name": operationName,
1321
+ eventCount
1322
+ }, "graphql subscription completed");
1323
+ } catch (error) {
1324
+ log.error({
1325
+ "graphql.operation.name": operationName,
1326
+ eventCount,
1327
+ err: error
1328
+ }, "graphql subscription threw");
1329
+ throw error;
1330
+ }
1331
+ }
1332
+ function buildTracedExecute(executeFn, rumbleInput) {
1333
+ return async (options) => {
1334
+ let log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
1335
+ const operationName = options.operationName ?? "anonymous";
1336
+ const start = Date.now();
1337
+ log?.info({ "graphql.operation.name": operationName }, "graphql execute start");
1338
+ const run = async () => {
1339
+ const result = await executeFn(options);
1340
+ if (result && "errors" in result && result.errors?.length) log?.error({
1341
+ "graphql.operation.name": operationName,
1342
+ durationMs: Date.now() - start,
1343
+ errors: result.errors.map((e) => e.message)
1344
+ }, "graphql execute completed with errors");
1345
+ else log?.info({
1346
+ "graphql.operation.name": operationName,
1347
+ durationMs: Date.now() - start
1348
+ }, "graphql execute completed");
1349
+ return result;
1350
+ };
1351
+ if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan(_pothos_tracing_opentelemetry.SpanNames.EXECUTE, { attributes: {
1352
+ [_pothos_tracing_opentelemetry.AttributeNames.OPERATION_NAME]: operationName,
1353
+ [_pothos_tracing_opentelemetry.AttributeNames.SOURCE]: options.document
1354
+ } }, async (span) => {
1355
+ if (log && rumbleInput.logger?.injectTraceId !== false) {
1356
+ const { traceId, spanId } = span.spanContext();
1357
+ log = log.child({
1358
+ traceId,
1359
+ spanId
1360
+ });
1361
+ }
1362
+ try {
1363
+ const result = await run();
1364
+ if (result && "errors" in result && result.errors?.length) {
1365
+ for (const error of result.errors) span.recordException(error);
1366
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1367
+ }
1368
+ return result;
1369
+ } catch (error) {
1370
+ if (error instanceof Error) span.recordException(error);
1371
+ log?.error({
1372
+ "graphql.operation.name": operationName,
1373
+ durationMs: Date.now() - start,
1374
+ err: error
1375
+ }, "graphql execute threw");
1376
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1377
+ throw error;
1378
+ } finally {
1379
+ span.end();
1380
+ }
1381
+ });
1382
+ try {
1383
+ return await run();
1384
+ } catch (error) {
1385
+ log?.error({
1386
+ "graphql.operation.name": operationName,
1387
+ durationMs: Date.now() - start,
1388
+ err: error
1389
+ }, "graphql execute threw");
1390
+ throw error;
1391
+ }
1392
+ };
1393
+ }
1394
+ function buildTracedSubscribe(subscribeFn, rumbleInput) {
1395
+ return async (options) => {
1396
+ let log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
1397
+ const operationName = options.operationName ?? "anonymous";
1398
+ const start = Date.now();
1399
+ const doSubscribe = async () => {
1400
+ log?.info({ "graphql.operation.name": operationName }, "graphql subscribe start");
1401
+ const result = await subscribeFn(options);
1402
+ if (!isAsyncIterable(result)) {
1403
+ const execResult = result;
1404
+ if (execResult.errors?.length) log?.error({
1405
+ "graphql.operation.name": operationName,
1406
+ durationMs: Date.now() - start,
1407
+ errors: execResult.errors.map((e) => e.message)
1408
+ }, "graphql subscribe completed with errors");
1409
+ return execResult;
1410
+ }
1411
+ log?.info({
1412
+ "graphql.operation.name": operationName,
1413
+ durationMs: Date.now() - start
1414
+ }, "graphql subscription established");
1415
+ if (log) return wrapSubscriptionIterator(result, log, operationName);
1416
+ return result;
1417
+ };
1418
+ if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan("graphql.subscribe", async (span) => {
1419
+ if (log && rumbleInput.logger?.injectTraceId !== false) {
1420
+ const { traceId, spanId } = span.spanContext();
1421
+ log = log.child({
1422
+ traceId,
1423
+ spanId
1424
+ });
1425
+ }
1426
+ try {
1427
+ const result = await doSubscribe();
1428
+ if (!isAsyncIterable(result)) {
1429
+ const execResult = result;
1430
+ if (execResult.errors?.length) {
1431
+ for (const error of execResult.errors) span.recordException(error);
1432
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1433
+ }
1434
+ }
1435
+ return result;
1436
+ } catch (error) {
1437
+ if (error instanceof Error) span.recordException(error);
1438
+ log?.error({
1439
+ "graphql.operation.name": operationName,
1440
+ durationMs: Date.now() - start,
1441
+ err: error
1442
+ }, "graphql subscribe threw");
1443
+ span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
1444
+ throw error;
1445
+ } finally {
1446
+ span.end();
1447
+ }
1448
+ });
1449
+ try {
1450
+ return await doSubscribe();
1451
+ } catch (error) {
1452
+ log?.error({
1453
+ "graphql.operation.name": operationName,
1454
+ durationMs: Date.now() - start,
1455
+ err: error
1456
+ }, "graphql subscribe threw");
1457
+ throw error;
1458
+ }
1459
+ };
1460
+ }
1461
+ //#endregion
1303
1462
  //#region lib/helpers/sqlTypes/mapDrizzleTypeToGraphQlType.ts
1304
1463
  function buildPothosResponseTypeFromGraphQLType({ builder, sqlType, fieldName, nullable, isArray }) {
1305
1464
  const gqlType = mapSQLTypeToGraphQLType({
@@ -2337,59 +2496,7 @@ export const r = rumble({
2337
2496
  ...args?.plugins ?? [],
2338
2497
  ...enableApiDocs ? [] : [(0, _graphql_yoga_plugin_disable_introspection.useDisableIntrospection)(), (0, _escape_tech_graphql_armor.EnvelopArmorPlugin)(args?.armorConfig)],
2339
2498
  rumbleInput.otel?.enabled || rumbleInput.logger?.enabled ? { onExecute: ({ setExecuteFn, executeFn }) => {
2340
- setExecuteFn(async (options) => {
2341
- const log = rumbleInput.logger?.enabled ? rumbleInput.logger.logger : void 0;
2342
- const operationName = options.operationName ?? "anonymous";
2343
- const start = Date.now();
2344
- log?.info({ "graphql.operation.name": operationName }, "graphql execute start");
2345
- const run = async () => {
2346
- const result = await executeFn(options);
2347
- if (result && "errors" in result && result.errors?.length) log?.error({
2348
- "graphql.operation.name": operationName,
2349
- durationMs: Date.now() - start,
2350
- errors: result.errors.map((e) => e.message)
2351
- }, "graphql execute completed with errors");
2352
- else log?.info({
2353
- "graphql.operation.name": operationName,
2354
- durationMs: Date.now() - start
2355
- }, "graphql execute completed");
2356
- return result;
2357
- };
2358
- if (rumbleInput.otel?.enabled) return rumbleInput.otel.tracer.startActiveSpan(_pothos_tracing_opentelemetry.SpanNames.EXECUTE, { attributes: {
2359
- [_pothos_tracing_opentelemetry.AttributeNames.OPERATION_NAME]: operationName,
2360
- [_pothos_tracing_opentelemetry.AttributeNames.SOURCE]: options.document
2361
- } }, async (span) => {
2362
- try {
2363
- const result = await run();
2364
- if (result && "errors" in result && result.errors?.length) {
2365
- for (const error of result.errors) span.recordException(error);
2366
- span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
2367
- }
2368
- return result;
2369
- } catch (error) {
2370
- if (error instanceof Error) span.recordException(error);
2371
- log?.error({
2372
- "graphql.operation.name": operationName,
2373
- durationMs: Date.now() - start,
2374
- err: error
2375
- }, "graphql execute threw");
2376
- span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
2377
- throw error;
2378
- } finally {
2379
- span.end();
2380
- }
2381
- });
2382
- try {
2383
- return await run();
2384
- } catch (error) {
2385
- log?.error({
2386
- "graphql.operation.name": operationName,
2387
- durationMs: Date.now() - start,
2388
- err: error
2389
- }, "graphql execute threw");
2390
- throw error;
2391
- }
2392
- });
2499
+ setExecuteFn(buildTracedExecute(executeFn, rumbleInput));
2393
2500
  } } : false
2394
2501
  ].filter(Boolean)
2395
2502
  });
@@ -2410,6 +2517,8 @@ export const r = rumble({
2410
2517
  ...args,
2411
2518
  schema: builtSchema(),
2412
2519
  context,
2520
+ execute: buildTracedExecute(args.execute ?? graphql.execute, rumbleInput),
2521
+ subscribe: buildTracedSubscribe(args.subscribe ?? graphql.subscribe, rumbleInput),
2413
2522
  errorHandler(errors) {
2414
2523
  const span = _opentelemetry_api.trace.getActiveSpan();
2415
2524
  for (const error of errors) span?.recordException(error);
@@ -2422,7 +2531,9 @@ export const r = rumble({
2422
2531
  return implementation({
2423
2532
  ...args,
2424
2533
  schema: builtSchema(),
2425
- context
2534
+ context,
2535
+ execute: buildTracedExecute(args.execute ?? graphql.execute, rumbleInput),
2536
+ subscribe: buildTracedSubscribe(args.subscribe ?? graphql.subscribe, rumbleInput)
2426
2537
  }, ...rest);
2427
2538
  };
2428
2539
  return {