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:

  1. Reads application logs from configurable local paths

  2. Exposes the logs via secure HTTP endpoints

  3. Provides filtering capabilities

  4. Includes authentication

  5. 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

  1. Create a project directory on your development machine:

  1. Initialize the Go module:

  1. Create the main.go file with the code provided above

  2. Install dependencies:

  1. Build the binary:

Deploying to AWS EC2

  1. Create a configuration directory and file on the EC2 instance:

  1. Copy and modify the example configuration file provided above to match your application's log paths.

  2. Copy the compiled binary to the EC2 instance:

  1. Set up the service on the EC2 instance:

  1. Create a systemd service file:

  1. Start and enable the service:

  1. Verify the service is running:

Security Configuration

To secure the API:

  1. Configure a secure API token in the config.json file

  2. Set up an AWS security group to only allow traffic from your monitoring systems

  3. 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:

  1. Add a Prometheus data source in Grafana

  2. 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

  1. API returns "Unauthorized":

    • Verify the API token in your request matches the one in config.json

  2. No logs appearing:

    • Check that the log paths in config.json are correct

    • Verify the service has permission to read those log files

  3. Service won't start:

    • Check logs with sudo journalctl -u logapi

    • Verify the logapi binary has execution permissions

  4. High CPU usage:

    • Increase the polling interval in the monitorLogs function

    • Consider reducing the number of monitored log files

Future Enhancements

  1. Add support for TLS/HTTPS

  2. Implement log rotation handling

  3. Add support for structured log formats (JSON, etc.)

  4. Implement alerting capabilities directly from the API

  5. Add support for distributed log collection across multiple instances

Last updated