@noony-serverless/core 0.3.4 → 0.4.1
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 +199 -0
- package/build/core/containerPool.d.ts +129 -26
- package/build/core/containerPool.js +213 -68
- package/build/core/handler.d.ts +2 -2
- package/build/core/handler.js +6 -12
- package/build/core/index.d.ts +1 -0
- package/build/core/index.js +1 -0
- package/build/core/logger.d.ts +89 -1
- package/build/core/logger.js +136 -5
- package/build/core/telemetry/config.d.ts +331 -0
- package/build/core/telemetry/config.js +153 -0
- package/build/core/telemetry/index.d.ts +22 -0
- package/build/core/telemetry/index.js +45 -0
- package/build/core/telemetry/provider.d.ts +203 -0
- package/build/core/telemetry/provider.js +3 -0
- package/build/core/telemetry/providers/console-provider.d.ts +54 -0
- package/build/core/telemetry/providers/console-provider.js +124 -0
- package/build/core/telemetry/providers/index.d.ts +10 -0
- package/build/core/telemetry/providers/index.js +19 -0
- package/build/core/telemetry/providers/noop-provider.d.ts +51 -0
- package/build/core/telemetry/providers/noop-provider.js +67 -0
- package/build/core/telemetry/providers/opentelemetry-provider.d.ts +102 -0
- package/build/core/telemetry/providers/opentelemetry-provider.js +342 -0
- package/build/middlewares/bodyValidationMiddleware.js +1 -1
- package/build/middlewares/dependencyInjectionMiddleware.d.ts +16 -8
- package/build/middlewares/dependencyInjectionMiddleware.js +31 -11
- package/build/middlewares/guards/adapters/CustomTokenVerificationPortAdapter.d.ts +1 -1
- package/build/middlewares/guards/guards/FastAuthGuard.d.ts +5 -5
- package/build/middlewares/guards/guards/FastAuthGuard.js +3 -2
- package/build/middlewares/guards/guards/PermissionGuardFactory.d.ts +7 -9
- package/build/middlewares/guards/resolvers/ExpressionPermissionResolver.d.ts +1 -1
- package/build/middlewares/guards/resolvers/ExpressionPermissionResolver.js +1 -1
- package/build/middlewares/guards/resolvers/PermissionResolver.d.ts +1 -1
- package/build/middlewares/guards/resolvers/PlainPermissionResolver.d.ts +1 -1
- package/build/middlewares/guards/resolvers/WildcardPermissionResolver.d.ts +1 -1
- package/build/middlewares/guards/services/FastUserContextService.d.ts +11 -32
- package/build/middlewares/index.d.ts +1 -0
- package/build/middlewares/index.js +1 -0
- package/build/middlewares/openTelemetryMiddleware.d.ts +162 -0
- package/build/middlewares/openTelemetryMiddleware.js +359 -0
- package/build/middlewares/rateLimitingMiddleware.js +16 -5
- package/build/utils/container.utils.js +4 -1
- package/build/utils/fastify-wrapper.d.ts +74 -0
- package/build/utils/fastify-wrapper.js +175 -0
- package/build/utils/index.d.ts +4 -0
- package/build/utils/index.js +23 -1
- package/build/utils/otel.helper.d.ts +122 -0
- package/build/utils/otel.helper.js +258 -0
- package/build/utils/pubsub-trace.utils.d.ts +102 -0
- package/build/utils/pubsub-trace.utils.js +155 -0
- package/build/utils/wrapper-utils.d.ts +177 -0
- package/build/utils/wrapper-utils.js +236 -0
- package/package.json +61 -2
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Telemetry Module
|
|
4
|
+
*
|
|
5
|
+
* Provides OpenTelemetry integration for the Noony framework with:
|
|
6
|
+
* - Extensible provider system (OTEL, New Relic, Datadog, Console, Noop)
|
|
7
|
+
* - Auto-detection from environment variables
|
|
8
|
+
* - Graceful degradation when configuration is missing
|
|
9
|
+
* - Zero-configuration local development support
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* import { OpenTelemetryMiddleware } from '@noony-serverless/core';
|
|
13
|
+
*
|
|
14
|
+
* const handler = new Handler()
|
|
15
|
+
* .use(new OpenTelemetryMiddleware()) // Auto-detects provider
|
|
16
|
+
* .handle(async (context) => {
|
|
17
|
+
* // Your business logic
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
23
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
24
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
25
|
+
}
|
|
26
|
+
Object.defineProperty(o, k2, desc);
|
|
27
|
+
}) : (function(o, m, k, k2) {
|
|
28
|
+
if (k2 === undefined) k2 = k;
|
|
29
|
+
o[k2] = m[k];
|
|
30
|
+
}));
|
|
31
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
32
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.getDefaultTelemetryConfig = exports.isRunningOnGCP = exports.TelemetryPresets = void 0;
|
|
36
|
+
// Core interfaces and types
|
|
37
|
+
__exportStar(require("./provider"), exports);
|
|
38
|
+
// Configuration and presets (explicit exports to avoid conflict)
|
|
39
|
+
var config_1 = require("./config");
|
|
40
|
+
Object.defineProperty(exports, "TelemetryPresets", { enumerable: true, get: function () { return config_1.TelemetryPresets; } });
|
|
41
|
+
Object.defineProperty(exports, "isRunningOnGCP", { enumerable: true, get: function () { return config_1.isRunningOnGCP; } });
|
|
42
|
+
Object.defineProperty(exports, "getDefaultTelemetryConfig", { enumerable: true, get: function () { return config_1.getDefaultTelemetryConfig; } });
|
|
43
|
+
// Provider implementations
|
|
44
|
+
__exportStar(require("./providers"), exports);
|
|
45
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { Context } from '../core';
|
|
2
|
+
/**
|
|
3
|
+
* Validation result from TelemetryProvider.validate()
|
|
4
|
+
*/
|
|
5
|
+
export interface ValidationResult {
|
|
6
|
+
valid: boolean;
|
|
7
|
+
reason?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generic span interface that works across different telemetry providers
|
|
11
|
+
* This provides a common interface for OTEL, New Relic, Datadog, etc.
|
|
12
|
+
*/
|
|
13
|
+
export interface GenericSpan {
|
|
14
|
+
setAttributes(attributes: Record<string, unknown>): void;
|
|
15
|
+
recordException(error: Error): void;
|
|
16
|
+
setStatus(status: {
|
|
17
|
+
code: number;
|
|
18
|
+
message?: string;
|
|
19
|
+
}): void;
|
|
20
|
+
end(): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Base interface for telemetry providers
|
|
24
|
+
*
|
|
25
|
+
* This interface allows custom implementations for different APM platforms:
|
|
26
|
+
* - Standard OpenTelemetry SDK
|
|
27
|
+
* - New Relic Agent
|
|
28
|
+
* - Datadog Tracer
|
|
29
|
+
* - Custom implementations
|
|
30
|
+
*
|
|
31
|
+
* All providers must implement validation to ensure graceful degradation
|
|
32
|
+
* when configuration is missing or invalid.
|
|
33
|
+
*/
|
|
34
|
+
export interface TelemetryProvider {
|
|
35
|
+
/**
|
|
36
|
+
* Provider name for identification
|
|
37
|
+
* Examples: 'opentelemetry', 'newrelic', 'datadog', 'console', 'noop'
|
|
38
|
+
*/
|
|
39
|
+
readonly name: string;
|
|
40
|
+
/**
|
|
41
|
+
* Validate provider configuration before initialization
|
|
42
|
+
*
|
|
43
|
+
* This method checks:
|
|
44
|
+
* - Required environment variables are set
|
|
45
|
+
* - Required npm packages are installed
|
|
46
|
+
* - Configuration is valid (e.g., valid URLs)
|
|
47
|
+
*
|
|
48
|
+
* @returns Validation result with optional reason if invalid
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const result = await provider.validate();
|
|
52
|
+
* if (!result.valid) {
|
|
53
|
+
* console.warn(`Provider validation failed: ${result.reason}`);
|
|
54
|
+
* // Fall back to NoopProvider
|
|
55
|
+
* }
|
|
56
|
+
*/
|
|
57
|
+
validate(): Promise<ValidationResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Initialize the telemetry provider with configuration
|
|
60
|
+
*
|
|
61
|
+
* This is called once at application startup, not per-request.
|
|
62
|
+
* Providers should set up SDK, exporters, and any global configuration.
|
|
63
|
+
*
|
|
64
|
+
* @param config Telemetry configuration
|
|
65
|
+
* @throws Should not throw - errors should be caught and logged internally
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* await provider.initialize({
|
|
69
|
+
* serviceName: 'order-service',
|
|
70
|
+
* serviceVersion: '1.0.0',
|
|
71
|
+
* environment: 'production'
|
|
72
|
+
* });
|
|
73
|
+
*/
|
|
74
|
+
initialize(config: TelemetryConfig): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Create a span for the current request
|
|
77
|
+
*
|
|
78
|
+
* This is called in the middleware `before` hook for each request.
|
|
79
|
+
* Returns undefined if provider is not ready or span creation fails.
|
|
80
|
+
*
|
|
81
|
+
* @param context The Noony request context
|
|
82
|
+
* @returns Span object or undefined if span creation fails
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const span = provider.createSpan(context);
|
|
86
|
+
* if (span) {
|
|
87
|
+
* span.setAttributes({ 'user.id': context.user?.id });
|
|
88
|
+
* context.businessData.set('otel_span', span);
|
|
89
|
+
* }
|
|
90
|
+
*/
|
|
91
|
+
createSpan(context: Context<unknown, unknown>): GenericSpan | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* Record a metric (histogram, counter, gauge, etc.)
|
|
94
|
+
*
|
|
95
|
+
* @param name Metric name (e.g., 'http.request.duration')
|
|
96
|
+
* @param value Metric value
|
|
97
|
+
* @param attributes Optional metric attributes/tags
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* provider.recordMetric('http.request.duration', 123.45, {
|
|
101
|
+
* 'http.method': 'POST',
|
|
102
|
+
* 'http.status_code': 200
|
|
103
|
+
* });
|
|
104
|
+
*/
|
|
105
|
+
recordMetric(name: string, value: number, attributes?: Record<string, unknown>): void;
|
|
106
|
+
/**
|
|
107
|
+
* Log with trace correlation
|
|
108
|
+
*
|
|
109
|
+
* Logs should include trace and span IDs when available for correlation.
|
|
110
|
+
*
|
|
111
|
+
* @param level Log level (info, error, warn, debug)
|
|
112
|
+
* @param message Log message
|
|
113
|
+
* @param attributes Additional log attributes
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* provider.log('error', 'Request failed', {
|
|
117
|
+
* 'error.type': 'ValidationError',
|
|
118
|
+
* 'user.id': userId
|
|
119
|
+
* });
|
|
120
|
+
*/
|
|
121
|
+
log(level: string, message: string, attributes?: Record<string, unknown>): void;
|
|
122
|
+
/**
|
|
123
|
+
* Check if provider is initialized and ready to use
|
|
124
|
+
*
|
|
125
|
+
* @returns True if provider is ready, false otherwise
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* if (!provider.isReady()) {
|
|
129
|
+
* console.warn('Provider not ready, skipping telemetry');
|
|
130
|
+
* }
|
|
131
|
+
*/
|
|
132
|
+
isReady(): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Shutdown the provider gracefully
|
|
135
|
+
*
|
|
136
|
+
* Called during application shutdown to flush any pending telemetry data.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* process.on('SIGTERM', async () => {
|
|
140
|
+
* await provider.shutdown();
|
|
141
|
+
* process.exit(0);
|
|
142
|
+
* });
|
|
143
|
+
*/
|
|
144
|
+
shutdown(): Promise<void>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Telemetry configuration interface
|
|
148
|
+
*/
|
|
149
|
+
export interface TelemetryConfig {
|
|
150
|
+
/** Enable telemetry (defaults to true) */
|
|
151
|
+
enabled?: boolean;
|
|
152
|
+
/** Service name for telemetry */
|
|
153
|
+
serviceName: string;
|
|
154
|
+
/** Service version (optional) */
|
|
155
|
+
serviceVersion?: string;
|
|
156
|
+
/** Environment (e.g., 'production', 'staging', 'development') */
|
|
157
|
+
environment?: string;
|
|
158
|
+
/** Exporter configurations */
|
|
159
|
+
exporters?: {
|
|
160
|
+
traces?: OTLPExporterConfig[];
|
|
161
|
+
metrics?: OTLPExporterConfig[];
|
|
162
|
+
logs?: OTLPExporterConfig[];
|
|
163
|
+
};
|
|
164
|
+
/** Sampling configuration */
|
|
165
|
+
sampling?: {
|
|
166
|
+
/** Sampling ratio (0.0 to 1.0) */
|
|
167
|
+
ratio?: number;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Propagation format configuration
|
|
171
|
+
* Controls which trace context formats to support
|
|
172
|
+
*/
|
|
173
|
+
propagation?: {
|
|
174
|
+
/**
|
|
175
|
+
* Enable Cloud Trace propagation for Google Cloud Platform
|
|
176
|
+
* Uses X-Cloud-Trace-Context header format
|
|
177
|
+
* @default true when running on GCP
|
|
178
|
+
*/
|
|
179
|
+
cloudTrace?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Enable W3C Trace Context propagation
|
|
182
|
+
* Uses traceparent/tracestate header format
|
|
183
|
+
* @default true
|
|
184
|
+
*/
|
|
185
|
+
w3c?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Additional custom propagators
|
|
188
|
+
*/
|
|
189
|
+
custom?: string[];
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* OTLP Exporter configuration
|
|
194
|
+
*/
|
|
195
|
+
export interface OTLPExporterConfig {
|
|
196
|
+
/** Exporter endpoint URL */
|
|
197
|
+
endpoint: string;
|
|
198
|
+
/** Optional headers (e.g., API keys) */
|
|
199
|
+
headers?: Record<string, string>;
|
|
200
|
+
/** Timeout in milliseconds (optional) */
|
|
201
|
+
timeout?: number;
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Context } from '../../core';
|
|
2
|
+
import { TelemetryProvider, ValidationResult, GenericSpan, TelemetryConfig } from '../provider';
|
|
3
|
+
/**
|
|
4
|
+
* Console Telemetry Provider
|
|
5
|
+
*
|
|
6
|
+
* Logs all telemetry data to the console for local development and debugging.
|
|
7
|
+
* This provider is useful for:
|
|
8
|
+
* 1. Local development without external telemetry infrastructure
|
|
9
|
+
* 2. Debugging telemetry integration
|
|
10
|
+
* 3. Testing span creation and attributes
|
|
11
|
+
*
|
|
12
|
+
* Auto-selected when NODE_ENV=development and no OTEL endpoint is configured.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Auto-detected in development
|
|
16
|
+
* NODE_ENV=development
|
|
17
|
+
*
|
|
18
|
+
* // Or explicitly configured
|
|
19
|
+
* const provider = new ConsoleProvider();
|
|
20
|
+
* await provider.initialize({ serviceName: 'my-service' });
|
|
21
|
+
*/
|
|
22
|
+
export declare class ConsoleProvider implements TelemetryProvider {
|
|
23
|
+
readonly name = "console";
|
|
24
|
+
private enabled;
|
|
25
|
+
/**
|
|
26
|
+
* Console provider is always valid (useful for local testing)
|
|
27
|
+
*/
|
|
28
|
+
validate(): Promise<ValidationResult>;
|
|
29
|
+
/**
|
|
30
|
+
* Initialize console provider
|
|
31
|
+
*/
|
|
32
|
+
initialize(config: TelemetryConfig): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Create a console span that logs to stdout
|
|
35
|
+
*/
|
|
36
|
+
createSpan(context: Context<unknown, unknown>): GenericSpan | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Log metric to console
|
|
39
|
+
*/
|
|
40
|
+
recordMetric(name: string, value: number, attributes?: Record<string, unknown>): void;
|
|
41
|
+
/**
|
|
42
|
+
* Log message to console with level
|
|
43
|
+
*/
|
|
44
|
+
log(level: string, message: string, attributes?: Record<string, unknown>): void;
|
|
45
|
+
/**
|
|
46
|
+
* Console provider is always ready if enabled
|
|
47
|
+
*/
|
|
48
|
+
isReady(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Shutdown console provider
|
|
51
|
+
*/
|
|
52
|
+
shutdown(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=console-provider.d.ts.map
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConsoleProvider = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Console Telemetry Provider
|
|
6
|
+
*
|
|
7
|
+
* Logs all telemetry data to the console for local development and debugging.
|
|
8
|
+
* This provider is useful for:
|
|
9
|
+
* 1. Local development without external telemetry infrastructure
|
|
10
|
+
* 2. Debugging telemetry integration
|
|
11
|
+
* 3. Testing span creation and attributes
|
|
12
|
+
*
|
|
13
|
+
* Auto-selected when NODE_ENV=development and no OTEL endpoint is configured.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Auto-detected in development
|
|
17
|
+
* NODE_ENV=development
|
|
18
|
+
*
|
|
19
|
+
* // Or explicitly configured
|
|
20
|
+
* const provider = new ConsoleProvider();
|
|
21
|
+
* await provider.initialize({ serviceName: 'my-service' });
|
|
22
|
+
*/
|
|
23
|
+
class ConsoleProvider {
|
|
24
|
+
name = 'console';
|
|
25
|
+
enabled = false;
|
|
26
|
+
/**
|
|
27
|
+
* Console provider is always valid (useful for local testing)
|
|
28
|
+
*/
|
|
29
|
+
async validate() {
|
|
30
|
+
return { valid: true };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Initialize console provider
|
|
34
|
+
*/
|
|
35
|
+
async initialize(config) {
|
|
36
|
+
this.enabled = config.enabled !== false;
|
|
37
|
+
if (this.enabled) {
|
|
38
|
+
console.log('[Telemetry] Console provider initialized');
|
|
39
|
+
console.log('[Telemetry] Service:', config.serviceName);
|
|
40
|
+
console.log('[Telemetry] Version:', config.serviceVersion || 'unknown');
|
|
41
|
+
console.log('[Telemetry] Environment:', config.environment || 'development');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a console span that logs to stdout
|
|
46
|
+
*/
|
|
47
|
+
createSpan(context) {
|
|
48
|
+
if (!this.enabled)
|
|
49
|
+
return undefined;
|
|
50
|
+
const spanData = {
|
|
51
|
+
startTime: Date.now(),
|
|
52
|
+
method: context.req.method,
|
|
53
|
+
path: context.req.path || context.req.url,
|
|
54
|
+
requestId: context.requestId,
|
|
55
|
+
};
|
|
56
|
+
console.log('[Telemetry] 🟢 Span started:', spanData);
|
|
57
|
+
return {
|
|
58
|
+
setAttributes: (attrs) => {
|
|
59
|
+
console.log('[Telemetry] 📊 Span attributes:', attrs);
|
|
60
|
+
},
|
|
61
|
+
recordException: (error) => {
|
|
62
|
+
console.error('[Telemetry] ❌ Exception:', {
|
|
63
|
+
message: error.message,
|
|
64
|
+
name: error.name,
|
|
65
|
+
stack: error.stack,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
setStatus: (status) => {
|
|
69
|
+
const statusIcon = status.code === 0 ? '✅' : '⚠️';
|
|
70
|
+
console.log(`[Telemetry] ${statusIcon} Span status:`, status);
|
|
71
|
+
},
|
|
72
|
+
end: () => {
|
|
73
|
+
const duration = Date.now() - spanData.startTime;
|
|
74
|
+
console.log('[Telemetry] 🔴 Span ended:', {
|
|
75
|
+
...spanData,
|
|
76
|
+
duration: `${duration}ms`,
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Log metric to console
|
|
83
|
+
*/
|
|
84
|
+
recordMetric(name, value, attributes) {
|
|
85
|
+
if (!this.enabled)
|
|
86
|
+
return;
|
|
87
|
+
console.log('[Telemetry] 📈 Metric:', {
|
|
88
|
+
name,
|
|
89
|
+
value,
|
|
90
|
+
attributes,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Log message to console with level
|
|
95
|
+
*/
|
|
96
|
+
log(level, message, attributes) {
|
|
97
|
+
if (!this.enabled)
|
|
98
|
+
return;
|
|
99
|
+
const levelIcon = {
|
|
100
|
+
info: 'ℹ️',
|
|
101
|
+
error: '❌',
|
|
102
|
+
warn: '⚠️',
|
|
103
|
+
debug: '🔍',
|
|
104
|
+
}[level] || '📝';
|
|
105
|
+
console.log(`[Telemetry] ${levelIcon} Log [${level}]:`, message, attributes);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Console provider is always ready if enabled
|
|
109
|
+
*/
|
|
110
|
+
isReady() {
|
|
111
|
+
return this.enabled;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Shutdown console provider
|
|
115
|
+
*/
|
|
116
|
+
async shutdown() {
|
|
117
|
+
if (this.enabled) {
|
|
118
|
+
console.log('[Telemetry] Console provider shutdown');
|
|
119
|
+
}
|
|
120
|
+
this.enabled = false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.ConsoleProvider = ConsoleProvider;
|
|
124
|
+
//# sourceMappingURL=console-provider.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry Providers
|
|
3
|
+
*
|
|
4
|
+
* This module exports all available telemetry providers.
|
|
5
|
+
* Providers can be used directly or auto-detected by OpenTelemetryMiddleware.
|
|
6
|
+
*/
|
|
7
|
+
export { NoopProvider } from './noop-provider';
|
|
8
|
+
export { ConsoleProvider } from './console-provider';
|
|
9
|
+
export { OpenTelemetryProvider } from './opentelemetry-provider';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Telemetry Providers
|
|
4
|
+
*
|
|
5
|
+
* This module exports all available telemetry providers.
|
|
6
|
+
* Providers can be used directly or auto-detected by OpenTelemetryMiddleware.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.OpenTelemetryProvider = exports.ConsoleProvider = exports.NoopProvider = void 0;
|
|
10
|
+
var noop_provider_1 = require("./noop-provider");
|
|
11
|
+
Object.defineProperty(exports, "NoopProvider", { enumerable: true, get: function () { return noop_provider_1.NoopProvider; } });
|
|
12
|
+
var console_provider_1 = require("./console-provider");
|
|
13
|
+
Object.defineProperty(exports, "ConsoleProvider", { enumerable: true, get: function () { return console_provider_1.ConsoleProvider; } });
|
|
14
|
+
var opentelemetry_provider_1 = require("./opentelemetry-provider");
|
|
15
|
+
Object.defineProperty(exports, "OpenTelemetryProvider", { enumerable: true, get: function () { return opentelemetry_provider_1.OpenTelemetryProvider; } });
|
|
16
|
+
// Note: New Relic and Datadog providers are optional and can be added when needed
|
|
17
|
+
// export { NewRelicProvider } from './newrelic-provider';
|
|
18
|
+
// export { DatadogProvider } from './datadog-provider';
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Context } from '../../core';
|
|
2
|
+
import { TelemetryProvider, ValidationResult, GenericSpan, TelemetryConfig } from '../provider';
|
|
3
|
+
/**
|
|
4
|
+
* Noop (No-operation) Telemetry Provider
|
|
5
|
+
*
|
|
6
|
+
* This provider does nothing and is used as:
|
|
7
|
+
* 1. Fallback when other providers fail validation
|
|
8
|
+
* 2. Default when telemetry is disabled (NODE_ENV=test)
|
|
9
|
+
* 3. Placeholder when no configuration is provided
|
|
10
|
+
*
|
|
11
|
+
* It implements all TelemetryProvider methods as no-ops to ensure
|
|
12
|
+
* the application continues to work even when telemetry fails.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Automatically used as fallback
|
|
16
|
+
* const provider = new NoopProvider();
|
|
17
|
+
* await provider.initialize({ serviceName: 'test' });
|
|
18
|
+
* const span = provider.createSpan(context); // Returns undefined
|
|
19
|
+
*/
|
|
20
|
+
export declare class NoopProvider implements TelemetryProvider {
|
|
21
|
+
readonly name = "noop";
|
|
22
|
+
/**
|
|
23
|
+
* Noop provider is always valid
|
|
24
|
+
*/
|
|
25
|
+
validate(): Promise<ValidationResult>;
|
|
26
|
+
/**
|
|
27
|
+
* No-op initialization
|
|
28
|
+
*/
|
|
29
|
+
initialize(_config: TelemetryConfig): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Always returns undefined (no span created)
|
|
32
|
+
*/
|
|
33
|
+
createSpan(_context: Context<unknown, unknown>): GenericSpan | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* No-op metric recording
|
|
36
|
+
*/
|
|
37
|
+
recordMetric(_name: string, _value: number, _attributes?: Record<string, unknown>): void;
|
|
38
|
+
/**
|
|
39
|
+
* No-op logging
|
|
40
|
+
*/
|
|
41
|
+
log(_level: string, _message: string, _attributes?: Record<string, unknown>): void;
|
|
42
|
+
/**
|
|
43
|
+
* Always ready (does nothing, so always ready)
|
|
44
|
+
*/
|
|
45
|
+
isReady(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* No-op shutdown
|
|
48
|
+
*/
|
|
49
|
+
shutdown(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=noop-provider.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoopProvider = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Noop (No-operation) Telemetry Provider
|
|
6
|
+
*
|
|
7
|
+
* This provider does nothing and is used as:
|
|
8
|
+
* 1. Fallback when other providers fail validation
|
|
9
|
+
* 2. Default when telemetry is disabled (NODE_ENV=test)
|
|
10
|
+
* 3. Placeholder when no configuration is provided
|
|
11
|
+
*
|
|
12
|
+
* It implements all TelemetryProvider methods as no-ops to ensure
|
|
13
|
+
* the application continues to work even when telemetry fails.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Automatically used as fallback
|
|
17
|
+
* const provider = new NoopProvider();
|
|
18
|
+
* await provider.initialize({ serviceName: 'test' });
|
|
19
|
+
* const span = provider.createSpan(context); // Returns undefined
|
|
20
|
+
*/
|
|
21
|
+
class NoopProvider {
|
|
22
|
+
name = 'noop';
|
|
23
|
+
/**
|
|
24
|
+
* Noop provider is always valid
|
|
25
|
+
*/
|
|
26
|
+
async validate() {
|
|
27
|
+
return { valid: true };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* No-op initialization
|
|
31
|
+
*/
|
|
32
|
+
async initialize(_config) {
|
|
33
|
+
// Do nothing
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Always returns undefined (no span created)
|
|
37
|
+
*/
|
|
38
|
+
createSpan(_context) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* No-op metric recording
|
|
43
|
+
*/
|
|
44
|
+
recordMetric(_name, _value, _attributes) {
|
|
45
|
+
// Do nothing
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* No-op logging
|
|
49
|
+
*/
|
|
50
|
+
log(_level, _message, _attributes) {
|
|
51
|
+
// Do nothing
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Always ready (does nothing, so always ready)
|
|
55
|
+
*/
|
|
56
|
+
isReady() {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* No-op shutdown
|
|
61
|
+
*/
|
|
62
|
+
async shutdown() {
|
|
63
|
+
// Do nothing
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.NoopProvider = NoopProvider;
|
|
67
|
+
//# sourceMappingURL=noop-provider.js.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Context } from '../../core';
|
|
2
|
+
import { TelemetryProvider, ValidationResult, GenericSpan, TelemetryConfig } from '../provider';
|
|
3
|
+
/**
|
|
4
|
+
* Standard OpenTelemetry SDK 2.0 Provider
|
|
5
|
+
*
|
|
6
|
+
* Implements telemetry using the official OpenTelemetry JavaScript SDK.
|
|
7
|
+
* Supports OTLP exporters for traces, metrics, and logs.
|
|
8
|
+
*
|
|
9
|
+
* Requirements:
|
|
10
|
+
* - Node.js >= 18.19.0 or >= 20.6.0
|
|
11
|
+
* - OTEL_EXPORTER_OTLP_ENDPOINT environment variable
|
|
12
|
+
* - @opentelemetry/sdk-node and related packages installed
|
|
13
|
+
*
|
|
14
|
+
* Auto-selected when OTEL_EXPORTER_OTLP_ENDPOINT is set.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // Environment setup
|
|
18
|
+
* OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/traces
|
|
19
|
+
*
|
|
20
|
+
* // Usage
|
|
21
|
+
* const provider = new OpenTelemetryProvider();
|
|
22
|
+
* const validation = await provider.validate();
|
|
23
|
+
* if (validation.valid) {
|
|
24
|
+
* await provider.initialize({
|
|
25
|
+
* serviceName: 'my-service',
|
|
26
|
+
* serviceVersion: '1.0.0'
|
|
27
|
+
* });
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
export declare class OpenTelemetryProvider implements TelemetryProvider {
|
|
31
|
+
readonly name = "opentelemetry";
|
|
32
|
+
private sdk?;
|
|
33
|
+
private tracer?;
|
|
34
|
+
private meter?;
|
|
35
|
+
private ready;
|
|
36
|
+
/**
|
|
37
|
+
* Validate OpenTelemetry configuration
|
|
38
|
+
*
|
|
39
|
+
* Checks:
|
|
40
|
+
* 1. OTEL_EXPORTER_OTLP_ENDPOINT is set
|
|
41
|
+
* 2. Endpoint is a valid URL
|
|
42
|
+
* 3. Required packages are available (checked lazily during init)
|
|
43
|
+
*/
|
|
44
|
+
validate(): Promise<ValidationResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Initialize OpenTelemetry SDK
|
|
47
|
+
*
|
|
48
|
+
* Sets up:
|
|
49
|
+
* - Resource attributes (service name, version, environment)
|
|
50
|
+
* - OTLP trace exporter
|
|
51
|
+
* - Composite propagator (W3C + Cloud Trace)
|
|
52
|
+
* - Tracer and Meter providers
|
|
53
|
+
*/
|
|
54
|
+
initialize(config: TelemetryConfig): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Create an OpenTelemetry span
|
|
57
|
+
*
|
|
58
|
+
* Creates a SERVER span with HTTP semantic conventions.
|
|
59
|
+
* Returns undefined if provider is not ready.
|
|
60
|
+
*/
|
|
61
|
+
createSpan(context: Context<unknown, unknown>): GenericSpan | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Record a metric as a histogram
|
|
64
|
+
*/
|
|
65
|
+
recordMetric(name: string, value: number, attributes?: Record<string, unknown>): void;
|
|
66
|
+
/**
|
|
67
|
+
* Log with trace correlation
|
|
68
|
+
*
|
|
69
|
+
* Adds trace and span IDs to log output when available.
|
|
70
|
+
*/
|
|
71
|
+
log(level: string, message: string, attributes?: Record<string, unknown>): void;
|
|
72
|
+
/**
|
|
73
|
+
* Check if OpenTelemetry provider is ready
|
|
74
|
+
*/
|
|
75
|
+
isReady(): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Shutdown OpenTelemetry SDK
|
|
78
|
+
*
|
|
79
|
+
* Flushes pending telemetry data and closes exporters.
|
|
80
|
+
*/
|
|
81
|
+
shutdown(): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Create composite propagator based on configuration
|
|
84
|
+
*
|
|
85
|
+
* Supports:
|
|
86
|
+
* - W3C Trace Context (traceparent/tracestate)
|
|
87
|
+
* - Google Cloud Trace Context (X-Cloud-Trace-Context)
|
|
88
|
+
*
|
|
89
|
+
* When running on GCP, both formats are enabled by default for compatibility.
|
|
90
|
+
* This allows trace IDs to be synchronized between Cloud Run Load Balancer
|
|
91
|
+
* and your application.
|
|
92
|
+
*
|
|
93
|
+
* @param config Telemetry configuration
|
|
94
|
+
* @returns Configured propagator (W3C, Cloud Trace, or Composite)
|
|
95
|
+
*/
|
|
96
|
+
private createPropagator;
|
|
97
|
+
/**
|
|
98
|
+
* Detect if running on Google Cloud Platform
|
|
99
|
+
*/
|
|
100
|
+
private isRunningOnGCP;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=opentelemetry-provider.d.ts.map
|