#!/usr/bin/env bash # ============================================================================= # Registers the `app-logs-*` data view in Kibana so the logs are searchable # straight after `make up`, without clicking through the UI. # # ./scripts/kibana_setup.sh [kibana-url] # ============================================================================= set -Eeuo pipefail KIBANA_URL="${1:-http://localhost:${KIBANA_PORT:-5601}}" DATA_VIEW_TITLE="app-logs-*" echo "Waiting for Kibana at ${KIBANA_URL} ..." for _ in $(seq 1 60); do if curl -fsS "${KIBANA_URL}/api/status" 2>/dev/null | grep -q '"level":"available"'; then echo "Kibana is available." break fi sleep 5 done echo "Creating the '${DATA_VIEW_TITLE}' data view ..." response=$( curl -sS -o /dev/null -w '%{http_code}' -X POST "${KIBANA_URL}/api/data_views/data_view" \ -H 'kbn-xsrf: true' \ -H 'Content-Type: application/json' \ -d "{\"data_view\":{\"title\":\"${DATA_VIEW_TITLE}\",\"name\":\"Application logs\",\"timeFieldName\":\"@timestamp\"}}" ) || true case "${response}" in 200|201) echo "Data view created." ;; 400|409) echo "Data view already exists - nothing to do." ;; *) echo "Unexpected response from Kibana: HTTP ${response}" >&2; exit 1 ;; esac echo echo "Open Kibana → Analytics → Discover and pick 'Application logs'." echo "Useful queries:" echo " event: \"search_executed\" and took_ms > 200" echo " event: \"search_executed\" and result_count: 0" echo " level: \"ERROR\"" echo " request_id: \"\""