This repository contains scripts and configuration to enable OpenTelemetry telemetry collection for Claude Code.
-
Configure your environment variables:
cp .env.example .env # Edit .env with your specific configuration -
Run the installation script:
./install.sh
-
Restart Claude Code for the changes to take effect.
The following environment variables control Claude Code's telemetry behavior:
| Variable | Description | Default Value |
|---|---|---|
CLAUDE_CODE_ENABLE_TELEMETRY |
Enable/disable telemetry collection | 1 |
OTEL_EXPORTER_OTLP_ENDPOINT |
OpenTelemetry collector endpoint | http://localhost:4317 |
OTEL_EXPORTER_OTLP_PROTOCOL |
Protocol for OTLP export | grpc |
OTEL_LOGS_EXPORTER |
Logs exporter type | otlp |
OTEL_LOG_USER_PROMPTS |
Enable logging of user prompts | 1 |
OTEL_METRICS_EXPORTER |
Metrics exporter type | otlp |
OTEL_RESOURCE_ATTRIBUTES |
Resource attributes for telemetry | department=engineering_success |
OTEL_SERVICE_NAME |
Service name for telemetry | claude-code |
The installation script looks for configuration in the following order:
.envfile in the current directory (if exists).env.examplefile as fallback
To customize your configuration:
# Copy the example and modify as needed
cp .env.example .envThen edit .env with your preferred values:
# Example custom configuration
CLAUDE_CODE_ENABLE_TELEMETRY=1
OTEL_EXPORTER_OTLP_ENDPOINT=https://your-collector.example.com:4317
OTEL_RESOURCE_ATTRIBUTES="department=your_team,environment=production"
OTEL_SERVICE_NAME="claude-code-your-team"The install.sh script automatically:
- Loads environment variables from
.envor.env.example - Detects your operating system (Linux, macOS, Windows/WSL)
- Locates the correct Claude Code settings directory
- Creates or updates
managed-settings.jsonwith telemetry configuration - Preserves any existing environment variables
# Run with default configuration
./install.sh
# Get help and see all options
./install.sh --helpThe script supports:
- Linux: Uses
$XDG_CONFIG_HOME/claude-codeor$HOME/.config/claude-code - macOS: Uses
$HOME/Library/Application Support/claude-code - Windows: Uses
%APPDATA%/claude-code(via Git Bash/WSL)
For a complete observability stack with Prometheus and Grafana, you can use the provided Docker Compose setup:
# Start the observability stack
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the stack
docker-compose down- OpenTelemetry Collector (
localhost:4317,localhost:4318): Receives telemetry data from Claude Code - Prometheus (
localhost:9090): Stores metrics data - Grafana (
localhost:3000): Visualizes metrics and logs (admin/admin)
The Docker setup automatically configures the collector to:
- Receive OTLP data on ports 4317 (gRPC) and 4318 (HTTP)
- Export metrics to Prometheus
- Expose Prometheus metrics on port 8889
When using Docker, ensure your .env file points to the correct collector endpoint:
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
OTEL_EXPORTER_OTLP_PROTOCOL=grpc- jq: Required for JSON manipulation
- macOS:
brew install jq - Linux:
apt-get install jqoryum install jq - Windows: Available via Git Bash or WSL
- macOS:
- Docker (optional): For running the complete observability stack
When enabled, Claude Code will send telemetry data including:
- Metrics: Usage statistics, performance metrics
- Logs: Application logs (optionally including user prompts if
OTEL_LOG_USER_PROMPTS=1) - Traces: Request/response timing and flow
All telemetry data includes the configured resource attributes for categorization and filtering.
Script fails with "jq not found":
# Install jq
# macOS:
brew install jq
# Linux:
sudo apt-get install jq # Ubuntu/Debian
sudo yum install jq # RHEL/CentOSEnvironment variables not loading:
- Ensure
.envor.env.exampleexists in the current directory - Check file permissions:
chmod 644 .env - Verify file format (no spaces around
=)
Changes not taking effect:
- Restart Claude Code completely
- Verify the managed-settings.json was created:
- macOS:
~/Library/Application Support/claude-code/managed-settings.json - Linux:
~/.config/claude-code/managed-settings.json
- macOS:
Check if the configuration was applied correctly:
# macOS
cat ~/Library/Application\ Support/claude-code/managed-settings.json
# Linux
cat ~/.config/claude-code/managed-settings.jsonThe file should contain your environment variables in the env section.
After modifying the script:
-
Test on your platform:
./install.sh
-
Verify the generated
managed-settings.jsoncontains expected values -
Test with different
.envconfigurations
When making changes:
- Test on multiple platforms if possible
- Update this README if adding new features
- Follow the existing code style and error handling patterns
- The
.envfile may contain sensitive endpoints or credentials - Add
.envto.gitignoreto prevent accidental commits - Use
.env.examplefor documentation and defaults only