Sign inSign up

calltelemetry/traceroute

By calltelemetry

โ€ขUpdated 3 months ago

Image
0

10K+

calltelemetry/traceroute repository overview

โ Traceroute Service

A high-performance, distributed traceroute service built with Elixir/OTP that provides both HTTP REST API and NATS message-based network path tracing capabilities.

โ ๐Ÿš€ Features

  • HTTP REST API - Simple HTTP POST endpoint for traceroute requests
  • NATS Integration - Event-driven traceroute processing via NATS messaging
  • Fault Tolerant - Built on Elixir/OTP supervision trees for reliability
  • JSON Responses - Structured JSON output with detailed path information
  • Docker Ready - Containerized deployment with health checks
  • Comprehensive Testing - 89% test coverage with property-based testing
  • Performance Optimized - Concurrent request handling with timeout management

โ ๐Ÿ“‹ Requirements

  • Elixir: 1.18.4+
  • Erlang/OTP: 26.2.5.2+
  • NATS Server: For message-based processing
  • Elevated Privileges: Required for ICMP traceroute operations

โ ๐Ÿ›  Installation

โ Local Development
# Clone the repository
git clone <repository-url>
cd traceroute

# Install dependencies
mix deps.get

# Set up environment
source local-test.env  # if available

# Run tests
mix test

# Start the service
mix run --no-halt
โ Docker Deployment
# Build the image
docker build -t traceroute:latest .

# Run with CAP_NET_RAW capability for ICMP (recommended - more secure)
docker run --cap-add=NET_RAW -p 4100:4100 traceroute:latest

# Alternative: Run with full privileged access (less secure)
docker run --privileged -p 4100:4100 traceroute:latest

Security Note: The image now runs as a non-root user (appuser:1001) with only the CAP_NET_RAW capability required for ICMP operations. This follows security best practices while maintaining full traceroute functionality.

โ ๐Ÿ”ง Configuration

โ Environment Variables
  • NATS_HOST - NATS server host (default: "nats")
  • NATS_PORT - NATS server port (default: 4222)
  • HTTP_PORT - HTTP service port (default: 4100)
  • MIX_ENV - Environment (dev/test/prod)
โ NATS Configuration

The service connects to NATS for distributed message processing:

# Configured in lib/tracer.ex
%{
  host: System.get_env("NATS_HOST", "nats"),
  port: String.to_integer(System.get_env("NATS_PORT", "4222"))
}

โ ๐Ÿ“ก API Usage

โ HTTP REST API

Endpoint: POST /traceroute

Request:

curl -X POST http://localhost:4100/traceroute \
  -H "Content-Type: application/json" \
  -d '{"host": "google.com"}'

Response:

{
  "status": 200,
  "body": {
    "result": "ok",
    "message": "Traceroute successful to google.com",
    "next_hop_ip": "172.217.14.46",
    "next_hop_hostname": "lga25s78-in-f14.1e100.net",
    "next_hop_fqdn": "lga25s78-in-f14.1e100.net"
  }
}
โ NATS Message API

Topic: traceroute.request

Message Format:

{
  "body": "google.com",
  "reply_to": "response.topic"
}

Response: JSON structure similar to HTTP API response

โ ๐Ÿ— Architecture

โ Core Components
  • Tracer - Main application supervisor
  • Tracer.Worker - Traceroute execution logic
  • Tracer.Web.Handler - HTTP request handler
  • NATS.Listener - NATS message processor
  • Tracer.Web.Supervisor - Web server supervision
โ Data Flow
  1. HTTP Request โ†’ Tracer.Web.Handler โ†’ Tracer.Worker โ†’ Response
  2. NATS Message โ†’ NATS.Listener โ†’ Tracer.Worker โ†’ Reply
โ Key Functions
โ Tracer.Worker
  • trace/1 - Main traceroute execution
  • clean_timeouts/1 - Filter timeout responses
  • get_last_hop/1 - Extract meaningful hop information
  • process_results/2 - Format response data

โ ๐Ÿงช Testing

โ Running Tests
# Run all tests
mix test

# Run with coverage
mix test --cover

# Run specific test files
mix test test/trace/worker_test.exs
mix test test/trace/handler_test.exs

# Run excluding privileged tests
mix test --exclude integration
โ Test Coverage

Current coverage: 89.0%

ModuleCoverageLinesMissed
lib/tracer.ex100.0%50
lib/trace/supervisor.ex100.0%30
lib/trace/listener.ex100.0%50
lib/trace/worker.ex92.8%282
lib/nats_listener.ex90.9%111
lib/trace/handler.ex88.8%91
โ Test Structure
  • Unit Tests - test/trace/worker_test.exs, test/trace/handler_test.exs
  • Integration Tests - test/integration_test.exs
  • NATS Tests - test/nats_listener_test.exs
  • Supervisor Tests - test/trace/supervisor_test.exs
  • Property-based Tests - StreamData generators (commented due to complexity)
โ Test Dependencies
# Test-only dependencies in mix.exs
{:mox, "~> 1.1", only: :test},           # Mocking
{:stream_data, "~> 1.0", only: :test},   # Property-based testing
{:excoveralls, "~> 0.18", only: :test},  # Coverage reporting
{:ex_machina, "~> 2.8", only: :test}     # Test factories

โ ๐Ÿณ Docker

โ Dockerfile
FROM elixir:1.18.4 AS builder
# Multi-stage build for optimized production image
# Includes libssl for cryptographic operations
โ DevSpace Configuration
# devspace.yaml for Kubernetes development
deployments:
  - name: traceroute
    helm:
      chart:
        name: ./charts/traceroute

โ ๐Ÿ” Monitoring & Debugging

โ Logging

The service provides structured logging:

  • Info: Successful traceroute operations
  • Error: Failed operations, privilege issues
  • Debug: NATS connection status
โ Health Checks
  • HTTP endpoint responds to basic connectivity
  • NATS connection monitoring
  • Process supervision with automatic restarts

โ ๐Ÿšจ Production Considerations

โ Security
  • Privileged Access: Required for ICMP operations
  • Input Validation: All host inputs are validated
  • Error Handling: Graceful degradation on permission errors
โ Performance
  • Concurrent Requests: Multiple traceroute operations can run simultaneously
  • Timeout Handling: Configurable timeouts prevent hanging operations
  • Memory Management: Automatic garbage collection and process cleanup
โ Deployment
# Production build
MIX_ENV=prod mix release

# Docker production
docker build --build-arg BUILD_ENV=prod -t traceroute:prod .

# Kubernetes deployment
kubectl apply -f k8s/

โ ๐Ÿค Contributing

โ Development Setup
  1. Install required versions (see .tool-versions)
  2. Run mix setup to install dependencies
  3. Ensure NATS server is running locally
  4. Run tests to verify setup: mix test
โ Code Quality
# Format code
mix format

# Run static analysis
mix credo

# Type checking (if configured)
mix dialyzer

# Full quality suite
mix quality
โ Adding Tests
  • Unit tests for pure functions
  • Integration tests for API endpoints
  • Mock external dependencies using Mox
  • Use property-based testing for edge cases

โ ๐Ÿ“š Additional Resources

โ ๐Ÿ“„ License

See LICENSE file for details.

โ ๐Ÿ†˜ Support

For issues and support:

  1. Check existing tests for usage examples
  2. Review logs for error details
  3. Ensure proper privileges for ICMP operations
  4. Verify NATS connectivity

Built with โค๏ธ using Elixir/OTP for reliable, concurrent network diagnostics.

Tag summary

Content type

Image

Digest

sha256:010451947โ€ฆ

Size

33.2 MB

Last updated

3 months ago

docker pull calltelemetry/traceroute