AWS VM Log Monitoring API
Scenario Description
Your team operates a critical service running on EC2 instances in AWS, but your current monitoring infrastructure lacks visibility into application-specific logs. The traditional approaches of installing agents or shipping logs aren't feasible due to security restrictions. You need a lightweight solution that can expose application logs through a secure API to integrate with your existing monitoring stack.
Problem Statement
Application logs are stored locally on EC2 instances
Security policies restrict installing third-party agents
Need real-time access to logs for monitoring and alerting
Solution must be lightweight and secure
Must integrate with existing monitoring tools (Prometheus, Grafana, etc.)
Solution: Log Exposition API in Golang
We'll create a lightweight HTTP API server in Golang that:
Reads application logs from configurable local paths
Exposes the logs via secure HTTP endpoints
Provides filtering capabilities
Includes authentication
Offers metrics collection points for Prometheus
Implementation
Complete Golang API Code
Here's the complete implementation of our log exposition API:
Configuration File Example
Deployment Guide
Prerequisites
Go 1.18 or higher
AWS EC2 instance with your application running
Access to install and run services on the EC2 instance
Building the API
Create a project directory on your development machine:
Initialize the Go module:
Create the main.go file with the code provided above
Install dependencies:
Build the binary:
Deploying to AWS EC2
Create a configuration directory and file on the EC2 instance:
Copy and modify the example configuration file provided above to match your application's log paths.
Copy the compiled binary to the EC2 instance:
Set up the service on the EC2 instance:
Create a systemd service file:
Start and enable the service:
Verify the service is running:
Security Configuration
To secure the API:
Configure a secure API token in the config.json file
Set up an AWS security group to only allow traffic from your monitoring systems
Consider setting up an HTTPS proxy with Nginx or similar if needed
Integration with Monitoring Systems
Prometheus Integration
Add this configuration to your Prometheus scrape configs:
Grafana Dashboard
Create a dashboard to visualize the metrics:
Add a Prometheus data source in Grafana
Create panels for metrics like:
logapi_logs_read_total(by file, by level)logapi_error_logs_total(by keyword)logapi_requests_total(by endpoint, status)
API Usage Examples
To fetch logs from your monitoring system:
Troubleshooting
Common Issues
API returns "Unauthorized":
Verify the API token in your request matches the one in config.json
No logs appearing:
Check that the log paths in config.json are correct
Verify the service has permission to read those log files
Service won't start:
Check logs with
sudo journalctl -u logapiVerify the logapi binary has execution permissions
High CPU usage:
Increase the polling interval in the monitorLogs function
Consider reducing the number of monitored log files
Future Enhancements
Add support for TLS/HTTPS
Implement log rotation handling
Add support for structured log formats (JSON, etc.)
Implement alerting capabilities directly from the API
Add support for distributed log collection across multiple instances
Last updated