@brickkit/core 0.16.0-beta.3 → 0.16.0-beta.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.
@@ -449,6 +449,7 @@ const INSTRUMENTATION_SCOPE = "@brickkit/core";
449
449
  const MAX_TEXT_LENGTH = 500;
450
450
  const MAX_STATEMENT_LENGTH = 2e3;
451
451
  const DATABASE_INSTRUMENTED = Symbol.for("brickkit.telemetry.database");
452
+ const FETCH_INSTRUMENTED = Symbol.for("brickkit.telemetry.fetch");
452
453
  const SQLITE_STATEMENT_METHODS = [
453
454
  "all",
454
455
  "get",
@@ -569,6 +570,53 @@ function instrumentDatabase(db) {
569
570
  else return;
570
571
  driver[DATABASE_INSTRUMENTED] = true;
571
572
  }
573
+ function instrumentFetch() {
574
+ const original = globalThis.fetch;
575
+ if (original[FETCH_INSTRUMENTED]) return;
576
+ const wrapped = new Proxy(original, {
577
+ get: (target, key) => key === FETCH_INSTRUMENTED ? true : Reflect.get(target, key),
578
+ apply(target, self, args) {
579
+ const [input, init] = args;
580
+ if (!trace.getActiveSpan()) return Reflect.apply(target, self, args);
581
+ let url;
582
+ try {
583
+ url = new URL(input instanceof Request ? input.url : String(input));
584
+ } catch {
585
+ return Reflect.apply(target, self, args);
586
+ }
587
+ const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
588
+ const span = trace.getTracer(INSTRUMENTATION_SCOPE).startSpan(truncate(redactText(`${method} ${url.host}${url.pathname}`)), {
589
+ kind: SpanKind.CLIENT,
590
+ attributes: {
591
+ [ATTR.httpMethod]: method,
592
+ [ATTR.urlFull]: truncate(redactText(`${url.origin}${url.pathname}`)),
593
+ [ATTR.serverAddress]: url.hostname
594
+ }
595
+ }, context.active());
596
+ const fail = (error) => {
597
+ recordThrown(span, error);
598
+ span.end();
599
+ throw error;
600
+ };
601
+ let pending;
602
+ try {
603
+ pending = Reflect.apply(target, self, args);
604
+ } catch (error) {
605
+ return fail(error);
606
+ }
607
+ return pending.then((response) => {
608
+ span.setAttribute(ATTR.httpStatus, response.status);
609
+ if (response.status >= 400) span.setStatus({
610
+ code: SpanStatusCode.ERROR,
611
+ message: `HTTP ${response.status}`
612
+ });
613
+ span.end();
614
+ return response;
615
+ }, fail);
616
+ }
617
+ });
618
+ globalThis.fetch = wrapped;
619
+ }
572
620
  function instrumentBunSqlite(client) {
573
621
  const prepare = client.prepare.bind(client);
574
622
  client.prepare = (sql) => {
@@ -6878,7 +6926,10 @@ function brick(options = {}) {
6878
6926
  } : options.events;
6879
6927
  const events = eventsOptions ? new EventsHub(eventsOptions, appDeps(services)).attach(services) : void 0;
6880
6928
  if (events?.workflows) app.all(WEBHOOK_ROUTE, ({ request }) => events.webhook(request));
6881
- if (telemetry) for (const service of services) instrumentDatabase(service.getDb());
6929
+ if (telemetry) {
6930
+ for (const service of services) instrumentDatabase(service.getDb());
6931
+ instrumentFetch();
6932
+ }
6882
6933
  for (const service of services) service.build();
6883
6934
  for (const service of services) checkFileStorages(service, [portOf(service).deps, ...installedPlugins(service).map(pluginDepsOf)]);
6884
6935
  const localDirs = localFileDirs([...services.map((service) => portOf(service).deps), ...plugins.deps]);