@vtvlive/interactive-apm 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +201 -123
  2. package/dist/factories/tracing-provider.factory.d.ts +8 -3
  3. package/dist/factories/tracing-provider.factory.d.ts.map +1 -1
  4. package/dist/factories/tracing-provider.factory.js +17 -13
  5. package/dist/index.d.ts +12 -10
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +3 -1
  8. package/dist/init/elastic-apm-init.d.ts +25 -3
  9. package/dist/init/elastic-apm-init.d.ts.map +1 -1
  10. package/dist/init/elastic-apm-init.js +29 -12
  11. package/dist/init/opentelemetry-init.d.ts +8 -1
  12. package/dist/init/opentelemetry-init.d.ts.map +1 -1
  13. package/dist/init/opentelemetry-init.js +145 -44
  14. package/dist/interfaces/tracing-provider.interface.d.ts +13 -3
  15. package/dist/interfaces/tracing-provider.interface.d.ts.map +1 -1
  16. package/dist/modules/tracing.module.d.ts +5 -5
  17. package/dist/modules/tracing.module.d.ts.map +1 -1
  18. package/dist/modules/tracing.module.js +2 -1
  19. package/dist/providers/elastic-apm.tracing-provider.d.ts +23 -5
  20. package/dist/providers/elastic-apm.tracing-provider.d.ts.map +1 -1
  21. package/dist/providers/elastic-apm.tracing-provider.js +127 -28
  22. package/dist/providers/opentelemetry.tracing-provider.d.ts +12 -4
  23. package/dist/providers/opentelemetry.tracing-provider.d.ts.map +1 -1
  24. package/dist/providers/opentelemetry.tracing-provider.js +328 -67
  25. package/dist/services/tracing.service.d.ts +6 -5
  26. package/dist/services/tracing.service.d.ts.map +1 -1
  27. package/dist/services/tracing.service.js +2 -2
  28. package/dist/types/apm.types.d.ts +162 -0
  29. package/dist/types/apm.types.d.ts.map +1 -0
  30. package/dist/types/apm.types.js +37 -0
  31. package/dist/types/otlp-transport.type.d.ts +14 -0
  32. package/dist/types/otlp-transport.type.d.ts.map +1 -0
  33. package/dist/types/otlp-transport.type.js +17 -0
  34. package/dist/utils/debug-exporter-wrapper.d.ts +36 -0
  35. package/dist/utils/debug-exporter-wrapper.d.ts.map +1 -0
  36. package/dist/utils/debug-exporter-wrapper.js +247 -0
  37. package/dist/utils/debug-logger.d.ts +81 -0
  38. package/dist/utils/debug-logger.d.ts.map +1 -0
  39. package/dist/utils/debug-logger.js +236 -0
  40. package/dist/utils/tracing.helper.d.ts +2 -2
  41. package/dist/utils/tracing.helper.d.ts.map +1 -1
  42. package/dist/utils/tracing.helper.js +8 -4
  43. package/package.json +24 -3
@@ -0,0 +1,162 @@
1
+ /**
2
+ * APM Common Types
3
+ *
4
+ * Defines common types that work with both Elastic APM and OpenTelemetry providers.
5
+ * This provides type safety and IntelliSense for users of this package.
6
+ */
7
+ /**
8
+ * Common Span interface - works with both Elastic APM and OpenTelemetry
9
+ */
10
+ export interface ISpan {
11
+ /** Span name */
12
+ name: string;
13
+ /** End the span */
14
+ end(): void;
15
+ /** Set an attribute on the span */
16
+ setAttribute(key: string, value: string | number | boolean | string[] | undefined): void;
17
+ /** Add an event to the span */
18
+ addEvent?(name: string, attributes?: Record<string, unknown>): void;
19
+ /** Set the span status */
20
+ setStatus?(status: SpanStatus): void;
21
+ /** Record an exception on the span */
22
+ recordException?(error: Error | unknown): void;
23
+ }
24
+ /**
25
+ * Span status
26
+ */
27
+ export interface SpanStatus {
28
+ code: number;
29
+ message?: string;
30
+ }
31
+ /**
32
+ * Common Tracer interface
33
+ */
34
+ export interface ITracer {
35
+ /** Start a new span */
36
+ startSpan(name: string, options?: SpanOptions): ISpan;
37
+ /** Get the currently active span */
38
+ getActiveSpan?(): ISpan | undefined;
39
+ /** Start a span with a parent context */
40
+ startSpanWithParent?<T>(name: string, fn: (span: ISpan) => Promise<T> | T, options?: SpanOptions): Promise<T>;
41
+ }
42
+ /**
43
+ * Span options
44
+ */
45
+ export interface SpanOptions {
46
+ /** Span attributes */
47
+ attributes?: Record<string, string | number | boolean>;
48
+ /** Span kind (server, client, internal, etc.) */
49
+ kind?: SpanKind;
50
+ /** Parent span context */
51
+ parent?: ISpan;
52
+ }
53
+ /**
54
+ * Span kind enumeration
55
+ */
56
+ export declare enum SpanKind {
57
+ INTERNAL = 0,
58
+ SERVER = 1,
59
+ CLIENT = 2,
60
+ PRODUCER = 3,
61
+ CONSUMER = 4
62
+ }
63
+ /**
64
+ * Common APM Provider configuration
65
+ */
66
+ export interface IApmProviderConfig {
67
+ /** Service name */
68
+ serviceName?: string;
69
+ /** Environment (development, staging, production) */
70
+ environment?: string;
71
+ /** Secret token for authentication */
72
+ secretToken?: string;
73
+ /** Enable debug logging */
74
+ debug?: boolean;
75
+ }
76
+ /**
77
+ * Elastic APM specific configuration
78
+ */
79
+ export interface IElasticApmConfig extends IApmProviderConfig {
80
+ /** APM server URL */
81
+ serverUrl?: string;
82
+ /** Secret token */
83
+ secretToken?: string;
84
+ /** API key */
85
+ apiKey?: string;
86
+ /** Server timeout in milliseconds */
87
+ serverTimeout?: number;
88
+ /** Disable instrumentations */
89
+ disableInstrumentations?: string[];
90
+ }
91
+ /**
92
+ * OpenTelemetry specific configuration
93
+ */
94
+ export interface IOpenTelemetryConfig extends IApmProviderConfig {
95
+ /** OTLP endpoint URL */
96
+ otlpEndpoint?: string;
97
+ /** OTLP transport type (http, grpc, proto) */
98
+ otlpTransport?: OtlpTransport | string;
99
+ /** OTLP auth token */
100
+ otlpAuthToken?: string;
101
+ /** OTLP headers */
102
+ otlpHeaders?: Record<string, string>;
103
+ /** Enable console exporter */
104
+ enableConsoleExporter?: boolean;
105
+ }
106
+ /**
107
+ * OTLP Transport types
108
+ */
109
+ export declare enum OtlpTransport {
110
+ HTTP = "http",
111
+ GRPC = "grpc",
112
+ PROTO = "proto"
113
+ }
114
+ /**
115
+ * OTLP Transport type string
116
+ */
117
+ export type OtlpTransportType = `${OtlpTransport}`;
118
+ /**
119
+ * APM Provider types
120
+ */
121
+ export declare enum ApmProvider {
122
+ ELASTIC = "elastic-apm",
123
+ OPENTELEMETRY = "opentelemetry"
124
+ }
125
+ /**
126
+ * APM Provider type string
127
+ */
128
+ export type ApmProviderType = `${ApmProvider}`;
129
+ /**
130
+ * Export result from span export
131
+ */
132
+ export interface IExportResult {
133
+ /** Export status code (0 = success, non-zero = error) */
134
+ code: number;
135
+ /** Error if export failed */
136
+ error?: Error;
137
+ }
138
+ /**
139
+ * Span processor interface
140
+ */
141
+ export interface ISpanProcessor {
142
+ /** Called when a span is started */
143
+ onStart(span: ISpan, parentContext?: unknown): void;
144
+ /** Called when a span is ended */
145
+ onEnd(span: ISpan): void;
146
+ /** Force flush any pending spans */
147
+ forceFlush(): Promise<void>;
148
+ /** Shutdown the processor */
149
+ shutdown(): Promise<void>;
150
+ }
151
+ /**
152
+ * Span exporter interface
153
+ */
154
+ export interface ISpanExporter {
155
+ /** Export spans */
156
+ export(spans: ISpan[], callback: (result: IExportResult) => void): void;
157
+ /** Shutdown the exporter */
158
+ shutdown(): Promise<void>;
159
+ /** Force flush (optional) */
160
+ forceFlush?(): Promise<void>;
161
+ }
162
+ //# sourceMappingURL=apm.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apm.types.d.ts","sourceRoot":"","sources":["../../src/types/apm.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IAEb,mBAAmB;IACnB,GAAG,IAAI,IAAI,CAAC;IAEZ,mCAAmC;IACnC,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,EAAE,GAAG,SAAS,GAAG,IAAI,CAAC;IAEzF,+BAA+B;IAC/B,QAAQ,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAEpE,0BAA0B;IAC1B,SAAS,CA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CAAoC;IACpC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAEpD,kCAAkC;IAClC,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC;IAEzB,oCAAoC;IACpC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B,6BAA6B;IAC7B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,mBAAmB;IACnB,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,IAAI,CAAC;IAExE,4BAA4B;IAC5B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1B,6BAA6B;IAC7B,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B"}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ /**
3
+ * APM Common Types
4
+ *
5
+ * Defines common types that work with both Elastic APM and OpenTelemetry providers.
6
+ * This provides type safety and IntelliSense for users of this package.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ApmProvider = exports.OtlpTransport = exports.SpanKind = void 0;
10
+ /**
11
+ * Span kind enumeration
12
+ */
13
+ var SpanKind;
14
+ (function (SpanKind) {
15
+ SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
16
+ SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
17
+ SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
18
+ SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
19
+ SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
20
+ })(SpanKind || (exports.SpanKind = SpanKind = {}));
21
+ /**
22
+ * OTLP Transport types
23
+ */
24
+ var OtlpTransport;
25
+ (function (OtlpTransport) {
26
+ OtlpTransport["HTTP"] = "http";
27
+ OtlpTransport["GRPC"] = "grpc";
28
+ OtlpTransport["PROTO"] = "proto";
29
+ })(OtlpTransport || (exports.OtlpTransport = OtlpTransport = {}));
30
+ /**
31
+ * APM Provider types
32
+ */
33
+ var ApmProvider;
34
+ (function (ApmProvider) {
35
+ ApmProvider["ELASTIC"] = "elastic-apm";
36
+ ApmProvider["OPENTELEMETRY"] = "opentelemetry";
37
+ })(ApmProvider || (exports.ApmProvider = ApmProvider = {}));
@@ -0,0 +1,14 @@
1
+ /**
2
+ * OTLP Transport Type
3
+ *
4
+ * Defines the transport protocol used for exporting traces to APM
5
+ * - HTTP: Uses HTTP/JSON protocol (@opentelemetry/exporter-trace-otlp-http)
6
+ * - GRPC: Uses gRPC/protobuf protocol (@opentelemetry/exporter-trace-otlp-grpc)
7
+ * - PROTO: Uses protobuf over HTTP (@opentelemetry/exporter-trace-otlp-proto) - recommended for Elastic APM
8
+ */
9
+ export declare enum OtlpTransport {
10
+ HTTP = "http",
11
+ GRPC = "grpc",
12
+ PROTO = "proto"
13
+ }
14
+ //# sourceMappingURL=otlp-transport.type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otlp-transport.type.d.ts","sourceRoot":"","sources":["../../src/types/otlp-transport.type.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,IAAI,SAAS;IACb,KAAK,UAAU;CAChB"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OtlpTransport = void 0;
4
+ /**
5
+ * OTLP Transport Type
6
+ *
7
+ * Defines the transport protocol used for exporting traces to APM
8
+ * - HTTP: Uses HTTP/JSON protocol (@opentelemetry/exporter-trace-otlp-http)
9
+ * - GRPC: Uses gRPC/protobuf protocol (@opentelemetry/exporter-trace-otlp-grpc)
10
+ * - PROTO: Uses protobuf over HTTP (@opentelemetry/exporter-trace-otlp-proto) - recommended for Elastic APM
11
+ */
12
+ var OtlpTransport;
13
+ (function (OtlpTransport) {
14
+ OtlpTransport["HTTP"] = "http";
15
+ OtlpTransport["GRPC"] = "grpc";
16
+ OtlpTransport["PROTO"] = "proto";
17
+ })(OtlpTransport || (exports.OtlpTransport = OtlpTransport = {}));
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Debug Exporter Wrapper for OpenTelemetry OTLP
3
+ *
4
+ * Wraps the OTLPTraceExporter to log full request/response details
5
+ * when APM_DEBUG=true. This helps debug connection issues with APM servers.
6
+ */
7
+ /**
8
+ * Wrap OTLPTraceExporter with debug logging
9
+ *
10
+ * This intercepts the export calls to log:
11
+ * - Outgoing request details (URL, headers, body)
12
+ * - Response details (status code, headers, body)
13
+ * - Success/failure of exports
14
+ *
15
+ * @param baseExporter The original OTLPTraceExporter instance
16
+ * @param endpointUrl The OTLP endpoint URL for logging
17
+ * @returns Wrapped exporter with debug capabilities
18
+ */
19
+ export declare function createDebugExporter(baseExporter: {
20
+ export(spans: unknown[], callback: (result: unknown) => void): void;
21
+ shutdown(): Promise<void>;
22
+ forceFlush?(): Promise<void>;
23
+ }, endpointUrl: string): {
24
+ export(spans: unknown[], callback: (result: unknown) => void): void;
25
+ shutdown(): Promise<void>;
26
+ forceFlush?(): Promise<void>;
27
+ };
28
+ /**
29
+ * Create an HTTP interceptor for more detailed debugging
30
+ * This can be used to intercept the actual HTTP requests made by OTLP exporter
31
+ */
32
+ export declare function createHttpRequestInterceptor(): {
33
+ install(): void;
34
+ uninstall(): void;
35
+ } | null;
36
+ //# sourceMappingURL=debug-exporter-wrapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug-exporter-wrapper.d.ts","sourceRoot":"","sources":["../../src/utils/debug-exporter-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE;IACZ,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IACpE,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,EACD,WAAW,EAAE,MAAM,GAClB;IACD,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI,CAAC;IACpE,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1B,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,CA6FA;AAyBD;;;GAGG;AACH,wBAAgB,4BAA4B,IAAI;IAC9C,OAAO,IAAI,IAAI,CAAC;IAChB,SAAS,IAAI,IAAI,CAAC;CACnB,GAAG,IAAI,CAwKP"}
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ /**
3
+ * Debug Exporter Wrapper for OpenTelemetry OTLP
4
+ *
5
+ * Wraps the OTLPTraceExporter to log full request/response details
6
+ * when APM_DEBUG=true. This helps debug connection issues with APM servers.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createDebugExporter = createDebugExporter;
10
+ exports.createHttpRequestInterceptor = createHttpRequestInterceptor;
11
+ const debug_logger_1 = require("./debug-logger");
12
+ /**
13
+ * Wrap OTLPTraceExporter with debug logging
14
+ *
15
+ * This intercepts the export calls to log:
16
+ * - Outgoing request details (URL, headers, body)
17
+ * - Response details (status code, headers, body)
18
+ * - Success/failure of exports
19
+ *
20
+ * @param baseExporter The original OTLPTraceExporter instance
21
+ * @param endpointUrl The OTLP endpoint URL for logging
22
+ * @returns Wrapped exporter with debug capabilities
23
+ */
24
+ function createDebugExporter(baseExporter, endpointUrl) {
25
+ if (!(0, debug_logger_1.isDebugEnabled)()) {
26
+ // When debug is off, just log errors only
27
+ return {
28
+ export: (spans, resultCallback) => {
29
+ baseExporter.export(spans, (result) => {
30
+ const exportResult = result;
31
+ if (exportResult.error) {
32
+ (0, debug_logger_1.logExportFailure)("OpenTelemetry", exportResult.error);
33
+ }
34
+ resultCallback(result);
35
+ });
36
+ },
37
+ shutdown: async () => {
38
+ await baseExporter.shutdown();
39
+ },
40
+ forceFlush: async () => {
41
+ if (baseExporter.forceFlush) {
42
+ await baseExporter.forceFlush();
43
+ }
44
+ },
45
+ };
46
+ }
47
+ // Debug mode ON - intercept everything
48
+ const wrappedExporter = {
49
+ export: (spans, resultCallback) => {
50
+ const startTime = Date.now();
51
+ // Log what we're about to send
52
+ (0, debug_logger_1.logRequest)("OpenTelemetry", endpointUrl, getExporterHeaders(baseExporter), {
53
+ spanCount: spans.length,
54
+ spans: spans.map((s) => {
55
+ const span = s;
56
+ return {
57
+ name: span.name,
58
+ kind: span.kind,
59
+ startTime: span.startTime,
60
+ endTime: span.endTime,
61
+ };
62
+ }),
63
+ });
64
+ // Wrap the result callback to capture response
65
+ baseExporter.export(spans, (result) => {
66
+ const duration = Date.now() - startTime;
67
+ const exportResult = result;
68
+ if (exportResult.error) {
69
+ (0, debug_logger_1.logExportFailure)("OpenTelemetry", exportResult.error, {
70
+ endpoint: endpointUrl,
71
+ spanCount: spans.length,
72
+ duration: `${duration}ms`,
73
+ });
74
+ }
75
+ else {
76
+ (0, debug_logger_1.logExportSuccess)("OpenTelemetry", spans.length);
77
+ (0, debug_logger_1.logResponse)("OpenTelemetry", 200, { "content-type": "application/json" }, {
78
+ message: `Exported ${spans.length} span(s) in ${duration}ms`,
79
+ });
80
+ }
81
+ resultCallback(result);
82
+ });
83
+ },
84
+ shutdown: async () => {
85
+ (0, debug_logger_1.logRequest)("OpenTelemetry", endpointUrl, {}, { action: "shutdown" });
86
+ try {
87
+ await baseExporter.shutdown();
88
+ (0, debug_logger_1.logResponse)("OpenTelemetry", 200, {}, { message: "Exporter shutdown complete" });
89
+ }
90
+ catch (err) {
91
+ const errorMessage = err instanceof Error ? err.message : String(err);
92
+ (0, debug_logger_1.logResponse)("OpenTelemetry", 500, {}, { message: "Exporter shutdown failed", error: errorMessage });
93
+ throw err;
94
+ }
95
+ },
96
+ forceFlush: async () => {
97
+ if (baseExporter.forceFlush) {
98
+ await baseExporter.forceFlush();
99
+ }
100
+ },
101
+ };
102
+ return wrappedExporter;
103
+ }
104
+ /**
105
+ * Get headers from the exporter instance for logging
106
+ * The OTLPTraceExporter stores headers internally, we need to extract them
107
+ */
108
+ function getExporterHeaders(exporter) {
109
+ try {
110
+ // Try to access headers from the exporter
111
+ // @ts-ignore - accessing internal property
112
+ if (exporter._headers) {
113
+ // @ts-ignore
114
+ return { ...exporter._headers };
115
+ }
116
+ // @ts-ignore - alternative internal property
117
+ if (exporter.headers) {
118
+ // @ts-ignore
119
+ return { ...exporter.headers };
120
+ }
121
+ }
122
+ catch {
123
+ // Silently fail if we can't access headers
124
+ }
125
+ return {};
126
+ }
127
+ /**
128
+ * Create an HTTP interceptor for more detailed debugging
129
+ * This can be used to intercept the actual HTTP requests made by OTLP exporter
130
+ */
131
+ function createHttpRequestInterceptor() {
132
+ if (!(0, debug_logger_1.isDebugEnabled)()) {
133
+ return null;
134
+ }
135
+ const originalRequest = require("http").request;
136
+ const originalHttpsRequest = require("https").request;
137
+ return {
138
+ install: () => {
139
+ const { logRequest: logReq, logResponse: logResp } = require("./debug-logger");
140
+ // Override http.request
141
+ require("http").request = function (options, ...args) {
142
+ const opts = typeof options === "string" ? undefined : options;
143
+ let url;
144
+ if (typeof options === "string") {
145
+ url = options;
146
+ }
147
+ else {
148
+ const protocol = opts?.protocol || "http:";
149
+ const hostname = opts?.hostname || opts?.host || "localhost";
150
+ const port = opts?.port;
151
+ const path = opts?.path || "/";
152
+ // Build URL with port if specified
153
+ const hostWithPort = port ? `${hostname}:${port}` : hostname;
154
+ url = `${protocol}//${hostWithPort}${path}`;
155
+ }
156
+ // Only intercept APM-related requests
157
+ if (url.includes("/v1/traces") ||
158
+ url.includes("/v1/metrics") ||
159
+ url.includes("/intake/v2/traces")) {
160
+ const req = originalRequest.call(this, options, ...args);
161
+ const originalWrite = req.write.bind(req);
162
+ req.write = function (chunk, ...rest) {
163
+ // Handle binary data safely - don't stringify protobuf/binary payloads
164
+ let logData;
165
+ if (Buffer.isBuffer(chunk) || chunk instanceof Uint8Array) {
166
+ logData = `<binary data: ${chunk.length} bytes>`;
167
+ }
168
+ else if (typeof chunk === "string") {
169
+ logData = chunk;
170
+ }
171
+ else {
172
+ logData = chunk?.toString();
173
+ }
174
+ logReq("OpenTelemetry", url, typeof opts === "object" ? (opts.headers || {}) : {}, logData);
175
+ return originalWrite(chunk, ...rest);
176
+ };
177
+ req.on("response", (res) => {
178
+ let body = "";
179
+ res.on("data", (chunk) => {
180
+ body += chunk;
181
+ });
182
+ res.on("end", () => {
183
+ logResp("OpenTelemetry", res.statusCode, res.headers, body);
184
+ });
185
+ });
186
+ return req;
187
+ }
188
+ return originalRequest.call(this, options, ...args);
189
+ };
190
+ // Override https.request
191
+ require("https").request = function (options, ...args) {
192
+ const opts = typeof options === "string" ? undefined : options;
193
+ let url;
194
+ if (typeof options === "string") {
195
+ url = options;
196
+ }
197
+ else {
198
+ const protocol = opts?.protocol || "https:";
199
+ const hostname = opts?.hostname || opts?.host || "localhost";
200
+ const port = opts?.port;
201
+ const path = opts?.path || "/";
202
+ // Build URL with port if specified
203
+ const hostWithPort = port ? `${hostname}:${port}` : hostname;
204
+ url = `${protocol}//${hostWithPort}${path}`;
205
+ }
206
+ // Only intercept APM-related requests
207
+ if (url.includes("/v1/traces") ||
208
+ url.includes("/v1/metrics") ||
209
+ url.includes("/intake/v2/traces")) {
210
+ const req = originalHttpsRequest.call(this, options, ...args);
211
+ const originalWrite = req.write.bind(req);
212
+ req.write = function (chunk, ...rest) {
213
+ // Handle binary data safely - don't stringify protobuf/binary payloads
214
+ let logData;
215
+ if (Buffer.isBuffer(chunk) || chunk instanceof Uint8Array) {
216
+ logData = `<binary data: ${chunk.length} bytes>`;
217
+ }
218
+ else if (typeof chunk === "string") {
219
+ logData = chunk;
220
+ }
221
+ else {
222
+ logData = chunk?.toString();
223
+ }
224
+ logReq("OpenTelemetry", url, typeof opts === "object" ? (opts.headers || {}) : {}, logData);
225
+ return originalWrite(chunk, ...rest);
226
+ };
227
+ req.on("response", (res) => {
228
+ let body = "";
229
+ res.on("data", (chunk) => {
230
+ body += chunk;
231
+ });
232
+ res.on("end", () => {
233
+ logResp("OpenTelemetry", res.statusCode, res.headers, body);
234
+ });
235
+ });
236
+ return req;
237
+ }
238
+ return originalHttpsRequest.call(this, options, ...args);
239
+ };
240
+ },
241
+ uninstall: () => {
242
+ // Restore original functions
243
+ require("http").request = originalRequest;
244
+ require("https").request = originalHttpsRequest;
245
+ },
246
+ };
247
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Debug Logger for APM Package
3
+ *
4
+ * Provides centralized debug logging that only outputs when APM_DEBUG=true.
5
+ * Used by both Elastic APM and OpenTelemetry providers.
6
+ */
7
+ /**
8
+ * Represents a span for logging purposes
9
+ */
10
+ interface LogSpan {
11
+ name: string;
12
+ kind: number;
13
+ startTime: number;
14
+ endTime: number;
15
+ attributes?: Record<string, unknown>;
16
+ status?: unknown;
17
+ }
18
+ /**
19
+ * Configuration object for APM providers
20
+ */
21
+ type ApmConfig = Record<string, unknown> | unknown[];
22
+ /**
23
+ * Request details for export failure logging
24
+ */
25
+ interface ExportFailureDetails {
26
+ endpoint?: string;
27
+ spanCount?: number;
28
+ duration?: string;
29
+ [key: string]: unknown;
30
+ }
31
+ /**
32
+ * Check if debug mode is enabled via environment variable
33
+ */
34
+ export declare function isDebugEnabled(): boolean;
35
+ /**
36
+ * Sanitize headers for logging (mask sensitive values)
37
+ */
38
+ export declare function sanitizeHeaders(headers: Record<string, string>): Record<string, string>;
39
+ /**
40
+ * Sanitize config object for logging (mask sensitive values)
41
+ * Handles arrays and circular references safely
42
+ */
43
+ export declare function sanitizeConfig<T extends ApmConfig>(config: T, seen?: WeakMap<object, unknown>): T;
44
+ /**
45
+ * Debug log - only outputs when APM_DEBUG=true
46
+ */
47
+ export declare function debugLog(message: string, ...args: unknown[]): void;
48
+ /**
49
+ * Info log - always outputs
50
+ */
51
+ export declare function infoLog(message: string, ...args: unknown[]): void;
52
+ /**
53
+ * Error log - always outputs
54
+ */
55
+ export declare function errorLog(message: string, ...args: unknown[]): void;
56
+ /**
57
+ * Log initialization details
58
+ */
59
+ export declare function logInitialization(provider: string, config: ApmConfig): void;
60
+ /**
61
+ * Log span export success (debug mode only)
62
+ */
63
+ export declare function logExportSuccess(provider: string, spanCount: number): void;
64
+ /**
65
+ * Log span export failure - ALWAYS logs regardless of debug mode
66
+ */
67
+ export declare function logExportFailure(provider: string, error: Error | unknown, requestDetails?: ExportFailureDetails): void;
68
+ /**
69
+ * Log outgoing HTTP request (for debugging APM communication)
70
+ */
71
+ export declare function logRequest(provider: string, url: string, headers: Record<string, string>, body?: unknown): void;
72
+ /**
73
+ * Log incoming HTTP response (for debugging APM communication)
74
+ */
75
+ export declare function logResponse(provider: string, statusCode: number, headers: Record<string, string>, body?: unknown): void;
76
+ /**
77
+ * Log span details (when debug mode is on)
78
+ */
79
+ export declare function logSpan(provider: string, span: LogSpan): void;
80
+ export {};
81
+ //# sourceMappingURL=debug-logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug-logger.d.ts","sourceRoot":"","sources":["../../src/utils/debug-logger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,UAAU,OAAO;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,KAAK,SAAS,GAAG,MAAM,CAAC,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