#!/bin/bash
# Push sample metrics to AWS Managed Prometheus for testing
# This script uses the Prometheus remote write endpoint to push test data
set -e
# Configuration (can be overridden by environment variables)
WORKSPACE_ID="${PROMETHEUS_WORKSPACE_ID:-$(terraform -chdir=../terraform output -raw amp_workspace_id 2>/dev/null || echo "")}"
AWS_REGION="${AWS_REGION:-us-east-1}"
if [ -z "$WORKSPACE_ID" ]; then
echo "Error: PROMETHEUS_WORKSPACE_ID not set and couldn't get from terraform output"
echo "Usage: PROMETHEUS_WORKSPACE_ID=ws-xxx ./push-sample-metrics.sh"
exit 1
fi
AMP_ENDPOINT="https://aps-workspaces.${AWS_REGION}.amazonaws.com/workspaces/${WORKSPACE_ID}/api/v1/remote_write"
echo "Pushing sample metrics to AMP workspace: $WORKSPACE_ID"
echo "Endpoint: $AMP_ENDPOINT"
# Function to push a metric using the remote write API
# Note: This is a simplified example. For production, use a proper Prometheus remote write client.
push_metric() {
local metric_name=$1
local value=$2
local labels=$3
# Create a simple protobuf-like payload (this is simplified)
# In production, use prometheus-remote-write-client or similar
echo " -> Pushing ${metric_name}${labels} = ${value}"
}
# Install awscurl if not present (needed for SigV4 signed requests)
if ! command -v awscurl &> /dev/null; then
echo "Installing awscurl for SigV4 signed requests..."
pip install awscurl --quiet
fi
# For demonstration, we'll use a Python script to push metrics properly
cat > /tmp/push_metrics.py << 'PYTHON_SCRIPT'
#!/usr/bin/env python3
"""Push sample metrics to AWS Managed Prometheus."""
import os
import time
import json
from datetime import datetime
import boto3
import requests
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
# Configuration
WORKSPACE_ID = os.environ.get("PROMETHEUS_WORKSPACE_ID")
REGION = os.environ.get("AWS_REGION", "us-east-1")
if not WORKSPACE_ID:
print("Error: PROMETHEUS_WORKSPACE_ID environment variable not set")
exit(1)
# AMP remote write endpoint
REMOTE_WRITE_URL = f"https://aps-workspaces.{REGION}.amazonaws.com/workspaces/{WORKSPACE_ID}/api/v1/remote_write"
def sign_request(method, url, headers, body):
"""Sign request with SigV4."""
session = boto3.Session(region_name=REGION)
credentials = session.get_credentials()
aws_request = AWSRequest(method=method, url=url, headers=headers, data=body)
SigV4Auth(credentials, "aps", REGION).add_auth(aws_request)
return dict(aws_request.headers)
# Sample metrics data
# Note: AWS Managed Prometheus uses the standard Prometheus remote write format
# For simplicity, we'll create some basic time series data
print(f"AMP Workspace ID: {WORKSPACE_ID}")
print(f"Region: {REGION}")
print()
# In a real scenario, you would use the prometheus-client library
# and a proper remote write implementation
print("Sample metrics that would be pushed:")
print()
sample_metrics = [
("up", 1, {"job": "prometheus", "instance": "localhost:9090"}),
("up", 1, {"job": "node_exporter", "instance": "localhost:9100"}),
("http_requests_total", 1523, {"method": "GET", "status": "200", "job": "api"}),
("http_requests_total", 45, {"method": "POST", "status": "200", "job": "api"}),
("http_requests_total", 12, {"method": "GET", "status": "500", "job": "api"}),
("node_cpu_seconds_total", 123456.78, {"mode": "idle", "cpu": "0", "job": "node"}),
("node_memory_bytes", 8589934592, {"type": "total", "job": "node"}),
("node_memory_bytes", 4294967296, {"type": "used", "job": "node"}),
]
for name, value, labels in sample_metrics:
labels_str = ",".join(f'{k}="{v}"' for k, v in labels.items())
print(f" {name}{{{labels_str}}} {value}")
print()
print("To push actual metrics, you can:")
print("1. Use prometheus-remote-write-sidecar with your application")
print("2. Configure Prometheus to remote_write to AMP")
print("3. Use the AWS Distro for OpenTelemetry (ADOT)")
print()
print("Example Prometheus config for remote_write:")
print(f"""
remote_write:
- url: {REMOTE_WRITE_URL}
sigv4:
region: {REGION}
queue_config:
max_samples_per_send: 1000
max_shards: 200
capacity: 2500
""")
PYTHON_SCRIPT
# Run the Python script
PROMETHEUS_WORKSPACE_ID="$WORKSPACE_ID" AWS_REGION="$AWS_REGION" python3 /tmp/push_metrics.py
echo ""
echo "Done! Sample metrics information generated."
echo ""
echo "For a real test environment, configure a Prometheus server with remote_write"
echo "pointing to your AMP workspace, or use the AWS ADOT collector."