@comprehend/telemetry-node 0.1.4 → 0.2.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 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,121 @@ 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:
85
+
86
+ ```bash
87
+ npm install @opentelemetry/instrumentation-runtime-node
88
+ ```
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
+
122
+ ### Kubernetes / container resources
123
+
124
+ When running in containers or Kubernetes, the `NodeSDK` default resource detectors (`env`, `process`, `host`) do not include container or pod attributes. To populate `container.id`, `k8s.pod.name`, `k8s.namespace.name`, and similar, add the container resource detector explicitly via `resourceDetectors`:
125
+
126
+ ```bash
127
+ npm install @opentelemetry/resource-detector-container
128
+ ```
129
+
130
+ ```typescript
131
+ import { NodeSDK } from '@opentelemetry/sdk-node';
132
+ import { envDetector, hostDetector, processDetector } from '@opentelemetry/resources';
133
+ import { containerDetector } from '@opentelemetry/resource-detector-container';
134
+
135
+ const sdk = new NodeSDK({
136
+ // ...
137
+ resourceDetectors: [envDetector, processDetector, hostDetector, containerDetector],
138
+ });
139
+ ```
74
140
 
75
- // Or if using TracerProvider directly:
76
- tracerProvider.addSpanProcessor(comprehendProcessor);
141
+ Note that passing `resourceDetectors` replaces the defaults, so make sure to include the built-in detectors alongside `containerDetector`. The container detector reads `container.id` from `/proc/self/cgroup`. For other k8s attributes (pod name, namespace, node), use the Kubernetes Downward API to inject them as `OTEL_RESOURCE_ATTRIBUTES`:
142
+
143
+ ```yaml
144
+ env:
145
+ - name: OTEL_RESOURCE_ATTRIBUTES
146
+ value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(POD_NAMESPACE),k8s.node.name=$(NODE_NAME)
147
+ - name: POD_NAME
148
+ valueFrom:
149
+ fieldRef:
150
+ fieldPath: metadata.name
151
+ - name: POD_NAMESPACE
152
+ valueFrom:
153
+ fieldRef:
154
+ fieldPath: metadata.namespace
155
+ - name: NODE_NAME
156
+ valueFrom:
157
+ fieldRef:
158
+ fieldPath: spec.nodeName
77
159
  ```
78
160
 
79
161
  ## Configuration
80
162
 
81
- Set your comprehend.dev ingestion token as an environment variable:
163
+ Set your comprehend.dev SDK token as an environment variable:
82
164
 
83
165
  ```bash
84
- export COMPREHEND_INGESTION_TOKEN=your-ingestion-token-here
166
+ export COMPREHEND_SDK_TOKEN=your-token-here
85
167
  ```
86
168
 
87
169
  **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 +173,18 @@ export COMPREHEND_INGESTION_TOKEN=your-ingestion-token-here
91
173
  This integration automatically captures:
92
174
 
93
175
  - **HTTP Routes** - API endpoints and their usage patterns
94
- - **Database Operations** - SQL queries with table operations analysis
176
+ - **Database Operations** - SQL queries (analysis done server-side)
95
177
  - **Service Dependencies** - HTTP client calls to external services
96
178
  - **Performance Metrics** - Request durations, response codes, error rates
97
179
  - **Service Architecture** - Automatically maps your service relationships
180
+ - **Trace Spans** - Span identity and parent relationships for connecting observations to traces
181
+ - **Runtime Metrics** - Node.js memory, event loop, and V8 metrics
182
+ - **Custom Metrics** - Server-configured custom metric and span collection
98
183
 
99
184
  ## Requirements
100
185
 
101
186
  - Node.js 14+
102
- - OpenTelemetry SDK (peer dependency)
187
+ - OpenTelemetry SDK (peer dependencies: `@opentelemetry/api`, `@opentelemetry/sdk-trace-base`, `@opentelemetry/sdk-metrics`)
103
188
 
104
189
  ## Framework Support
105
190
 
@@ -1,25 +1,29 @@
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
+ private processSpan;
17
18
  private discoverService;
18
19
  private processHTTPRoute;
19
20
  private processDatabaseOperation;
20
21
  private processHttpRequest;
22
+ private reportTraceSpan;
23
+ private checkCustomSpanMatching;
21
24
  private getInteractions;
22
25
  private ingestMessage;
23
26
  forceFlush(): Promise<void>;
24
27
  shutdown(): Promise<void>;
25
28
  }
29
+ export declare function matchSpanRule(span: ReadableSpan, rule: SpanMatcherRule): boolean;