@osfactory/otel-hook 0.6.0 → 0.8.0

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.
@@ -368,6 +368,22 @@ interface DurableLogSpool {
368
368
  }
369
369
  declare const createFileDurableLogSpool: (options: DurableLogSpoolOptions) => DurableLogSpool;
370
370
 
371
+ /**
372
+ * OTLP HTTP/protobuf log sink.
373
+ *
374
+ * The logs counterpart of `otlp-sink.ts`, and deliberately its mirror image:
375
+ * semantic mapping lives entirely in `log-records.ts`, and this module owns only
376
+ * batching, delivery, bounded flush/shutdown, health tracking, and — when a spool
377
+ * is configured — persisting what a down collector rejected. Every failure mode
378
+ * (disabled config, missing or underivable endpoint, export failure, timeout,
379
+ * spool write failure) degrades to a reported diagnostic rather than a thrown
380
+ * error, preserving the hook's fail-open contract.
381
+ *
382
+ * The two sinks are separate objects rather than one exporter with two encoders
383
+ * because they fail independently: a collector that has no logs receiver, or an
384
+ * operator who pointed logs at the wrong port, must cost the logs signal and
385
+ * nothing else. The trace pipeline is the one an installation already depends on.
386
+ */
371
387
  /**
372
388
  * Where log records are posted.
373
389
  *
@@ -1,7 +1,7 @@
1
1
  import { b as SpanCorrelationResolver } from './semconv-fPRb-dpU.js';
2
2
  export { D as DEFAULT_INSTRUMENTATION_SCOPE, E as EventTraceIdentity, M as MAX_RECOVERED_START_ATTRIBUTES, S as SemanticMappingOptions, a as SpanCorrelation, c as SpanDisposition, d as SpanFamily, e as SpanOrphanClassification, f as SpanPairing, g as SpanScopeRef, h as canonicalEventTraceIdentities, i as canonicalEventsToReadableSpans, p as parentScopeRefOf, s as spanScopeRefOf, j as startOnlySpanAttributes } from './semconv-fPRb-dpU.js';
3
- import { o as OtlpTelemetrySink, n as OtlpLogTelemetrySink } from './otlp-log-sink-eUEjaLhk.js';
4
- export { C as ContentWithholdingReason, D as DurableLogSpool, a as DurableLogSpoolOptions, b as DurableSpool, c as DurableSpoolOptions, L as LOG_MAPPING_VERSION, d as LOG_SIGNALS, e as LogContentPolicy, f as LogMappingOptions, g as LogMappingResult, h as LogSignal, i as LogSpoolBatch, j as LogsDeliverability, M as MAX_LOG_BODY_CHARACTERS, k as MAX_LOG_RECORDS_PER_BATCH, l as MAX_LOG_RECORDS_PER_EVENT, m as MCP_TOOL_NAME_PATTERN, N as NO_LOG_CONTENT, O as OtlpLogSinkOptions, p as OtlpTraceSinkOptions, S as SerializedLogRecord, q as SerializedSpan, r as SpoolBatch, s as SpoolDrainResult, t as SpoolEnqueueResult, u as canonicalEventsToLogRecords, v as createFileDurableLogSpool, w as createFileDurableSpool, x as createOtlpLogSink, y as createOtlpTraceSink, z as describeLogsDeliverability, A as logSignalOf, B as logSignalsForLifecycleEvents, E as resolveLogsEndpoint } from './otlp-log-sink-eUEjaLhk.js';
3
+ import { o as OtlpTelemetrySink, n as OtlpLogTelemetrySink } from './otlp-log-sink-zuHojHSP.js';
4
+ export { C as ContentWithholdingReason, D as DurableLogSpool, a as DurableLogSpoolOptions, b as DurableSpool, c as DurableSpoolOptions, L as LOG_MAPPING_VERSION, d as LOG_SIGNALS, e as LogContentPolicy, f as LogMappingOptions, g as LogMappingResult, h as LogSignal, i as LogSpoolBatch, j as LogsDeliverability, M as MAX_LOG_BODY_CHARACTERS, k as MAX_LOG_RECORDS_PER_BATCH, l as MAX_LOG_RECORDS_PER_EVENT, m as MCP_TOOL_NAME_PATTERN, N as NO_LOG_CONTENT, O as OtlpLogSinkOptions, p as OtlpTraceSinkOptions, S as SerializedLogRecord, q as SerializedSpan, r as SpoolBatch, s as SpoolDrainResult, t as SpoolEnqueueResult, u as canonicalEventsToLogRecords, v as createFileDurableLogSpool, w as createFileDurableSpool, x as createOtlpLogSink, y as createOtlpTraceSink, z as describeLogsDeliverability, A as logSignalOf, B as logSignalsForLifecycleEvents, E as resolveLogsEndpoint } from './otlp-log-sink-zuHojHSP.js';
5
5
  import { DeliveryHealthSnapshot } from './diagnostics.js';
6
6
  import { g as TelemetrySink } from './ports-n7KFOpKo.js';
7
7
  import '@opentelemetry/api';
package/dist/telemetry.js CHANGED
@@ -1,27 +1,7 @@
1
1
  // src/telemetry/semconv.ts
2
2
  import { createHash } from "crypto";
3
3
  import { SpanKind, SpanStatusCode, TraceFlags } from "@opentelemetry/api";
4
-
5
- // node_modules/@opentelemetry/core/build/esm/common/time.js
6
- var NANOSECOND_DIGITS = 9;
7
- var NANOSECOND_DIGITS_IN_MILLIS = 6;
8
- var MILLISECONDS_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS_IN_MILLIS);
9
- var SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS);
10
- function millisToHrTime(epochMillis) {
11
- const epochSeconds = epochMillis / 1e3;
12
- const seconds = Math.trunc(epochSeconds);
13
- const nanos = Math.round(epochMillis % 1e3 * MILLISECONDS_TO_NANOSECONDS);
14
- return [seconds, nanos];
15
- }
16
-
17
- // node_modules/@opentelemetry/core/build/esm/ExportResult.js
18
- var ExportResultCode;
19
- (function(ExportResultCode2) {
20
- ExportResultCode2[ExportResultCode2["SUCCESS"] = 0] = "SUCCESS";
21
- ExportResultCode2[ExportResultCode2["FAILED"] = 1] = "FAILED";
22
- })(ExportResultCode || (ExportResultCode = {}));
23
-
24
- // src/telemetry/semconv.ts
4
+ import { millisToHrTime } from "@opentelemetry/core";
25
5
  import {
26
6
  ATTR_ERROR_TYPE,
27
7
  ATTR_GEN_AI_OPERATION_NAME,
@@ -38,7 +18,7 @@ import {
38
18
  } from "@opentelemetry/semantic-conventions/incubating";
39
19
 
40
20
  // src/version.ts
41
- var VERSION = "0.6.0";
21
+ var VERSION = "0.8.0";
42
22
 
43
23
  // src/telemetry/export-identity.ts
44
24
  import { ATTR_SESSION_ID } from "@opentelemetry/semantic-conventions/incubating";
@@ -825,7 +805,10 @@ var ATTR_OTELHOOK_INVOCATION_ID = "otelhook.invocation.id";
825
805
  var ATTR_OTELHOOK_PROVIDER_ID = "otelhook.provider.id";
826
806
  var ATTR_OTELHOOK_PROVIDER_VERSION = "otelhook.provider.version";
827
807
  var ATTR_OTELHOOK_WORKSPACE_ID = "otelhook.workspace.id";
828
- var HAR_SESSION_KEY = "har.session_key";
808
+ var EXTERNAL_SESSION_KEY_ATTRIBUTES = Object.freeze([
809
+ ATTR_SESSION_ID,
810
+ "har.session_key"
811
+ ]);
829
812
  var ATTR_OTELHOOK_SUBAGENT_ID = "otelhook.subagent.id";
830
813
  var ATTR_VCS_REF_HEAD_NAME = "vcs.ref.head.name";
831
814
  var ATTR_OTELHOOK_TARGET_WORKSPACE_ID = "otelhook.target.workspace.id";
@@ -848,21 +831,26 @@ var targetAttributes = (event2) => {
848
831
  };
849
832
  var usageTemporalityAttributes = (usage) => usage === void 0 ? {} : { [ATTR_OTELHOOK_USAGE_TEMPORALITY]: usage.temporality };
850
833
  var isPrimitiveAttribute = (value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean";
851
- var passthroughHarAttributes = (resource) => {
834
+ var PASSTHROUGH_RESOURCE_PREFIXES = Object.freeze(["har.", "factory.", "vcs."]);
835
+ var LOCAL_ONLY_RESOURCE_PREFIX = "factory.local.";
836
+ var isPassthroughKey = (key) => PASSTHROUGH_RESOURCE_PREFIXES.some((prefix) => key.startsWith(prefix)) && !key.startsWith(LOCAL_ONLY_RESOURCE_PREFIX) && key !== "factory.local";
837
+ var passthroughResourceAttributes = (resource) => {
852
838
  const attributes = {};
853
839
  for (const [key, value] of Object.entries(resource.attributes)) {
854
- if (key.startsWith("har.") && isPrimitiveAttribute(value)) {
840
+ if (isPassthroughKey(key) && isPrimitiveAttribute(value)) {
855
841
  attributes[key] = value;
856
842
  }
857
843
  }
858
844
  return attributes;
859
845
  };
860
846
  var resolveExportedSessionId = (eventSessionId, resource) => {
861
- const har = resource.attributes[HAR_SESSION_KEY];
862
- if (typeof har === "string") {
863
- const trimmed = har.trim();
864
- if (trimmed.length > 0) {
865
- return { sessionId: trimmed, providerSessionId: eventSessionId };
847
+ for (const key of EXTERNAL_SESSION_KEY_ATTRIBUTES) {
848
+ const supplied = resource.attributes[key];
849
+ if (typeof supplied === "string") {
850
+ const trimmed = supplied.trim();
851
+ if (trimmed.length > 0) {
852
+ return { sessionId: trimmed, providerSessionId: eventSessionId };
853
+ }
866
854
  }
867
855
  }
868
856
  return { sessionId: eventSessionId, providerSessionId: eventSessionId };
@@ -883,7 +871,7 @@ var exportIdentityAttributes = (event2, resource, ids) => {
883
871
  ...event2.provenance.providerVersion === void 0 ? {} : { [ATTR_OTELHOOK_PROVIDER_VERSION]: event2.provenance.providerVersion },
884
872
  [ATTR_OTELHOOK_WORKSPACE_ID]: event2.workspace.workspaceId,
885
873
  ...event2.subagentId === void 0 ? {} : { [ATTR_OTELHOOK_SUBAGENT_ID]: event2.subagentId },
886
- ...passthroughHarAttributes(resource),
874
+ ...passthroughResourceAttributes(resource),
887
875
  ...hexTraceAttributes(ids)
888
876
  };
889
877
  };
@@ -1033,12 +1021,12 @@ var pickTarget = (attributes) => {
1033
1021
  }
1034
1022
  return picked;
1035
1023
  };
1036
- var withoutTarget = (attributes) => {
1024
+ var withoutTarget = (attributes, resource) => {
1037
1025
  const rest = { ...attributes };
1038
1026
  for (const key of TARGET_KEYS) {
1039
1027
  delete rest[key];
1040
1028
  }
1041
- return rest;
1029
+ return { ...rest, ...pickTarget(passthroughResourceAttributes(resource)) };
1042
1030
  };
1043
1031
  var rankOf = (candidate) => {
1044
1032
  const source = candidate[ATTR_OTELHOOK_TARGET_SOURCE];
@@ -1143,7 +1131,7 @@ var buildLifecycleSpan = (plan, resource, scope) => {
1143
1131
  name = "session";
1144
1132
  kind = SpanKind.INTERNAL;
1145
1133
  attributes = {
1146
- ...withoutTarget(attributes),
1134
+ ...withoutTarget(attributes, resource),
1147
1135
  ...strongestTarget([pickTarget(resolved.recovered), start === void 0 ? {} : targetAttributes(start)]),
1148
1136
  ...start?.agentName === void 0 ? {} : { [ATTR_OTELHOOK_AGENT_NAME]: start.agentName },
1149
1137
  ...start?.agentVersion === void 0 ? {} : { [ATTR_OTELHOOK_AGENT_VERSION]: start.agentVersion },
@@ -1184,7 +1172,7 @@ var buildLifecycleSpan = (plan, resource, scope) => {
1184
1172
  kind = SpanKind.CLIENT;
1185
1173
  attributes = {
1186
1174
  // Target keys are re-chosen below as one unit, never mixed across edges.
1187
- ...withoutTarget(attributes),
1175
+ ...withoutTarget(attributes, resource),
1188
1176
  [ATTR_GEN_AI_TOOL_NAME]: toolName,
1189
1177
  [ATTR_GEN_AI_TOOL_CALL_ID]: start?.toolCallId ?? end?.toolCallId ?? group.ref.scopeKey,
1190
1178
  ...start?.toolKind === void 0 ? {} : { [ATTR_OTELHOOK_TOOL_KIND]: start.toolKind },
@@ -1655,6 +1643,7 @@ var createFileDurableSpool = (options) => createSpoolQueue({
1655
1643
  });
1656
1644
 
1657
1645
  // src/telemetry/otlp-sink.ts
1646
+ import { ExportResultCode } from "@opentelemetry/core";
1658
1647
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
1659
1648
 
1660
1649
  // src/errors/index.ts
@@ -2110,8 +2099,8 @@ var createOtlpTraceSink = (options) => {
2110
2099
  };
2111
2100
 
2112
2101
  // src/telemetry/log-records.ts
2113
- import { TraceFlags as TraceFlags2 } from "@opentelemetry/api";
2114
- import { SeverityNumber } from "@opentelemetry/api-logs";
2102
+ import { SeverityNumber, TraceFlags as TraceFlags2 } from "@opentelemetry/api";
2103
+ import { millisToHrTime as millisToHrTime2 } from "@opentelemetry/core";
2115
2104
  import {
2116
2105
  ATTR_ERROR_TYPE as ATTR_ERROR_TYPE2,
2117
2106
  ATTR_GEN_AI_OPERATION_NAME as ATTR_GEN_AI_OPERATION_NAME2,
@@ -2432,14 +2421,14 @@ var contentAttributes = (fact, body) => ({
2432
2421
  });
2433
2422
  var buildRecord = (input) => {
2434
2423
  const severityNumber = severityFor(input.event);
2435
- const hrTime2 = millisToHrTime(input.event.occurredAt);
2424
+ const hrTime = millisToHrTime2(input.event.occurredAt);
2436
2425
  return {
2437
- hrTime: hrTime2,
2426
+ hrTime,
2438
2427
  // A hook observes an event that already happened, and this mapping is pure —
2439
2428
  // there is no clock here to read a real observation time from. Reporting the
2440
2429
  // occurrence time for both is the honest answer: it says "this is when it
2441
2430
  // happened" twice rather than inventing a second, wrong timestamp.
2442
- hrTimeObserved: hrTime2,
2431
+ hrTimeObserved: hrTime,
2443
2432
  spanContext: input.spanContext,
2444
2433
  severityNumber,
2445
2434
  severityText: SEVERITY_TEXT[severityNumber] ?? "INFO",
@@ -2629,7 +2618,16 @@ var createFileDurableLogSpool = (options) => createSpoolQueue({
2629
2618
 
2630
2619
  // src/telemetry/otlp-log-sink.ts
2631
2620
  import { TraceFlags as TraceFlags3 } from "@opentelemetry/api";
2621
+ import { ExportResultCode as ExportResultCode2 } from "@opentelemetry/core";
2622
+ import { millisToHrTime as millisToHrTime3 } from "@opentelemetry/core";
2632
2623
  import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-proto";
2624
+
2625
+ // src/config/otlp-endpoint.ts
2626
+ var OTLP_ENDPOINT_VARIABLES = Object.freeze({
2627
+ generic: "OTEL_EXPORTER_OTLP_ENDPOINT",
2628
+ traces: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
2629
+ logs: "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT"
2630
+ });
2633
2631
  var OTLP_TRACES_PATH = "/v1/traces";
2634
2632
  var OTLP_LOGS_PATH = "/v1/logs";
2635
2633
  var withoutTrailingSlashes = (value) => {
@@ -2639,6 +2637,8 @@ var withoutTrailingSlashes = (value) => {
2639
2637
  }
2640
2638
  return value.slice(0, end);
2641
2639
  };
2640
+
2641
+ // src/telemetry/otlp-log-sink.ts
2642
2642
  var resolveLogsEndpoint = (exporter) => {
2643
2643
  if (exporter.logs.endpoint !== void 0) {
2644
2644
  return { endpoint: exporter.logs.endpoint, derived: false };
@@ -2692,8 +2692,8 @@ var deserializeRecord = (serialized, resource, scope) => {
2692
2692
  isRemote: false
2693
2693
  };
2694
2694
  return {
2695
- hrTime: millisToHrTime(serialized.timeMillis),
2696
- hrTimeObserved: millisToHrTime(serialized.observedTimeMillis),
2695
+ hrTime: millisToHrTime3(serialized.timeMillis),
2696
+ hrTimeObserved: millisToHrTime3(serialized.observedTimeMillis),
2697
2697
  ...spanContext === void 0 ? {} : { spanContext },
2698
2698
  severityNumber: serialized.severityNumber,
2699
2699
  ...serialized.severityText === void 0 ? {} : { severityText: serialized.severityText },
@@ -2794,7 +2794,7 @@ var createOtlpLogSink = (options) => {
2794
2794
  (record) => deserializeRecord(record, batchResource, batch.instrumentationScope)
2795
2795
  );
2796
2796
  const result = await exportRecords(records);
2797
- return result.code === ExportResultCode.SUCCESS;
2797
+ return result.code === ExportResultCode2.SUCCESS;
2798
2798
  };
2799
2799
  const resolveCorrelations = async (events) => {
2800
2800
  if (options.correlate === void 0) {
@@ -2836,11 +2836,11 @@ var createOtlpLogSink = (options) => {
2836
2836
  result = await exportRecords(batch);
2837
2837
  } catch (thrown) {
2838
2838
  result = {
2839
- code: ExportResultCode.FAILED,
2839
+ code: ExportResultCode2.FAILED,
2840
2840
  error: thrown instanceof Error ? thrown : new Error("export threw")
2841
2841
  };
2842
2842
  }
2843
- if (result.code === ExportResultCode.SUCCESS) {
2843
+ if (result.code === ExportResultCode2.SUCCESS) {
2844
2844
  accepted += batch.length;
2845
2845
  health.recordSuccess(batch.length, options.clock.now());
2846
2846
  continue;