@ceschiatti/redistail 0.0.3 → 0.0.4

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,680 @@
1
+ A command-line utility for monitoring Redis messages in real-time, similar to how `tail -f` works for log files. Redistail supports both Redis Pub/Sub channels and Redis Streams, making it an essential tool for debugging and monitoring Redis-based applications.
2
+
3
+ Originally part of the `effect-redis` library, it is now available as a standalone package for easier installation and use.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Installation](#installation)
8
+ - [Quick Start](#quick-start)
9
+ - [Usage](#usage)
10
+ - [Configuration](#configuration)
11
+ - [Examples](#examples)
12
+ - [Environment Variables](#environment-variables)
13
+ - [Output Format](#output-format)
14
+ - [Troubleshooting](#troubleshooting)
15
+ - [Advanced Usage](#advanced-usage)
16
+
17
+ ## Installation
18
+
19
+ ### Global Installation
20
+
21
+ Install `redistail` globally to make the `redistail` command available everywhere on your system:
22
+
23
+ ```bash
24
+ # Using npm
25
+ npm install -g @ceschiatti/redistail
26
+
27
+ # Using pnpm
28
+ pnpm add -g @ceschiatti/redistail
29
+
30
+ # Using bun
31
+ bun add -g @ceschiatti/redistail
32
+ ```
33
+
34
+ ### Local Installation
35
+
36
+ If you're working within a project:
37
+
38
+ ```bash
39
+ # Using npm
40
+ npm install @ceschiatti/redistail
41
+
42
+ # Using pnpm
43
+ pnpm add @ceschiatti/redistail
44
+
45
+ # Using bun
46
+ bun add @ceschiatti/redistail
47
+
48
+ # Run locally using npx
49
+ npx redistail --help
50
+ ```
51
+
52
+ ### Verify Installation
53
+
54
+ ```bash
55
+ redistail --version
56
+ redistail --help
57
+ ```
58
+
59
+ ## Quick Start
60
+
61
+ ### Monitor a Pub/Sub Channel
62
+
63
+ ```bash
64
+ redistail pubsub my-channel
65
+ ```
66
+
67
+ ### Monitor a Redis Stream
68
+
69
+ ```bash
70
+ redistail stream my-stream
71
+ ```
72
+
73
+ ### With Custom Redis Connection
74
+
75
+ ```bash
76
+ REDIS_HOST=redis.example.com redistail pubsub notifications
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ ### Basic Syntax
82
+
83
+ ```bash
84
+ redistail <CONNECTION_TYPE> <TOPIC_NAME> [OPTIONS]
85
+ ```
86
+
87
+ ### Arguments
88
+
89
+ - **CONNECTION_TYPE**: Either `pubsub` or `stream`
90
+ - **TOPIC_NAME**: The channel name (for Pub/Sub) or stream key (for Streams)
91
+
92
+ ### Options
93
+
94
+ - `-h, --help`: Show help message
95
+ - `-v, --version`: Show version information
96
+
97
+ ### Connection Types
98
+
99
+ #### Pub/Sub Monitoring
100
+
101
+ Monitor Redis Pub/Sub channels for real-time message broadcasting:
102
+
103
+ ```bash
104
+ redistail pubsub channel-name
105
+ ```
106
+
107
+ - Subscribes to the specified channel
108
+ - Displays messages as they are published
109
+ - Shows timestamp, channel name, and message content
110
+ - Continues running until interrupted (Ctrl+C)
111
+
112
+ #### Stream Monitoring
113
+
114
+ Monitor Redis Streams for persistent message queues:
115
+
116
+ ```bash
117
+ redistail stream stream-name
118
+ ```
119
+
120
+ - Reads from the stream starting from the latest entry
121
+ - Uses blocking XREAD to wait for new entries
122
+ - Shows timestamp, entry ID, and all message fields
123
+ - Maintains continuity across reconnections
124
+
125
+ ## Configuration
126
+
127
+ Redistail can be configured through environment variables to work with different Redis instances and customize display options.
128
+
129
+ ### Redis Connection
130
+
131
+ Configure Redis connection settings:
132
+
133
+ ```bash
134
+ # Basic connection
135
+ export REDIS_HOST=localhost
136
+ export REDIS_PORT=6379
137
+
138
+ # Or use a complete connection URL
139
+ export REDIS_URL=redis://username:password@redis.example.com:6380
140
+
141
+ # Connection timeouts and retries
142
+ export REDIS_TIMEOUT=5000
143
+ export REDIS_RETRY_ATTEMPTS=3
144
+ export REDIS_RETRY_DELAY=1000
145
+ ```
146
+
147
+ ### Display Options
148
+
149
+ Customize output formatting:
150
+
151
+ ```bash
152
+ # Enable/disable colored output
153
+ export REDISTAIL_COLORS=true
154
+
155
+ # Enable/disable timestamps
156
+ export REDISTAIL_TIMESTAMPS=true
157
+
158
+ # Enable/disable JSON pretty-printing
159
+ export REDISTAIL_PRETTY_JSON=true
160
+ ```
161
+
162
+ ### Monitoring Options
163
+
164
+ Configure monitoring behavior:
165
+
166
+ ```bash
167
+ # Blocking timeout for stream reads (milliseconds)
168
+ export REDISTAIL_BLOCK_TIMEOUT=5000
169
+
170
+ # Maximum reconnection attempts
171
+ export REDISTAIL_MAX_RECONNECT_ATTEMPTS=5
172
+ ```
173
+
174
+ ## Examples
175
+
176
+ ### Basic Pub/Sub Monitoring
177
+
178
+ Monitor a simple notification channel:
179
+
180
+ ```bash
181
+ redistail pubsub notifications
182
+ ```
183
+
184
+ Output:
185
+ ```
186
+ 2024-01-07 15:30:45.123 [notifications] Welcome to the system!
187
+ 2024-01-07 15:30:47.456 [notifications] New user registered: john@example.com
188
+ 2024-01-07 15:30:50.789 [notifications] System maintenance scheduled for tonight
189
+ ```
190
+
191
+ ### Stream Monitoring with JSON Messages
192
+
193
+ Monitor a stream containing structured data:
194
+
195
+ ```bash
196
+ redistail stream user-events
197
+ ```
198
+
199
+ Output:
200
+ ```
201
+ 2024-01-07 15:31:12.345 [user-events:1704636672345-0] event=login user=alice timestamp=1704636672345
202
+ 2024-01-07 15:31:15.678 [user-events:1704636675678-0] event=purchase user=bob amount=29.99 product=premium-plan
203
+ 2024-01-07 15:31:18.901 [user-events:1704636678901-0] event=logout user=alice session_duration=300
204
+ ```
205
+
206
+ ### Remote Redis Instance
207
+
208
+ Connect to a remote Redis server:
209
+
210
+ ```bash
211
+ REDIS_HOST=redis.production.com REDIS_PORT=6380 redistail pubsub alerts
212
+ ```
213
+
214
+ ### Authenticated Redis Connection
215
+
216
+ Use Redis with authentication:
217
+
218
+ ```bash
219
+ REDIS_URL=redis://myuser:mypassword@redis.example.com:6379 redistail stream orders
220
+ ```
221
+
222
+ ### Custom Display Settings
223
+
224
+ Monitor with custom formatting:
225
+
226
+ ```bash
227
+ # Disable colors and timestamps for clean output
228
+ REDISTAIL_COLORS=false REDISTAIL_TIMESTAMPS=false redistail pubsub logs
229
+ ```
230
+
231
+ ### JSON Message Pretty-Printing
232
+
233
+ When messages contain JSON, redistail automatically formats them:
234
+
235
+ ```bash
236
+ redistail pubsub api-events
237
+ ```
238
+
239
+ Input message: `{"user":"alice","action":"login","timestamp":1704636672345}`
240
+
241
+ Output:
242
+ ```
243
+ 2024-01-07 15:31:12.345 [api-events] {
244
+ "user": "alice",
245
+ "action": "login",
246
+ "timestamp": 1704636672345
247
+ }
248
+ ```
249
+
250
+ ### Multiple Instances
251
+
252
+ Run multiple redistail instances to monitor different channels:
253
+
254
+ ```bash
255
+ # Terminal 1: Monitor user events
256
+ redistail stream user-events
257
+
258
+ # Terminal 2: Monitor system notifications
259
+ redistail pubsub system-notifications
260
+
261
+ # Terminal 3: Monitor error logs
262
+ redistail pubsub error-logs
263
+ ```
264
+
265
+ ### Development Workflow
266
+
267
+ Common development patterns:
268
+
269
+ ```bash
270
+ # Monitor application logs during development
271
+ redistail pubsub app-logs &
272
+
273
+ # Monitor user activity in another terminal
274
+ redistail stream user-activity &
275
+
276
+ # Start your application
277
+ npm run dev
278
+
279
+ # Stop monitoring (kill background processes)
280
+ jobs
281
+ kill %1 %2
282
+ ```
283
+
284
+ ## Environment Variables
285
+
286
+ ### Redis Connection Variables
287
+
288
+ | Variable | Default | Description |
289
+ |----------|---------|-------------|
290
+ | `REDIS_HOST` | `127.0.0.1` | Redis server hostname |
291
+ | `REDIS_PORT` | `6379` | Redis server port |
292
+ | `REDIS_URL` | - | Complete Redis connection URL (overrides host/port) |
293
+ | `REDIS_TIMEOUT` | `5000` | Connection timeout in milliseconds |
294
+ | `REDIS_RETRY_ATTEMPTS` | `3` | Number of connection retry attempts |
295
+ | `REDIS_RETRY_DELAY` | `1000` | Delay between retry attempts in milliseconds |
296
+
297
+ ### Display Variables
298
+
299
+ | Variable | Default | Description |
300
+ |----------|---------|-------------|
301
+ | `REDISTAIL_COLORS` | `true` | Enable colored output |
302
+ | `REDISTAIL_TIMESTAMPS` | `true` | Show timestamps in output |
303
+ | `REDISTAIL_PRETTY_JSON` | `true` | Pretty-print JSON content |
304
+
305
+ ### Monitoring Variables
306
+
307
+ | Variable | Default | Description |
308
+ |----------|---------|-------------|
309
+ | `REDISTAIL_BLOCK_TIMEOUT` | `5000` | Stream blocking timeout in milliseconds |
310
+ | `REDISTAIL_MAX_RECONNECT_ATTEMPTS` | `5` | Maximum reconnection attempts |
311
+
312
+ ### Configuration Examples
313
+
314
+ #### Production Environment
315
+
316
+ ```bash
317
+ # .env.production
318
+ REDIS_URL=redis://prod-user:secure-password@redis-cluster.prod.com:6380
319
+ REDISTAIL_COLORS=false
320
+ REDISTAIL_TIMESTAMPS=true
321
+ REDISTAIL_MAX_RECONNECT_ATTEMPTS=10
322
+ ```
323
+
324
+ #### Development Environment
325
+
326
+ ```bash
327
+ # .env.development
328
+ REDIS_HOST=localhost
329
+ REDIS_PORT=6379
330
+ REDISTAIL_COLORS=true
331
+ REDISTAIL_PRETTY_JSON=true
332
+ REDISTAIL_BLOCK_TIMEOUT=1000
333
+ ```
334
+
335
+ #### CI/CD Environment
336
+
337
+ ```bash
338
+ # Minimal output for automated systems
339
+ REDISTAIL_COLORS=false
340
+ REDISTAIL_TIMESTAMPS=false
341
+ REDISTAIL_PRETTY_JSON=false
342
+ ```
343
+
344
+ ## Output Format
345
+
346
+ ### Pub/Sub Messages
347
+
348
+ ```
349
+ [TIMESTAMP] [CHANNEL] MESSAGE_CONTENT
350
+ ```
351
+
352
+ Example:
353
+ ```
354
+ 2024-01-07 15:30:45.123 [notifications] User alice logged in
355
+ ```
356
+
357
+ ### Stream Messages
358
+
359
+ ```
360
+ [TIMESTAMP] [STREAM_KEY:ENTRY_ID] FIELD1=VALUE1 FIELD2=VALUE2 ...
361
+ ```
362
+
363
+ Example:
364
+ ```
365
+ 2024-01-07 15:31:12.345 [events:1704636672345-0] user=alice action=login ip=192.168.1.100
366
+ ```
367
+
368
+ ### Color Coding
369
+
370
+ When colors are enabled (`REDISTAIL_COLORS=true`):
371
+
372
+ - **Gray**: Timestamps
373
+ - **Cyan**: Pub/Sub channel names
374
+ - **Magenta**: Stream names and entry IDs
375
+ - **Yellow**: Field names in streams
376
+ - **White**: Message content and field values
377
+ - **Red**: Error messages
378
+
379
+ ### Error Messages
380
+
381
+ Errors are displayed on stderr with clear descriptions:
382
+
383
+ ```
384
+ ❌ Error: Connection failed to redis://localhost:6379
385
+ ❌ Error: Invalid topic name: cannot be empty
386
+ ❌ Error: Maximum retry attempts (5) exceeded
387
+ ```
388
+
389
+ ## Troubleshooting
390
+
391
+ ### Common Issues
392
+
393
+ #### Connection Refused
394
+
395
+ **Problem**: `Error: Connection failed to redis://localhost:6379`
396
+
397
+ **Solutions**:
398
+ 1. Verify Redis is running: `redis-cli ping`
399
+ 2. Check Redis configuration: `redis-cli info server`
400
+ 3. Verify host and port: `REDIS_HOST=localhost REDIS_PORT=6379 redistail --help`
401
+ 4. Test with redis-cli: `redis-cli -h localhost -p 6379 ping`
402
+
403
+ #### Authentication Failed
404
+
405
+ **Problem**: `Error: Authentication failed`
406
+
407
+ **Solutions**:
408
+ 1. Include credentials in URL: `REDIS_URL=redis://user:pass@host:port`
409
+ 2. Check Redis AUTH configuration: `redis-cli config get requirepass`
410
+ 3. Verify username/password are correct
411
+
412
+ #### No Messages Appearing
413
+
414
+ **Problem**: Redistail connects but shows no messages
415
+
416
+ **Solutions**:
417
+ 1. Verify the channel/stream exists:
418
+ ```bash
419
+ # For Pub/Sub
420
+ redis-cli publish test-channel "test message"
421
+
422
+ # For Streams
423
+ redis-cli xadd test-stream * field value
424
+ ```
425
+ 2. Check if you're monitoring the correct topic name
426
+ 3. Ensure messages are being published to the topic
427
+ 4. For streams, verify entries exist: `redis-cli xlen stream-name`
428
+
429
+ #### Permission Denied
430
+
431
+ **Problem**: `Error: Permission denied for command`
432
+
433
+ **Solutions**:
434
+ 1. Check Redis ACL permissions: `redis-cli acl whoami`
435
+ 2. Verify user has subscribe permissions: `redis-cli acl getuser username`
436
+ 3. Use a user with appropriate permissions
437
+
438
+ #### High Memory Usage
439
+
440
+ **Problem**: Redistail consuming too much memory
441
+
442
+ **Solutions**:
443
+ 1. Reduce block timeout: `REDISTAIL_BLOCK_TIMEOUT=1000`
444
+ 2. Monitor smaller streams or channels
445
+ 3. Disable JSON pretty-printing: `REDISTAIL_PRETTY_JSON=false`
446
+ 4. Use multiple instances for different topics instead of one large stream
447
+
448
+ ### Network Issues
449
+
450
+ #### Intermittent Disconnections
451
+
452
+ **Problem**: Frequent connection drops
453
+
454
+ **Solutions**:
455
+ 1. Increase retry attempts: `REDISTAIL_MAX_RECONNECT_ATTEMPTS=10`
456
+ 2. Adjust retry delay: `REDIS_RETRY_DELAY=2000`
457
+ 3. Check network stability between client and Redis server
458
+ 4. Monitor Redis server logs for connection issues
459
+
460
+ #### Slow Performance
461
+
462
+ **Problem**: Messages appear with delay
463
+
464
+ **Solutions**:
465
+ 1. Reduce block timeout: `REDISTAIL_BLOCK_TIMEOUT=1000`
466
+ 2. Check Redis server performance: `redis-cli --latency`
467
+ 3. Monitor network latency: `ping redis-host`
468
+ 4. Verify Redis server isn't overloaded: `redis-cli info stats`
469
+
470
+ ### Configuration Issues
471
+
472
+ #### Environment Variables Not Working
473
+
474
+ **Problem**: Environment variables seem to be ignored
475
+
476
+ **Solutions**:
477
+ 1. Verify variable names are correct (case-sensitive)
478
+ 2. Export variables in current shell: `export REDIS_HOST=myhost`
479
+ 3. Check variable values: `echo $REDIS_HOST`
480
+ 4. Use inline variables: `REDIS_HOST=myhost redistail pubsub test`
481
+
482
+ #### Colors Not Displaying
483
+
484
+ **Problem**: Output appears without colors
485
+
486
+ **Solutions**:
487
+ 1. Enable colors explicitly: `REDISTAIL_COLORS=true`
488
+ 2. Check terminal color support: `echo $TERM`
489
+ 3. Test with a color-supporting terminal
490
+ 4. Verify output isn't being piped: `redistail pubsub test | cat` (disables colors)
491
+
492
+ ### Debugging Steps
493
+
494
+ #### Enable Verbose Logging
495
+
496
+ While redistail doesn't have a verbose mode, you can debug connection issues:
497
+
498
+ ```bash
499
+ # Test Redis connection manually
500
+ redis-cli -h $REDIS_HOST -p $REDIS_PORT ping
501
+
502
+ # Monitor Redis server logs
503
+ redis-cli monitor
504
+
505
+ # Check Redis configuration
506
+ redis-cli config get "*"
507
+ ```
508
+
509
+ #### Test with Simple Commands
510
+
511
+ ```bash
512
+ # Test basic connectivity
513
+ redis-cli ping
514
+
515
+ # Test pub/sub manually
516
+ redis-cli subscribe test-channel
517
+ # In another terminal:
518
+ redis-cli publish test-channel "hello"
519
+
520
+ # Test streams manually
521
+ redis-cli xadd test-stream * message "hello"
522
+ redis-cli xread STREAMS test-stream 0
523
+ ```
524
+
525
+ #### Check System Resources
526
+
527
+ ```bash
528
+ # Check available memory
529
+ free -h
530
+
531
+ # Check network connectivity
532
+ netstat -an | grep 6379
533
+
534
+ # Check process limits
535
+ ulimit -a
536
+ ```
537
+
538
+ ### Getting Help
539
+
540
+ If you encounter issues not covered here:
541
+
542
+ 1. Check the [GitHub Issues](https://github.com/6qat/monorepo/issues)
543
+ 2. Review Redis server logs
544
+ 3. Test with the standard `redis-cli` tool first
545
+ 4. Create a minimal reproduction case
546
+ 5. Include environment details (OS, Redis version, Node.js version)
547
+
548
+ ### Performance Tips
549
+
550
+ #### Optimize for High-Volume Streams
551
+
552
+ ```bash
553
+ # Reduce output overhead
554
+ REDISTAIL_COLORS=false REDISTAIL_TIMESTAMPS=false redistail stream high-volume
555
+
556
+ # Use shorter block timeouts
557
+ REDISTAIL_BLOCK_TIMEOUT=100 redistail stream fast-stream
558
+
559
+ # Monitor specific time ranges (use redis-cli for historical data)
560
+ redis-cli xrange stream-name - + COUNT 100
561
+ ```
562
+
563
+ #### Monitor Multiple Topics Efficiently
564
+
565
+ ```bash
566
+ # Use separate processes for different priorities
567
+ redistail pubsub critical-alerts &
568
+ redistail pubsub info-logs > /dev/null & # Discard low-priority output
569
+ ```
570
+
571
+ ## Advanced Usage
572
+
573
+ ### Integration with Other Tools
574
+
575
+ #### Log Aggregation
576
+
577
+ ```bash
578
+ # Send output to syslog
579
+ redistail pubsub app-logs | logger -t redistail
580
+
581
+ # Append to log files with timestamps
582
+ redistail stream events >> /var/log/redis-events.log
583
+
584
+ # Filter and process messages
585
+ redistail pubsub notifications | grep "ERROR" | mail -s "Redis Errors" admin@example.com
586
+ ```
587
+
588
+ #### Monitoring and Alerting
589
+
590
+ ```bash
591
+ # Count messages per minute
592
+ redistail pubsub events | pv -l -i 60 > /dev/null
593
+
594
+ # Alert on specific patterns
595
+ redistail stream errors | grep "CRITICAL" | while read line; do
596
+ echo "Critical error detected: $line" | mail -s "ALERT" ops@example.com
597
+ done
598
+
599
+ # Integration with monitoring systems
600
+ redistail pubsub metrics | while read metric; do
601
+ curl -X POST http://monitoring-system/api/metrics -d "$metric"
602
+ done
603
+ ```
604
+
605
+ #### Development Workflows
606
+
607
+ ```bash
608
+ # Auto-restart on configuration changes
609
+ while inotifywait -e modify redis.conf; do
610
+ pkill redistail
611
+ redistail pubsub app-logs &
612
+ done
613
+
614
+ # Conditional monitoring based on environment
615
+ if [ "$NODE_ENV" = "development" ]; then
616
+ REDISTAIL_COLORS=true redistail stream debug-events
617
+ else
618
+ REDISTAIL_COLORS=false redistail stream production-events | logger
619
+ fi
620
+ ```
621
+
622
+ ### Scripting Examples
623
+
624
+ #### Bash Script for Multiple Channels
625
+
626
+ ```bash
627
+ #!/bin/bash
628
+ # monitor-all.sh
629
+
630
+ CHANNELS=("user-events" "system-logs" "error-alerts")
631
+ PIDS=()
632
+
633
+ # Start monitoring each channel
634
+ for channel in "${CHANNELS[@]}"; do
635
+ redistail pubsub "$channel" > "/var/log/redis-$channel.log" &
636
+ PIDS+=($!)
637
+ echo "Started monitoring $channel (PID: $!)"
638
+ done
639
+
640
+ # Wait for interrupt
641
+ trap 'kill "${PIDS[@]}"; exit' INT TERM
642
+
643
+ # Keep script running
644
+ wait
645
+ ```
646
+
647
+ #### Python Integration
648
+
649
+ ```python
650
+ #!/usr/bin/env python3
651
+ import subprocess
652
+ import json
653
+ import sys
654
+
655
+ def monitor_json_stream(stream_name):
656
+ """Monitor a Redis stream and parse JSON messages."""
657
+ cmd = ['redistail', 'stream', stream_name]
658
+ env = {'REDISTAIL_COLORS': 'false', 'REDISTAIL_TIMESTAMPS': 'false'}
659
+
660
+ with subprocess.Popen(cmd, stdout=subprocess.PIPE,
661
+ stderr=subprocess.PIPE, text=True, env=env) as proc:
662
+ try:
663
+ for line in proc.stdout:
664
+ try:
665
+ # Parse redistail output and extract JSON
666
+ parts = line.strip().split(' ', 2)
667
+ if len(parts) >= 3:
668
+ message_data = parts[2]
669
+ # Process the message
670
+ print(f"Processed: {message_data}")
671
+ except Exception as e:
672
+ print(f"Error processing line: {e}", file=sys.stderr)
673
+ except KeyboardInterrupt:
674
+ proc.terminate()
675
+
676
+ if __name__ == "__main__":
677
+ monitor_json_stream("api-events")
678
+ ```
679
+
680
+ This comprehensive documentation covers all aspects of using the redistail CLI tool, from basic usage to advanced integration scenarios, troubleshooting common issues, and providing practical examples for different use cases.
Binary file