@comprehend/telemetry-node 0.1.4 → 0.2.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.
Files changed (38) hide show
  1. package/.claude/settings.local.json +2 -2
  2. package/.idea/telemetry-node.iml +0 -1
  3. package/README.md +73 -27
  4. package/dist/ComprehendDevSpanProcessor.d.ts +9 -6
  5. package/dist/ComprehendDevSpanProcessor.js +145 -87
  6. package/dist/ComprehendDevSpanProcessor.test.js +270 -449
  7. package/dist/ComprehendMetricsExporter.d.ts +18 -0
  8. package/dist/ComprehendMetricsExporter.js +178 -0
  9. package/dist/ComprehendMetricsExporter.test.d.ts +1 -0
  10. package/dist/ComprehendMetricsExporter.test.js +266 -0
  11. package/dist/ComprehendSDK.d.ts +18 -0
  12. package/dist/ComprehendSDK.js +56 -0
  13. package/dist/ComprehendSDK.test.d.ts +1 -0
  14. package/dist/ComprehendSDK.test.js +126 -0
  15. package/dist/WebSocketConnection.d.ts +23 -3
  16. package/dist/WebSocketConnection.js +106 -12
  17. package/dist/WebSocketConnection.test.js +236 -169
  18. package/dist/index.d.ts +3 -1
  19. package/dist/index.js +5 -1
  20. package/dist/sql-analyzer.js +2 -11
  21. package/dist/sql-analyzer.test.js +0 -12
  22. package/dist/util.d.ts +2 -0
  23. package/dist/util.js +7 -0
  24. package/dist/wire-protocol.d.ts +168 -28
  25. package/package.json +3 -1
  26. package/src/ComprehendDevSpanProcessor.test.ts +311 -507
  27. package/src/ComprehendDevSpanProcessor.ts +169 -105
  28. package/src/ComprehendMetricsExporter.test.ts +334 -0
  29. package/src/ComprehendMetricsExporter.ts +225 -0
  30. package/src/ComprehendSDK.test.ts +160 -0
  31. package/src/ComprehendSDK.ts +63 -0
  32. package/src/WebSocketConnection.test.ts +286 -205
  33. package/src/WebSocketConnection.ts +135 -13
  34. package/src/index.ts +3 -2
  35. package/src/util.ts +6 -0
  36. package/src/wire-protocol.ts +204 -29
  37. package/src/sql-analyzer.test.ts +0 -599
  38. package/src/sql-analyzer.ts +0 -439
@@ -2,8 +2,8 @@
2
2
  "permissions": {
3
3
  "allow": [
4
4
  "Bash(npm test:*)",
5
- "Bash(pnpm run test:*)"
5
+ "Bash(grep:*)"
6
6
  ],
7
7
  "deny": []
8
8
  }
9
- }
9
+ }
@@ -3,7 +3,6 @@
3
3
  <component name="NewModuleRootManager">
4
4
  <content url="file://$MODULE_DIR$">
5
5
  <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/dist" />
7
6
  <excludeFolder url="file://$MODULE_DIR$/temp" />
8
7
  <excludeFolder url="file://$MODULE_DIR$/tmp" />
9
8
  </content>
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @comprehend/telemetry-node
2
2
 
3
- OpenTelemetry integration for [comprehend.dev](https://comprehend.dev) - automatically capture and analyze your Node.js application's architecture and performance.
3
+ OpenTelemetry integration for [comprehend.dev](https://comprehend.dev) - automatically capture and analyze your Node.js application's architecture, performance, and runtime metrics.
4
4
 
5
5
  ## Installation
6
6
 
@@ -15,29 +15,33 @@ npm install @comprehend/telemetry-node
15
15
  If you don't have OpenTelemetry set up yet, here's a complete setup:
16
16
 
17
17
  ```bash
18
- npm install @comprehend/telemetry-node @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node
18
+ npm install @comprehend/telemetry-node @opentelemetry/sdk-node @opentelemetry/sdk-metrics @opentelemetry/auto-instrumentations-node
19
19
  ```
20
20
 
21
21
  ```typescript
22
+ import { ComprehendSDK } from '@comprehend/telemetry-node';
22
23
  import { NodeSDK } from '@opentelemetry/sdk-node';
23
- import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
24
+ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
24
25
  import { Resource } from '@opentelemetry/resources';
25
- import { ComprehendDevSpanProcessor } from '@comprehend/telemetry-node';
26
+ import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
27
+
28
+ const comprehend = new ComprehendSDK({
29
+ organization: 'your-org',
30
+ token: process.env.COMPREHEND_SDK_TOKEN!,
31
+ debug: true // Optional: enable debug logging (or pass a custom logger function)
32
+ });
26
33
 
27
- // Set up OpenTelemetry with your service information
28
34
  const sdk = new NodeSDK({
29
35
  resource: new Resource({
30
36
  'service.name': 'my-node-service',
31
37
  'service.namespace': 'production',
32
38
  'deployment.environment': 'prod'
33
39
  }),
34
- spanProcessors: [
35
- new ComprehendDevSpanProcessor({
36
- organization: 'your-org', // Use your organization name
37
- token: process.env.COMPREHEND_INGESTION_TOKEN!,
38
- debug: true // Optional: enable debug logging (or pass a custom logger function)
39
- })
40
- ],
40
+ spanProcessors: [comprehend.getSpanProcessor()],
41
+ metricReader: new PeriodicExportingMetricReader({
42
+ exporter: comprehend.getMetricsExporter(),
43
+ exportIntervalMillis: 15000
44
+ }),
41
45
  instrumentations: [getNodeAutoInstrumentations()],
42
46
  });
43
47
 
@@ -45,43 +49,82 @@ sdk.start();
45
49
 
46
50
  // Graceful shutdown
47
51
  process.on('SIGTERM', () => {
48
- sdk.shutdown().then(() => console.log('Tracing terminated'));
52
+ sdk.shutdown().then(() => console.log('Telemetry terminated'));
49
53
  });
50
54
  ```
51
55
 
52
56
  ### Adding to existing OpenTelemetry setup
53
57
 
54
- If you already have OpenTelemetry configured, simply add the ComprehendDevSpanProcessor to your existing span processors:
58
+ If you already have OpenTelemetry configured, create a `ComprehendSDK` instance and add its processor and exporter:
55
59
 
56
60
  ```typescript
57
- import { ComprehendDevSpanProcessor } from '@comprehend/telemetry-node';
61
+ import { ComprehendSDK } from '@comprehend/telemetry-node';
58
62
 
59
- // Add to your existing span processors array
60
- const comprehendProcessor = new ComprehendDevSpanProcessor({
63
+ const comprehend = new ComprehendSDK({
61
64
  organization: 'your-org',
62
- token: process.env.COMPREHEND_INGESTION_TOKEN!,
63
- debug: false // Optional
65
+ token: process.env.COMPREHEND_SDK_TOKEN!,
64
66
  });
65
67
 
66
- // If using NodeSDK:
68
+ // Add to your NodeSDK config:
67
69
  const sdk = new NodeSDK({
68
70
  // ... your existing config
69
71
  spanProcessors: [
70
72
  // ... your existing processors
71
- comprehendProcessor
73
+ comprehend.getSpanProcessor()
72
74
  ],
75
+ metricReader: new PeriodicExportingMetricReader({
76
+ exporter: comprehend.getMetricsExporter(),
77
+ exportIntervalMillis: 15000
78
+ }),
73
79
  });
80
+ ```
81
+
82
+ ### Process and runtime metrics
83
+
84
+ For process-level and V8 runtime metrics (memory usage, event loop delay, GC duration, etc.), include `@opentelemetry/instrumentation-runtime-node` in your instrumentations:
74
85
 
75
- // Or if using TracerProvider directly:
76
- tracerProvider.addSpanProcessor(comprehendProcessor);
86
+ ```bash
87
+ npm install @opentelemetry/instrumentation-runtime-node
77
88
  ```
78
89
 
90
+ ```typescript
91
+ import { RuntimeNodeInstrumentation } from '@opentelemetry/instrumentation-runtime-node';
92
+
93
+ const sdk = new NodeSDK({
94
+ // ...
95
+ instrumentations: [
96
+ getNodeAutoInstrumentations(),
97
+ new RuntimeNodeInstrumentation(),
98
+ ],
99
+ });
100
+ ```
101
+
102
+ ### Host metrics (CPU and memory)
103
+
104
+ For process-level CPU utilization and memory usage, add `@opentelemetry/host-metrics`:
105
+
106
+ ```bash
107
+ npm install @opentelemetry/host-metrics
108
+ ```
109
+
110
+ ```typescript
111
+ import { HostMetrics } from '@opentelemetry/host-metrics';
112
+
113
+ // Start after sdk.start()
114
+ const hostMetrics = new HostMetrics({
115
+ metricGroups: ['process.cpu', 'process.memory'],
116
+ });
117
+ hostMetrics.start();
118
+ ```
119
+
120
+ This collects `process.cpu.time`, `process.cpu.utilization`, and `process.memory.usage`. The `metricGroups` option limits collection to process-level metrics only, avoiding the overhead of system-wide CPU, memory, and network data gathering.
121
+
79
122
  ## Configuration
80
123
 
81
- Set your comprehend.dev ingestion token as an environment variable:
124
+ Set your comprehend.dev SDK token as an environment variable:
82
125
 
83
126
  ```bash
84
- export COMPREHEND_INGESTION_TOKEN=your-ingestion-token-here
127
+ export COMPREHEND_SDK_TOKEN=your-token-here
85
128
  ```
86
129
 
87
130
  **Note:** In production environments, the token should be stored in a secure secret management system (such as AWS Secrets Manager, HashiCorp Vault, Azure Key Vault, Kubernetes Secrets, or your cloud provider's secret management service) and injected into the environment through your container orchestrator's workload definition or service configuration.
@@ -91,15 +134,18 @@ export COMPREHEND_INGESTION_TOKEN=your-ingestion-token-here
91
134
  This integration automatically captures:
92
135
 
93
136
  - **HTTP Routes** - API endpoints and their usage patterns
94
- - **Database Operations** - SQL queries with table operations analysis
137
+ - **Database Operations** - SQL queries (analysis done server-side)
95
138
  - **Service Dependencies** - HTTP client calls to external services
96
139
  - **Performance Metrics** - Request durations, response codes, error rates
97
140
  - **Service Architecture** - Automatically maps your service relationships
141
+ - **Trace Spans** - Span identity and parent relationships for connecting observations to traces
142
+ - **Runtime Metrics** - Node.js memory, event loop, and V8 metrics
143
+ - **Custom Metrics** - Server-configured custom metric and span collection
98
144
 
99
145
  ## Requirements
100
146
 
101
147
  - Node.js 14+
102
- - OpenTelemetry SDK (peer dependency)
148
+ - OpenTelemetry SDK (peer dependencies: `@opentelemetry/api`, `@opentelemetry/sdk-trace-base`, `@opentelemetry/sdk-metrics`)
103
149
 
104
150
  ## Framework Support
105
151
 
@@ -1,25 +1,28 @@
1
1
  import { Context } from "@opentelemetry/api";
2
2
  import { ReadableSpan, Span, SpanProcessor } from "@opentelemetry/sdk-trace-base";
3
+ import { SpanMatcherRule, CustomMetricSpecification } from "./wire-protocol";
4
+ import { WebSocketConnection } from "./WebSocketConnection";
3
5
  export declare class ComprehendDevSpanProcessor implements SpanProcessor {
4
6
  private readonly connection;
5
7
  private observedServices;
6
8
  private observedDatabases;
7
9
  private observedHttpServices;
8
10
  private observedInteractions;
9
- private observationsSeq;
10
- constructor(options: {
11
- organization: string;
12
- token: string;
13
- debug?: boolean | ((message: string) => void);
14
- });
11
+ private processContextSet;
12
+ private spanObservationSpecs;
13
+ constructor(connection: WebSocketConnection);
14
+ updateCustomMetrics(specs: CustomMetricSpecification[]): void;
15
15
  onStart(span: Span, parentContext: Context): void;
16
16
  onEnd(span: ReadableSpan): void;
17
17
  private discoverService;
18
18
  private processHTTPRoute;
19
19
  private processDatabaseOperation;
20
20
  private processHttpRequest;
21
+ private reportTraceSpan;
22
+ private checkCustomSpanMatching;
21
23
  private getInteractions;
22
24
  private ingestMessage;
23
25
  forceFlush(): Promise<void>;
24
26
  shutdown(): Promise<void>;
25
27
  }
28
+ export declare function matchSpanRule(span: ReadableSpan, rule: SpanMatcherRule): boolean;