|
| 1 | +# Prometheus Metrics Setup Guide |
| 2 | + |
| 3 | +The items-service exposes Prometheus metrics at the `/metrics` endpoint using OpenTelemetry's Prometheus exporter. |
| 4 | + |
| 5 | +## Available Metrics |
| 6 | + |
| 7 | +### HTTP Metrics |
| 8 | +- `http_server_duration` - Request duration histogram (milliseconds) |
| 9 | +- `http_server_requests_total` - Total request count |
| 10 | + |
| 11 | +### Custom Metrics |
| 12 | +You can add custom metrics in `apps/items-service/src/metrics.ts` using the MeterProvider. |
| 13 | + |
| 14 | + |
| 15 | +## Testing Metrics |
| 16 | + |
| 17 | +### Generate Traffic |
| 18 | + |
| 19 | +```bash |
| 20 | +for i in {1..20}; do |
| 21 | + curl -s http://localhost:8081/v1/health > /dev/null |
| 22 | + curl -s http://localhost:8081/v1/items > /dev/null |
| 23 | + sleep 0.5 |
| 24 | +done |
| 25 | +``` |
| 26 | + |
| 27 | +### View Metrics |
| 28 | + |
| 29 | +**Local Development:** |
| 30 | +```bash |
| 31 | +curl http://localhost:8081/metrics | grep http_server |
| 32 | +``` |
| 33 | + |
| 34 | +**Production:** |
| 35 | +```bash |
| 36 | +curl https://app.roussev.com/items/metrics | grep http_server |
| 37 | +``` |
| 38 | + |
| 39 | +**Browser:** |
| 40 | +- Local: http://localhost:8081/metrics |
| 41 | +- Production: https://app.roussev.com/items/metrics |
| 42 | + |
| 43 | +## 📊 Setting Up Prometheus Server (Optional) |
| 44 | + |
| 45 | +To collect and store these metrics over time, deploy a Prometheus server to scrape the `/metrics` endpoint. |
| 46 | + |
| 47 | +### Option 1: Deploy Prometheus to Kubernetes |
| 48 | + |
| 49 | +Create `infra/k8s/observability/prometheus-deployment.yaml`: |
| 50 | + |
| 51 | +```yaml |
| 52 | +apiVersion: v1 |
| 53 | +kind: ConfigMap |
| 54 | +metadata: |
| 55 | + name: prometheus-config |
| 56 | + namespace: default |
| 57 | +data: |
| 58 | + prometheus.yml: | |
| 59 | + global: |
| 60 | + scrape_interval: 15s |
| 61 | + scrape_configs: |
| 62 | + - job_name: 'items-service' |
| 63 | + kubernetes_sd_configs: |
| 64 | + - role: pod |
| 65 | + namespaces: |
| 66 | + names: |
| 67 | + - default |
| 68 | + relabel_configs: |
| 69 | + - source_labels: [__meta_kubernetes_pod_label_app] |
| 70 | + action: keep |
| 71 | + regex: items-service |
| 72 | + - source_labels: [__meta_kubernetes_pod_ip] |
| 73 | + action: replace |
| 74 | + target_label: __address__ |
| 75 | + replacement: $1:8080 |
| 76 | +--- |
| 77 | +apiVersion: apps/v1 |
| 78 | +kind: Deployment |
| 79 | +metadata: |
| 80 | + name: prometheus |
| 81 | + namespace: default |
| 82 | +spec: |
| 83 | + replicas: 1 |
| 84 | + selector: |
| 85 | + matchLabels: |
| 86 | + app: prometheus |
| 87 | + template: |
| 88 | + metadata: |
| 89 | + labels: |
| 90 | + app: prometheus |
| 91 | + spec: |
| 92 | + containers: |
| 93 | + - name: prometheus |
| 94 | + image: prom/prometheus:latest |
| 95 | + ports: |
| 96 | + - containerPort: 9090 |
| 97 | + volumeMounts: |
| 98 | + - name: config |
| 99 | + mountPath: /etc/prometheus |
| 100 | + volumes: |
| 101 | + - name: config |
| 102 | + configMap: |
| 103 | + name: prometheus-config |
| 104 | +--- |
| 105 | +apiVersion: v1 |
| 106 | +kind: Service |
| 107 | +metadata: |
| 108 | + name: prometheus |
| 109 | + namespace: default |
| 110 | +spec: |
| 111 | + type: ClusterIP |
| 112 | + ports: |
| 113 | + - port: 9090 |
| 114 | + targetPort: 9090 |
| 115 | + selector: |
| 116 | + app: prometheus |
| 117 | +``` |
| 118 | +
|
| 119 | +Deploy: |
| 120 | +```bash |
| 121 | +kubectl --kubeconfig=./infra/terraform/kubeconfig.yaml apply -f infra/k8s/observability/prometheus-deployment.yaml |
| 122 | +``` |
| 123 | + |
| 124 | +### Option 2: Use Existing Prometheus |
| 125 | + |
| 126 | +If you already have Prometheus running, add a scrape config: |
| 127 | + |
| 128 | +```yaml |
| 129 | +scrape_configs: |
| 130 | + - job_name: 'items-service' |
| 131 | + static_configs: |
| 132 | + - targets: ['items-service.default.svc.cluster.local:80'] |
| 133 | + metrics_path: '/metrics' |
| 134 | + scrape_interval: 15s |
| 135 | +``` |
0 commit comments