# Redis

> Monitor Redis performance, memory usage, and key metrics with OpenTelemetry and Last9

Source: https://last9.io/docs/integrations/databases/redis/

Use OpenTelemetry to monitor Redis performance and send telemetry data to Last9. This integration provides comprehensive monitoring of Redis server metrics, memory usage, command statistics, and log analysis.

## Prerequisites

Before setting up Redis monitoring, ensure you have:

- Redis server installed and running
- Administrative access to your Redis server
- OpenTelemetry Collector installation access
- Last9 account with integration credentials

1.  **Verify Redis Installation**

    Ensure Redis server is installed and running on your system:

    ```bash
    sudo apt update
    sudo apt install redis-server
    sudo systemctl status redis-server
    ```

2.  **Install OpenTelemetry Collector**

    Choose the appropriate package for your operating system. Note that systemd is required for automatic service configuration.

**DEB Package**

    For Debian/Ubuntu systems:

    ```bash
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.deb
    sudo dpkg -i otelcol-contrib_0.118.0_linux_amd64.deb
    ```

**RPM Package**

    For Red Hat/CentOS systems:

    ```bash
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.118.0/otelcol-contrib_0.118.0_linux_amd64.rpm
    sudo rpm -ivh otelcol-contrib_0.118.0_linux_amd64.rpm
    ```

    More installation options can be found in the [official OpenTelemetry documentation](https://opentelemetry.io/docs/collector/installation/#linux).

3.  **Configure OpenTelemetry Collector**

    Create the collector configuration file:

    ```bash
    sudo nano /etc/otelcol-contrib/config.yaml
    ```

    Add the following configuration to monitor Redis metrics, logs, and system resources:

    ```yaml
    receivers:
      filelog:
        # File path pattern to read logs from. Update this to the destination from where you want to read Redis logs.
        include: [/var/log/redis/*.log]
        include_file_path: true
        retry_on_failure:
          enabled: true
      hostmetrics:
        collection_interval: 60s
        scrapers:
          cpu:
            metrics:
              system.cpu.logical.count:
                enabled: true
          memory:
            metrics:
              system.memory.utilization:
                enabled: true
              system.memory.limit:
                enabled: true
          load:
          disk:
          filesystem:
            metrics:
              system.filesystem.utilization:
                enabled: true
          network:
          paging:
      redis:
        endpoint: "localhost:6379"
        collection_interval: 60s

    processors:
      batch:
        timeout: 20s
        send_batch_size: 10000
        send_batch_max_size: 10000
      resourcedetection/cloud:
        detectors: ["aws", "gcp", "azure"]
      resourcedetection/system:
        detectors: ["system"]
        system:
          hostname_sources: ["os"]
      transform/logs:
        flatten_data: true
        log_statements:
          - context: log
            statements:
              - set(observed_time, Now())
              - set(time_unix_nano, observed_time_unix_nano) where time_unix_nano == 0
              - set(resource.attributes["service.name"], "redis") # Change this to appropriate service name
              - set(resource.attributes["deployment.environment"], "staging") # Set to environment

    exporters:
      otlp/last9:
        endpoint: "{{ .Logs.WriteURL }}"
        headers:
          "Authorization": "{{ .Logs.AuthValue }}"
      debug:
        verbosity: detailed

    service:
      pipelines:
        logs:
          receivers: [filelog]
          processors: [batch, resourcedetection/system, transform/logs]
          exporters: [otlp/last9]
        metrics:
          receivers: [redis, hostmetrics]
          processors: [batch, resourcedetection/system, resourcedetection/cloud]
          exporters: [otlp/last9]
    ```

    Replace the placeholder values in the `exporters` section with your actual Last9 credentials from the Last9 Integrations page.

    Update the following settings as needed:

    - Redis endpoint (default: `localhost:6379`)
    - Service name and environment in the transform processor
    - Log file paths based on your Redis configuration

4.  **Configure OpenTelemetry Collector Service**

    Create a systemd service file for the collector:

    ```bash
    sudo nano /etc/systemd/system/otelcol-contrib.service
    ```

    Add the following service configuration:

    ```bash
    [Unit]
    Description=OpenTelemetry Collector Contrib with custom flags
    After=network.target

    [Service]
    ExecStart=/usr/bin/otelcol-contrib --config /etc/otelcol-contrib/config.yaml --feature-gates transform.flatten.logs
    Restart=always
    User=root
    Group=root

    [Install]
    WantedBy=multi-user.target
    ```

5.  **Start and Enable the Service**

    Reload systemd configuration and start the collector service:

    ```bash
    sudo systemctl daemon-reload
    sudo systemctl enable otelcol-contrib
    sudo systemctl start otelcol-contrib
    ```

## Understanding the Setup

### Redis Receiver

The Redis receiver connects directly to your Redis server and collects comprehensive metrics including:

- **Memory Metrics**: Used memory, peak memory, memory fragmentation ratio
- **Performance Metrics**: Commands processed, keyspace hits/misses, evicted keys
- **Connection Metrics**: Connected clients, blocked clients, total connections
- **Persistence Metrics**: RDB and AOF statistics
- **Replication Metrics**: Master/slave sync status and lag

### Host Metrics Collection

The collector also gathers system-level metrics to provide context for Redis performance:

- CPU utilization and system load
- Memory usage and availability
- Disk I/O and filesystem utilization
- Network traffic statistics

### Log Collection

The filelog receiver monitors Redis log files for:

- Connection and disconnection events
- Configuration changes
- Error messages and warnings
- Slow query logs and performance issues

## Verification and Monitoring

1. **Check Service Status**

   Verify the OpenTelemetry Collector service is running:

   ```bash
   sudo systemctl status otelcol-contrib
   ```

2. **Monitor Service Logs**

   Check for any configuration errors or connection issues:

   ```bash
   sudo journalctl -u otelcol-contrib -f
   ```

3. **Verify Redis Connection**

   Test that the collector can connect to your Redis instance:

   ```bash
   # Test Redis connectivity
   redis-cli ping

   # Check Redis info
   redis-cli info
   ```

4. **Check Metrics Collection**

   Look for successful metric collection in the collector logs. You should see log entries indicating successful data export to Last9.

5. **Verify Data in Last9**

   Log into your Last9 account and check that Redis metrics are being received in [Grafana](https://app.last9.io/grafana).

## Key Redis Metrics to Monitor

The integration automatically collects important Redis metrics including:

### Memory Metrics

- `redis.memory.used`: Current memory usage
- `redis.memory.peak`: Peak memory usage
- `redis.memory.rss`: Resident set size
- `redis.memory.fragmentation.ratio`: Memory fragmentation ratio

### Performance Metrics

- `redis.commands.processed`: Total commands processed
- `redis.keyspace.hits`: Number of successful key lookups
- `redis.keyspace.misses`: Number of failed key lookups
- `redis.keys.evicted`: Number of evicted keys

### Connection Metrics

- `redis.clients.connected`: Currently connected clients
- `redis.clients.blocked`: Clients blocked on operations
- `redis.connections.received`: Total connections received

## Configuration Tips

### Redis Authentication

If your Redis instance requires authentication, update the receiver configuration:

```yaml
redis:
  endpoint: "localhost:6379"
  password: "your_redis_password"
  collection_interval: 60s
```

### Custom Log Paths

Update the log file paths based on your Redis configuration:

```yaml
filelog:
  include: [/path/to/your/redis/logs/*.log]
  include_file_path: true
```

### Resource Attribution

Customize resource attributes for better organization:

```yaml
transform/logs:
  flatten_data: true
  log_statements:
    - context: log
      statements:
        - set(resource.attributes["service.name"], "redis-primary")
        - set(resource.attributes["deployment.environment"], "production")
        - set(resource.attributes["redis.instance"], "cache-cluster-1")
```

## Troubleshooting

### Service Issues

If the collector service fails to start:

```bash
# Check service status
sudo systemctl status otelcol-contrib

# View detailed logs
sudo journalctl -u otelcol-contrib --no-pager

# Validate configuration
sudo /usr/bin/otelcol-contrib --config /etc/otelcol-contrib/config.yaml --dry-run
```

### Redis Connection Issues

For Redis connection problems:

```bash
# Test Redis connectivity
redis-cli -h localhost -p 6379 ping

# Check Redis logs
sudo tail -f /var/log/redis/redis-server.log

# Verify Redis configuration
redis-cli config get '*'
```

### Performance Considerations

- Adjust `collection_interval` based on your monitoring needs
- Monitor the collector's resource usage with high-traffic Redis instances
- Consider using Redis replica instances for monitoring to reduce load on primary

## Need Help?

If you encounter any issues or have questions:

- Join our [Discord community](https://discord.com/channels/652153247672729619/652153247672729621) for real-time support
- Contact our support team at [support@last9.io](mailto:support@last9.io)
