autotel 2.6.0 → 2.7.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.
- package/README.md +20 -20
- package/dist/auto.cjs +11 -10
- package/dist/auto.cjs.map +1 -1
- package/dist/auto.js +9 -8
- package/dist/auto.js.map +1 -1
- package/dist/{chunk-DPP5Y5XN.cjs → chunk-2EKAKQUG.cjs} +4 -4
- package/dist/{chunk-DPP5Y5XN.cjs.map → chunk-2EKAKQUG.cjs.map} +1 -1
- package/dist/chunk-33WTKH7X.js +21 -0
- package/dist/chunk-33WTKH7X.js.map +1 -0
- package/dist/{chunk-W7LHZVQF.js → chunk-4F5YVTKZ.js} +3 -3
- package/dist/{chunk-W7LHZVQF.js.map → chunk-4F5YVTKZ.js.map} +1 -1
- package/dist/{chunk-KVGNW3FC.js → chunk-4XKOSC37.js} +2 -2
- package/dist/{chunk-KVGNW3FC.js.map → chunk-4XKOSC37.js.map} +1 -1
- package/dist/{chunk-2EHB2KXS.cjs → chunk-5PZZHX4Y.cjs} +4 -5
- package/dist/chunk-5PZZHX4Y.cjs.map +1 -0
- package/dist/{chunk-CMWDPNRB.cjs → chunk-6T2EJMZG.cjs} +52 -6
- package/dist/chunk-6T2EJMZG.cjs.map +1 -0
- package/dist/{chunk-5JSIRHVW.cjs → chunk-ATLM4S6S.cjs} +70 -70
- package/dist/chunk-ATLM4S6S.cjs.map +1 -0
- package/dist/{chunk-MHGJOJZW.js → chunk-B4Z26USW.js} +7 -7
- package/dist/chunk-B4Z26USW.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/{chunk-Z6ZWNWWR.js.map → chunk-DGUM43GV.js.map} +1 -1
- package/dist/{chunk-EDJKNUGP.cjs → chunk-ELIWLU7M.cjs} +52 -102
- package/dist/chunk-ELIWLU7M.cjs.map +1 -0
- package/dist/{chunk-VL63L5JL.js → chunk-F4BQHYU2.js} +132 -3
- package/dist/chunk-F4BQHYU2.js.map +1 -0
- package/dist/{chunk-Y4Y2S7BM.cjs → chunk-HA4UB3LG.cjs} +2 -2
- package/dist/{chunk-Y4Y2S7BM.cjs.map → chunk-HA4UB3LG.cjs.map} +1 -1
- package/dist/{chunk-AXCCOSA3.js → chunk-HJPXNUZR.js} +4 -5
- package/dist/chunk-HJPXNUZR.js.map +1 -0
- package/dist/chunk-JEQ2X3Z6.cjs +12 -0
- package/dist/{chunk-G7VZBCD6.cjs.map → chunk-JEQ2X3Z6.cjs.map} +1 -1
- package/dist/{chunk-HRL4GTXD.js → chunk-KHPARJOH.js} +43 -43
- package/dist/chunk-KHPARJOH.js.map +1 -0
- package/dist/{chunk-2LNRY4QK.js → chunk-LBTNNJ7I.js} +3 -3
- package/dist/{chunk-2LNRY4QK.js.map → chunk-LBTNNJ7I.js.map} +1 -1
- package/dist/{chunk-NC52UBR2.cjs → chunk-LP2P3RP5.cjs} +4 -4
- package/dist/{chunk-NC52UBR2.cjs.map → chunk-LP2P3RP5.cjs.map} +1 -1
- package/dist/{chunk-2WSBYOW7.cjs → chunk-MLXWX5OT.cjs} +138 -9
- package/dist/chunk-MLXWX5OT.cjs.map +1 -0
- package/dist/{chunk-W3253FGB.cjs → chunk-OAS5IIYE.cjs} +4 -4
- package/dist/{chunk-W3253FGB.cjs.map → chunk-OAS5IIYE.cjs.map} +1 -1
- package/dist/{chunk-EKU6O7WG.js → chunk-P3GQ2CGY.js} +52 -6
- package/dist/chunk-P3GQ2CGY.js.map +1 -0
- package/dist/{chunk-X2ZZ65N4.cjs → chunk-QMYK3TWB.cjs} +11 -11
- package/dist/chunk-QMYK3TWB.cjs.map +1 -0
- package/dist/{chunk-YHW3MAS7.js → chunk-SR35DG5A.js} +4 -17
- package/dist/chunk-SR35DG5A.js.map +1 -0
- package/dist/{chunk-NGEG2JQ5.js → chunk-TQ3DJJ3N.js} +3 -3
- package/dist/{chunk-NGEG2JQ5.js.map → chunk-TQ3DJJ3N.js.map} +1 -1
- package/dist/{chunk-IW37CN6L.cjs → chunk-W4EUTSB2.cjs} +3 -18
- package/dist/chunk-W4EUTSB2.cjs.map +1 -0
- package/dist/{chunk-UHCAPEP7.js → chunk-YJBSLU3K.js} +51 -101
- package/dist/chunk-YJBSLU3K.js.map +1 -0
- package/dist/chunk-YS6C2YJE.cjs +25 -0
- package/dist/chunk-YS6C2YJE.cjs.map +1 -0
- package/dist/config.cjs +6 -6
- package/dist/config.d.cts +8 -5
- package/dist/config.d.ts +8 -5
- package/dist/config.js +2 -2
- package/dist/db.cjs +4 -4
- package/dist/db.js +2 -2
- package/dist/decorators.cjs +11 -12
- package/dist/decorators.cjs.map +1 -1
- package/dist/decorators.js +9 -10
- package/dist/decorators.js.map +1 -1
- package/dist/event-subscriber.d.cts +39 -1
- package/dist/event-subscriber.d.ts +39 -1
- package/dist/event-testing.cjs +1 -1
- package/dist/event-testing.js +1 -1
- package/dist/event.cjs +10 -9
- package/dist/event.d.cts +70 -2
- package/dist/event.d.ts +70 -2
- package/dist/event.js +7 -6
- package/dist/exporters.cjs +1 -1
- package/dist/exporters.js +1 -1
- package/dist/functional.cjs +16 -15
- package/dist/functional.d.cts +3 -3
- package/dist/functional.d.ts +3 -3
- package/dist/functional.js +9 -8
- package/dist/http.cjs +6 -7
- package/dist/http.cjs.map +1 -1
- package/dist/http.js +3 -4
- package/dist/http.js.map +1 -1
- package/dist/index.cjs +59 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17 -20
- package/dist/index.js.map +1 -1
- package/dist/{init-Cj2D8f1S.d.cts → init-iPJE1d2N.d.cts} +41 -32
- package/dist/{init-CDiC7CVz.d.ts → init-inG_x7Zr.d.ts} +41 -32
- package/dist/instrumentation.cjs +15 -14
- package/dist/instrumentation.cjs.map +1 -1
- package/dist/instrumentation.d.cts +2 -2
- package/dist/instrumentation.d.ts +2 -2
- package/dist/instrumentation.js +8 -7
- package/dist/instrumentation.js.map +1 -1
- package/dist/logger.cjs +10 -10
- package/dist/logger.js +3 -3
- package/dist/metric-helpers.cjs +8 -8
- package/dist/metric-helpers.js +3 -3
- package/dist/metric-testing.cjs +1 -1
- package/dist/metric-testing.js +1 -1
- package/dist/metric.cjs +7 -7
- package/dist/metric.js +4 -4
- package/dist/processors.cjs +1 -1
- package/dist/processors.js +1 -1
- package/dist/register.cjs.map +1 -1
- package/dist/register.js.map +1 -1
- package/dist/sampling.cjs +3 -3
- package/dist/sampling.js +3 -3
- package/dist/semantic-helpers.cjs +14 -13
- package/dist/semantic-helpers.d.cts +9 -9
- package/dist/semantic-helpers.d.ts +9 -9
- package/dist/semantic-helpers.js +10 -9
- package/dist/tail-sampling-processor.cjs +1 -1
- package/dist/tail-sampling-processor.js +1 -1
- package/dist/testing.cjs +4 -4
- package/dist/testing.js +3 -3
- package/dist/trace-helpers.cjs +14 -14
- package/dist/trace-helpers.js +3 -3
- package/dist/tracer-provider.cjs +1 -1
- package/dist/tracer-provider.js +1 -1
- package/dist/yaml-config.cjs +10 -5
- package/dist/yaml-config.d.cts +2 -2
- package/dist/yaml-config.d.ts +2 -2
- package/dist/yaml-config.js +7 -2
- package/package.json +6 -6
- package/src/auto.ts +14 -11
- package/src/baggage-span-processor.ts +4 -2
- package/src/config.ts +10 -7
- package/src/env-config.test.ts +2 -2
- package/src/env-config.ts +2 -2
- package/src/event-subscriber.ts +48 -0
- package/src/event.ts +154 -0
- package/src/functional.ts +42 -39
- package/src/init.customization.test.ts +1 -1
- package/src/init.integrations.test.ts +10 -10
- package/src/init.openllmetry.test.ts +1 -1
- package/src/init.ts +122 -191
- package/src/instrumentation.ts +8 -6
- package/src/node-require.ts +74 -0
- package/src/register.ts +1 -1
- package/src/semantic-helpers.test.ts +3 -3
- package/src/semantic-helpers.ts +13 -13
- package/src/trace-helpers.ts +4 -2
- package/src/yaml-config.test.ts +11 -11
- package/src/yaml-config.ts +64 -9
- package/dist/chunk-2EHB2KXS.cjs.map +0 -1
- package/dist/chunk-2WSBYOW7.cjs.map +0 -1
- package/dist/chunk-5JSIRHVW.cjs.map +0 -1
- package/dist/chunk-AXCCOSA3.js.map +0 -1
- package/dist/chunk-CMWDPNRB.cjs.map +0 -1
- package/dist/chunk-EDJKNUGP.cjs.map +0 -1
- package/dist/chunk-EKU6O7WG.js.map +0 -1
- package/dist/chunk-G7VZBCD6.cjs +0 -35
- package/dist/chunk-HRL4GTXD.js.map +0 -1
- package/dist/chunk-IW37CN6L.cjs.map +0 -1
- package/dist/chunk-MHGJOJZW.js.map +0 -1
- package/dist/chunk-UHCAPEP7.js.map +0 -1
- package/dist/chunk-VL63L5JL.js.map +0 -1
- package/dist/chunk-X2ZZ65N4.cjs.map +0 -1
- package/dist/chunk-YHW3MAS7.js.map +0 -1
- package/dist/chunk-Z6ZWNWWR.js +0 -30
package/README.md
CHANGED
|
@@ -180,7 +180,7 @@ init({
|
|
|
180
180
|
service: 'my-app',
|
|
181
181
|
// Datadog (traces + metrics + logs via OTLP)
|
|
182
182
|
endpoint: 'https://otlp.datadoghq.com',
|
|
183
|
-
|
|
183
|
+
headers: 'dd-api-key=...',
|
|
184
184
|
});
|
|
185
185
|
|
|
186
186
|
init({
|
|
@@ -188,7 +188,7 @@ init({
|
|
|
188
188
|
// Honeycomb (gRPC protocol)
|
|
189
189
|
protocol: 'grpc',
|
|
190
190
|
endpoint: 'api.honeycomb.io:443',
|
|
191
|
-
|
|
191
|
+
headers: {
|
|
192
192
|
'x-honeycomb-team': process.env.HONEYCOMB_API_KEY!,
|
|
193
193
|
},
|
|
194
194
|
});
|
|
@@ -572,7 +572,7 @@ const db = drizzle(pool);
|
|
|
572
572
|
|
|
573
573
|
instrumentDatabase(db, {
|
|
574
574
|
dbSystem: 'postgresql',
|
|
575
|
-
|
|
575
|
+
database: 'myapp',
|
|
576
576
|
});
|
|
577
577
|
|
|
578
578
|
await db.select().from(users); // queries emit spans automatically
|
|
@@ -750,11 +750,11 @@ init({
|
|
|
750
750
|
|
|
751
751
|
## Auto Instrumentation & Advanced Configuration
|
|
752
752
|
|
|
753
|
-
- `
|
|
753
|
+
- `autoInstrumentations` – Enable OpenTelemetry auto-instrumentations (HTTP, Express, Fastify, Prisma, Pino…). Requires `@opentelemetry/auto-instrumentations-node`.
|
|
754
754
|
- `instrumentations` – Provide manual instrumentation instances, e.g., `new HttpInstrumentation()`.
|
|
755
755
|
- `resource` / `resourceAttributes` – Declare cluster/region/tenant metadata once and it flows everywhere.
|
|
756
756
|
- `spanProcessor`, `metricReader`, `logRecordProcessors` – Plug in any OpenTelemetry exporter or your in-house pipeline.
|
|
757
|
-
- `
|
|
757
|
+
- `headers` – Attach vendor auth headers when using the built-in OTLP HTTP exporters.
|
|
758
758
|
- `sdkFactory` – Receive the Autotel defaults and return a fully customized `NodeSDK` for the rare cases you need complete control.
|
|
759
759
|
|
|
760
760
|
```typescript
|
|
@@ -768,23 +768,23 @@ init({
|
|
|
768
768
|
'cloud.region': 'us-east-1',
|
|
769
769
|
'deployment.environment': 'production',
|
|
770
770
|
},
|
|
771
|
-
|
|
771
|
+
autoInstrumentations: ['http', 'express', 'pino'],
|
|
772
772
|
instrumentations: [new HttpInstrumentation()],
|
|
773
|
-
|
|
773
|
+
headers: 'Authorization=Basic ...',
|
|
774
774
|
subscribers: [new PostHogSubscriber({ apiKey: 'phc_xxx' })],
|
|
775
775
|
});
|
|
776
776
|
```
|
|
777
777
|
|
|
778
|
-
### ⚠️
|
|
778
|
+
### ⚠️ autoInstrumentations vs. Manual Instrumentations
|
|
779
779
|
|
|
780
|
-
When using both `
|
|
780
|
+
When using both `autoInstrumentations` and `instrumentations`, manual instrumentations always take precedence. If you need custom configs (like `requireParentSpan: false` for standalone scripts), use **one or the other**:
|
|
781
781
|
|
|
782
782
|
#### Option A: Auto-instrumentations only (all defaults)
|
|
783
783
|
|
|
784
784
|
```typescript
|
|
785
785
|
init({
|
|
786
786
|
service: 'my-app',
|
|
787
|
-
|
|
787
|
+
autoInstrumentations: true, // All libraries with default configs
|
|
788
788
|
});
|
|
789
789
|
```
|
|
790
790
|
|
|
@@ -796,7 +796,7 @@ import { MongooseInstrumentation } from '@opentelemetry/instrumentation-mongoose
|
|
|
796
796
|
|
|
797
797
|
init({
|
|
798
798
|
service: 'my-app',
|
|
799
|
-
|
|
799
|
+
autoInstrumentations: false, // Must be false to avoid conflicts
|
|
800
800
|
instrumentations: [
|
|
801
801
|
new MongoDBInstrumentation({
|
|
802
802
|
requireParentSpan: false, // Custom config for scripts/cron jobs
|
|
@@ -815,7 +815,7 @@ import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
|
|
|
815
815
|
|
|
816
816
|
init({
|
|
817
817
|
service: 'my-app',
|
|
818
|
-
|
|
818
|
+
autoInstrumentations: ['http', 'express'], // Auto for most libraries
|
|
819
819
|
instrumentations: [
|
|
820
820
|
// Manual config only for libraries that need custom settings
|
|
821
821
|
new MongoDBInstrumentation({
|
|
@@ -899,7 +899,7 @@ NODE_OPTIONS="--experimental-loader=@opentelemetry/instrumentation/hook.mjs --im
|
|
|
899
899
|
- **Rate limiting & circuit breakers** – Prevent telemetry storms when backends misbehave.
|
|
900
900
|
- **Validation** – Configurable attribute/event name lengths, maximum counts, and nesting depth.
|
|
901
901
|
- **Sensitive data redaction** – Passwords, tokens, API keys, and any custom regex you provide are automatically masked before export.
|
|
902
|
-
- **Auto-flush** – Events buffers drain when root spans end (disable with `
|
|
902
|
+
- **Auto-flush** – Events buffers drain when root spans end (disable with `flushOnRootSpanEnd: false`).
|
|
903
903
|
- **Runtime flags** – Toggle metrics or swap endpoints via env vars without code edits.
|
|
904
904
|
|
|
905
905
|
```bash
|
|
@@ -923,16 +923,16 @@ init({
|
|
|
923
923
|
version?: string;
|
|
924
924
|
environment?: string;
|
|
925
925
|
baggage?: boolean | string; // Auto-copy baggage to span attributes
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
926
|
+
flushOnRootSpanEnd?: boolean; // Auto-flush events (default: true)
|
|
927
|
+
forceFlushOnShutdown?: boolean; // Force-flush spans on shutdown (default: false)
|
|
928
|
+
autoInstrumentations?: string[] | boolean | Record<string, { enabled?: boolean }>;
|
|
929
929
|
instrumentations?: NodeSDKConfiguration['instrumentations'];
|
|
930
930
|
spanProcessor?: SpanProcessor;
|
|
931
931
|
metricReader?: MetricReader;
|
|
932
932
|
logRecordProcessors?: LogRecordProcessor[];
|
|
933
933
|
resource?: Resource;
|
|
934
934
|
resourceAttributes?: Record<string, string>;
|
|
935
|
-
|
|
935
|
+
headers?: Record<string, string> | string;
|
|
936
936
|
sdkFactory?: (defaults: NodeSDK) => NodeSDK;
|
|
937
937
|
validation?: Partial<ValidationConfig>;
|
|
938
938
|
logger?: Logger; // created via createLogger() or bring your own
|
|
@@ -1368,8 +1368,8 @@ Enable automatic span flushing on root span completion:
|
|
|
1368
1368
|
```typescript
|
|
1369
1369
|
init({
|
|
1370
1370
|
service: 'my-lambda',
|
|
1371
|
-
|
|
1372
|
-
|
|
1371
|
+
flushOnRootSpanEnd: true, // enabled by default (events only)
|
|
1372
|
+
forceFlushOnShutdown: true, // flush spans on root completion
|
|
1373
1373
|
});
|
|
1374
1374
|
|
|
1375
1375
|
export const handler = trace(async (event) => {
|
|
@@ -1388,7 +1388,7 @@ export const handler = trace(async (event) => {
|
|
|
1388
1388
|
|
|
1389
1389
|
**When to use:**
|
|
1390
1390
|
|
|
1391
|
-
- Use `
|
|
1391
|
+
- Use `forceFlushOnShutdown: true` for serverless functions where latency is acceptable
|
|
1392
1392
|
- Use manual `flush()` for more control over when flushing occurs
|
|
1393
1393
|
- Use neither for long-running services (batch export is more efficient)
|
|
1394
1394
|
|
package/dist/auto.cjs
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkELIWLU7M_cjs = require('./chunk-ELIWLU7M.cjs');
|
|
4
|
+
var chunk6T2EJMZG_cjs = require('./chunk-6T2EJMZG.cjs');
|
|
5
5
|
require('./chunk-GVLK7YUU.cjs');
|
|
6
|
+
require('./chunk-YS6C2YJE.cjs');
|
|
6
7
|
require('./chunk-HE6T6FIX.cjs');
|
|
7
|
-
require('./chunk-
|
|
8
|
-
require('./chunk-
|
|
8
|
+
require('./chunk-2EKAKQUG.cjs');
|
|
9
|
+
require('./chunk-HA4UB3LG.cjs');
|
|
9
10
|
require('./chunk-URRW6M2C.cjs');
|
|
10
|
-
require('./chunk-
|
|
11
|
+
require('./chunk-JEQ2X3Z6.cjs');
|
|
11
12
|
var module$1 = require('module');
|
|
12
13
|
var importInTheMiddle = require('import-in-the-middle');
|
|
13
14
|
|
|
14
15
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
15
16
|
var { registerOptions } = importInTheMiddle.createAddHookMessageChannel();
|
|
16
17
|
module$1.register("import-in-the-middle/hook.mjs", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('auto.cjs', document.baseURI).href)), registerOptions);
|
|
17
|
-
var yamlConfig =
|
|
18
|
-
var
|
|
19
|
-
var
|
|
20
|
-
|
|
18
|
+
var yamlConfig = chunk6T2EJMZG_cjs.loadYamlConfig();
|
|
19
|
+
var autoInstrumentationsEnv = process.env.AUTOTEL_INTEGRATIONS;
|
|
20
|
+
var autoInstrumentations = autoInstrumentationsEnv === "true" ? true : autoInstrumentationsEnv ? autoInstrumentationsEnv.split(",").map((s) => s.trim()) : yamlConfig?.autoInstrumentations ?? ["http", "express"];
|
|
21
|
+
chunkELIWLU7M_cjs.init({
|
|
21
22
|
service: yamlConfig?.service ?? process.env.OTEL_SERVICE_NAME ?? "unknown-service",
|
|
22
23
|
debug: yamlConfig?.debug ?? process.env.AUTOTEL_DEBUG === "true",
|
|
23
|
-
|
|
24
|
+
autoInstrumentations
|
|
24
25
|
});
|
|
25
26
|
//# sourceMappingURL=auto.cjs.map
|
|
26
27
|
//# sourceMappingURL=auto.cjs.map
|
package/dist/auto.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/auto.ts"],"names":["createAddHookMessageChannel","register","loadYamlConfig","init"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/auto.ts"],"names":["createAddHookMessageChannel","register","loadYamlConfig","init"],"mappings":";;;;;;;;;;;;;;;AAyCA,IAAM,EAAE,eAAA,EAAgB,GAAIA,6CAAA,EAA4B;AACxDC,iBAAA,CAAS,+BAAA,EAAiC,0PAAY,EAAK,eAAe,CAAA;AAG1E,IAAM,aAAaC,gCAAA,EAAe;AAGlC,IAAM,uBAAA,GAA0B,QAAQ,GAAA,CAAI,oBAAA;AAC5C,IAAM,oBAAA,GAIJ,4BAA4B,MAAA,GACxB,IAAA,GACA,0BACE,uBAAA,CAAwB,KAAA,CAAM,GAAG,CAAA,CAAE,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,CAAE,MAAM,CAAA,GACrD,YAAY,oBAAA,IAAwB,CAAC,QAAQ,SAAS,CAAA;AAI/DC,sBAAA,CAAK;AAAA,EACH,OAAA,EACE,UAAA,EAAY,OAAA,IAAW,OAAA,CAAQ,IAAI,iBAAA,IAAqB,iBAAA;AAAA,EAC1D,KAAA,EAAO,UAAA,EAAY,KAAA,IAAS,OAAA,CAAQ,IAAI,aAAA,KAAkB,MAAA;AAAA,EAC1D;AACF,CAAC,CAAA","file":"auto.cjs","sourcesContent":["/**\n * Zero-config ESM instrumentation with auto-init from YAML/environment variables\n *\n * This module provides the simplest possible setup for OpenTelemetry instrumentation.\n * Just import it and everything is configured from autotel.yaml or environment variables.\n *\n * Usage with YAML config (recommended):\n * ```bash\n * # Create autotel.yaml in project root, then:\n * tsx --import autotel/auto src/index.ts\n * ```\n *\n * Usage with environment variables:\n * ```bash\n * OTEL_SERVICE_NAME=my-app tsx --import autotel/auto src/index.ts\n * ```\n *\n * No instrumentation.ts file needed!\n *\n * Configuration Priority (highest to lowest):\n * 1. YAML file (autotel.yaml or AUTOTEL_CONFIG_FILE)\n * 2. Environment variables (OTEL_*, AUTOTEL_*)\n * 3. Built-in defaults\n *\n * Environment Variables:\n * - OTEL_SERVICE_NAME: Service name (required for meaningful traces)\n * - OTEL_EXPORTER_OTLP_ENDPOINT: OTLP collector endpoint (e.g., http://localhost:4318)\n * - OTEL_EXPORTER_OTLP_HEADERS: Auth headers (e.g., x-honeycomb-team=YOUR_KEY)\n * - AUTOTEL_INTEGRATIONS: Comma-separated list or 'true' for all (default: http,express)\n * - AUTOTEL_DEBUG: Set to 'true' to enable console span output\n * - AUTOTEL_CONFIG_FILE: Path to YAML config file (overrides autotel.yaml discovery)\n *\n * @requires Node.js 20.6.0 or later\n */\n\nimport { register } from 'node:module';\nimport { createAddHookMessageChannel } from 'import-in-the-middle';\nimport { init } from './init';\nimport { loadYamlConfig } from './yaml-config';\n\n// Register ESM hooks first (must happen before any instrumented modules load)\nconst { registerOptions } = createAddHookMessageChannel();\nregister('import-in-the-middle/hook.mjs', import.meta.url, registerOptions);\n\n// Load YAML config if present (init.ts will also load it, but we need values here)\nconst yamlConfig = loadYamlConfig();\n\n// Parse auto-instrumentations from environment variable (fallback if not in YAML)\nconst autoInstrumentationsEnv = process.env.AUTOTEL_INTEGRATIONS;\nconst autoInstrumentations:\n | string[]\n | boolean\n | Record<string, { enabled?: boolean }> =\n autoInstrumentationsEnv === 'true'\n ? true // Enable all auto-instrumentations\n : autoInstrumentationsEnv\n ? autoInstrumentationsEnv.split(',').map((s) => s.trim())\n : (yamlConfig?.autoInstrumentations ?? ['http', 'express']); // YAML > default\n\n// Auto-initialize with YAML config merged with env var defaults\n// init() will load YAML again and merge properly, but we pass overrides here\ninit({\n service:\n yamlConfig?.service ?? process.env.OTEL_SERVICE_NAME ?? 'unknown-service',\n debug: yamlConfig?.debug ?? process.env.AUTOTEL_DEBUG === 'true',\n autoInstrumentations,\n});\n"]}
|
package/dist/auto.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import { init } from './chunk-
|
|
2
|
-
import { loadYamlConfig } from './chunk-
|
|
1
|
+
import { init } from './chunk-YJBSLU3K.js';
|
|
2
|
+
import { loadYamlConfig } from './chunk-P3GQ2CGY.js';
|
|
3
3
|
import './chunk-X4RMFFMR.js';
|
|
4
|
+
import './chunk-33WTKH7X.js';
|
|
4
5
|
import './chunk-5R2M36QB.js';
|
|
5
|
-
import './chunk-
|
|
6
|
-
import './chunk-
|
|
6
|
+
import './chunk-TQ3DJJ3N.js';
|
|
7
|
+
import './chunk-4XKOSC37.js';
|
|
7
8
|
import './chunk-P6JUDYNO.js';
|
|
8
|
-
import './chunk-
|
|
9
|
+
import './chunk-DGUM43GV.js';
|
|
9
10
|
import { register } from 'module';
|
|
10
11
|
import { createAddHookMessageChannel } from 'import-in-the-middle';
|
|
11
12
|
|
|
12
13
|
var { registerOptions } = createAddHookMessageChannel();
|
|
13
14
|
register("import-in-the-middle/hook.mjs", import.meta.url, registerOptions);
|
|
14
15
|
var yamlConfig = loadYamlConfig();
|
|
15
|
-
var
|
|
16
|
-
var
|
|
16
|
+
var autoInstrumentationsEnv = process.env.AUTOTEL_INTEGRATIONS;
|
|
17
|
+
var autoInstrumentations = autoInstrumentationsEnv === "true" ? true : autoInstrumentationsEnv ? autoInstrumentationsEnv.split(",").map((s) => s.trim()) : yamlConfig?.autoInstrumentations ?? ["http", "express"];
|
|
17
18
|
init({
|
|
18
19
|
service: yamlConfig?.service ?? process.env.OTEL_SERVICE_NAME ?? "unknown-service",
|
|
19
20
|
debug: yamlConfig?.debug ?? process.env.AUTOTEL_DEBUG === "true",
|
|
20
|
-
|
|
21
|
+
autoInstrumentations
|
|
21
22
|
});
|
|
22
23
|
//# sourceMappingURL=auto.js.map
|
|
23
24
|
//# sourceMappingURL=auto.js.map
|
package/dist/auto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/auto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/auto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAyCA,IAAM,EAAE,eAAA,EAAgB,GAAI,2BAAA,EAA4B;AACxD,QAAA,CAAS,+BAAA,EAAiC,MAAA,CAAA,IAAA,CAAY,GAAA,EAAK,eAAe,CAAA;AAG1E,IAAM,aAAa,cAAA,EAAe;AAGlC,IAAM,uBAAA,GAA0B,QAAQ,GAAA,CAAI,oBAAA;AAC5C,IAAM,oBAAA,GAIJ,4BAA4B,MAAA,GACxB,IAAA,GACA,0BACE,uBAAA,CAAwB,KAAA,CAAM,GAAG,CAAA,CAAE,GAAA,CAAI,CAAC,CAAA,KAAM,CAAA,CAAE,MAAM,CAAA,GACrD,YAAY,oBAAA,IAAwB,CAAC,QAAQ,SAAS,CAAA;AAI/D,IAAA,CAAK;AAAA,EACH,OAAA,EACE,UAAA,EAAY,OAAA,IAAW,OAAA,CAAQ,IAAI,iBAAA,IAAqB,iBAAA;AAAA,EAC1D,KAAA,EAAO,UAAA,EAAY,KAAA,IAAS,OAAA,CAAQ,IAAI,aAAA,KAAkB,MAAA;AAAA,EAC1D;AACF,CAAC,CAAA","file":"auto.js","sourcesContent":["/**\n * Zero-config ESM instrumentation with auto-init from YAML/environment variables\n *\n * This module provides the simplest possible setup for OpenTelemetry instrumentation.\n * Just import it and everything is configured from autotel.yaml or environment variables.\n *\n * Usage with YAML config (recommended):\n * ```bash\n * # Create autotel.yaml in project root, then:\n * tsx --import autotel/auto src/index.ts\n * ```\n *\n * Usage with environment variables:\n * ```bash\n * OTEL_SERVICE_NAME=my-app tsx --import autotel/auto src/index.ts\n * ```\n *\n * No instrumentation.ts file needed!\n *\n * Configuration Priority (highest to lowest):\n * 1. YAML file (autotel.yaml or AUTOTEL_CONFIG_FILE)\n * 2. Environment variables (OTEL_*, AUTOTEL_*)\n * 3. Built-in defaults\n *\n * Environment Variables:\n * - OTEL_SERVICE_NAME: Service name (required for meaningful traces)\n * - OTEL_EXPORTER_OTLP_ENDPOINT: OTLP collector endpoint (e.g., http://localhost:4318)\n * - OTEL_EXPORTER_OTLP_HEADERS: Auth headers (e.g., x-honeycomb-team=YOUR_KEY)\n * - AUTOTEL_INTEGRATIONS: Comma-separated list or 'true' for all (default: http,express)\n * - AUTOTEL_DEBUG: Set to 'true' to enable console span output\n * - AUTOTEL_CONFIG_FILE: Path to YAML config file (overrides autotel.yaml discovery)\n *\n * @requires Node.js 20.6.0 or later\n */\n\nimport { register } from 'node:module';\nimport { createAddHookMessageChannel } from 'import-in-the-middle';\nimport { init } from './init';\nimport { loadYamlConfig } from './yaml-config';\n\n// Register ESM hooks first (must happen before any instrumented modules load)\nconst { registerOptions } = createAddHookMessageChannel();\nregister('import-in-the-middle/hook.mjs', import.meta.url, registerOptions);\n\n// Load YAML config if present (init.ts will also load it, but we need values here)\nconst yamlConfig = loadYamlConfig();\n\n// Parse auto-instrumentations from environment variable (fallback if not in YAML)\nconst autoInstrumentationsEnv = process.env.AUTOTEL_INTEGRATIONS;\nconst autoInstrumentations:\n | string[]\n | boolean\n | Record<string, { enabled?: boolean }> =\n autoInstrumentationsEnv === 'true'\n ? true // Enable all auto-instrumentations\n : autoInstrumentationsEnv\n ? autoInstrumentationsEnv.split(',').map((s) => s.trim())\n : (yamlConfig?.autoInstrumentations ?? ['http', 'express']); // YAML > default\n\n// Auto-initialize with YAML config merged with env var defaults\n// init() will load YAML again and merge properly, but we pass overrides here\ninit({\n service:\n yamlConfig?.service ?? process.env.OTEL_SERVICE_NAME ?? 'unknown-service',\n debug: yamlConfig?.debug ?? process.env.AUTOTEL_DEBUG === 'true',\n autoInstrumentations,\n});\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkHA4UB3LG_cjs = require('./chunk-HA4UB3LG.cjs');
|
|
4
4
|
var api = require('@opentelemetry/api');
|
|
5
5
|
|
|
6
6
|
var LOG_LEVEL_KEY = api.createContextKey("autotel-log-level");
|
|
@@ -105,7 +105,7 @@ function LoggedOperation(operationNameOrOptions) {
|
|
|
105
105
|
return async function(...args) {
|
|
106
106
|
const log = this.deps?.log;
|
|
107
107
|
const startTime = performance.now();
|
|
108
|
-
const config =
|
|
108
|
+
const config = chunkHA4UB3LG_cjs.getConfig();
|
|
109
109
|
const tracer = config.tracer;
|
|
110
110
|
return tracer.startActiveSpan(operationName, async (span) => {
|
|
111
111
|
try {
|
|
@@ -163,5 +163,5 @@ exports.createBuiltinLogger = createBuiltinLogger;
|
|
|
163
163
|
exports.getActiveLogLevel = getActiveLogLevel;
|
|
164
164
|
exports.getTraceContext = getTraceContext;
|
|
165
165
|
exports.runWithLogLevel = runWithLogLevel;
|
|
166
|
-
//# sourceMappingURL=chunk-
|
|
167
|
-
//# sourceMappingURL=chunk-
|
|
166
|
+
//# sourceMappingURL=chunk-2EKAKQUG.cjs.map
|
|
167
|
+
//# sourceMappingURL=chunk-2EKAKQUG.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/autotel-logger.ts","../src/logger.ts"],"names":["createContextKey","api_context","trace","getConfig","SpanStatusCode"],"mappings":";;;;;AAwCA,IAAM,aAAA,GAAgBA,qBAAiB,mBAAmB,CAAA;AAMnD,SAAS,iBAAA,GAAiD;AAC/D,EAAA,OAAOC,WAAA,CAAY,MAAA,EAAO,CAAE,QAAA,CAAS,aAAa,CAAA;AAGpD;AAoBO,SAAS,eAAA,CACd,OACA,QAAA,EACG;AACH,EAAA,MAAM,MAAMA,WAAA,CAAY,MAAA,EAAO,CAAE,QAAA,CAAS,eAAe,KAAK,CAAA;AAC9D,EAAA,OAAOA,WAAA,CAAY,IAAA,CAAK,GAAA,EAAK,QAAQ,CAAA;AACvC;AAMA,SAAS,uBAAA,GAIA;AACP,EAAA,MAAM,IAAA,GAAOC,UAAM,aAAA,EAAc;AACjC,EAAA,IAAI,CAAC,MAAM,OAAO,IAAA;AAElB,EAAA,MAAM,GAAA,GAAM,KAAK,WAAA,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AAA,CAAK,GAAA,EAAI;AAAA,QACX;AAAA,MACF,CAAC,CAAA;AAAA,IACH,CAAA;AAAA,EACF,CAAA;AACF","file":"chunk-DPP5Y5XN.cjs","sourcesContent":["/**\n * Zero-dependency structured logger for autotel\n *\n * This logger provides:\n * - Structured JSON logging (production) or pretty print (development)\n * - Auto trace context injection (traceId, spanId, correlationId)\n * - Dynamic log level control (per-request via OTel context)\n * - Level support (debug, info, warn, error, none)\n * - Zero additional dependencies (uses @opentelemetry/api, already a dep)\n *\n * Used as the default fallback when users don't provide Pino/Winston.\n * Can also be used directly: import { createBuiltinLogger } from 'autotel/logger'\n *\n * @example\n * ```typescript\n * import { createBuiltinLogger, runWithLogLevel } from 'autotel/logger';\n *\n * const log = createBuiltinLogger('my-service');\n * log.info('User created', { userId: '123' });\n * // Output: {\"level\":\"info\",\"service\":\"my-service\",\"msg\":\"User created\",\"userId\":\"123\",\"traceId\":\"...\"}\n *\n * // Dynamic log level per-request\n * runWithLogLevel('debug', () => {\n * log.debug('This will log even if default level is \"info\"');\n * });\n * ```\n */\n\nimport {\n trace,\n context as api_context,\n createContextKey,\n} from '@opentelemetry/api';\nimport type { Logger } from './logger';\n\nexport type BuiltinLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';\n\n/**\n * Context key for storing active log level (enables per-request log levels)\n */\nconst LOG_LEVEL_KEY = createContextKey('autotel-log-level');\n\n/**\n * Get the active log level from context (if set)\n * Falls back to undefined if no log level is set in context\n */\nexport function getActiveLogLevel(): BuiltinLogLevel | undefined {\n return api_context.active().getValue(LOG_LEVEL_KEY) as\n | BuiltinLogLevel\n | undefined;\n}\n\n/**\n * Run a function with a specific log level\n * The log level is stored in OpenTelemetry context and applies to all logger calls within the callback\n *\n * @example\n * ```typescript\n * // Enable debug logging for a specific request\n * runWithLogLevel('debug', () => {\n * log.debug('This will be logged');\n * processRequest();\n * });\n *\n * // Disable logging temporarily\n * runWithLogLevel('none', () => {\n * log.info('This will NOT be logged');\n * });\n * ```\n */\nexport function runWithLogLevel<T>(\n level: BuiltinLogLevel,\n callback: () => T,\n): T {\n const ctx = api_context.active().setValue(LOG_LEVEL_KEY, level);\n return api_context.with(ctx, callback);\n}\n\n/**\n * Get current trace context from active span\n * Returns null if no active span exists\n */\nfunction getTraceContextInternal(): {\n traceId: string;\n spanId: string;\n correlationId: string;\n} | null {\n const span = trace.getActiveSpan();\n if (!span) return null;\n\n const ctx = span.spanContext();\n return {\n traceId: ctx.traceId,\n spanId: ctx.spanId,\n correlationId: ctx.traceId.slice(0, 16), // First 16 chars for grouping\n };\n}\n\n/**\n * Helper to get trace context (useful for BYOL - Bring Your Own Logger)\n *\n * @example\n * ```typescript\n * import bunyan from 'bunyan';\n * import { getTraceContext } from 'autotel/logger';\n *\n * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });\n * const ctx = getTraceContext();\n * bunyanLogger.info({ ...ctx, email: 'test@example.com' }, 'Creating user');\n * ```\n */\nexport function getTraceContext() {\n return getTraceContextInternal();\n}\n\nexport interface BuiltinLoggerOptions {\n /** Minimum log level. Default: 'info' */\n level?: BuiltinLogLevel;\n /** Pretty print for development. Default: false (JSON output) */\n pretty?: boolean;\n}\n\n/**\n * Create a lightweight structured logger\n *\n * @param service - Service name for logging\n * @param options - Optional configuration\n *\n * @example\n * ```typescript\n * const log = createBuiltinLogger('user-service');\n *\n * log.info('Creating user', { email: 'test@example.com' });\n * // Output: {\"level\":\"info\",\"service\":\"user-service\",\"msg\":\"Creating user\",\n * // \"email\":\"test@example.com\",\"traceId\":\"...\",\"spanId\":\"...\"}\n *\n * // Dynamic log level control per-request\n * runWithLogLevel('debug', () => {\n * log.debug('This will be logged even if logger was created with level: \"info\"');\n * });\n * ```\n */\nexport function createBuiltinLogger(\n service: string,\n options?: BuiltinLoggerOptions,\n): Logger {\n const defaultLevel = options?.level || 'info';\n const pretty = options?.pretty || false;\n\n const levelPriority: Record<BuiltinLogLevel, number> = {\n none: -1,\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n };\n\n const shouldLog = (level: BuiltinLogLevel): boolean => {\n // Priority: context level > options level > 'info' default\n const activeLevel = getActiveLogLevel() ?? defaultLevel;\n\n // 'none' means suppress all logging\n if (activeLevel === 'none') return false;\n\n return levelPriority[level] >= levelPriority[activeLevel];\n };\n\n const log = (\n level: 'info' | 'error' | 'warn' | 'debug',\n msg: string,\n attrs?: Record<string, unknown>,\n ) => {\n if (!shouldLog(level)) return;\n\n const ctx = getTraceContextInternal();\n const logEntry: Record<string, unknown> = {\n level,\n service,\n msg,\n ...attrs,\n ...ctx, // Auto-inject traceId, spanId, correlationId\n timestamp: new Date().toISOString(),\n };\n\n if (pretty) {\n // Pretty print for development\n const traceInfo = ctx\n ? ` [${ctx.traceId.slice(0, 8)}.../${ctx.spanId.slice(0, 8)}...]`\n : '';\n console.log(\n `[${level.toUpperCase()}]${traceInfo} ${service}: ${msg}`,\n attrs || '',\n );\n } else {\n // Structured JSON for production\n console.log(JSON.stringify(logEntry));\n }\n };\n\n return {\n info: (msg: string, attrs?: Record<string, unknown>) =>\n log('info', msg, attrs),\n\n error: (\n msg: string,\n error?: Error | unknown,\n attrs?: Record<string, unknown>,\n ) => {\n let errorAttrs: Record<string, unknown> | undefined;\n if (error instanceof Error) {\n errorAttrs = {\n error: error.message,\n stack: error.stack,\n name: error.name,\n ...attrs,\n };\n } else if (error === undefined) {\n errorAttrs = attrs;\n } else {\n errorAttrs = { error: String(error), ...attrs };\n }\n\n log('error', msg, errorAttrs);\n },\n\n warn: (msg: string, attrs?: Record<string, unknown>) =>\n log('warn', msg, attrs),\n\n debug: (msg: string, attrs?: Record<string, unknown>) =>\n log('debug', msg, attrs),\n };\n}\n\n/**\n * Pino-like factory function for creating an autotel logger\n *\n * @example\n * ```typescript\n * import { autotelLogger } from 'autotel/logger';\n *\n * const log = autotelLogger();\n * log.info('User created', { userId: '123' });\n *\n * // With options\n * const log = autotelLogger({ service: 'my-app', level: 'debug', pretty: true });\n * ```\n */\nexport function autotelLogger(options?: {\n service?: string;\n level?: BuiltinLogLevel;\n pretty?: boolean;\n}): Logger {\n return createBuiltinLogger(options?.service || 'app', {\n level: options?.level,\n pretty: options?.pretty,\n });\n}\n","/**\n * Logger types and utilities for autotel\n *\n * **Zero-Config Option:** Don't provide a logger to `init()` and autotel uses\n * a built-in structured JSON logger with automatic trace context injection.\n *\n * **BYOL (Bring Your Own Logger):** Pass Pino or Winston to `init()` for\n * automatic instrumentation with trace context and OTLP log export.\n *\n * @example Zero-config (uses built-in logger)\n * ```typescript\n * import { init } from 'autotel';\n *\n * init({ service: 'my-app' });\n * // Internal logs: {\"level\":\"info\",\"service\":\"my-app\",\"msg\":\"...\",\"traceId\":\"...\"}\n * ```\n *\n * @example Using built-in logger directly\n * ```typescript\n * import { createBuiltinLogger, runWithLogLevel } from 'autotel/logger';\n *\n * const log = createBuiltinLogger('my-service');\n * log.info('User created', { userId: '123' });\n * // Output: {\"level\":\"info\",\"service\":\"my-service\",\"msg\":\"User created\",...,\"traceId\":\"...\"}\n *\n * // Dynamic log level per-request\n * runWithLogLevel('debug', () => {\n * log.debug('Debug info for this request only');\n * });\n * ```\n *\n * @example Using Pino (recommended for production, auto-instrumented)\n * ```typescript\n * import pino from 'pino'; // npm install pino\n * import { init } from 'autotel';\n *\n * const logger = pino({ level: 'info' });\n * init({ service: 'my-app', logger });\n *\n * // Logs automatically include traceId/spanId and export via OTLP!\n * logger.info('User created', { userId: '123' });\n * ```\n *\n * @example Using Winston (auto-instrumented)\n * ```typescript\n * import winston from 'winston'; // npm install winston\n * import { init } from 'autotel';\n *\n * const logger = winston.createLogger({\n * level: 'info',\n * format: winston.format.json(),\n * transports: [new winston.transports.Console()]\n * });\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example Using Bunyan (manual instrumentation)\n * ```typescript\n * import bunyan from 'bunyan'; // npm install bunyan @opentelemetry/instrumentation-bunyan\n * import { init } from 'autotel';\n * import { BunyanInstrumentation } from '@opentelemetry/instrumentation-bunyan';\n *\n * const logger = bunyan.createLogger({ name: 'my-app' });\n * init({\n * service: 'my-app',\n * logger,\n * instrumentations: [new BunyanInstrumentation()]\n * });\n * ```\n *\n * @example Custom logger (any logger with 4 methods)\n * ```typescript\n * const logger = {\n * info: (msg, extra) => console.log(msg, extra),\n * warn: (msg, extra) => console.warn(msg, extra),\n * error: (msg, err, extra) => console.error(msg, err, extra),\n * debug: (msg, extra) => console.debug(msg, extra),\n * };\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example BYOL helper: inject trace context into any logger\n * ```typescript\n * import bunyan from 'bunyan';\n * import { getTraceContext } from 'autotel/logger';\n *\n * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });\n * const ctx = getTraceContext();\n * bunyanLogger.info({ ...ctx, userId: '123' }, 'Creating user');\n * ```\n */\n\nimport { SpanStatusCode } from '@opentelemetry/api';\nimport { getConfig } from './config';\n\n// ============================================================================\n// Logger Types\n// ============================================================================\n\n/**\n * Log level constants\n */\nexport const LOG_LEVEL = {\n DEBUG: 'debug',\n INFO: 'info',\n WARN: 'warn',\n ERROR: 'error',\n} as const;\n\nexport type LogLevel = (typeof LOG_LEVEL)[keyof typeof LOG_LEVEL];\n\n/**\n * Logger configuration (for reference - not needed with BYOL approach)\n */\nexport interface LoggerConfig {\n service: string;\n level?: LogLevel;\n pretty?: boolean;\n redact?: string[] | false;\n}\n\n/**\n * Simple logger interface - minimal contract for any logger\n *\n * Bring your own Pino, Winston, or any logger with these 4 methods.\n * Autotel automatically instruments Pino and Winston loggers to:\n * - Inject trace context (traceId, spanId) into log records\n * - Record errors in the active span\n * - Bridge logs to OpenTelemetry Logs API for OTLP export\n *\n * @example Using Pino\n * ```typescript\n * import pino from 'pino';\n * const logger = pino({ level: 'info' });\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example Using Winston\n * ```typescript\n * import winston from 'winston';\n * const logger = winston.createLogger({ level: 'info' });\n * init({ service: 'my-app', logger });\n * ```\n */\nexport interface Logger {\n info(message: string, extra?: Record<string, unknown>): void;\n warn(message: string, extra?: Record<string, unknown>): void;\n error(message: string, error?: Error, extra?: Record<string, unknown>): void;\n debug(message: string, extra?: Record<string, unknown>): void;\n}\n\n/**\n * Alias for Logger interface (backwards compatibility)\n * @deprecated Use Logger instead\n */\nexport type ILogger = Logger;\n\n/**\n * Pino logger type - re-exported for convenience\n *\n * Note: This is a type-only export. To use Pino, install it as a peer dependency:\n * `npm install pino`\n */\nexport type { Logger as PinoLogger } from 'pino';\n\n// ============================================================================\n// LoggedOperation Decorator\n// ============================================================================\n\nexport interface LoggedOperationOptions {\n /** Operation name for tracing (e.g., 'user.createUser') */\n operationName: string;\n}\n\n/**\n * TS5+ Standard Decorator for logging and tracing operations\n * Uses TC39 Stage 3 decorator syntax\n *\n * This is the traditional per-method decorator approach.\n * For zero-boilerplate solution, see @Instrumented class decorator.\n *\n * @example\n * // Simple usage\n * class OrderService {\n * constructor(private readonly deps: { log: Logger }) {}\n *\n * @LoggedOperation('order.create')\n * async createOrder(data: CreateOrderData) {\n * this.deps.logger.info('Creating order', data)\n * }\n * }\n *\n * // Advanced usage (future-proof for options)\n * @LoggedOperation({ operationName: 'order.create' })\n * async createOrder(data: CreateOrderData) { }\n */\nexport function LoggedOperation(\n operationNameOrOptions: string | LoggedOperationOptions,\n) {\n const operationName =\n typeof operationNameOrOptions === 'string'\n ? operationNameOrOptions\n : operationNameOrOptions.operationName;\n\n return function <This, Args extends unknown[], Return>(\n originalMethod: (this: This, ...args: Args) => Promise<Return>,\n context: ClassMethodDecoratorContext<\n This,\n (this: This, ...args: Args) => Promise<Return>\n >,\n ) {\n const methodName = String(context.name);\n\n return async function (this: This, ...args: Args): Promise<Return> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const log = (this as any).deps?.log;\n const startTime = performance.now();\n\n const config = getConfig();\n const tracer = config.tracer;\n\n return tracer.startActiveSpan(operationName, async (span) => {\n try {\n log?.info('Operation started', {\n operation: operationName,\n method: methodName,\n args,\n });\n\n const result = await originalMethod.apply(this, args);\n\n const duration = performance.now() - startTime;\n log?.info('Operation completed', {\n operation: operationName,\n method: methodName,\n duration,\n });\n\n span.setStatus({ code: SpanStatusCode.OK });\n span.setAttributes({\n 'operation.name': operationName,\n 'operation.method': methodName,\n 'operation.duration': duration,\n 'operation.success': true,\n });\n\n return result;\n } catch (error) {\n const duration = performance.now() - startTime;\n log?.error(\n 'Operation failed',\n error instanceof Error ? error : undefined,\n { operation: operationName, method: methodName, duration },\n );\n\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error instanceof Error ? error.message : 'Unknown error',\n });\n span.setAttributes({\n 'operation.name': operationName,\n 'operation.method': methodName,\n 'operation.duration': duration,\n 'operation.success': false,\n 'error.type':\n error instanceof Error ? error.constructor.name : 'Unknown',\n });\n\n throw error;\n } finally {\n span.end();\n }\n });\n };\n };\n}\n\n// ============================================================================\n// Built-in Logger (re-exports)\n// ============================================================================\n\nexport {\n autotelLogger,\n createBuiltinLogger,\n runWithLogLevel,\n getTraceContext,\n getActiveLogLevel,\n type BuiltinLogLevel,\n type BuiltinLoggerOptions,\n} from './autotel-logger';\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/autotel-logger.ts","../src/logger.ts"],"names":["createContextKey","api_context","trace","getConfig","SpanStatusCode"],"mappings":";;;;;AAwCA,IAAM,aAAA,GAAgBA,qBAAiB,mBAAmB,CAAA;AAMnD,SAAS,iBAAA,GAAiD;AAC/D,EAAA,OAAOC,WAAA,CAAY,MAAA,EAAO,CAAE,QAAA,CAAS,aAAa,CAAA;AAGpD;AAoBO,SAAS,eAAA,CACd,OACA,QAAA,EACG;AACH,EAAA,MAAM,MAAMA,WAAA,CAAY,MAAA,EAAO,CAAE,QAAA,CAAS,eAAe,KAAK,CAAA;AAC9D,EAAA,OAAOA,WAAA,CAAY,IAAA,CAAK,GAAA,EAAK,QAAQ,CAAA;AACvC;AAMA,SAAS,uBAAA,GAIA;AACP,EAAA,MAAM,IAAA,GAAOC,UAAM,aAAA,EAAc;AACjC,EAAA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AA,WACrD,CAAA;AAED,UAAA,MAAM,KAAA;AAAA,QACR,CAAA,SAAE;AACA,UAAA,IAAA,CAAK,GAAA,EAAI;AAAA,QACX;AAAA,MACF,CAAC,CAAA;AAAA,IACH,CAAA;AAAA,EACF,CAAA;AACF","file":"chunk-2EKAKQUG.cjs","sourcesContent":["/**\n * Zero-dependency structured logger for autotel\n *\n * This logger provides:\n * - Structured JSON logging (production) or pretty print (development)\n * - Auto trace context injection (traceId, spanId, correlationId)\n * - Dynamic log level control (per-request via OTel context)\n * - Level support (debug, info, warn, error, none)\n * - Zero additional dependencies (uses @opentelemetry/api, already a dep)\n *\n * Used as the default fallback when users don't provide Pino/Winston.\n * Can also be used directly: import { createBuiltinLogger } from 'autotel/logger'\n *\n * @example\n * ```typescript\n * import { createBuiltinLogger, runWithLogLevel } from 'autotel/logger';\n *\n * const log = createBuiltinLogger('my-service');\n * log.info('User created', { userId: '123' });\n * // Output: {\"level\":\"info\",\"service\":\"my-service\",\"msg\":\"User created\",\"userId\":\"123\",\"traceId\":\"...\"}\n *\n * // Dynamic log level per-request\n * runWithLogLevel('debug', () => {\n * log.debug('This will log even if default level is \"info\"');\n * });\n * ```\n */\n\nimport {\n trace,\n context as api_context,\n createContextKey,\n} from '@opentelemetry/api';\nimport type { Logger } from './logger';\n\nexport type BuiltinLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';\n\n/**\n * Context key for storing active log level (enables per-request log levels)\n */\nconst LOG_LEVEL_KEY = createContextKey('autotel-log-level');\n\n/**\n * Get the active log level from context (if set)\n * Falls back to undefined if no log level is set in context\n */\nexport function getActiveLogLevel(): BuiltinLogLevel | undefined {\n return api_context.active().getValue(LOG_LEVEL_KEY) as\n | BuiltinLogLevel\n | undefined;\n}\n\n/**\n * Run a function with a specific log level\n * The log level is stored in OpenTelemetry context and applies to all logger calls within the callback\n *\n * @example\n * ```typescript\n * // Enable debug logging for a specific request\n * runWithLogLevel('debug', () => {\n * log.debug('This will be logged');\n * processRequest();\n * });\n *\n * // Disable logging temporarily\n * runWithLogLevel('none', () => {\n * log.info('This will NOT be logged');\n * });\n * ```\n */\nexport function runWithLogLevel<T>(\n level: BuiltinLogLevel,\n callback: () => T,\n): T {\n const ctx = api_context.active().setValue(LOG_LEVEL_KEY, level);\n return api_context.with(ctx, callback);\n}\n\n/**\n * Get current trace context from active span\n * Returns null if no active span exists\n */\nfunction getTraceContextInternal(): {\n traceId: string;\n spanId: string;\n correlationId: string;\n} | null {\n const span = trace.getActiveSpan();\n if (!span) return null;\n\n const ctx = span.spanContext();\n return {\n traceId: ctx.traceId,\n spanId: ctx.spanId,\n correlationId: ctx.traceId.slice(0, 16), // First 16 chars for grouping\n };\n}\n\n/**\n * Helper to get trace context (useful for BYOL - Bring Your Own Logger)\n *\n * @example\n * ```typescript\n * import bunyan from 'bunyan';\n * import { getTraceContext } from 'autotel/logger';\n *\n * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });\n * const ctx = getTraceContext();\n * bunyanLogger.info({ ...ctx, email: 'test@example.com' }, 'Creating user');\n * ```\n */\nexport function getTraceContext() {\n return getTraceContextInternal();\n}\n\nexport interface BuiltinLoggerOptions {\n /** Minimum log level. Default: 'info' */\n level?: BuiltinLogLevel;\n /** Pretty print for development. Default: false (JSON output) */\n pretty?: boolean;\n}\n\n/**\n * Create a lightweight structured logger\n *\n * @param service - Service name for logging\n * @param options - Optional configuration\n *\n * @example\n * ```typescript\n * const log = createBuiltinLogger('user-service');\n *\n * log.info('Creating user', { email: 'test@example.com' });\n * // Output: {\"level\":\"info\",\"service\":\"user-service\",\"msg\":\"Creating user\",\n * // \"email\":\"test@example.com\",\"traceId\":\"...\",\"spanId\":\"...\"}\n *\n * // Dynamic log level control per-request\n * runWithLogLevel('debug', () => {\n * log.debug('This will be logged even if logger was created with level: \"info\"');\n * });\n * ```\n */\nexport function createBuiltinLogger(\n service: string,\n options?: BuiltinLoggerOptions,\n): Logger {\n const defaultLevel = options?.level || 'info';\n const pretty = options?.pretty || false;\n\n const levelPriority: Record<BuiltinLogLevel, number> = {\n none: -1,\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n };\n\n const shouldLog = (level: BuiltinLogLevel): boolean => {\n // Priority: context level > options level > 'info' default\n const activeLevel = getActiveLogLevel() ?? defaultLevel;\n\n // 'none' means suppress all logging\n if (activeLevel === 'none') return false;\n\n return levelPriority[level] >= levelPriority[activeLevel];\n };\n\n const log = (\n level: 'info' | 'error' | 'warn' | 'debug',\n msg: string,\n attrs?: Record<string, unknown>,\n ) => {\n if (!shouldLog(level)) return;\n\n const ctx = getTraceContextInternal();\n const logEntry: Record<string, unknown> = {\n level,\n service,\n msg,\n ...attrs,\n ...ctx, // Auto-inject traceId, spanId, correlationId\n timestamp: new Date().toISOString(),\n };\n\n if (pretty) {\n // Pretty print for development\n const traceInfo = ctx\n ? ` [${ctx.traceId.slice(0, 8)}.../${ctx.spanId.slice(0, 8)}...]`\n : '';\n console.log(\n `[${level.toUpperCase()}]${traceInfo} ${service}: ${msg}`,\n attrs || '',\n );\n } else {\n // Structured JSON for production\n console.log(JSON.stringify(logEntry));\n }\n };\n\n return {\n info: (msg: string, attrs?: Record<string, unknown>) =>\n log('info', msg, attrs),\n\n error: (\n msg: string,\n error?: Error | unknown,\n attrs?: Record<string, unknown>,\n ) => {\n let errorAttrs: Record<string, unknown> | undefined;\n if (error instanceof Error) {\n errorAttrs = {\n error: error.message,\n stack: error.stack,\n name: error.name,\n ...attrs,\n };\n } else if (error === undefined) {\n errorAttrs = attrs;\n } else {\n errorAttrs = { error: String(error), ...attrs };\n }\n\n log('error', msg, errorAttrs);\n },\n\n warn: (msg: string, attrs?: Record<string, unknown>) =>\n log('warn', msg, attrs),\n\n debug: (msg: string, attrs?: Record<string, unknown>) =>\n log('debug', msg, attrs),\n };\n}\n\n/**\n * Pino-like factory function for creating an autotel logger\n *\n * @example\n * ```typescript\n * import { autotelLogger } from 'autotel/logger';\n *\n * const log = autotelLogger();\n * log.info('User created', { userId: '123' });\n *\n * // With options\n * const log = autotelLogger({ service: 'my-app', level: 'debug', pretty: true });\n * ```\n */\nexport function autotelLogger(options?: {\n service?: string;\n level?: BuiltinLogLevel;\n pretty?: boolean;\n}): Logger {\n return createBuiltinLogger(options?.service || 'app', {\n level: options?.level,\n pretty: options?.pretty,\n });\n}\n","/**\n * Logger types and utilities for autotel\n *\n * **Zero-Config Option:** Don't provide a logger to `init()` and autotel uses\n * a built-in structured JSON logger with automatic trace context injection.\n *\n * **BYOL (Bring Your Own Logger):** Pass Pino or Winston to `init()` for\n * automatic instrumentation with trace context and OTLP log export.\n *\n * @example Zero-config (uses built-in logger)\n * ```typescript\n * import { init } from 'autotel';\n *\n * init({ service: 'my-app' });\n * // Internal logs: {\"level\":\"info\",\"service\":\"my-app\",\"msg\":\"...\",\"traceId\":\"...\"}\n * ```\n *\n * @example Using built-in logger directly\n * ```typescript\n * import { createBuiltinLogger, runWithLogLevel } from 'autotel/logger';\n *\n * const log = createBuiltinLogger('my-service');\n * log.info('User created', { userId: '123' });\n * // Output: {\"level\":\"info\",\"service\":\"my-service\",\"msg\":\"User created\",...,\"traceId\":\"...\"}\n *\n * // Dynamic log level per-request\n * runWithLogLevel('debug', () => {\n * log.debug('Debug info for this request only');\n * });\n * ```\n *\n * @example Using Pino (recommended for production, auto-instrumented)\n * ```typescript\n * import pino from 'pino'; // npm install pino\n * import { init } from 'autotel';\n *\n * const logger = pino({ level: 'info' });\n * init({ service: 'my-app', logger });\n *\n * // Logs automatically include traceId/spanId and export via OTLP!\n * logger.info('User created', { userId: '123' });\n * ```\n *\n * @example Using Winston (auto-instrumented)\n * ```typescript\n * import winston from 'winston'; // npm install winston\n * import { init } from 'autotel';\n *\n * const logger = winston.createLogger({\n * level: 'info',\n * format: winston.format.json(),\n * transports: [new winston.transports.Console()]\n * });\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example Using Bunyan (manual instrumentation)\n * ```typescript\n * import bunyan from 'bunyan'; // npm install bunyan @opentelemetry/instrumentation-bunyan\n * import { init } from 'autotel';\n * import { BunyanInstrumentation } from '@opentelemetry/instrumentation-bunyan';\n *\n * const logger = bunyan.createLogger({ name: 'my-app' });\n * init({\n * service: 'my-app',\n * logger,\n * instrumentations: [new BunyanInstrumentation()]\n * });\n * ```\n *\n * @example Custom logger (any logger with 4 methods)\n * ```typescript\n * const logger = {\n * info: (msg, extra) => console.log(msg, extra),\n * warn: (msg, extra) => console.warn(msg, extra),\n * error: (msg, err, extra) => console.error(msg, err, extra),\n * debug: (msg, extra) => console.debug(msg, extra),\n * };\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example BYOL helper: inject trace context into any logger\n * ```typescript\n * import bunyan from 'bunyan';\n * import { getTraceContext } from 'autotel/logger';\n *\n * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });\n * const ctx = getTraceContext();\n * bunyanLogger.info({ ...ctx, userId: '123' }, 'Creating user');\n * ```\n */\n\nimport { SpanStatusCode } from '@opentelemetry/api';\nimport { getConfig } from './config';\n\n// ============================================================================\n// Logger Types\n// ============================================================================\n\n/**\n * Log level constants\n */\nexport const LOG_LEVEL = {\n DEBUG: 'debug',\n INFO: 'info',\n WARN: 'warn',\n ERROR: 'error',\n} as const;\n\nexport type LogLevel = (typeof LOG_LEVEL)[keyof typeof LOG_LEVEL];\n\n/**\n * Logger configuration (for reference - not needed with BYOL approach)\n */\nexport interface LoggerConfig {\n service: string;\n level?: LogLevel;\n pretty?: boolean;\n redact?: string[] | false;\n}\n\n/**\n * Simple logger interface - minimal contract for any logger\n *\n * Bring your own Pino, Winston, or any logger with these 4 methods.\n * Autotel automatically instruments Pino and Winston loggers to:\n * - Inject trace context (traceId, spanId) into log records\n * - Record errors in the active span\n * - Bridge logs to OpenTelemetry Logs API for OTLP export\n *\n * @example Using Pino\n * ```typescript\n * import pino from 'pino';\n * const logger = pino({ level: 'info' });\n * init({ service: 'my-app', logger });\n * ```\n *\n * @example Using Winston\n * ```typescript\n * import winston from 'winston';\n * const logger = winston.createLogger({ level: 'info' });\n * init({ service: 'my-app', logger });\n * ```\n */\nexport interface Logger {\n info(message: string, extra?: Record<string, unknown>): void;\n warn(message: string, extra?: Record<string, unknown>): void;\n error(message: string, error?: Error, extra?: Record<string, unknown>): void;\n debug(message: string, extra?: Record<string, unknown>): void;\n}\n\n/**\n * Alias for Logger interface (backwards compatibility)\n * @deprecated Use Logger instead\n */\nexport type ILogger = Logger;\n\n/**\n * Pino logger type - re-exported for convenience\n *\n * Note: This is a type-only export. To use Pino, install it as a peer dependency:\n * `npm install pino`\n */\nexport type { Logger as PinoLogger } from 'pino';\n\n// ============================================================================\n// LoggedOperation Decorator\n// ============================================================================\n\nexport interface LoggedOperationOptions {\n /** Operation name for tracing (e.g., 'user.createUser') */\n operationName: string;\n}\n\n/**\n * TS5+ Standard Decorator for logging and tracing operations\n * Uses TC39 Stage 3 decorator syntax\n *\n * This is the traditional per-method decorator approach.\n * For zero-boilerplate solution, see @Instrumented class decorator.\n *\n * @example\n * // Simple usage\n * class OrderService {\n * constructor(private readonly deps: { log: Logger }) {}\n *\n * @LoggedOperation('order.create')\n * async createOrder(data: CreateOrderData) {\n * this.deps.logger.info('Creating order', data)\n * }\n * }\n *\n * // Advanced usage (future-proof for options)\n * @LoggedOperation({ operationName: 'order.create' })\n * async createOrder(data: CreateOrderData) { }\n */\nexport function LoggedOperation(\n operationNameOrOptions: string | LoggedOperationOptions,\n) {\n const operationName =\n typeof operationNameOrOptions === 'string'\n ? operationNameOrOptions\n : operationNameOrOptions.operationName;\n\n return function <This, Args extends unknown[], Return>(\n originalMethod: (this: This, ...args: Args) => Promise<Return>,\n context: ClassMethodDecoratorContext<\n This,\n (this: This, ...args: Args) => Promise<Return>\n >,\n ) {\n const methodName = String(context.name);\n\n return async function (this: This, ...args: Args): Promise<Return> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const log = (this as any).deps?.log;\n const startTime = performance.now();\n\n const config = getConfig();\n const tracer = config.tracer;\n\n return tracer.startActiveSpan(operationName, async (span) => {\n try {\n log?.info('Operation started', {\n operation: operationName,\n method: methodName,\n args,\n });\n\n const result = await originalMethod.apply(this, args);\n\n const duration = performance.now() - startTime;\n log?.info('Operation completed', {\n operation: operationName,\n method: methodName,\n duration,\n });\n\n span.setStatus({ code: SpanStatusCode.OK });\n span.setAttributes({\n 'operation.name': operationName,\n 'operation.method': methodName,\n 'operation.duration': duration,\n 'operation.success': true,\n });\n\n return result;\n } catch (error) {\n const duration = performance.now() - startTime;\n log?.error(\n 'Operation failed',\n error instanceof Error ? error : undefined,\n { operation: operationName, method: methodName, duration },\n );\n\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error instanceof Error ? error.message : 'Unknown error',\n });\n span.setAttributes({\n 'operation.name': operationName,\n 'operation.method': methodName,\n 'operation.duration': duration,\n 'operation.success': false,\n 'error.type':\n error instanceof Error ? error.constructor.name : 'Unknown',\n });\n\n throw error;\n } finally {\n span.end();\n }\n });\n };\n };\n}\n\n// ============================================================================\n// Built-in Logger (re-exports)\n// ============================================================================\n\nexport {\n autotelLogger,\n createBuiltinLogger,\n runWithLogLevel,\n getTraceContext,\n getActiveLogLevel,\n type BuiltinLogLevel,\n type BuiltinLoggerOptions,\n} from './autotel-logger';\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __require } from './chunk-DGUM43GV.js';
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
|
|
4
|
+
var nodeRequire = typeof __require === "undefined" ? createRequire(import.meta.url) : __require;
|
|
5
|
+
function safeRequire(id) {
|
|
6
|
+
try {
|
|
7
|
+
return nodeRequire(id);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error && error.code === "MODULE_NOT_FOUND") {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function requireModule(id) {
|
|
16
|
+
return nodeRequire(id);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { requireModule, safeRequire };
|
|
20
|
+
//# sourceMappingURL=chunk-33WTKH7X.js.map
|
|
21
|
+
//# sourceMappingURL=chunk-33WTKH7X.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/node-require.ts"],"names":[],"mappings":";;;AAgBA,IAAM,cACJ,OAAO,SAAA,KAAY,cAAc,aAAA,CAAc,MAAA,CAAA,IAAA,CAAY,GAAG,CAAA,GAAI,SAAA;AAmB7D,SAAS,YAAyB,EAAA,EAA2B;AAClE,EAAA,IAAI;AACF,IAAA,OAAO,YAAY,EAAE,CAAA;AAAA,EACvB,SAAS,KAAA,EAAO;AACd,IAAA,IAAI,KAAA,IAAU,KAAA,CAAgC,IAAA,KAAS,kBAAA,EAAoB;AAEzE,MAAA,OAAO,MAAA;AAAA,IACT;AAEA,IAAA,MAAM,KAAA;AAAA,EACR;AACF;AAmBO,SAAS,cAA2B,EAAA,EAAe;AACxD,EAAA,OAAO,YAAY,EAAE,CAAA;AACvB","file":"chunk-33WTKH7X.js","sourcesContent":["/**\n * Cross-format require() helper for CJS and ESM compatibility\n *\n * Provides a synchronous `require()` function that works in both:\n * - CJS builds: Uses native `require`\n * - ESM builds: Uses `createRequire(import.meta.url)`\n *\n * This allows optional peer dependencies and dynamic module loading\n * to work synchronously in both module formats.\n */\n\nimport { createRequire } from 'node:module';\n\n// Use native `require` in CJS, `createRequire` in ESM.\n// tsup will compile this appropriately for each format.\n// Type is ReturnType<typeof createRequire> which is a function that loads modules.\nconst nodeRequire =\n typeof require === 'undefined' ? createRequire(import.meta.url) : require;\n\n/**\n * Synchronously require a module (works in both CJS and ESM)\n *\n * @param id - Module ID to require\n * @returns The required module\n * @throws Error if module cannot be loaded\n *\n * @example\n * ```typescript\n * import { safeRequire } from './node-require';\n *\n * const traceloop = safeRequire('@traceloop/node-server-sdk');\n * if (traceloop) {\n * traceloop.initialize({ ... });\n * }\n * ```\n */\nexport function safeRequire<T = unknown>(id: string): T | undefined {\n try {\n return nodeRequire(id) as T;\n } catch (error) {\n if (error && (error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {\n // Optional dependency missing – return undefined\n return undefined;\n }\n // Any other error is a real bug: rethrow\n throw error;\n }\n}\n\n/**\n * Synchronously require a module (throws if not found)\n *\n * Use this when the module is required (not optional).\n *\n * @param id - Module ID to require\n * @returns The required module\n * @throws Error if module cannot be loaded\n *\n * @example\n * ```typescript\n * import { requireModule } from './node-require';\n *\n * const fs = requireModule<typeof import('node:fs')>('node:fs');\n * const content = fs.readFileSync('file.txt', 'utf8');\n * ```\n */\nexport function requireModule<T = unknown>(id: string): T {\n return nodeRequire(id) as T;\n}\n\n/**\n * Direct access to the nodeRequire function (for advanced use cases)\n */\nexport { nodeRequire };\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getConfig } from './chunk-
|
|
1
|
+
import { getConfig } from './chunk-4XKOSC37.js';
|
|
2
2
|
|
|
3
3
|
// src/metric-helpers.ts
|
|
4
4
|
function getActiveMeter() {
|
|
@@ -22,5 +22,5 @@ function createObservableGauge(name, options) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export { createCounter, createHistogram, createObservableGauge, createUpDownCounter, getMeter };
|
|
25
|
-
//# sourceMappingURL=chunk-
|
|
26
|
-
//# sourceMappingURL=chunk-
|
|
25
|
+
//# sourceMappingURL=chunk-4F5YVTKZ.js.map
|
|
26
|
+
//# sourceMappingURL=chunk-4F5YVTKZ.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/metric-helpers.ts"],"names":[],"mappings":";;;AASA,SAAS,cAAA,GAAwB;AAC/B,EAAA,MAAM,SAAS,SAAA,EAAU;AACzB,EAAA,OAAO,MAAA,CAAO,KAAA;AAChB;AAEO,SAAS,QAAA,GAAkB;AAChC,EAAA,OAAO,cAAA,EAAe;AACxB;AAOO,SAAS,aAAA,CAAc,MAAc,OAAA,EAAmC;AAC7E,EAAA,OAAO,cAAA,EAAe,CAAE,aAAA,CAAc,IAAA,EAAM,OAAO,CAAA;AACrD;AAEO,SAAS,eAAA,CACd,MACA,OAAA,EACW;AACX,EAAA,OAAO,cAAA,EAAe,CAAE,eAAA,CAAgB,IAAA,EAAM,OAAO,CAAA;AACvD;AAEO,SAAS,mBAAA,CACd,MACA,OAAA,EACe;AACf,EAAA,OAAO,cAAA,EAAe,CAAE,mBAAA,CAAoB,IAAA,EAAM,OAAO,CAAA;AAC3D;AAEO,SAAS,qBAAA,CACd,MACA,OAAA,EACiB;AACjB,EAAA,OAAO,cAAA,EAAe,CAAE,qBAAA,CAAsB,IAAA,EAAM,OAAO,CAAA;AAC7D","file":"chunk-
|
|
1
|
+
{"version":3,"sources":["../src/metric-helpers.ts"],"names":[],"mappings":";;;AASA,SAAS,cAAA,GAAwB;AAC/B,EAAA,MAAM,SAAS,SAAA,EAAU;AACzB,EAAA,OAAO,MAAA,CAAO,KAAA;AAChB;AAEO,SAAS,QAAA,GAAkB;AAChC,EAAA,OAAO,cAAA,EAAe;AACxB;AAOO,SAAS,aAAA,CAAc,MAAc,OAAA,EAAmC;AAC7E,EAAA,OAAO,cAAA,EAAe,CAAE,aAAA,CAAc,IAAA,EAAM,OAAO,CAAA;AACrD;AAEO,SAAS,eAAA,CACd,MACA,OAAA,EACW;AACX,EAAA,OAAO,cAAA,EAAe,CAAE,eAAA,CAAgB,IAAA,EAAM,OAAO,CAAA;AACvD;AAEO,SAAS,mBAAA,CACd,MACA,OAAA,EACe;AACf,EAAA,OAAO,cAAA,EAAe,CAAE,mBAAA,CAAoB,IAAA,EAAM,OAAO,CAAA;AAC3D;AAEO,SAAS,qBAAA,CACd,MACA,OAAA,EACiB;AACjB,EAAA,OAAO,cAAA,EAAe,CAAE,qBAAA,CAAsB,IAAA,EAAM,OAAO,CAAA;AAC7D","file":"chunk-4F5YVTKZ.js","sourcesContent":["import type {\n Counter,\n Histogram,\n Meter,\n ObservableGauge,\n UpDownCounter,\n} from '@opentelemetry/api';\nimport { getConfig } from './config';\n\nfunction getActiveMeter(): Meter {\n const config = getConfig();\n return config.meter;\n}\n\nexport function getMeter(): Meter {\n return getActiveMeter();\n}\n\ntype CounterOptions = Parameters<Meter['createCounter']>[1];\ntype HistogramOptions = Parameters<Meter['createHistogram']>[1];\ntype UpDownCounterOptions = Parameters<Meter['createUpDownCounter']>[1];\ntype ObservableGaugeOptions = Parameters<Meter['createObservableGauge']>[1];\n\nexport function createCounter(name: string, options?: CounterOptions): Counter {\n return getActiveMeter().createCounter(name, options);\n}\n\nexport function createHistogram(\n name: string,\n options?: HistogramOptions,\n): Histogram {\n return getActiveMeter().createHistogram(name, options);\n}\n\nexport function createUpDownCounter(\n name: string,\n options?: UpDownCounterOptions,\n): UpDownCounter {\n return getActiveMeter().createUpDownCounter(name, options);\n}\n\nexport function createObservableGauge(\n name: string,\n options?: ObservableGaugeOptions,\n): ObservableGauge {\n return getActiveMeter().createObservableGauge(name, options);\n}\n"]}
|
|
@@ -83,5 +83,5 @@ function resetConfig() {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
export { FEATURE_FLAGS, configure, getConfig, resetConfig };
|
|
86
|
-
//# sourceMappingURL=chunk-
|
|
87
|
-
//# sourceMappingURL=chunk-
|
|
86
|
+
//# sourceMappingURL=chunk-4XKOSC37.js.map
|
|
87
|
+
//# sourceMappingURL=chunk-4XKOSC37.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/config.ts"],"names":[],"mappings":";;;AAkBA,IAAM,aAAA,GAAgB,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa,YAAA;AAC/C,IAAM,MAAA,GAAS,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa,aAAA;AAExB,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa;AAElC,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,2BAAA,EACE,aAAA,IAAiB,OAAA,CAAQ,GAAA,CAAI,uBAAA,KAA4B,OAAA;AAAA;AAAA,EAG3D,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A;AAAA,MAC7B,KAAA,EAAO,OAAA,CAAQ,QAAA,CAAS,KAAK;AAAA,KAC/B;AAAA,EACF;AACF,CAAA;AAEA,IAAM,YAAA,GAAe,IAAI,MAAA,EAAO;AAczB,SAAS,UAAU,OAAA,EAA8B;AACtD,EAAA,YAAA,CAAa,UAAU,OAAO,CAAA;AAChC;AAKO,SAAS,SAAA,GAEd;AACA,EAAA,OAAO;AAAA,IACL,GAAG,aAAa,GAAA,EAAI;AAAA,IACpB,YAAA,EAAc;AAAA,GAChB;AACF;AAKO,SAAS,WAAA,GAAoB;AAClC,EAAA,YAAA,CAAa,KAAA,EAAM;AACrB","file":"chunk-4XKOSC37.js","sourcesContent":["/**\n * Global configuration for OpenTelemetry decorators\n *\n * Allows users to inject custom loggers, tracers, and meters\n * while maintaining sensible defaults.\n */\n\nimport { trace, metrics, type Tracer, type Meter } from '@opentelemetry/api';\nimport { getAutotelTracer } from './tracer-provider';\n\nexport type { ILogger } from './logger';\n\n/**\n * Environment-based feature flags for performance optimization\n *\n * Disables expensive features in development while maintaining\n * full observability in production.\n */\nconst IS_PRODUCTION = process.env.NODE_ENV === 'production';\nconst IS_DEV = process.env.NODE_ENV === 'development';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst IS_TEST = process.env.NODE_ENV === 'test';\n\nexport const FEATURE_FLAGS = {\n /** Enable full auto-instrumentation (expensive, production only) */\n ENABLE_AUTO_INSTRUMENTATION:\n IS_PRODUCTION && process.env.autotel_AUTO_INSTRUMENT !== 'false',\n\n /** Enable verbose logging (development only) */\n ENABLE_VERBOSE_LOGGING: IS_DEV || process.env.autotel_VERBOSE === 'true',\n\n /** Enable metrics collection (production only) */\n ENABLE_METRICS_BY_DEFAULT:\n IS_PRODUCTION && process.env.autotel_METRICS !== 'false',\n\n /** Enable async resource detection (production only) */\n ENABLE_RESOURCE_DETECTION:\n IS_PRODUCTION && process.env.autotel_RESOURCE_DETECTION === 'true',\n\n /** Enable tracing in all environments (can be disabled via autotel_TRACING=false) */\n ENABLE_TRACING: process.env.autotel_TRACING !== 'false',\n\n /** Enable log redaction for sensitive fields (can be disabled via autotel_REDACTION=false) */\n ENABLE_REDACTION: process.env.autotel_REDACTION !== 'false',\n} as const;\n\n/**\n * Runtime configuration for OpenTelemetry instrumentation\n *\n * This configures the tracer and meter used by autotel's functional API.\n * Use `configure()` to set custom tracer/meter instances.\n */\nexport interface RuntimeConfig {\n /**\n * Tracer name for OpenTelemetry\n * @default 'app'\n */\n tracerName?: string;\n\n /**\n * Meter name for OpenTelemetry metrics\n * @default 'app'\n */\n meterName?: string;\n\n /**\n * Custom tracer instance (for advanced use cases like Datadog direct)\n * @default trace.getTracer(tracerName)\n */\n tracer?: Tracer;\n\n /**\n * Custom meter instance\n * @default metrics.getMeter(meterName)\n */\n meter?: Meter;\n}\n\n/**\n * Internal configuration state\n */\nclass Config {\n private config: Required<RuntimeConfig> = {\n tracerName: 'app',\n meterName: 'app',\n tracer: getAutotelTracer('app'),\n meter: metrics.getMeter('app'),\n };\n\n /**\n * Get feature flags\n */\n get featureFlags() {\n return FEATURE_FLAGS;\n }\n\n /**\n * Update global configuration\n */\n configure(options: RuntimeConfig): void {\n if (options.tracerName) {\n this.config.tracerName = options.tracerName;\n this.config.tracer = getAutotelTracer(options.tracerName);\n }\n if (options.meterName) {\n this.config.meterName = options.meterName;\n this.config.meter = metrics.getMeter(options.meterName);\n }\n if (options.tracer) {\n this.config.tracer = options.tracer;\n }\n if (options.meter) {\n this.config.meter = options.meter;\n }\n }\n\n /**\n * Get current configuration\n */\n get(): Required<RuntimeConfig> {\n return this.config;\n }\n\n /**\n * Reset to defaults (mainly for testing)\n */\n reset(): void {\n this.config = {\n tracerName: 'app',\n meterName: 'app',\n tracer: trace.getTracer('app'),\n meter: metrics.getMeter('app'),\n };\n }\n}\n\nconst globalConfig = new Config();\n\n/**\n * Configure global instrumentation behavior\n *\n * @example\n * ```typescript\n * import { configure } from 'autotel/config'\n *\n * configure({\n * tracerName: 'my-app'\n * })\n * ```\n */\nexport function configure(options: RuntimeConfig): void {\n globalConfig.configure(options);\n}\n\n/**\n * Get current configuration (internal use)\n */\nexport function getConfig(): Required<RuntimeConfig> & {\n featureFlags: typeof FEATURE_FLAGS;\n} {\n return {\n ...globalConfig.get(),\n featureFlags: FEATURE_FLAGS,\n };\n}\n\n/**\n * Reset configuration to defaults (internal use - mainly for testing)\n */\nexport function resetConfig(): void {\n globalConfig.reset();\n}\n"]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var chunkG7VZBCD6_cjs = require('./chunk-G7VZBCD6.cjs');
|
|
3
|
+
var chunkYS6C2YJE_cjs = require('./chunk-YS6C2YJE.cjs');
|
|
5
4
|
var api = require('@opentelemetry/api');
|
|
6
5
|
|
|
7
6
|
var spanNameMap = /* @__PURE__ */ new WeakMap();
|
|
@@ -46,7 +45,7 @@ function getActiveSpan() {
|
|
|
46
45
|
}
|
|
47
46
|
function getActiveContext() {
|
|
48
47
|
try {
|
|
49
|
-
const { getActiveContextWithBaggage } =
|
|
48
|
+
const { getActiveContextWithBaggage } = chunkYS6C2YJE_cjs.requireModule("./trace-context");
|
|
50
49
|
return getActiveContextWithBaggage();
|
|
51
50
|
} catch {
|
|
52
51
|
return api.context.active();
|
|
@@ -122,5 +121,5 @@ exports.getTracer = getTracer;
|
|
|
122
121
|
exports.isTracing = isTracing;
|
|
123
122
|
exports.runWithSpan = runWithSpan;
|
|
124
123
|
exports.setSpanName = setSpanName;
|
|
125
|
-
//# sourceMappingURL=chunk-
|
|
126
|
-
//# sourceMappingURL=chunk-
|
|
124
|
+
//# sourceMappingURL=chunk-5PZZHX4Y.cjs.map
|
|
125
|
+
//# sourceMappingURL=chunk-5PZZHX4Y.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/trace-helpers.ts"],"names":["trace","context","requireModule","SpanStatusCode"],"mappings":";;;;;AA2CA,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QAAA,IAAI,IAAA,CAAK,GAAA,CAAI,KAAK,CAAA,EAAG;AACnB,UAAA,SAAA,CAAU,YAAY,CAAA,GAAI,sBAAA;AAC1B,UAAA;AAAA,QACF;AAGA,QAAA,IAAA,CAAK,IAAI,KAAK,CAAA;AACd,QAAA,OAAA,CAAQ,OAAkC,YAAY,CAAA;AACtD,QAAA;AAAA,MACF;AAGA,MAAA,IAAI;AACF,QAAA,SAAA,CAAU,YAAY,CAAA,GAAI,IAAA,CAAK,SAAA,CAAU,KAAK,CAAA;AAAA,MAChD,CAAA,CAAA,MAAQ;AAEN,QAAA,SAAA,CAAU,YAAY,CAAA,GAAI,wBAAA;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAA,CAAQ,UAAU,MAAM,CAAA;AACxB,EAAA,OAAO,SAAA;AACT","file":"chunk-5PZZHX4Y.cjs","sourcesContent":["/**\n * Trace context helpers - Core primitives for trace correlation\n *\n * These are the building blocks that allow users to bring their own logger\n * (bunyan, log4js, custom, etc.) and add trace correlation.\n *\n * @example Using with bunyan\n * ```typescript\n * import bunyan from 'bunyan';\n * import { enrichWithTraceContext } from 'autotel/trace-helpers';\n *\n * const bunyanLogger = bunyan.createLogger({ name: 'myapp' });\n *\n * const logger = {\n * info: (msg: string, extra?: object) => {\n * bunyanLogger.info(enrichWithTraceContext(extra || {}), msg);\n * }\n * };\n * ```\n *\n * @example Using with log4js\n * ```typescript\n * import log4js from 'log4js';\n * import { getTraceContext } from 'autotel/trace-helpers';\n *\n * const log4jsLogger = log4js.getLogger();\n *\n * function logWithTrace(level: string, msg: string, extra?: object) {\n * const context = getTraceContext();\n * log4jsLogger[level](msg, { ...extra, ...context });\n * }\n * ```\n */\n\nimport { trace, context, SpanStatusCode } from '@opentelemetry/api';\nimport type { Span, Tracer, Context } from '@opentelemetry/api';\nimport { requireModule } from './node-require';\n\n/**\n * WeakMap to store span names for active spans\n * This allows us to retrieve the span name even though OpenTelemetry\n * doesn't expose it through the public API\n */\nconst spanNameMap = new WeakMap<Span, string>();\n\n/**\n * Store span name for a given span\n * Called internally when spans are created\n */\nexport function setSpanName(span: Span, name: string): void {\n spanNameMap.set(span, name);\n}\n\n/**\n * Trace context extracted from active span\n */\nexport interface TraceContext {\n /** Full 32-character hex trace ID */\n traceId: string;\n /** 16-character hex span ID */\n spanId: string;\n /** First 16 characters of trace ID (for log grouping/correlation) */\n correlationId: string;\n /** Function/operation name (OpenTelemetry semantic convention: code.function) */\n 'code.function'?: string;\n /** Datadog trace ID in decimal format (lower 64 bits) for log-trace correlation */\n 'dd.trace_id'?: string;\n /** Datadog span ID in decimal format for log-trace correlation */\n 'dd.span_id'?: string;\n}\n\n/**\n * Convert hex string to decimal string representation\n * Handles 64-bit unsigned integers for Datadog correlation\n *\n * @param hex - Hex string (up to 16 characters for 64-bit)\n * @returns Decimal string representation\n */\nfunction hexToDecimal(hex: string): string {\n // For 64-bit values, use BigInt to avoid precision loss\n return BigInt('0x' + hex).toString(10);\n}\n\n/**\n * Get current trace context from active span\n *\n * Returns null if no span is active (e.g., outside of trace operation)\n *\n * Includes both OpenTelemetry standard fields (hex) and Datadog-specific\n * fields (decimal) for maximum compatibility.\n *\n * @returns Trace context with traceId, spanId, correlationId, and Datadog decimal IDs, or null\n *\n * @example\n * ```typescript\n * import { getTraceContext } from 'autotel/trace-helpers';\n *\n * const context = getTraceContext();\n * if (context) {\n * console.log('Current trace:', context.traceId);\n * // Current trace: 4bf92f3577b34da6a3ce929d0e0e4736\n * console.log('Datadog trace ID:', context['dd.trace_id']);\n * // Datadog trace ID: 12007117331170166582 (decimal for log correlation)\n * }\n * ```\n */\nexport function getTraceContext(): TraceContext | null {\n const span = trace.getActiveSpan();\n if (!span) return null;\n\n const spanContext = span.spanContext();\n const traceId = spanContext.traceId;\n const spanId = spanContext.spanId;\n\n // Get span name from WeakMap (set when span is created)\n // Map to OpenTelemetry semantic convention: code.function\n const spanName = spanNameMap.get(span);\n\n // Datadog uses the lower 64 bits of the 128-bit OpenTelemetry trace ID\n // Convert from hex to decimal for Datadog's log-trace correlation\n const traceIdLower64 = traceId.slice(-16); // Last 16 hex chars = lower 64 bits\n const ddTraceId = hexToDecimal(traceIdLower64);\n const ddSpanId = hexToDecimal(spanId);\n\n return {\n traceId,\n spanId,\n correlationId: traceId.slice(0, 16),\n ...(spanName && { 'code.function': spanName }),\n // Datadog-specific fields for log-trace correlation\n 'dd.trace_id': ddTraceId,\n 'dd.span_id': ddSpanId,\n };\n}\n\n/**\n * Enrich object with trace context (traceId, spanId, correlationId, and Datadog fields)\n *\n * If no span is active, returns the object unchanged.\n * This prevents \"undefined\" or \"null\" values in logs.\n *\n * Automatically adds both OpenTelemetry standard fields (hex) and Datadog-specific\n * fields (decimal) for maximum compatibility with observability backends.\n *\n * @param obj - Object to enrich (e.g., log metadata)\n * @returns Object with trace context merged in, or unchanged if no active span\n *\n * @example\n * ```typescript\n * import { enrichWithTraceContext } from 'autotel/trace-helpers';\n *\n * // Inside a trace operation:\n * const enriched = enrichWithTraceContext({ userId: '123' });\n * // {\n * // userId: '123',\n * // traceId: '4bf92f3577b34da6a3ce929d0e0e4736',\n * // spanId: '00f067aa0ba902b7',\n * // correlationId: '4bf92f3577b34da6',\n * // 'dd.trace_id': '12007117331170166582', // Datadog decimal format\n * // 'dd.span_id': '67667974448284583' // Datadog decimal format\n * // }\n *\n * // Outside trace operation:\n * const unchanged = enrichWithTraceContext({ userId: '123' });\n * // { userId: '123' } - no trace fields added\n * ```\n */\nexport function enrichWithTraceContext<T extends Record<string, unknown>>(\n obj: T,\n): T {\n const context = getTraceContext();\n return context ? ({ ...obj, ...context } as T) : obj;\n}\n\n/**\n * Check if currently in a trace context\n *\n * Useful for conditional logic based on trace presence\n *\n * @returns true if active span exists, false otherwise\n *\n * @example\n * ```typescript\n * import { isTracing } from 'autotel/trace-helpers';\n *\n * if (isTracing()) {\n * // Add expensive debug metadata only when tracing\n * logger.debug('Detailed context', expensiveDebugData());\n * }\n * ```\n */\nexport function isTracing(): boolean {\n return trace.getActiveSpan() !== undefined;\n}\n\n/**\n * Get a tracer instance for creating custom spans\n *\n * Use this when you need low-level control over span lifecycle.\n * For most use cases, prefer trace(), span(), or instrument() instead.\n *\n * @param name - Tracer name (usually your service or module name)\n * @param version - Optional version string\n * @returns OpenTelemetry Tracer instance\n *\n * @example Basic usage\n * ```typescript\n * import { getTracer } from 'autotel';\n *\n * const tracer = getTracer('my-service');\n * const span = tracer.startSpan('custom.operation');\n * try {\n * // Your logic\n * span.setAttribute('key', 'value');\n * } finally {\n * span.end();\n * }\n * ```\n *\n * @example With AI SDK\n * ```typescript\n * import { getTracer } from 'autotel';\n * import { generateText } from 'ai';\n *\n * const tracer = getTracer('ai-agent');\n * const result = await generateText({\n * model: myModel,\n * prompt: 'Hello',\n * experimental_telemetry: {\n * isEnabled: true,\n * tracer,\n * },\n * });\n * ```\n */\nexport function getTracer(name: string, version?: string): Tracer {\n return trace.getTracer(name, version);\n}\n\n/**\n * Get the currently active span\n *\n * Returns undefined if no span is currently active.\n * Useful for adding attributes or events to the current span.\n *\n * @returns Active span or undefined\n *\n * @example Adding attributes to active span\n * ```typescript\n * import { getActiveSpan } from 'autotel';\n *\n * const span = getActiveSpan();\n * if (span) {\n * span.setAttribute('user.id', userId);\n * span.addEvent('User action', { action: 'click' });\n * }\n * ```\n *\n * @example Checking span status\n * ```typescript\n * import { getActiveSpan, SpanStatusCode } from 'autotel';\n *\n * const span = getActiveSpan();\n * if (span?.isRecording()) {\n * span.setStatus({ code: SpanStatusCode.OK });\n * }\n * ```\n */\nexport function getActiveSpan(): Span | undefined {\n return trace.getActiveSpan();\n}\n\n/**\n * Get the currently active OpenTelemetry context\n *\n * The context contains the active span and any baggage.\n * Useful for context propagation and custom instrumentation.\n *\n * @returns Current active context\n *\n * @example Propagating context\n * ```typescript\n * import { getActiveContext } from 'autotel';\n *\n * const currentContext = getActiveContext();\n * // Pass context to another function or service\n * ```\n *\n * @example With context injection\n * ```typescript\n * import { getActiveContext, injectTraceContext } from 'autotel';\n *\n * const headers = {};\n * injectTraceContext(headers);\n * // Headers now contain trace propagation data\n * ```\n */\nexport function getActiveContext(): Context {\n // Check stored context first (from baggage setters), then fall back to active context\n // This ensures ctx.setBaggage() changes are visible to OpenTelemetry operations\n try {\n const { getActiveContextWithBaggage } = requireModule<{\n getActiveContextWithBaggage: () => Context;\n }>('./trace-context');\n return getActiveContextWithBaggage();\n } catch {\n // Fallback if trace-context isn't available\n return context.active();\n }\n}\n\n/**\n * Run a function with a specific span set as active\n *\n * This is a convenience wrapper around the two-step process of\n * setting a span in context and running code within that context.\n *\n * @param span - The span to set as active\n * @param fn - Function to execute with the span active\n * @returns The return value of the function\n *\n * @example Running code with a custom span\n * ```typescript\n * import { getTracer, runWithSpan } from 'autotel';\n *\n * const tracer = getTracer('my-service');\n * const span = tracer.startSpan('background.job');\n *\n * try {\n * const result = await runWithSpan(span, async () => {\n * // Any spans created here will be children of 'background.job'\n * await processData();\n * return { success: true };\n * });\n * console.log(result);\n * } finally {\n * span.end();\n * }\n * ```\n *\n * @example Testing with custom spans\n * ```typescript\n * import { runWithSpan, otelTrace } from 'autotel';\n *\n * const tracer = otelTrace.getTracer('test');\n * const span = tracer.startSpan('test.operation');\n *\n * const result = runWithSpan(span, () => {\n * // Code under test runs with this span as active\n * return myFunction();\n * });\n *\n * span.end();\n * ```\n */\nexport function runWithSpan<T>(span: Span, fn: () => T): T {\n const ctx = trace.setSpan(context.active(), span);\n return context.with(ctx, fn);\n}\n\n/**\n * Finalize a span with appropriate status and optional error recording\n *\n * This is a convenience function that:\n * - Records exceptions if an error is provided\n * - Sets span status to ERROR if error exists, OK otherwise\n * - Ends the span\n *\n * @param span - The span to finalize\n * @param error - Optional error to record\n *\n * @example Without error (success case)\n * ```typescript\n * import { getTracer, finalizeSpan } from 'autotel';\n *\n * const tracer = getTracer('my-service');\n * const span = tracer.startSpan('operation');\n *\n * try {\n * await doWork();\n * finalizeSpan(span);\n * } catch (error) {\n * finalizeSpan(span, error);\n * throw error;\n * }\n * ```\n *\n * @example With error\n * ```typescript\n * import { getTracer, finalizeSpan } from 'autotel';\n *\n * const tracer = getTracer('my-service');\n * const span = tracer.startSpan('operation');\n *\n * try {\n * await riskyOperation();\n * finalizeSpan(span);\n * } catch (error) {\n * finalizeSpan(span, error); // Records exception and sets ERROR status\n * throw error;\n * }\n * ```\n *\n * @example In instrumentation\n * ```typescript\n * import { getTracer, runWithSpan, finalizeSpan } from 'autotel';\n *\n * function instrumentedQuery(query: string) {\n * const tracer = getTracer('db');\n * const span = tracer.startSpan('db.query');\n *\n * return runWithSpan(span, () => {\n * try {\n * const result = executeQuery(query);\n * finalizeSpan(span);\n * return result;\n * } catch (error) {\n * finalizeSpan(span, error);\n * throw error;\n * }\n * });\n * }\n * ```\n */\nexport function finalizeSpan(span: Span, error?: unknown): void {\n if (error) {\n if (error instanceof Error) {\n span.recordException(error);\n } else {\n span.recordException(new Error(String(error)));\n }\n span.setStatus({ code: SpanStatusCode.ERROR });\n } else {\n span.setStatus({ code: SpanStatusCode.OK });\n }\n span.end();\n}\n\n/**\n * Creates a deterministic trace ID from a seed string.\n *\n * Generates a consistent 128-bit trace ID (32 hex characters) from an input seed\n * using SHA-256 hashing. Useful for correlating external system IDs (request IDs,\n * order IDs, session IDs) with OpenTelemetry trace IDs.\n *\n * **Use Cases:**\n * - Correlate external request IDs with traces\n * - Link customer support tickets to trace data\n * - Associate business entities (orders, sessions) with observability data\n * - Debug specific user flows by deterministic trace lookup\n *\n * **Important:** Only use this when you need deterministic trace IDs for correlation.\n * For normal tracing, let OpenTelemetry generate random trace IDs automatically.\n *\n * **Runtime Support:**\n * - Node.js 15+ (native crypto.subtle)\n * - All modern browsers\n * - Edge runtimes (Cloudflare Workers, Deno, etc.)\n *\n * @param seed - Input string to generate trace ID from (e.g., request ID, order ID)\n * @returns Promise resolving to a 32-character hex trace ID (128 bits)\n *\n * @example Correlate external request ID with trace\n * ```typescript\n * import { createDeterministicTraceId } from 'autotel/trace-helpers'\n * import { trace, context } from '@opentelemetry/api'\n *\n * // In middleware or request handler\n * const requestId = req.headers['x-request-id']\n * const traceId = await createDeterministicTraceId(requestId)\n *\n * // Use with manual span creation (advanced - not needed with trace/span functions)\n * const tracer = trace.getTracer('my-service')\n * const spanContext = {\n * traceId,\n * spanId: '0123456789abcdef', // Still random\n * traceFlags: 1\n * }\n * ```\n *\n * @example Link customer support tickets to traces\n * ```typescript\n * import { createDeterministicTraceId } from 'autotel/trace-helpers'\n *\n * // Support dashboard integration\n * const ticketId = 'TICKET-12345'\n * const traceId = await createDeterministicTraceId(ticketId)\n *\n * // Generate direct link to traces in observability backend\n * const traceUrl = `https://your-otel-backend.com/traces/${traceId}`\n * console.log(`View related traces: ${traceUrl}`)\n * ```\n *\n * @example Session-based correlation\n * ```typescript\n * import { createDeterministicTraceId } from 'autotel/trace-helpers'\n *\n * // Track all operations for a user session\n * const sessionId = req.session.id\n * const traceId = await createDeterministicTraceId(sessionId)\n *\n * // All operations in this session share the same trace ID\n * // Makes it easy to find all activity for a specific session\n * ```\n *\n * @public\n */\nexport async function createDeterministicTraceId(\n seed: string,\n): Promise<string> {\n // Encode seed string to bytes\n const encoder = new TextEncoder();\n const data = encoder.encode(seed);\n\n // Generate SHA-256 hash (256 bits)\n const hashBuffer = await crypto.subtle.digest('SHA-256', data);\n\n // Convert to hex string and truncate to 32 characters (128 bits)\n // OpenTelemetry trace IDs are 128 bits (16 bytes, 32 hex characters)\n const hashArray = new Uint8Array(hashBuffer);\n return [...hashArray]\n .map((byte) => byte.toString(16).padStart(2, '0'))\n .join('')\n .slice(0, 32);\n}\n\n/**\n * Flattens nested metadata objects into dot-notation span attributes.\n *\n * Converts complex nested objects into flat key-value pairs suitable for\n * OpenTelemetry span attributes. Non-string values are JSON serialized.\n * Handles serialization failures gracefully with a fallback value.\n *\n * **Use Cases:**\n * - Structured metadata with nested objects\n * - User context with multiple properties\n * - Request/response metadata\n * - Business entity attributes\n *\n * **Note:** Filters out null/undefined values automatically to keep spans clean.\n *\n * @param metadata - Nested metadata object to flatten\n * @param prefix - Prefix for all attribute keys (default: 'metadata')\n * @returns Flattened attributes as { [key: string]: string }\n *\n * @example Basic metadata flattening\n * ```typescript\n * import { flattenMetadata } from 'autotel/trace-helpers'\n * import { trace } from 'autotel'\n *\n * export const processOrder = trace(ctx => async (orderId: string) => {\n * const order = await getOrder(orderId)\n *\n * // Flatten complex order metadata\n * const flattened = flattenMetadata({\n * user: { id: order.userId, tier: 'premium' },\n * payment: { method: 'card', processor: 'stripe' },\n * items: order.items.length\n * })\n *\n * ctx.setAttributes(flattened)\n * // Results in:\n * // {\n * // 'metadata.user.id': 'user-123',\n * // 'metadata.user.tier': 'premium',\n * // 'metadata.payment.method': 'card',\n * // 'metadata.payment.processor': 'stripe',\n * // 'metadata.items': '5'\n * // }\n * })\n * ```\n *\n * @example Custom prefix for semantic conventions\n * ```typescript\n * import { flattenMetadata } from 'autotel/trace-helpers'\n * import { trace } from 'autotel'\n *\n * export const fetchUser = trace(ctx => async (userId: string) => {\n * const user = await db.users.findOne({ id: userId })\n *\n * // Use semantic convention prefix\n * const userAttrs = flattenMetadata(\n * {\n * id: user.id,\n * email: user.email,\n * plan: user.subscription.plan\n * },\n * 'user' // Custom prefix\n * )\n *\n * ctx.setAttributes(userAttrs)\n * // Results in:\n * // {\n * // 'user.id': 'user-123',\n * // 'user.email': 'user@example.com',\n * // 'user.plan': 'enterprise'\n * // }\n * })\n * ```\n *\n * @example With complex objects (auto-serialized)\n * ```typescript\n * import { flattenMetadata } from 'autotel/trace-helpers'\n * import { trace } from 'autotel'\n *\n * export const analyzeRequest = trace(ctx => async (req: Request) => {\n * const metadata = flattenMetadata({\n * headers: req.headers, // Object - will be JSON serialized\n * query: req.query, // Object - will be JSON serialized\n * timestamp: new Date() // Non-string - will be JSON serialized\n * })\n *\n * ctx.setAttributes(metadata)\n * // Results in:\n * // {\n * // 'metadata.headers': '{\"accept\":\"application/json\",...}',\n * // 'metadata.query': '{\"page\":\"1\",\"limit\":\"10\"}',\n * // 'metadata.timestamp': '\"2024-01-15T12:00:00.000Z\"'\n * // }\n * })\n * ```\n *\n * @example Error handling\n * ```typescript\n * import { flattenMetadata } from 'autotel/trace-helpers'\n *\n * // Objects with circular references are handled gracefully\n * const circular: any = { a: 1 }\n * circular.self = circular\n *\n * const flattened = flattenMetadata({ data: circular })\n * // Results in:\n * // { 'metadata.data': '<serialization-failed>' }\n * ```\n *\n * @public\n */\nexport function flattenMetadata(\n metadata: Record<string, unknown>,\n prefix = 'metadata',\n): Record<string, string> {\n const flattened: Record<string, string> = {};\n const seen = new WeakSet<object>(); // Track visited objects to detect cycles\n\n function flatten(obj: Record<string, unknown>, currentPrefix: string): void {\n for (const [key, value] of Object.entries(obj)) {\n // Skip null/undefined values\n if (value == null) continue;\n\n const attributeKey = `${currentPrefix}.${key}`;\n\n // Handle primitives directly (string, number, boolean)\n if (typeof value === 'string') {\n flattened[attributeKey] = value;\n continue;\n }\n if (typeof value === 'number' || typeof value === 'boolean') {\n flattened[attributeKey] = String(value);\n continue;\n }\n\n // Recursively flatten plain objects (with cycle detection)\n if (\n typeof value === 'object' &&\n value !== null &&\n value.constructor === Object\n ) {\n // Detect circular references\n if (seen.has(value)) {\n flattened[attributeKey] = '<circular-reference>';\n continue;\n }\n\n // Mark as visited and recursively flatten\n seen.add(value);\n flatten(value as Record<string, unknown>, attributeKey);\n continue;\n }\n\n // Serialize arrays and other non-plain objects to JSON\n try {\n flattened[attributeKey] = JSON.stringify(value);\n } catch {\n // Handle circular references or non-serializable objects\n flattened[attributeKey] = '<serialization-failed>';\n }\n }\n }\n\n flatten(metadata, prefix);\n return flattened;\n}\n"]}
|