CodeBlanks Logo Icon CodeBlanks

Challenge - Advanced project

Set up a project combining the production app.

Challenge: the advanced project

In the last challenge you'll complete the observability of the shop in production: the API's metrics will be collected by Prometheus, an alert will go out when the error rate rises, and the HPA will work correctly under load.

ServiceMonitor  -> api:http/metrics (30 s)
PrometheusRule  -> error rate > 5%, 10 min -> severity: critical
HPA             -> cpu 70%, 2-10 replicas
load test       -> watch with hpa -w

Each part is a separate YAML or command; together they are the "eyes and reflexes" of a production service.

Practice The ServiceMonitor

Write a ServiceMonitor YAML: monitoring.coreos.com/v1; name api, namespace magaza, label release: izleme; selector.matchLabels app: api; a single endpoint: port: http, path: /metrics, interval: 30s.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: api
  namespace: magaza
  labels:
    release: izleme
spec:
  selector:
    matchLabels:
      app: api
  endpoints:
    - port: http
      path: /metrics
      interval: 30s

Expected output

servicemonitor.monitoring.coreos.com/api created

Practice The alert rule

Write a PrometheusRule's spec section (starting with spec:): name: api under groups; a single rule: alert: YuksekHataOrani, expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05, for: 10m, labels.severity: critical.

Starter code

terminal
# buraya yaz

Sample solution

terminal
spec:
  groups:
    - name: api
      rules:
        - alert: YuksekHataOrani
          expr: sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) > 0.05
          for: 10m
          labels:
            severity: critical

Expected output

prometheusrule.monitoring.coreos.com/api-uyarilari created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if for: 0m is chosen instead of for: 10m for the error-rate alert?

  • The alert never goes out
  • Prometheus crashes
  • The HPA turns off
  • Even a single short spike sends an alert; alert fatigue increases Correct answer

Explanation: A waiting period is given to separate lasting problems from brief noise.

2 Write the metric scrape interval.

Question
  endpoints:
    - port: http
      path: /metrics
      ____: 30s
Answer
  endpoints:
    - port: http
      path: /metrics
      interval: 30s

Explanation: interval is the scrape interval.

3 Match.

Term Meaning
ServiceMonitor Metric collection
PrometheusRule Alerting
HPA Autoscaling
hpa -w Observation

All lessons in Advanced