@egintegrations/telemetry 0.1.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 ADDED
@@ -0,0 +1,132 @@
1
+ # @egi/telemetry
2
+
3
+ Official JavaScript/TypeScript telemetry SDK for EGIntegrations client engines.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ # npm
9
+ npm install @egi/telemetry
10
+
11
+ # pnpm
12
+ pnpm add @egi/telemetry
13
+
14
+ # yarn
15
+ yarn add @egi/telemetry
16
+ ```
17
+
18
+ ## Quick Start
19
+
20
+ ```typescript
21
+ import { TelemetryClient } from '@egi/telemetry';
22
+
23
+ // Initialize the client
24
+ const telemetry = new TelemetryClient({
25
+ engineName: 'my-awesome-engine',
26
+ skuVersion: '1.0.0',
27
+ controlCenterUrl: 'https://control-center.egintegrations.com',
28
+ authToken: 'optional-auth-token', // Optional
29
+ enabled: true, // Default: true
30
+ });
31
+
32
+ // Register your engine
33
+ await telemetry.register({
34
+ environment: 'production',
35
+ region: 'us-east-1',
36
+ });
37
+
38
+ // Report status
39
+ await telemetry.reportStatus({
40
+ status: 'healthy',
41
+ metadata: {
42
+ activeConnections: 42,
43
+ queueSize: 100,
44
+ },
45
+ metrics: {
46
+ cpu_usage: 45.2,
47
+ memory_mb: 512,
48
+ },
49
+ });
50
+
51
+ // Send custom metrics
52
+ await telemetry.sendMetrics({
53
+ metrics: {
54
+ orders_processed: 150,
55
+ revenue_usd: 5420.50,
56
+ },
57
+ });
58
+ ```
59
+
60
+ ## Express/Fastify Integration
61
+
62
+ ```typescript
63
+ import express from 'express';
64
+ import { TelemetryClient } from '@egi/telemetry';
65
+
66
+ const app = express();
67
+ const telemetry = new TelemetryClient({
68
+ engineName: 'api-server',
69
+ skuVersion: '1.0.0',
70
+ controlCenterUrl: 'https://control-center.egintegrations.com',
71
+ });
72
+
73
+ // Automatic request metrics
74
+ app.use(telemetry.middleware());
75
+
76
+ app.listen(3000);
77
+ ```
78
+
79
+ ## Automatic Health Checks
80
+
81
+ ```typescript
82
+ // Report health every 60 seconds
83
+ const healthCheckTimer = telemetry.startHealthCheck(60000);
84
+
85
+ // Stop health checks
86
+ clearInterval(healthCheckTimer);
87
+ ```
88
+
89
+ ## Graceful Shutdown
90
+
91
+ ```typescript
92
+ process.on('SIGTERM', async () => {
93
+ await telemetry.shutdown();
94
+ process.exit(0);
95
+ });
96
+ ```
97
+
98
+ ## API Reference
99
+
100
+ ### `TelemetryClient`
101
+
102
+ #### Constructor Options
103
+
104
+ - `engineName` (string, required) - Unique name for your engine
105
+ - `skuVersion` (string, required) - Version of your engine (e.g., "1.0.0")
106
+ - `controlCenterUrl` (string, required) - URL of your control center
107
+ - `authToken` (string, optional) - Authentication token if required
108
+ - `enabled` (boolean, optional) - Enable/disable telemetry (default: true)
109
+
110
+ #### Methods
111
+
112
+ ##### `register(metadata?: object): Promise<void>`
113
+ Register your engine with the control center.
114
+
115
+ ##### `reportStatus(payload: StatusPayload): Promise<void>`
116
+ Report engine status. Status can be: 'healthy', 'degraded', 'unhealthy', 'starting', 'stopping'.
117
+
118
+ ##### `sendMetrics(payload: MetricsPayload): Promise<void>`
119
+ Send custom metrics to the control center.
120
+
121
+ ##### `startHealthCheck(intervalMs?: number): NodeJS.Timeout`
122
+ Start automatic health checks (default: every 60 seconds).
123
+
124
+ ##### `middleware(): Function`
125
+ Express/Fastify middleware for automatic request tracking.
126
+
127
+ ##### `shutdown(): Promise<void>`
128
+ Report graceful shutdown to control center.
129
+
130
+ ## License
131
+
132
+ Proprietary - EGIntegrations
@@ -0,0 +1,49 @@
1
+ interface TelemetryConfig {
2
+ engineName: string;
3
+ skuVersion: string;
4
+ controlCenterUrl: string;
5
+ authToken?: string;
6
+ enabled?: boolean;
7
+ }
8
+ interface StatusPayload {
9
+ status: 'healthy' | 'degraded' | 'unhealthy' | 'starting' | 'stopping';
10
+ metadata?: Record<string, any>;
11
+ metrics?: Record<string, number>;
12
+ message?: string;
13
+ }
14
+ interface MetricsPayload {
15
+ metrics: Record<string, number>;
16
+ timestamp?: string;
17
+ }
18
+ declare class TelemetryClient {
19
+ private config;
20
+ private client;
21
+ private engineId?;
22
+ constructor(config: TelemetryConfig);
23
+ /**
24
+ * Register the engine with the control center
25
+ */
26
+ register(metadata?: Record<string, any>): Promise<void>;
27
+ /**
28
+ * Report engine status to the control center
29
+ */
30
+ reportStatus(payload: StatusPayload): Promise<void>;
31
+ /**
32
+ * Send metrics to the control center
33
+ */
34
+ sendMetrics(payload: MetricsPayload): Promise<void>;
35
+ /**
36
+ * Start periodic health checks (reports status every interval)
37
+ */
38
+ startHealthCheck(intervalMs?: number): NodeJS.Timeout;
39
+ /**
40
+ * Create Express/Fastify middleware for automatic telemetry
41
+ */
42
+ middleware(): (req: any, res: any, next: any) => Promise<void>;
43
+ /**
44
+ * Graceful shutdown - report stopping status
45
+ */
46
+ shutdown(): Promise<void>;
47
+ }
48
+
49
+ export { type MetricsPayload, type StatusPayload, TelemetryClient, type TelemetryConfig, TelemetryClient as default };
@@ -0,0 +1,49 @@
1
+ interface TelemetryConfig {
2
+ engineName: string;
3
+ skuVersion: string;
4
+ controlCenterUrl: string;
5
+ authToken?: string;
6
+ enabled?: boolean;
7
+ }
8
+ interface StatusPayload {
9
+ status: 'healthy' | 'degraded' | 'unhealthy' | 'starting' | 'stopping';
10
+ metadata?: Record<string, any>;
11
+ metrics?: Record<string, number>;
12
+ message?: string;
13
+ }
14
+ interface MetricsPayload {
15
+ metrics: Record<string, number>;
16
+ timestamp?: string;
17
+ }
18
+ declare class TelemetryClient {
19
+ private config;
20
+ private client;
21
+ private engineId?;
22
+ constructor(config: TelemetryConfig);
23
+ /**
24
+ * Register the engine with the control center
25
+ */
26
+ register(metadata?: Record<string, any>): Promise<void>;
27
+ /**
28
+ * Report engine status to the control center
29
+ */
30
+ reportStatus(payload: StatusPayload): Promise<void>;
31
+ /**
32
+ * Send metrics to the control center
33
+ */
34
+ sendMetrics(payload: MetricsPayload): Promise<void>;
35
+ /**
36
+ * Start periodic health checks (reports status every interval)
37
+ */
38
+ startHealthCheck(intervalMs?: number): NodeJS.Timeout;
39
+ /**
40
+ * Create Express/Fastify middleware for automatic telemetry
41
+ */
42
+ middleware(): (req: any, res: any, next: any) => Promise<void>;
43
+ /**
44
+ * Graceful shutdown - report stopping status
45
+ */
46
+ shutdown(): Promise<void>;
47
+ }
48
+
49
+ export { type MetricsPayload, type StatusPayload, TelemetryClient, type TelemetryConfig, TelemetryClient as default };
package/dist/index.js ADDED
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ TelemetryClient: () => TelemetryClient,
34
+ default: () => index_default
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var import_axios = __toESM(require("axios"));
38
+ var TelemetryClient = class {
39
+ constructor(config) {
40
+ this.config = {
41
+ ...config,
42
+ enabled: config.enabled ?? true
43
+ };
44
+ this.client = import_axios.default.create({
45
+ baseURL: this.config.controlCenterUrl,
46
+ timeout: 5e3,
47
+ headers: {
48
+ "Content-Type": "application/json",
49
+ ...this.config.authToken && { Authorization: `Bearer ${this.config.authToken}` }
50
+ }
51
+ });
52
+ }
53
+ /**
54
+ * Register the engine with the control center
55
+ */
56
+ async register(metadata) {
57
+ if (!this.config.enabled) return;
58
+ try {
59
+ const response = await this.client.post("/api/telemetry/register", {
60
+ engine_name: this.config.engineName,
61
+ sku_version: this.config.skuVersion,
62
+ metadata: metadata || {}
63
+ });
64
+ this.engineId = response.data.engine_id;
65
+ console.log(`[EGI Telemetry] Registered engine: ${this.config.engineName} (${this.engineId})`);
66
+ } catch (error) {
67
+ console.error("[EGI Telemetry] Failed to register:", error);
68
+ throw error;
69
+ }
70
+ }
71
+ /**
72
+ * Report engine status to the control center
73
+ */
74
+ async reportStatus(payload) {
75
+ if (!this.config.enabled) return;
76
+ try {
77
+ await this.client.post("/api/telemetry/status", {
78
+ engine_name: this.config.engineName,
79
+ sku_version: this.config.skuVersion,
80
+ status: payload.status,
81
+ metadata: payload.metadata || {},
82
+ metrics: payload.metrics || {},
83
+ message: payload.message,
84
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
85
+ });
86
+ } catch (error) {
87
+ console.error("[EGI Telemetry] Failed to report status:", error);
88
+ }
89
+ }
90
+ /**
91
+ * Send metrics to the control center
92
+ */
93
+ async sendMetrics(payload) {
94
+ if (!this.config.enabled) return;
95
+ try {
96
+ await this.client.post("/api/telemetry/metrics", {
97
+ engine_name: this.config.engineName,
98
+ sku_version: this.config.skuVersion,
99
+ metrics: payload.metrics,
100
+ timestamp: payload.timestamp || (/* @__PURE__ */ new Date()).toISOString()
101
+ });
102
+ } catch (error) {
103
+ console.error("[EGI Telemetry] Failed to send metrics:", error);
104
+ }
105
+ }
106
+ /**
107
+ * Start periodic health checks (reports status every interval)
108
+ */
109
+ startHealthCheck(intervalMs = 6e4) {
110
+ return setInterval(async () => {
111
+ await this.reportStatus({
112
+ status: "healthy",
113
+ metadata: {
114
+ uptime: process.uptime(),
115
+ memory: process.memoryUsage()
116
+ }
117
+ });
118
+ }, intervalMs);
119
+ }
120
+ /**
121
+ * Create Express/Fastify middleware for automatic telemetry
122
+ */
123
+ middleware() {
124
+ return async (req, res, next) => {
125
+ const start = Date.now();
126
+ res.on("finish", async () => {
127
+ const duration = Date.now() - start;
128
+ await this.sendMetrics({
129
+ metrics: {
130
+ request_duration_ms: duration,
131
+ status_code: res.statusCode
132
+ }
133
+ });
134
+ });
135
+ next();
136
+ };
137
+ }
138
+ /**
139
+ * Graceful shutdown - report stopping status
140
+ */
141
+ async shutdown() {
142
+ await this.reportStatus({
143
+ status: "stopping",
144
+ message: "Engine shutting down"
145
+ });
146
+ }
147
+ };
148
+ var index_default = TelemetryClient;
149
+ // Annotate the CommonJS export names for ESM import in node:
150
+ 0 && (module.exports = {
151
+ TelemetryClient
152
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,117 @@
1
+ // src/index.ts
2
+ import axios from "axios";
3
+ var TelemetryClient = class {
4
+ constructor(config) {
5
+ this.config = {
6
+ ...config,
7
+ enabled: config.enabled ?? true
8
+ };
9
+ this.client = axios.create({
10
+ baseURL: this.config.controlCenterUrl,
11
+ timeout: 5e3,
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ ...this.config.authToken && { Authorization: `Bearer ${this.config.authToken}` }
15
+ }
16
+ });
17
+ }
18
+ /**
19
+ * Register the engine with the control center
20
+ */
21
+ async register(metadata) {
22
+ if (!this.config.enabled) return;
23
+ try {
24
+ const response = await this.client.post("/api/telemetry/register", {
25
+ engine_name: this.config.engineName,
26
+ sku_version: this.config.skuVersion,
27
+ metadata: metadata || {}
28
+ });
29
+ this.engineId = response.data.engine_id;
30
+ console.log(`[EGI Telemetry] Registered engine: ${this.config.engineName} (${this.engineId})`);
31
+ } catch (error) {
32
+ console.error("[EGI Telemetry] Failed to register:", error);
33
+ throw error;
34
+ }
35
+ }
36
+ /**
37
+ * Report engine status to the control center
38
+ */
39
+ async reportStatus(payload) {
40
+ if (!this.config.enabled) return;
41
+ try {
42
+ await this.client.post("/api/telemetry/status", {
43
+ engine_name: this.config.engineName,
44
+ sku_version: this.config.skuVersion,
45
+ status: payload.status,
46
+ metadata: payload.metadata || {},
47
+ metrics: payload.metrics || {},
48
+ message: payload.message,
49
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
50
+ });
51
+ } catch (error) {
52
+ console.error("[EGI Telemetry] Failed to report status:", error);
53
+ }
54
+ }
55
+ /**
56
+ * Send metrics to the control center
57
+ */
58
+ async sendMetrics(payload) {
59
+ if (!this.config.enabled) return;
60
+ try {
61
+ await this.client.post("/api/telemetry/metrics", {
62
+ engine_name: this.config.engineName,
63
+ sku_version: this.config.skuVersion,
64
+ metrics: payload.metrics,
65
+ timestamp: payload.timestamp || (/* @__PURE__ */ new Date()).toISOString()
66
+ });
67
+ } catch (error) {
68
+ console.error("[EGI Telemetry] Failed to send metrics:", error);
69
+ }
70
+ }
71
+ /**
72
+ * Start periodic health checks (reports status every interval)
73
+ */
74
+ startHealthCheck(intervalMs = 6e4) {
75
+ return setInterval(async () => {
76
+ await this.reportStatus({
77
+ status: "healthy",
78
+ metadata: {
79
+ uptime: process.uptime(),
80
+ memory: process.memoryUsage()
81
+ }
82
+ });
83
+ }, intervalMs);
84
+ }
85
+ /**
86
+ * Create Express/Fastify middleware for automatic telemetry
87
+ */
88
+ middleware() {
89
+ return async (req, res, next) => {
90
+ const start = Date.now();
91
+ res.on("finish", async () => {
92
+ const duration = Date.now() - start;
93
+ await this.sendMetrics({
94
+ metrics: {
95
+ request_duration_ms: duration,
96
+ status_code: res.statusCode
97
+ }
98
+ });
99
+ });
100
+ next();
101
+ };
102
+ }
103
+ /**
104
+ * Graceful shutdown - report stopping status
105
+ */
106
+ async shutdown() {
107
+ await this.reportStatus({
108
+ status: "stopping",
109
+ message: "Engine shutting down"
110
+ });
111
+ }
112
+ };
113
+ var index_default = TelemetryClient;
114
+ export {
115
+ TelemetryClient,
116
+ index_default as default
117
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@egintegrations/telemetry",
3
+ "version": "0.1.0",
4
+ "description": "Telemetry SDK for EGIntegrations client engines",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "require": "./dist/index.js",
11
+ "import": "./dist/index.mjs",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
17
+ "prepublishOnly": "npm run build",
18
+ "test": "echo \"No tests yet\""
19
+ },
20
+ "keywords": [
21
+ "telemetry",
22
+ "monitoring",
23
+ "egi",
24
+ "metrics"
25
+ ],
26
+ "author": "EGIntegrations",
27
+ "license": "PROPRIETARY",
28
+ "dependencies": {
29
+ "axios": "^1.6.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.0.0",
33
+ "tsup": "^8.0.0",
34
+ "typescript": "^5.3.0"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md"
39
+ ]
40
+ }