@@ -404,53 +404,132 @@ Apply this step to create alert-rule trait instances for:
404404 - Trigger: memory usage of the cart component exceeds 70% for 2 minutes
405405 - Trait instance name: ` cartservice-high-memory-alert `
406406
407- Attach the alert-rule traits to the existing components by appending to ` spec.traits ` :
407+ Attach the alert-rule traits to existing components by appending them to spec.traits if the field already exists,
408+ or creating the array if it does not:
408409
409410``` bash
410411# frontend component: log-based alert
411- kubectl patch component frontend -n default --type=' json' -p=' [
412- {"op":"add","path":"/spec/traits/-","value":{
413- "name":"observability-alert-rule",
414- "kind":"ClusterTrait",
415- "instanceName":"frontend-rpc-unavailable-error-log-alert",
416- "parameters":{
417- "description":"Alert when frontend logs indicate rpc error: code = Unavailable",
418- "severity":"critical",
419- "source":{"type":"log","query":"rpc error: code = Unavailable"},
420- "condition":{"window":"5m","interval":"1m","operator":"gt","threshold":5}
412+ if kubectl get component frontend -n default \
413+ -o jsonpath=' {.spec.traits[*].instanceName}' 2> /dev/null \
414+ | tr ' ' ' \n' | grep -qx " frontend-rpc-unavailable-error-log-alert" ; then
415+ echo " Trait 'frontend-rpc-unavailable-error-log-alert' already exists on component 'frontend', skipping."
416+ else
417+ kubectl patch component frontend -n default --type=json -p=' [
418+ {
419+ "op": "add",
420+ "path": "/spec/traits/-",
421+ "value": {
422+ "name": "observability-alert-rule",
423+ "kind": "ClusterTrait",
424+ "instanceName": "frontend-rpc-unavailable-error-log-alert",
425+ "parameters": {
426+ "description": "Alert when frontend logs indicate rpc error: code = Unavailable",
427+ "severity": "critical",
428+ "source": { "type": "log", "query": "rpc error: code = Unavailable" },
429+ "condition": { "window": "5m", "interval": "1m", "operator": "gt", "threshold": 5 }
430+ }
431+ }
432+ }
433+ ]' 2> /dev/null || kubectl patch component frontend -n default --type=json -p=' [
434+ {
435+ "op": "add",
436+ "path": "/spec/traits",
437+ "value": [{
438+ "name": "observability-alert-rule",
439+ "kind": "ClusterTrait",
440+ "instanceName": "frontend-rpc-unavailable-error-log-alert",
441+ "parameters": {
442+ "description": "Alert when frontend logs indicate rpc error: code = Unavailable",
443+ "severity": "critical",
444+ "source": { "type": "log", "query": "rpc error: code = Unavailable" },
445+ "condition": { "window": "5m", "interval": "1m", "operator": "gt", "threshold": 5 }
446+ }
447+ }]
448+ }
449+ ]'
450+ fi
451+
452+ # recommendation component: metric-based alert (cpu_usage)
453+ if kubectl get component recommendation -n default \
454+ -o jsonpath=' {.spec.traits[*].instanceName}' 2> /dev/null \
455+ | tr ' ' ' \n' | grep -qx " recommendation-high-cpu-alert" ; then
456+ echo " Trait 'recommendation-high-cpu-alert' already exists on component 'recommendation', skipping."
457+ else
458+ kubectl patch component recommendation -n default --type=json -p=' [
459+ {
460+ "op": "add",
461+ "path": "/spec/traits/-",
462+ "value": {
463+ "name": "observability-alert-rule",
464+ "kind": "ClusterTrait",
465+ "instanceName": "recommendation-high-cpu-alert",
466+ "parameters": {
467+ "description": "Alert when recommendationservice CPU usage is greater than 80% for last 5 minutes",
468+ "severity": "critical",
469+ "source": { "type": "metric", "metric": "cpu_usage" },
470+ "condition": { "window": "5m", "interval": "1m", "operator": "gt", "threshold": 80 }
471+ }
472+ }
473+ }
474+ ]' 2> /dev/null || kubectl patch component recommendation -n default --type=json -p=' [
475+ {
476+ "op": "add",
477+ "path": "/spec/traits",
478+ "value": [{
479+ "name": "observability-alert-rule",
480+ "kind": "ClusterTrait",
481+ "instanceName": "recommendation-high-cpu-alert",
482+ "parameters": {
483+ "description": "Alert when recommendationservice CPU usage is greater than 80% for last 5 minutes",
484+ "severity": "critical",
485+ "source": { "type": "metric", "metric": "cpu_usage" },
486+ "condition": { "window": "5m", "interval": "1m", "operator": "gt", "threshold": 80 }
487+ }
488+ }]
421489 }
422- }}
423- ]'
424-
425- # recommendation: metric-based alert (cpu_usage)
426- kubectl patch component recommendation -n default --type=' json' -p=' [
427- {"op":"add","path":"/spec/traits/-","value":{
428- "name":"observability-alert-rule",
429- "kind":"ClusterTrait",
430- "instanceName":"recommendation-high-cpu-alert",
431- "parameters":{
432- "description":"Alert when recommendationservice CPU usage is greater than 80% for last 5 minutes",
433- "severity":"critical",
434- "source":{"type":"metric","metric":"cpu_usage"},
435- "condition":{"window":"5m","interval":"1m","operator":"gt","threshold":80}
490+ ]'
491+ fi
492+
493+ # cart component: metric-based alert (memory_usage)
494+ if kubectl get component cart -n default \
495+ -o jsonpath=' {.spec.traits[*].instanceName}' 2> /dev/null \
496+ | tr ' ' ' \n' | grep -qx " cartservice-high-memory-alert" ; then
497+ echo " Trait 'cartservice-high-memory-alert' already exists on component 'cart', skipping."
498+ else
499+ kubectl patch component cart -n default --type=json -p=' [
500+ {
501+ "op": "add",
502+ "path": "/spec/traits/-",
503+ "value": {
504+ "name": "observability-alert-rule",
505+ "kind": "ClusterTrait",
506+ "instanceName": "cartservice-high-memory-alert",
507+ "parameters": {
508+ "description": "Alert when cartservice memory usage is greater than 70% for last 2 minutes",
509+ "severity": "critical",
510+ "source": { "type": "metric", "metric": "memory_usage" },
511+ "condition": { "window": "2m", "interval": "1m", "operator": "gt", "threshold": 70 }
512+ }
513+ }
436514 }
437- }}
438- ]'
439-
440- # cart: metric-based alert (memory_usage)
441- kubectl patch component cart -n default --type=' json' -p=' [
442- {"op":"add","path":"/spec/traits/-","value":{
443- "name":"observability-alert-rule",
444- "kind":"ClusterTrait",
445- "instanceName":"cartservice-high-memory-alert",
446- "parameters":{
447- "description":"Alert when cartservice memory usage is greater than 70% for last 2 minutes",
448- "severity":"critical",
449- "source":{"type":"metric","metric":"memory_usage"},
450- "condition":{"window":"2m","interval":"1m","operator":"gt","threshold":70}
515+ ]' 2> /dev/null || kubectl patch component cart -n default --type=json -p=' [
516+ {
517+ "op": "add",
518+ "path": "/spec/traits",
519+ "value": [{
520+ "name": "observability-alert-rule",
521+ "kind": "ClusterTrait",
522+ "instanceName": "cartservice-high-memory-alert",
523+ "parameters": {
524+ "description": "Alert when cartservice memory usage is greater than 70% for last 2 minutes",
525+ "severity": "critical",
526+ "source": { "type": "metric", "metric": "memory_usage" },
527+ "condition": { "window": "2m", "interval": "1m", "operator": "gt", "threshold": 70 }
528+ }
529+ }]
451530 }
452- }}
453- ] '
531+ ] '
532+ fi
454533```
455534
456535#### Notification Channels Are Configured Per Environment
0 commit comments