From 673b7ac429c3eaf94dd9f59cd5009d202bdecb88 Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Wed, 23 Oct 2024 22:32:16 -0400 Subject: [PATCH 1/2] Adds more scripts for post-setup --- scripts/rca-demo/README.md | 13 ---- .../rca-demo/data/mapping-message-alias.json | 11 ++++ scripts/rca-demo/data/rule-01.json | 52 ++++++++++++++++ scripts/rca-demo/post-setup | 59 +++++++++++++++++++ scripts/rca-demo/setup | 34 +---------- scripts/rca-demo/shared.sh | 33 +++++++++++ 6 files changed, 157 insertions(+), 45 deletions(-) create mode 100644 scripts/rca-demo/data/mapping-message-alias.json create mode 100644 scripts/rca-demo/data/rule-01.json create mode 100755 scripts/rca-demo/post-setup create mode 100644 scripts/rca-demo/shared.sh diff --git a/scripts/rca-demo/README.md b/scripts/rca-demo/README.md index aaacd64142..8cc3dc0335 100644 --- a/scripts/rca-demo/README.md +++ b/scripts/rca-demo/README.md @@ -2,19 +2,6 @@ Docs for running scripts in this folder. -### Prerequisites - -On a mac you can easily install these prerequisites with: `brew install minikube helm kubernetes-cli` - -- Create a Kubernetes cluster. For local development `minikube` is recommended. There are no specific requirements, so you can create a local one, or use a managed Kubernetes cluster, such as [GKE](https://cloud.google.com/kubernetes-engine), [EKS](https://aws.amazon.com/eks/), or [AKS](https://azure.microsoft.com/en-us/products/kubernetes-service). -- Set up [kubectl](https://kubernetes.io/docs/reference/kubectl/). -- Set up [Helm](https://helm.sh/). -- Install the Nginx Ingress Controller: -- Set up [Minikube](https://minikube.sigs.k8s.io/docs/) - -``` -helm install --namespace kube-system nginx ingress-nginx --repo https://kubernetes.github.io/ingress-nginx -``` ## setup diff --git a/scripts/rca-demo/data/mapping-message-alias.json b/scripts/rca-demo/data/mapping-message-alias.json new file mode 100644 index 0000000000..f10c154ee5 --- /dev/null +++ b/scripts/rca-demo/data/mapping-message-alias.json @@ -0,0 +1,11 @@ +{ + "properties": { + "body_text": { + "type": "match_only_text" + }, + "message": { + "type": "alias", + "path": "body_text" + } + } +} \ No newline at end of file diff --git a/scripts/rca-demo/data/rule-01.json b/scripts/rca-demo/data/rule-01.json new file mode 100644 index 0000000000..c474bda58c --- /dev/null +++ b/scripts/rca-demo/data/rule-01.json @@ -0,0 +1,52 @@ +{ + "params": { + "criteria": [ + { + "comparator": ">", + "metrics": [ + { + "name": "A", + "filter": "http.response.status_code:*", + "aggType": "count" + }, + { + "name": "B", + "filter": "http.response.status_code>=500", + "aggType": "count" + } + ], + "threshold": [ + 1 + ], + "timeSize": 1, + "timeUnit": "m", + "equation": "(B/A) * 100", + "label": "500 Errors" + } + ], + "alertOnNoData": false, + "alertOnGroupDisappear": false, + "searchConfiguration": { + "query": { + "query": "k8s.namespace.name: \"ingress-nginx\" AND url.path: /api/*", + "language": "kuery" + }, + "index": "otel_logs_data" + }, + "groupBy": [ + "service.name", + "url.path" + ] + }, + "consumer": "logs", + "schedule": { + "interval": "1m" + }, + "tags": [ + "demo", + "cli-created" + ], + "name": "NGINX 500s", + "rule_type_id": "observability.rules.custom_threshold", + "actions": [] +} \ No newline at end of file diff --git a/scripts/rca-demo/post-setup b/scripts/rca-demo/post-setup new file mode 100755 index 0000000000..77ce182578 --- /dev/null +++ b/scripts/rca-demo/post-setup @@ -0,0 +1,59 @@ +#!/bin/bash + +SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}") +source $SCRIPTS_DIR/shared.sh +source $SCRIPTS_DIR/.env + +create_data_view () { + ID=$1 + INDICES=$2 + + EXISTS=$(curl -s $KIBANA_URL/api/data_views/data_view/$ID -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H 'elastic-api-version: 2023-10-31') + + if [[ $EXISTS == '{"statusCode":404,"error":"Not Found"'* ]]; then + echo "Creating data view $1 for indices $2" + curl -X POST $KIBANA_URL/api/data_views/data_view -H 'kbn-xsrf: bananas' -H 'Content-Type: application/json' -H 'elastic-api-version: 2023-10-31' -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -d "{\"data_view\": { \"name\": \"$1 (Automated by Demo CLI)\", \"title\": \"$2\", \"id\": \"$1\", \"timeFieldName\": \"@timestamp\" }}" + else + echo "Data view $1 already exists" + fi +} + +create_rule () { + ID=$1 + DATA_FILE=$2 + + curl -X POST $KIBANA_URL/api/alerting/rule/$ID -H 'kbn-xsrf: bananas' -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H 'Content-Type: application/json; elastic-api-version: 2023-10-31' -d @$SCRIPTS_DIR/data/$DATA_FILE + + echo "" +} + +echo "Setting up Kibana assets for the demo..." + +# Load local user .env file +ENV_FILE="./scripts/rca-demo/.env" +if [ -f $ENV_FILE ]; then + echo "Sourcing env vars from $ENV_FILE ..." + source $ENV_FILE +fi + +if [ -z "${KIBANA_URL}" ]; then + die "You must set KIBANA_URL so that we can post API requests there (please include base path if one exists)" +fi + +if [ -z "${OTEL_DEMO_ES_API_KEY}" ]; then + die "You must set OTEL_DEMO_ES_API_KEY (must be valid API key for ES and Kibana APIs)" +fi + +# Create data views +create_data_view otel_logs_data logs-*otel* + +# Create rules +create_rule 9055220c-8fb1-4f9f-be7c-0a33eb2bafc5 rule-01.json + +# Update mappints in ES +curl -X PUT http://localhost:9200/logs-*otel*/_mapping -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H "Content-Type: application/json" -d @$SCRIPTS_DIR/data/mapping-message-alias.json + +echo "" + +# Enable EEM +curl -X PUT -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H "kbn-xsrf: bananas" $KIBANA_URL/internal/entities/managed/enablement \ No newline at end of file diff --git a/scripts/rca-demo/setup b/scripts/rca-demo/setup index 9cb996805f..64def5f90b 100755 --- a/scripts/rca-demo/setup +++ b/scripts/rca-demo/setup @@ -2,38 +2,8 @@ APP_NAME=my-otel-demo -# Define bash colors -# Reset -Color_Off='\033[0m' # Text Reset - -# Regular Colors -Black='\033[0;30m' # Black -Red='\033[0;31m' # Red -Green='\033[0;32m' # Green -Yellow='\033[0;33m' # Yellow -Blue='\033[0;34m' # Blue -Purple='\033[0;35m' # Purple -Cyan='\033[0;36m' # Cyan -White='\033[0;37m' # White - -# Bold -BBlack='\033[1;30m' # Black -BRed='\033[1;31m' # Red -BGreen='\033[1;32m' # Green -BYellow='\033[1;33m' # Yellow -BBlue='\033[1;34m' # Blue -BPurple='\033[1;35m' # Purple -BCyan='\033[1;36m' # Cyan -BWhite='\033[1;37m' # White - -title () { - echo -e ${Cyan}"<[${BBlue} $1 ${Cyan}]>${Color_Off}" -} - -die () { - echo -e "${BRed}ERROR: $1${Color_Off}" - exit 1 -} +# source shared utils +source ./shared.sh title "Checking environment for $APP_NAME..." diff --git a/scripts/rca-demo/shared.sh b/scripts/rca-demo/shared.sh new file mode 100644 index 0000000000..c4674b0b5c --- /dev/null +++ b/scripts/rca-demo/shared.sh @@ -0,0 +1,33 @@ + +# Define bash colors +# Reset +Color_Off='\033[0m' # Text Reset + +# Regular Colors +Black='\033[0;30m' # Black +Red='\033[0;31m' # Red +Green='\033[0;32m' # Green +Yellow='\033[0;33m' # Yellow +Blue='\033[0;34m' # Blue +Purple='\033[0;35m' # Purple +Cyan='\033[0;36m' # Cyan +White='\033[0;37m' # White + +# Bold +BBlack='\033[1;30m' # Black +BRed='\033[1;31m' # Red +BGreen='\033[1;32m' # Green +BYellow='\033[1;33m' # Yellow +BBlue='\033[1;34m' # Blue +BPurple='\033[1;35m' # Purple +BCyan='\033[1;36m' # Cyan +BWhite='\033[1;37m' # White + +title () { + echo -e ${Cyan}"<[${BBlue} $1 ${Cyan}]>${Color_Off}" +} + +die () { + echo -e "${BRed}ERROR: $1${Color_Off}" + exit 1 +} \ No newline at end of file From 0f9d39c33c04f160ba246932089c13b4e201780a Mon Sep 17 00:00:00 2001 From: Jason Rhodes Date: Thu, 24 Oct 2024 13:23:31 -0400 Subject: [PATCH 2/2] Cleaned up logging and function sharing --- scripts/rca-demo/post-setup | 28 ++++++++++++++-------------- scripts/rca-demo/setup | 2 ++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/scripts/rca-demo/post-setup b/scripts/rca-demo/post-setup index 77ce182578..05080efa81 100755 --- a/scripts/rca-demo/post-setup +++ b/scripts/rca-demo/post-setup @@ -1,8 +1,11 @@ #!/bin/bash SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}") + +set -a source $SCRIPTS_DIR/shared.sh source $SCRIPTS_DIR/.env +set +a create_data_view () { ID=$1 @@ -11,10 +14,10 @@ create_data_view () { EXISTS=$(curl -s $KIBANA_URL/api/data_views/data_view/$ID -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H 'elastic-api-version: 2023-10-31') if [[ $EXISTS == '{"statusCode":404,"error":"Not Found"'* ]]; then - echo "Creating data view $1 for indices $2" + title "Creating data view $1 for indices $2" curl -X POST $KIBANA_URL/api/data_views/data_view -H 'kbn-xsrf: bananas' -H 'Content-Type: application/json' -H 'elastic-api-version: 2023-10-31' -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -d "{\"data_view\": { \"name\": \"$1 (Automated by Demo CLI)\", \"title\": \"$2\", \"id\": \"$1\", \"timeFieldName\": \"@timestamp\" }}" else - echo "Data view $1 already exists" + title "Data view $1 already exists" fi } @@ -23,18 +26,9 @@ create_rule () { DATA_FILE=$2 curl -X POST $KIBANA_URL/api/alerting/rule/$ID -H 'kbn-xsrf: bananas' -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H 'Content-Type: application/json; elastic-api-version: 2023-10-31' -d @$SCRIPTS_DIR/data/$DATA_FILE - - echo "" } -echo "Setting up Kibana assets for the demo..." - -# Load local user .env file -ENV_FILE="./scripts/rca-demo/.env" -if [ -f $ENV_FILE ]; then - echo "Sourcing env vars from $ENV_FILE ..." - source $ENV_FILE -fi +title "Setting up Kibana assets for the demo..." if [ -z "${KIBANA_URL}" ]; then die "You must set KIBANA_URL so that we can post API requests there (please include base path if one exists)" @@ -50,10 +44,16 @@ create_data_view otel_logs_data logs-*otel* # Create rules create_rule 9055220c-8fb1-4f9f-be7c-0a33eb2bafc5 rule-01.json -# Update mappints in ES +echo -e "\n" + +# Update mappings in ES +# NOTE: this will fail if the data stream is not created +# TODO: Update the component template also, and just swallow errors on this call +title "Attempting to add message alias for otel logs" curl -X PUT http://localhost:9200/logs-*otel*/_mapping -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H "Content-Type: application/json" -d @$SCRIPTS_DIR/data/mapping-message-alias.json -echo "" +echo -e "\n" # Enable EEM +title "Attempting to enable EEM" curl -X PUT -H "Authorization: ApiKey $OTEL_DEMO_ES_API_KEY" -H "kbn-xsrf: bananas" $KIBANA_URL/internal/entities/managed/enablement \ No newline at end of file diff --git a/scripts/rca-demo/setup b/scripts/rca-demo/setup index 64def5f90b..d7e09aafdc 100755 --- a/scripts/rca-demo/setup +++ b/scripts/rca-demo/setup @@ -3,7 +3,9 @@ APP_NAME=my-otel-demo # source shared utils +set -a source ./shared.sh +set +a title "Checking environment for $APP_NAME..."