htm 0.0.11 → 0.0.14
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.
- checksums.yaml +4 -4
- data/.dictate.toml +46 -0
- data/.envrc +2 -0
- data/CHANGELOG.md +52 -2
- data/README.md +79 -0
- data/Rakefile +14 -2
- data/bin/htm_mcp.rb +94 -0
- data/config/database.yml +20 -13
- data/db/migrate/00010_add_soft_delete_to_associations.rb +29 -0
- data/db/migrate/00011_add_performance_indexes.rb +21 -0
- data/db/migrate/00012_add_tags_trigram_index.rb +18 -0
- data/db/migrate/00013_enable_lz4_compression.rb +43 -0
- data/db/schema.sql +49 -92
- data/docs/api/index.md +1 -1
- data/docs/api/yard/HTM.md +2 -4
- data/docs/architecture/index.md +1 -1
- data/docs/development/index.md +1 -1
- data/docs/getting-started/index.md +1 -1
- data/docs/guides/index.md +1 -1
- data/docs/images/telemetry-architecture.svg +153 -0
- data/docs/telemetry.md +391 -0
- data/examples/README.md +46 -1
- data/examples/cli_app/README.md +1 -1
- data/examples/cli_app/htm_cli.rb +1 -1
- data/examples/sinatra_app/app.rb +1 -1
- data/examples/telemetry/README.md +147 -0
- data/examples/telemetry/SETUP_README.md +169 -0
- data/examples/telemetry/demo.rb +498 -0
- data/examples/telemetry/grafana/dashboards/htm-metrics.json +457 -0
- data/lib/htm/configuration.rb +261 -70
- data/lib/htm/database.rb +46 -22
- data/lib/htm/embedding_service.rb +24 -14
- data/lib/htm/errors.rb +15 -1
- data/lib/htm/jobs/generate_embedding_job.rb +19 -0
- data/lib/htm/jobs/generate_propositions_job.rb +103 -0
- data/lib/htm/jobs/generate_tags_job.rb +24 -0
- data/lib/htm/loaders/markdown_chunker.rb +79 -0
- data/lib/htm/loaders/markdown_loader.rb +41 -15
- data/lib/htm/long_term_memory/fulltext_search.rb +138 -0
- data/lib/htm/long_term_memory/hybrid_search.rb +324 -0
- data/lib/htm/long_term_memory/node_operations.rb +209 -0
- data/lib/htm/long_term_memory/relevance_scorer.rb +355 -0
- data/lib/htm/long_term_memory/robot_operations.rb +34 -0
- data/lib/htm/long_term_memory/tag_operations.rb +428 -0
- data/lib/htm/long_term_memory/vector_search.rb +109 -0
- data/lib/htm/long_term_memory.rb +51 -1153
- data/lib/htm/models/node.rb +35 -2
- data/lib/htm/models/node_tag.rb +31 -0
- data/lib/htm/models/robot_node.rb +31 -0
- data/lib/htm/models/tag.rb +44 -0
- data/lib/htm/proposition_service.rb +169 -0
- data/lib/htm/query_cache.rb +214 -0
- data/lib/htm/sql_builder.rb +178 -0
- data/lib/htm/tag_service.rb +16 -6
- data/lib/htm/tasks.rb +8 -2
- data/lib/htm/telemetry.rb +224 -0
- data/lib/htm/version.rb +1 -1
- data/lib/htm.rb +64 -3
- data/lib/tasks/doc.rake +1 -1
- data/lib/tasks/htm.rake +259 -13
- data/mkdocs.yml +96 -96
- metadata +42 -16
- data/.aigcm_msg +0 -1
- data/.claude/settings.local.json +0 -95
- data/CLAUDE.md +0 -603
- data/examples/cli_app/temp.log +0 -93
- data/lib/htm/loaders/paragraph_chunker.rb +0 -112
- data/notes/ARCHITECTURE_REVIEW.md +0 -1167
- data/notes/IMPLEMENTATION_SUMMARY.md +0 -606
- data/notes/MULTI_FRAMEWORK_IMPLEMENTATION.md +0 -451
- data/notes/next_steps.md +0 -100
- data/notes/plan.md +0 -627
- data/notes/tag_ontology_enhancement_ideas.md +0 -222
- data/notes/timescaledb_removal_summary.md +0 -200
data/docs/telemetry.md
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
# Telemetry (OpenTelemetry Metrics)
|
|
2
|
+
|
|
3
|
+
HTM includes optional OpenTelemetry-based metrics for production observability. This document provides detailed configuration and usage information.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
HTM uses [OpenTelemetry](https://opentelemetry.io/) for metrics collection, the industry-standard observability framework. This provides:
|
|
8
|
+
|
|
9
|
+
- **Universal compatibility**: Works with 50+ observability backends
|
|
10
|
+
- **Zero vendor lock-in**: Emit standard OTLP, route anywhere
|
|
11
|
+
- **Zero overhead when disabled**: Null object pattern ensures no performance impact
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### 1. Install Dependencies
|
|
16
|
+
|
|
17
|
+
Add the OpenTelemetry gems to your Gemfile:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
gem 'opentelemetry-sdk'
|
|
21
|
+
gem 'opentelemetry-metrics-sdk'
|
|
22
|
+
gem 'opentelemetry-exporter-otlp' # For OTLP export
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Enable Telemetry
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
HTM.configure do |config|
|
|
29
|
+
config.telemetry_enabled = true
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or via environment variable:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
export HTM_TELEMETRY_ENABLED="true"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 3. Configure Destination
|
|
40
|
+
|
|
41
|
+
Set standard OpenTelemetry environment variables:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
export OTEL_METRICS_EXPORTER="otlp"
|
|
45
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Configuration Options
|
|
49
|
+
|
|
50
|
+
### HTM Configuration
|
|
51
|
+
|
|
52
|
+
| Option | Type | Default | Description |
|
|
53
|
+
|--------|------|---------|-------------|
|
|
54
|
+
| `telemetry_enabled` | Boolean | `false` | Enable/disable telemetry |
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
HTM.configure do |config|
|
|
58
|
+
config.telemetry_enabled = true
|
|
59
|
+
end
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Environment Variables
|
|
63
|
+
|
|
64
|
+
#### HTM-specific
|
|
65
|
+
|
|
66
|
+
| Variable | Default | Description |
|
|
67
|
+
|----------|---------|-------------|
|
|
68
|
+
| `HTM_TELEMETRY_ENABLED` | `false` | Enable telemetry (`true`/`false`) |
|
|
69
|
+
|
|
70
|
+
#### OpenTelemetry Standard
|
|
71
|
+
|
|
72
|
+
These are standard OpenTelemetry environment variables (not HTM-specific):
|
|
73
|
+
|
|
74
|
+
| Variable | Example | Description |
|
|
75
|
+
|----------|---------|-------------|
|
|
76
|
+
| `OTEL_SERVICE_NAME` | `htm` | Service name in telemetry data |
|
|
77
|
+
| `OTEL_METRICS_EXPORTER` | `otlp` | Metrics exporter type |
|
|
78
|
+
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` | OTLP collector endpoint |
|
|
79
|
+
| `OTEL_EXPORTER_OTLP_HEADERS` | `Authorization=Bearer token` | Headers for OTLP requests |
|
|
80
|
+
|
|
81
|
+
## Available Metrics
|
|
82
|
+
|
|
83
|
+
### htm.jobs (Counter)
|
|
84
|
+
|
|
85
|
+
Counts job executions by type and outcome.
|
|
86
|
+
|
|
87
|
+
**Attributes:**
|
|
88
|
+
- `job`: Job type (`embedding`, `tags`)
|
|
89
|
+
- `status`: Outcome (`success`, `error`, `circuit_open`)
|
|
90
|
+
|
|
91
|
+
**Example queries (PromQL):**
|
|
92
|
+
```promql
|
|
93
|
+
# Jobs per minute by type
|
|
94
|
+
rate(htm_jobs_total[1m])
|
|
95
|
+
|
|
96
|
+
# Error rate for embedding jobs
|
|
97
|
+
rate(htm_jobs_total{job="embedding",status="error"}[5m])
|
|
98
|
+
/ rate(htm_jobs_total{job="embedding"}[5m])
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### htm.embedding.latency (Histogram)
|
|
102
|
+
|
|
103
|
+
Measures embedding generation time in milliseconds.
|
|
104
|
+
|
|
105
|
+
**Attributes:**
|
|
106
|
+
- `provider`: LLM provider (`ollama`, `openai`, etc.)
|
|
107
|
+
- `status`: Outcome (`success`, `error`)
|
|
108
|
+
|
|
109
|
+
**Example queries (PromQL):**
|
|
110
|
+
```promql
|
|
111
|
+
# p95 embedding latency
|
|
112
|
+
histogram_quantile(0.95, rate(htm_embedding_latency_bucket[5m]))
|
|
113
|
+
|
|
114
|
+
# Average latency by provider
|
|
115
|
+
rate(htm_embedding_latency_sum[5m]) / rate(htm_embedding_latency_count[5m])
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### htm.tag.latency (Histogram)
|
|
119
|
+
|
|
120
|
+
Measures tag extraction time in milliseconds.
|
|
121
|
+
|
|
122
|
+
**Attributes:**
|
|
123
|
+
- `provider`: LLM provider (`ollama`, `openai`, etc.)
|
|
124
|
+
- `status`: Outcome (`success`, `error`)
|
|
125
|
+
|
|
126
|
+
**Example queries (PromQL):**
|
|
127
|
+
```promql
|
|
128
|
+
# p99 tag extraction latency
|
|
129
|
+
histogram_quantile(0.99, rate(htm_tag_latency_bucket[5m]))
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### htm.search.latency (Histogram)
|
|
133
|
+
|
|
134
|
+
Measures search operation time in milliseconds.
|
|
135
|
+
|
|
136
|
+
**Attributes:**
|
|
137
|
+
- `strategy`: Search strategy (`vector`, `fulltext`, `hybrid`)
|
|
138
|
+
|
|
139
|
+
**Example queries (PromQL):**
|
|
140
|
+
```promql
|
|
141
|
+
# Search latency by strategy
|
|
142
|
+
histogram_quantile(0.95, rate(htm_search_latency_bucket[5m])) by (strategy)
|
|
143
|
+
|
|
144
|
+
# Hybrid search avg latency
|
|
145
|
+
rate(htm_search_latency_sum{strategy="hybrid"}[5m])
|
|
146
|
+
/ rate(htm_search_latency_count{strategy="hybrid"}[5m])
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### htm.cache.operations (Counter)
|
|
150
|
+
|
|
151
|
+
Counts cache hits and misses.
|
|
152
|
+
|
|
153
|
+
**Attributes:**
|
|
154
|
+
- `operation`: Operation type (`hit`, `miss`)
|
|
155
|
+
|
|
156
|
+
**Example queries (PromQL):**
|
|
157
|
+
```promql
|
|
158
|
+
# Cache hit rate
|
|
159
|
+
rate(htm_cache_operations_total{operation="hit"}[5m])
|
|
160
|
+
/ (rate(htm_cache_operations_total{operation="hit"}[5m])
|
|
161
|
+
+ rate(htm_cache_operations_total{operation="miss"}[5m]))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Compatible Backends
|
|
165
|
+
|
|
166
|
+
HTM metrics work with any OTLP-compatible platform:
|
|
167
|
+
|
|
168
|
+
### Open Source / Self-Hosted
|
|
169
|
+
|
|
170
|
+
| Platform | OTLP Support | Notes |
|
|
171
|
+
|----------|--------------|-------|
|
|
172
|
+
| **Jaeger** | Native (v1.35+) | Distributed tracing with metrics |
|
|
173
|
+
| **Prometheus** | Via receiver | Popular metrics platform |
|
|
174
|
+
| **Grafana Tempo** | Native | Traces with metrics correlation |
|
|
175
|
+
| **Grafana Mimir** | Native | Scalable Prometheus-compatible |
|
|
176
|
+
| **SigNoz** | Native | Full-stack observability |
|
|
177
|
+
| **Uptrace** | Native | Open source APM |
|
|
178
|
+
|
|
179
|
+
### Commercial / SaaS
|
|
180
|
+
|
|
181
|
+
| Platform | OTLP Support | Notes |
|
|
182
|
+
|----------|--------------|-------|
|
|
183
|
+
| **Datadog** | Native | Full-stack observability |
|
|
184
|
+
| **New Relic** | Native | APM and infrastructure |
|
|
185
|
+
| **Honeycomb** | Native (preferred) | High-cardinality observability |
|
|
186
|
+
| **Splunk APM** | Native | Enterprise observability |
|
|
187
|
+
| **Dynatrace** | Native | AI-powered monitoring |
|
|
188
|
+
| **AWS X-Ray** | Via ADOT | AWS native tracing |
|
|
189
|
+
| **Google Cloud Trace** | Native | GCP native tracing |
|
|
190
|
+
| **Azure Monitor** | Native | Azure native monitoring |
|
|
191
|
+
| **Grafana Cloud** | Native | Managed Grafana stack |
|
|
192
|
+
|
|
193
|
+
## Setup Examples
|
|
194
|
+
|
|
195
|
+
### Jaeger (Local Development)
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
# Start Jaeger with OTLP support
|
|
199
|
+
docker run -d --name jaeger \
|
|
200
|
+
-p 4318:4318 \
|
|
201
|
+
-p 16686:16686 \
|
|
202
|
+
jaegertracing/all-in-one:latest
|
|
203
|
+
|
|
204
|
+
# Configure HTM
|
|
205
|
+
export HTM_TELEMETRY_ENABLED="true"
|
|
206
|
+
export OTEL_METRICS_EXPORTER="otlp"
|
|
207
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Prometheus (via OpenTelemetry Collector)
|
|
211
|
+
|
|
212
|
+
```yaml
|
|
213
|
+
# otel-collector-config.yaml
|
|
214
|
+
receivers:
|
|
215
|
+
otlp:
|
|
216
|
+
protocols:
|
|
217
|
+
grpc:
|
|
218
|
+
http:
|
|
219
|
+
|
|
220
|
+
exporters:
|
|
221
|
+
prometheus:
|
|
222
|
+
endpoint: "0.0.0.0:8889"
|
|
223
|
+
|
|
224
|
+
service:
|
|
225
|
+
pipelines:
|
|
226
|
+
metrics:
|
|
227
|
+
receivers: [otlp]
|
|
228
|
+
exporters: [prometheus]
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
docker run -d \
|
|
233
|
+
-p 4317:4317 \
|
|
234
|
+
-p 4318:4318 \
|
|
235
|
+
-p 8889:8889 \
|
|
236
|
+
-v $(pwd)/otel-collector-config.yaml:/etc/otel-collector-config.yaml \
|
|
237
|
+
otel/opentelemetry-collector:latest \
|
|
238
|
+
--config=/etc/otel-collector-config.yaml
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Datadog
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
export HTM_TELEMETRY_ENABLED="true"
|
|
245
|
+
export OTEL_METRICS_EXPORTER="otlp"
|
|
246
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.datadoghq.com"
|
|
247
|
+
export OTEL_EXPORTER_OTLP_HEADERS="DD-API-KEY=your-api-key"
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Honeycomb
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
export HTM_TELEMETRY_ENABLED="true"
|
|
254
|
+
export OTEL_METRICS_EXPORTER="otlp"
|
|
255
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io"
|
|
256
|
+
export OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=your-api-key"
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### New Relic
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
export HTM_TELEMETRY_ENABLED="true"
|
|
263
|
+
export OTEL_METRICS_EXPORTER="otlp"
|
|
264
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.nr-data.net"
|
|
265
|
+
export OTEL_EXPORTER_OTLP_HEADERS="api-key=your-license-key"
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Architecture
|
|
269
|
+
|
|
270
|
+
### Null Object Pattern
|
|
271
|
+
|
|
272
|
+
HTM uses a null object pattern for telemetry. When disabled:
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
# This code runs identically whether telemetry is enabled or not
|
|
276
|
+
HTM::Telemetry.job_counter.add(1, attributes: { 'job' => 'embedding' })
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
When telemetry is disabled or the SDK is not installed:
|
|
280
|
+
- `HTM::Telemetry.meter` returns a `NullMeter`
|
|
281
|
+
- All instruments return `NullInstrument` instances
|
|
282
|
+
- All operations (`add`, `record`) are no-ops returning `nil`
|
|
283
|
+
- Zero memory allocation, zero CPU overhead
|
|
284
|
+
|
|
285
|
+
### Code Flow
|
|
286
|
+
|
|
287
|
+

|
|
288
|
+
|
|
289
|
+
### Instrumentation Points
|
|
290
|
+
|
|
291
|
+
HTM instruments these key operations:
|
|
292
|
+
|
|
293
|
+
| Component | Metrics Recorded |
|
|
294
|
+
|-----------|-----------------|
|
|
295
|
+
| `GenerateEmbeddingJob` | `htm.jobs`, `htm.embedding.latency` |
|
|
296
|
+
| `GenerateTagsJob` | `htm.jobs`, `htm.tag.latency` |
|
|
297
|
+
| `VectorSearch#search` | `htm.search.latency` (strategy: vector) |
|
|
298
|
+
| `FulltextSearch#search_fulltext` | `htm.search.latency` (strategy: fulltext) |
|
|
299
|
+
| `HybridSearch#search_hybrid` | `htm.search.latency` (strategy: hybrid) |
|
|
300
|
+
| `QueryCache#fetch` | `htm.cache.operations` |
|
|
301
|
+
|
|
302
|
+
## Testing
|
|
303
|
+
|
|
304
|
+
When writing tests, reset telemetry state between tests:
|
|
305
|
+
|
|
306
|
+
```ruby
|
|
307
|
+
def setup
|
|
308
|
+
HTM::Telemetry.reset!
|
|
309
|
+
HTM.configuration.telemetry_enabled = false
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def teardown
|
|
313
|
+
HTM::Telemetry.reset!
|
|
314
|
+
end
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Verifying Metrics in Tests
|
|
318
|
+
|
|
319
|
+
```ruby
|
|
320
|
+
def test_records_embedding_latency
|
|
321
|
+
# Create a mock histogram
|
|
322
|
+
recorded = []
|
|
323
|
+
mock_histogram = Object.new
|
|
324
|
+
mock_histogram.define_singleton_method(:record) do |value, **kwargs|
|
|
325
|
+
recorded << { value: value, attributes: kwargs[:attributes] }
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
# Inject mock (requires test helper)
|
|
329
|
+
HTM::Telemetry.instance_variable_set(:@embedding_latency, mock_histogram)
|
|
330
|
+
|
|
331
|
+
# Run operation
|
|
332
|
+
HTM::Jobs::GenerateEmbeddingJob.perform(node_id: node.id)
|
|
333
|
+
|
|
334
|
+
# Verify
|
|
335
|
+
assert recorded.any? { |r| r[:attributes]['status'] == 'success' }
|
|
336
|
+
end
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## Troubleshooting
|
|
340
|
+
|
|
341
|
+
### Metrics Not Appearing
|
|
342
|
+
|
|
343
|
+
1. **Check telemetry is enabled:**
|
|
344
|
+
```ruby
|
|
345
|
+
puts HTM.configuration.telemetry_enabled # Should be true
|
|
346
|
+
puts HTM::Telemetry.enabled? # Should be true
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
2. **Check SDK is installed:**
|
|
350
|
+
```ruby
|
|
351
|
+
puts HTM::Telemetry.sdk_available? # Should be true
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
3. **Check environment variables:**
|
|
355
|
+
```bash
|
|
356
|
+
echo $OTEL_METRICS_EXPORTER
|
|
357
|
+
echo $OTEL_EXPORTER_OTLP_ENDPOINT
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
4. **Check collector is running:**
|
|
361
|
+
```bash
|
|
362
|
+
curl http://localhost:4318/v1/metrics # Should not 404
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### High Cardinality Issues
|
|
366
|
+
|
|
367
|
+
Avoid high-cardinality attributes in metrics. HTM uses low-cardinality attributes by design:
|
|
368
|
+
|
|
369
|
+
- `job`: 2-3 values (embedding, tags, propositions)
|
|
370
|
+
- `status`: 3 values (success, error, circuit_open)
|
|
371
|
+
- `provider`: ~10 values (ollama, openai, etc.)
|
|
372
|
+
- `strategy`: 3 values (vector, fulltext, hybrid)
|
|
373
|
+
- `operation`: 2 values (hit, miss)
|
|
374
|
+
|
|
375
|
+
### Performance Concerns
|
|
376
|
+
|
|
377
|
+
With telemetry disabled (default), there is zero overhead:
|
|
378
|
+
- No gem loading
|
|
379
|
+
- No object allocation
|
|
380
|
+
- No method calls beyond the null check
|
|
381
|
+
|
|
382
|
+
With telemetry enabled, overhead is minimal:
|
|
383
|
+
- ~1-5μs per metric recording
|
|
384
|
+
- Memory for histogram buckets (~1KB per histogram)
|
|
385
|
+
|
|
386
|
+
## References
|
|
387
|
+
|
|
388
|
+
- [OpenTelemetry Ruby SDK](https://github.com/open-telemetry/opentelemetry-ruby)
|
|
389
|
+
- [OpenTelemetry Metrics SDK](https://github.com/open-telemetry/opentelemetry-ruby/tree/main/metrics_sdk)
|
|
390
|
+
- [OpenTelemetry Specification](https://opentelemetry.io/docs/specs/otel/)
|
|
391
|
+
- [OTLP Protocol](https://opentelemetry.io/docs/specs/otlp/)
|
data/examples/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# HTM Examples
|
|
2
2
|
|
|
3
|
-
This directory contains example applications demonstrating various ways to use the HTM (Hierarchical
|
|
3
|
+
This directory contains example applications demonstrating various ways to use the HTM (Hierarchical Temporal Memory) gem.
|
|
4
4
|
|
|
5
5
|
## Prerequisites
|
|
6
6
|
|
|
@@ -11,6 +11,12 @@ All examples require:
|
|
|
11
11
|
export HTM_DBURL="postgresql://user@localhost:5432/htm_development"
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
> **Note**: Database selection now respects `RAILS_ENV`. If `RAILS_ENV` is set,
|
|
15
|
+
> HTM extracts the base name from `HTM_DBURL` and appends the environment suffix.
|
|
16
|
+
> For example, with `HTM_DBURL=...htm_development` and `RAILS_ENV=test`, HTM
|
|
17
|
+
> connects to `htm_test`. When `RAILS_ENV` is unset (typical for examples),
|
|
18
|
+
> behavior is unchanged.
|
|
19
|
+
|
|
14
20
|
2. **Ollama** (recommended for local LLM):
|
|
15
21
|
```bash
|
|
16
22
|
ollama pull nomic-embed-text # For embeddings
|
|
@@ -107,6 +113,39 @@ ruby examples/timeframe_demo.rb
|
|
|
107
113
|
|
|
108
114
|
---
|
|
109
115
|
|
|
116
|
+
### telemetry/
|
|
117
|
+
|
|
118
|
+
**Live Grafana dashboard for HTM metrics.**
|
|
119
|
+
|
|
120
|
+
A complete telemetry demo using Homebrew-installed Prometheus and Grafana to visualize HTM metrics in real-time graphs.
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
cd examples/telemetry
|
|
124
|
+
ruby demo.rb
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The demo automatically:
|
|
128
|
+
- Checks/installs Prometheus and Grafana via Homebrew
|
|
129
|
+
- Starts both services if not running
|
|
130
|
+
- Configures Prometheus to scrape the demo's metrics
|
|
131
|
+
- Cleans up previous demo data
|
|
132
|
+
- Opens Grafana in your browser
|
|
133
|
+
|
|
134
|
+
**Features:**
|
|
135
|
+
- Live updating dashboard with job counts, latencies, cache hit rates
|
|
136
|
+
- Pre-built Grafana dashboard JSON
|
|
137
|
+
- No Docker required - uses native macOS services
|
|
138
|
+
|
|
139
|
+
**Dependencies:**
|
|
140
|
+
```bash
|
|
141
|
+
brew install prometheus grafana
|
|
142
|
+
gem install prometheus-client webrick
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
See [telemetry/README.md](telemetry/README.md) for detailed setup instructions.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
110
149
|
### mcp_server.rb & mcp_client.rb
|
|
111
150
|
|
|
112
151
|
**Model Context Protocol (MCP) integration for AI assistants.**
|
|
@@ -366,6 +405,11 @@ examples/
|
|
|
366
405
|
├── custom_llm_configuration.rb # LLM integration patterns
|
|
367
406
|
├── file_loader_usage.rb # Document loading
|
|
368
407
|
├── timeframe_demo.rb # Time-based filtering
|
|
408
|
+
├── telemetry/
|
|
409
|
+
│ ├── demo.rb # Live Grafana metrics dashboard
|
|
410
|
+
│ ├── README.md
|
|
411
|
+
│ ├── SETUP_README.md
|
|
412
|
+
│ └── grafana/dashboards/htm-metrics.json
|
|
369
413
|
├── mcp_server.rb # MCP server exposing HTM tools
|
|
370
414
|
├── mcp_client.rb # MCP client with chat interface
|
|
371
415
|
├── example_app/
|
|
@@ -397,6 +441,7 @@ examples/
|
|
|
397
441
|
| Custom LLM integration | `custom_llm_configuration.rb` |
|
|
398
442
|
| Loading documents/files | `file_loader_usage.rb` |
|
|
399
443
|
| Time-based queries | `timeframe_demo.rb` |
|
|
444
|
+
| Production observability | `telemetry/` |
|
|
400
445
|
| MCP server for AI assistants | `mcp_server.rb` |
|
|
401
446
|
| MCP client with chat interface | `mcp_client.rb` |
|
|
402
447
|
| Web application | `sinatra_app/` |
|
data/examples/cli_app/README.md
CHANGED
|
@@ -111,7 +111,7 @@ This means:
|
|
|
111
111
|
$ ruby htm_cli.rb
|
|
112
112
|
|
|
113
113
|
============================================================
|
|
114
|
-
HTM CLI - Hierarchical
|
|
114
|
+
HTM CLI - Hierarchical Temporal Memory Assistant
|
|
115
115
|
============================================================
|
|
116
116
|
|
|
117
117
|
Job Backend: inline (synchronous execution)
|
data/examples/cli_app/htm_cli.rb
CHANGED
|
@@ -67,7 +67,7 @@ class HTMCli
|
|
|
67
67
|
def run
|
|
68
68
|
puts
|
|
69
69
|
puts "=" * 60
|
|
70
|
-
puts "HTM CLI - Hierarchical
|
|
70
|
+
puts "HTM CLI - Hierarchical Temporal Memory Assistant"
|
|
71
71
|
puts "=" * 60
|
|
72
72
|
puts
|
|
73
73
|
puts "Job Backend: #{HTM.configuration.job_backend} (synchronous execution)"
|
data/examples/sinatra_app/app.rb
CHANGED
|
@@ -281,7 +281,7 @@ __END__
|
|
|
281
281
|
</head>
|
|
282
282
|
<body>
|
|
283
283
|
<h1>HTM Sinatra Example</h1>
|
|
284
|
-
<p>Hierarchical
|
|
284
|
+
<p>Hierarchical Temporal Memory with tag-enhanced hybrid search and Sidekiq background jobs</p>
|
|
285
285
|
|
|
286
286
|
<div class="section">
|
|
287
287
|
<h2>Remember Information</h2>
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# HTM Telemetry Demo
|
|
2
|
+
|
|
3
|
+
This demo shows HTM metrics in a **live Grafana dashboard** using locally installed Prometheus and Grafana via Homebrew.
|
|
4
|
+
|
|
5
|
+
> **First time setup?** See [SETUP_README.md](SETUP_README.md) for detailed installation and configuration instructions.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# 1. Install Prometheus and Grafana via Homebrew
|
|
11
|
+
brew install prometheus grafana
|
|
12
|
+
|
|
13
|
+
# 2. Install required Ruby gems
|
|
14
|
+
gem install prometheus-client webrick
|
|
15
|
+
|
|
16
|
+
# 3. Run the demo
|
|
17
|
+
cd examples/telemetry
|
|
18
|
+
ruby demo.rb
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The demo will automatically:
|
|
22
|
+
- Check that Prometheus and Grafana are installed
|
|
23
|
+
- Start both services if not already running
|
|
24
|
+
- Configure Prometheus to scrape the demo's metrics
|
|
25
|
+
- Clean up any previous demo data (hard delete)
|
|
26
|
+
- Open Grafana in your browser
|
|
27
|
+
- Run HTM operations and export live metrics
|
|
28
|
+
|
|
29
|
+
## What You'll See
|
|
30
|
+
|
|
31
|
+
Once running, open Grafana at http://localhost:3000 (login: admin/admin) and import the dashboard.
|
|
32
|
+
|
|
33
|
+
### Dashboard Panels
|
|
34
|
+
|
|
35
|
+
| Panel | Description |
|
|
36
|
+
|-------|-------------|
|
|
37
|
+
| **Total Successful Jobs** | Count of completed embedding and tag jobs |
|
|
38
|
+
| **Total Failed Jobs** | Count of failed jobs |
|
|
39
|
+
| **Cache Hit Rate** | Percentage of queries served from cache |
|
|
40
|
+
| **LLM Job Latency (p95)** | 95th percentile latency for embedding/tag generation |
|
|
41
|
+
| **Search Latency by Strategy** | p95 latency for vector, fulltext, hybrid search |
|
|
42
|
+
| **Jobs per Minute** | Throughput by job type |
|
|
43
|
+
| **Cache Operations** | Hit/miss rate over time |
|
|
44
|
+
|
|
45
|
+
## Importing the Dashboard
|
|
46
|
+
|
|
47
|
+
1. Open Grafana: http://localhost:3000
|
|
48
|
+
2. Go to: **Dashboards** → **Import**
|
|
49
|
+
3. Click "Upload JSON file"
|
|
50
|
+
4. Select: `examples/telemetry/grafana/dashboards/htm-metrics.json`
|
|
51
|
+
5. Select your Prometheus datasource
|
|
52
|
+
6. Click **Import**
|
|
53
|
+
|
|
54
|
+
If you don't have a Prometheus datasource configured:
|
|
55
|
+
1. Go to: **Connections** → **Data sources** → **Add data source**
|
|
56
|
+
2. Select **Prometheus**
|
|
57
|
+
3. URL: `http://localhost:9090`
|
|
58
|
+
4. Click **Save & test**
|
|
59
|
+
|
|
60
|
+
## Architecture
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
┌─────────────┐ ┌─────────────────┐
|
|
64
|
+
│ demo.rb │ ──── scrapes ────▶ │ Prometheus │
|
|
65
|
+
│ (metrics │ :9394 │ :9090 │
|
|
66
|
+
│ server) │ └────────┬────────┘
|
|
67
|
+
└─────────────┘ │
|
|
68
|
+
│ PromQL queries
|
|
69
|
+
│ │
|
|
70
|
+
▼ ▼
|
|
71
|
+
┌─────────────┐ ┌─────────────────┐
|
|
72
|
+
│ HTM │ │ Grafana │
|
|
73
|
+
│ operations │ │ :3000 │
|
|
74
|
+
└─────────────┘ └─────────────────┘
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Available Metrics
|
|
78
|
+
|
|
79
|
+
| Metric | Type | Labels | Description |
|
|
80
|
+
|--------|------|--------|-------------|
|
|
81
|
+
| `htm_jobs_total` | Counter | job, status | Job execution count |
|
|
82
|
+
| `htm_embedding_latency_milliseconds` | Histogram | provider, status | Embedding time |
|
|
83
|
+
| `htm_tag_latency_milliseconds` | Histogram | provider, status | Tag extraction time |
|
|
84
|
+
| `htm_search_latency_milliseconds` | Histogram | strategy | Search operation time |
|
|
85
|
+
| `htm_cache_operations_total` | Counter | operation | Cache hit/miss count |
|
|
86
|
+
|
|
87
|
+
## Endpoints
|
|
88
|
+
|
|
89
|
+
| Service | URL | Purpose |
|
|
90
|
+
|---------|-----|---------|
|
|
91
|
+
| Demo Metrics | http://localhost:9394/metrics | Raw Prometheus metrics |
|
|
92
|
+
| Prometheus | http://localhost:9090 | Metrics storage & queries |
|
|
93
|
+
| Grafana | http://localhost:3000 | Visualization (admin/admin) |
|
|
94
|
+
|
|
95
|
+
## Stopping Services
|
|
96
|
+
|
|
97
|
+
The demo leaves Prometheus and Grafana running for convenience. To stop them:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
brew services stop prometheus grafana
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Troubleshooting
|
|
104
|
+
|
|
105
|
+
### No metrics in Grafana
|
|
106
|
+
|
|
107
|
+
1. Verify the demo is running and exposing metrics:
|
|
108
|
+
```bash
|
|
109
|
+
curl http://localhost:9394/metrics
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
2. Check Prometheus is scraping:
|
|
113
|
+
- Open http://localhost:9090/targets
|
|
114
|
+
- Look for `htm-demo` target with state "UP"
|
|
115
|
+
|
|
116
|
+
3. If target is missing, check Prometheus config:
|
|
117
|
+
```bash
|
|
118
|
+
cat /opt/homebrew/etc/prometheus.yml
|
|
119
|
+
# Should include htm-demo job
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Port already in use
|
|
123
|
+
|
|
124
|
+
If port 9394 is busy, edit `demo.rb` and change `METRICS_PORT`.
|
|
125
|
+
|
|
126
|
+
### Services won't start
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
# Check service status
|
|
130
|
+
brew services list
|
|
131
|
+
|
|
132
|
+
# View logs
|
|
133
|
+
brew services info prometheus
|
|
134
|
+
brew services info grafana
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Files
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
examples/telemetry/
|
|
141
|
+
├── README.md # This file
|
|
142
|
+
├── SETUP_README.md # Detailed setup instructions
|
|
143
|
+
├── demo.rb # Main demo script
|
|
144
|
+
└── grafana/
|
|
145
|
+
└── dashboards/
|
|
146
|
+
└── htm-metrics.json # Import this into Grafana
|
|
147
|
+
```
|