@alumnium/core 0.24.0-alpha.1 → 0.24.0-alpha.2

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/src/internal.js CHANGED
@@ -745,7 +745,6 @@ import {
745
745
  getLogger as logtapeGetLogger,
746
746
  getTextFormatter,
747
747
  } from "@logtape/logtape";
748
- import { getOpenTelemetrySink } from "@logtape/otel";
749
748
  import { always as always2, never } from "alwaysly";
750
749
  import filenamify2 from "filenamify";
751
750
  async function ensureDir(dir) {
@@ -798,12 +797,22 @@ class Instrumentation {
798
797
  }
799
798
  }
800
799
 
800
+ // core/src/telemetry/loadTelemetry.ts
801
+ var telemetryPromise;
802
+ function loadTelemetry() {
803
+ return (telemetryPromise ??= import("@alumnium/telemetry").then(
804
+ (module) => module,
805
+ () => {
806
+ console.warn(
807
+ 'ALUMNIUM_TRACE is enabled, but traces and logs aren\'t exported without the "@alumnium/telemetry" package. Install it, e.g. `npm install @alumnium/telemetry`.',
808
+ );
809
+ return;
810
+ },
811
+ ));
812
+ }
813
+
801
814
  // core/src/telemetry/Tracer.ts
802
815
  import { context, SpanStatusCode, trace } from "@opentelemetry/api";
803
- import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
804
- import { resourceFromAttributes } from "@opentelemetry/resources";
805
- import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
806
- import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
807
816
  import { nanoid } from "nanoid";
808
817
 
809
818
  // core/src/typeUtils.ts
@@ -827,47 +836,40 @@ class Tracer {
827
836
  }
828
837
  const spanMethod = (spanName, ...args) => {
829
838
  const [attrs, bodyFnOrKey] = this.#normalizeSpanArgs(args);
830
- const provider = this.#configure();
839
+ const tracer = this.#configure();
831
840
  if (typeof bodyFnOrKey === "string" || !bodyFnOrKey) {
832
841
  const key = bodyFnOrKey || nanoid();
833
- if (!provider) {
842
+ if (!tracer) {
834
843
  const span = this.#dummySpan;
835
844
  this.#spans.set(key, span);
836
845
  return span;
837
846
  }
838
- const otelSpan = this.#tracer(provider).startSpan(
839
- spanName,
840
- traceAttrs(attrs),
841
- );
847
+ const otelSpan = tracer.startSpan(spanName, traceAttrs(attrs));
842
848
  return this.#span(otelSpan, key);
843
849
  }
844
850
  const bodyFn = bodyFnOrKey;
845
- if (!provider) return bodyFn(this.#dummySpan);
846
- return this.#tracer(provider).startActiveSpan(
847
- spanName,
848
- traceAttrs(attrs),
849
- (otelSpan) => {
850
- const span = this.#span(otelSpan);
851
- try {
852
- const result = bodyFn(span);
853
- if (result instanceof Promise)
854
- return result
855
- .then((res) => {
856
- span.succeed();
857
- return res;
858
- })
859
- .catch((error) => {
860
- span.fail(error);
861
- throw error;
862
- });
863
- span.succeed();
864
- return result;
865
- } catch (error) {
866
- span.fail(error);
867
- throw error;
868
- }
869
- },
870
- );
851
+ if (!tracer) return bodyFn(this.#dummySpan);
852
+ return tracer.startActiveSpan(spanName, traceAttrs(attrs), (otelSpan) => {
853
+ const span = this.#span(otelSpan);
854
+ try {
855
+ const result = bodyFn(span);
856
+ if (result instanceof Promise)
857
+ return result
858
+ .then((res) => {
859
+ span.succeed();
860
+ return res;
861
+ })
862
+ .catch((error) => {
863
+ span.fail(error);
864
+ throw error;
865
+ });
866
+ span.succeed();
867
+ return result;
868
+ } catch (error) {
869
+ span.fail(error);
870
+ throw error;
871
+ }
872
+ });
871
873
  };
872
874
  return {
873
875
  span: spanMethod,
@@ -879,8 +881,8 @@ class Tracer {
879
881
  }
880
882
  },
881
883
  event: (eventName, ...args) => {
882
- const provider = this.#configure();
883
- if (!provider) return;
884
+ const tracer = this.#configure();
885
+ if (!tracer) return;
884
886
  const attrs = args[0] || {};
885
887
  const fullEventName = `${moduleName}.${eventName}`;
886
888
  const eventAttrs = Tracer.#compactAttrs(attrs);
@@ -889,7 +891,7 @@ class Tracer {
889
891
  activeSpan.addEvent(fullEventName, eventAttrs);
890
892
  return;
891
893
  }
892
- const span = this.#tracer(provider).startSpan(fullEventName, {
894
+ const span = tracer.startSpan(fullEventName, {
893
895
  attributes: eventAttrs,
894
896
  });
895
897
  span.addEvent(fullEventName, eventAttrs);
@@ -930,9 +932,6 @@ class Tracer {
930
932
  }),
931
933
  };
932
934
  }
933
- static #tracer(provider) {
934
- return provider.getTracer(this.serviceName);
935
- }
936
935
  static #compactAttrs(attrs) {
937
936
  return Object.fromEntries(
938
937
  Object.entries(attrs).filter(([, value]) => value != null),
@@ -958,10 +957,14 @@ class Tracer {
958
957
  }
959
958
  }
960
959
  }
960
+ static async init() {
961
+ this.#configure();
962
+ await this.#tracing;
963
+ }
961
964
  static async flush() {
962
- const provider = this.#configure();
963
- if (!provider) return;
964
- return this.#flush(provider);
965
+ this.#configure();
966
+ const tracing = await this.#tracing;
967
+ if (tracing) return this.#flush(tracing);
965
968
  }
966
969
  static #spans = new Map();
967
970
  static #dummySpan = {
@@ -1037,29 +1040,25 @@ class Tracer {
1037
1040
  );
1038
1041
  otelSpan.setStatus(otelStatus);
1039
1042
  }
1040
- static async #flush(provider) {
1043
+ static async #flush(tracing) {
1041
1044
  this.#spans.forEach((span) => {
1042
1045
  span.fail("Span not ended before flush");
1043
1046
  });
1044
- await provider.forceFlush().catch(() => {});
1047
+ await tracing.flush().catch(() => {});
1045
1048
  }
1046
- static #provider;
1049
+ static #tracing;
1047
1050
  static #configure() {
1048
- if (this.#provider !== undefined) return this.#provider;
1049
- if (this.enabled) {
1050
- const provider = new NodeTracerProvider({
1051
- resource: resourceFromAttributes({
1052
- "service.name": Tracer.serviceName,
1053
- }),
1054
- spanProcessors: [new BatchSpanProcessor(new OTLPTraceExporter())],
1055
- });
1056
- provider.register();
1057
- process.once("beforeExit", () => void this.#flush(provider));
1058
- this.#provider = provider;
1059
- } else {
1060
- this.#provider = null;
1051
+ if (this.#tracing === undefined) {
1052
+ this.#tracing = this.enabled
1053
+ ? loadTelemetry().then((telemetry) => {
1054
+ const tracing = telemetry?.startTracing(this.serviceName);
1055
+ if (tracing)
1056
+ process.once("beforeExit", () => void this.#flush(tracing));
1057
+ return tracing;
1058
+ })
1059
+ : null;
1061
1060
  }
1062
- return this.#provider;
1061
+ return this.#tracing ? trace.getTracer(this.serviceName) : null;
1063
1062
  }
1064
1063
  }
1065
1064
 
@@ -1112,6 +1111,7 @@ class Logger {
1112
1111
  await this.#flush();
1113
1112
  process.exit(1);
1114
1113
  }
1114
+ await Tracer.init();
1115
1115
  }
1116
1116
  static #level;
1117
1117
  static set level(newLevel) {
@@ -1188,11 +1188,10 @@ class Logger {
1188
1188
  this.#flush();
1189
1189
  });
1190
1190
  }
1191
- if (Tracer.enabled) {
1191
+ const telemetry = Tracer.enabled ? await loadTelemetry() : undefined;
1192
+ if (telemetry) {
1192
1193
  mainSinks.push("otel");
1193
- sinks.otel = getOpenTelemetrySink({
1194
- serviceName: Instrumentation.serviceName,
1195
- });
1194
+ sinks.otel = telemetry.getLogSink(Instrumentation.serviceName);
1196
1195
  }
1197
1196
  return {
1198
1197
  sinks,
@@ -8707,7 +8706,7 @@ function usesAdaptiveThinking(modelName) {
8707
8706
  // core/package.json
8708
8707
  var package_default = {
8709
8708
  name: "@alumnium/core",
8710
- version: "0.24.0-alpha.1",
8709
+ version: "0.24.0-alpha.2",
8711
8710
  description:
8712
8711
  "Alumnium core: Alumni, agents, environment, telemetry, storage, accessibility trees, tools and driver contracts",
8713
8712
  license: "MIT",
@@ -8723,12 +8722,7 @@ var package_default = {
8723
8722
  "@js-fns/xxhash": "^0.1.0",
8724
8723
  "@logtape/file": "^2.0.2",
8725
8724
  "@logtape/logtape": "^2.0.2",
8726
- "@logtape/otel": "^2.0.5",
8727
8725
  "@opentelemetry/api": "^1.9.1",
8728
- "@opentelemetry/exporter-trace-otlp-http": "^0.215.0",
8729
- "@opentelemetry/resources": "^2.7.0",
8730
- "@opentelemetry/sdk-trace-base": "^2.7.0",
8731
- "@opentelemetry/sdk-trace-node": "^2.7.0",
8732
8726
  alwaysly: "^0.4.0",
8733
8727
  cac: "^7.0.0",
8734
8728
  "case-anything": "^3.1.2",
@@ -8744,13 +8738,18 @@ var package_default = {
8744
8738
  zod: "^4.4.1",
8745
8739
  },
8746
8740
  devDependencies: {
8741
+ "@alumnium/telemetry": "workspace:*",
8747
8742
  ai: "^7.0.78",
8748
8743
  oxfmt: "^0.65.0",
8749
8744
  },
8750
8745
  peerDependencies: {
8746
+ "@alumnium/telemetry": "workspace:*",
8751
8747
  ai: "^7.0.78",
8752
8748
  },
8753
8749
  peerDependenciesMeta: {
8750
+ "@alumnium/telemetry": {
8751
+ optional: true,
8752
+ },
8754
8753
  ai: {
8755
8754
  optional: true,
8756
8755
  },
@@ -8882,5 +8881,5 @@ export {
8882
8881
  withCallDefaults,
8883
8882
  };
8884
8883
 
8885
- //# debugId=9A37782E9305DF0264756E2164756E21
8884
+ //# debugId=C149851D6B93896D64756E2164756E21
8886
8885
  //# sourceMappingURL=internal.js.map