Skip to content

Commit a7387c1

Browse files
update script and execution design
1 parent 4e9b67b commit a7387c1

File tree

5 files changed

+109
-322
lines changed

5 files changed

+109
-322
lines changed

Cargo.lock

Lines changed: 26 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ thiserror = "2.0"
122122
ulid = { version = "1.0", features = ["serde"] }
123123
xxhash-rust = { version = "0.8", features = ["xxh3"] }
124124
futures-core = "0.3.31"
125+
tempfile = "3.20.0"
125126

126127
[build-dependencies]
127128
cargo_toml = "0.21"

resources/filters_demo_data.sh

Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,11 @@
11
#!/usr/bin/env bash
22

3-
# Configuration with validation
3+
# Configuration
44
P_URL=${P_URL:-"http://localhost:8000"}
55
P_USERNAME=${P_USERNAME:-"admin"}
66
P_PASSWORD=${P_PASSWORD:-"admin"}
77
P_STREAM=${P_STREAM:-"demodata"}
88

9-
# Silent mode handling
10-
SILENT=${SILENT:-false}
11-
for arg in "$@"; do
12-
case $arg in
13-
--silent)
14-
SILENT=true
15-
shift
16-
;;
17-
-h|--help)
18-
echo "Usage: $0 [--silent]"
19-
echo " --silent Run in silent mode"
20-
exit 0
21-
;;
22-
esac
23-
done
24-
25-
# Only show config if not silent
26-
if [[ "$SILENT" != "true" ]]; then
27-
echo "Configuration:"
28-
echo " URL: $P_URL"
29-
echo " Username: $P_USERNAME"
30-
echo " Stream: $P_STREAM"
31-
echo
32-
fi
33-
349
# Pre-compute auth header
3510
AUTH_HEADER="Authorization: Basic $(echo -n "$P_USERNAME:$P_PASSWORD" | base64)"
3611

@@ -41,26 +16,20 @@ curl_with_retry() {
4116
local data="$3"
4217
local content_type="${4:-application/json}"
4318
local max_retries="${5:-3}"
44-
local base_timeout="${6:-15}"
4519
local retry_count=0
4620

47-
# Set timeout based on retry attempt: 10s, 20s, 30s
48-
local max_time=$((10 + (retry_count * 10)))
49-
local connect_timeout=5
50-
5121
# Create temp file if data is provided
5222
if [[ -n "$data" ]]; then
5323
temp_file=$(mktemp)
5424
if [[ $? -ne 0 ]]; then
55-
print_error "Failed to create temporary file"
5625
return 1
5726
fi
5827
echo "$data" > "$temp_file"
5928
fi
6029

6130
while [[ $retry_count -lt $max_retries ]]; do
62-
# Current timeout: 10s, 20s, 30s for attempts 1, 2, 3
63-
max_time=$((10 + (retry_count * 10)))
31+
local max_time=$((10 + (retry_count * 10)))
32+
local connect_timeout=5
6433

6534
local curl_cmd="curl -s -w \"\n%{http_code}\" --max-time $max_time --connect-timeout $connect_timeout"
6635

@@ -93,81 +62,38 @@ curl_with_retry() {
9362

9463
# Check curl exit code
9564
if [[ $curl_exit_code -eq 0 ]]; then
96-
# Success - extract status code and return
9765
local status_code
9866
if [[ -n "$response" ]]; then
9967
status_code=$(echo "$response" | tail -n1)
100-
local response_body=$(echo "$response" | sed '$d')
10168

10269
# Clean up temp file
10370
[[ -n "$temp_file" ]] && rm -f "$temp_file"
10471

10572
if [[ "$status_code" == "200" || "$status_code" == "201" ]]; then
10673
return 0
10774
else
108-
print_error "HTTP $status_code: $response_body"
10975
return 1
11076
fi
11177
else
112-
print_error "No response from server"
11378
return 1
11479
fi
11580
elif [[ $curl_exit_code -eq 28 ]]; then
116-
# Timeout - retry immediately with next timeout level
81+
# Timeout - retry
11782
retry_count=$((retry_count + 1))
118-
119-
if [[ "$SILENT" != "true" && -n "$data" ]]; then
120-
echo "Timeout (${#data} chars) - retry $retry_count with $((10 + (retry_count * 10)))s timeout"
121-
elif [[ "$SILENT" != "true" ]]; then
122-
echo "Timeout - retry $retry_count with $((10 + (retry_count * 10)))s timeout"
123-
fi
124-
125-
# Brief pause before retry
12683
sleep 1
12784
else
128-
# Other error - break and report
12985
break
13086
fi
13187
done
13288

13389
# Clean up temp file on failure
13490
[[ -n "$temp_file" ]] && rm -f "$temp_file"
13591

136-
# Final error reporting
137-
print_error "curl failed with exit code $curl_exit_code after $retry_count retries"
138-
if [[ -n "$data" ]]; then
139-
print_error "Data size: ${#data} characters, Final timeout: ${max_time}s"
140-
fi
141-
[[ "$SILENT" != "true" ]] && print_error "Response: $response"
142-
14392
return 1
14493
}
14594

146-
# Colors
147-
RED='\033[0;31m'
148-
GREEN='\033[0;32m'
149-
BLUE='\033[0;34m'
150-
NC='\033[0m'
151-
152-
print_info() { [[ "$SILENT" != "true" ]] && echo -e "${BLUE}[INFO]${NC} $1"; }
153-
print_success() { [[ "$SILENT" != "true" ]] && echo -e "${GREEN}[SUCCESS]${NC} $1"; }
154-
print_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
155-
156-
# Test connection before creating filters
157-
if [[ "$SILENT" != "true" ]]; then
158-
print_info "Testing connectivity..."
159-
if curl_with_retry "$P_URL" "GET" "" "text/html" 1 5; then
160-
print_info "Basic connectivity OK"
161-
else
162-
print_error "Cannot connect to $P_URL - check if server is running"
163-
exit 1
164-
fi
165-
fi
166-
167-
# Create comprehensive SQL filters (10 filters)
95+
# Create SQL filters
16896
create_sql_filters() {
169-
print_info "Creating 10 SQL filters..."
170-
17197
sql_filters=(
17298
"error_logs|Monitor all ERROR and FATAL severity events|SELECT * FROM $P_STREAM WHERE severity_text IN ('ERROR', 'FATAL') ORDER BY time_unix_nano DESC LIMIT 100"
17399
"high_response_time|Identify requests with extended response times|SELECT \"service.name\", \"url.path\", body FROM $P_STREAM WHERE body LIKE '%duration%' ORDER BY time_unix_nano DESC LIMIT 50"
@@ -182,43 +108,28 @@ create_sql_filters() {
182108
)
183109

184110
sql_success_count=0
185-
filter_number=1
186111

187112
for filter_config in "${sql_filters[@]}"; do
188113
IFS='|' read -r name description query <<< "$filter_config"
189114

190-
[[ "$SILENT" != "true" ]] && echo "Creating SQL filter $filter_number/10: $name"
191-
192115
# Escape quotes for JSON
193116
escaped_query=$(echo "$query" | sed 's/"/\\"/g')
194117
escaped_desc=$(echo "$description" | sed 's/"/\\"/g')
195118

196119
json="{\"stream_name\":\"sql\",\"filter_name\":\"$name\",\"filter_description\":\"$escaped_desc\",\"query\":{\"filter_type\":\"sql\",\"filter_query\":\"$escaped_query\"},\"time_filter\":null}"
197120

198-
# Add timeout and better error handling
199121
if curl_with_retry "$P_URL/api/v1/filters" "POST" "$json" "application/json" 3 10; then
200-
[[ "$SILENT" != "true" ]] && echo "✓ SQL Filter: $name"
201122
sql_success_count=$((sql_success_count + 1))
202-
else
203-
[[ "$SILENT" != "true" ]] && echo "✗ Failed after retries: $name"
204123
fi
205124

206-
# Small delay between requests to avoid overwhelming server
207125
sleep 0.5
208-
filter_number=$((filter_number + 1))
209126
done
210127

211-
[[ "$SILENT" != "true" ]] && print_success "Created $sql_success_count/10 SQL filters"
212-
213-
# Wait a bit before creating saved filters
214-
[[ "$SILENT" != "true" ]] && echo "Waiting 3 seconds before creating saved filters..."
215128
sleep 3
216129
}
217130

218-
# Create comprehensive saved filters (10 filters)
131+
# Create saved filters
219132
create_saved_filters() {
220-
print_info "Creating 10 saved filters..."
221-
222133
saved_filters=(
223134
"service_errors|Monitor service errors and failures|SELECT * FROM $P_STREAM WHERE severity_text IN ('ERROR', 'FATAL') LIMIT 500|Ingestion Time,Data,service.name,severity_text,url.path|service.name"
224135
"auth_security_events|Authentication and authorization monitoring|SELECT * FROM $P_STREAM WHERE url.path LIKE '%login%' AND severity_text IN ('WARN', 'ERROR', 'FATAL') LIMIT 500|Ingestion Time,Data,service.name,severity_text,source.address,user_agent.original|severity_text"
@@ -233,13 +144,10 @@ create_saved_filters() {
233144
)
234145

235146
saved_success_count=0
236-
filter_number=1
237147

238148
for filter_config in "${saved_filters[@]}"; do
239149
IFS='|' read -r name description query visible_columns group_by <<< "$filter_config"
240150

241-
[[ "$SILENT" != "true" ]] && echo "Creating saved filter $filter_number/10: $name"
242-
243151
# Escape quotes
244152
escaped_query=$(echo "$query" | sed 's/"/\\"/g')
245153
escaped_desc=$(echo "$description" | sed 's/"/\\"/g')
@@ -254,27 +162,16 @@ create_saved_filters() {
254162

255163
json="{\"stream_name\":\"$P_STREAM\",\"filter_name\":\"$name\",\"filter_description\":\"$escaped_desc\",\"query\":{\"filter_type\":\"filter\",\"filter_query\":\"$escaped_query\"},\"time_filter\":null,\"tableConfig\":{\"visibleColumns\":[$visible_cols_json],\"pinnedColumns\":[]},\"groupBy\":\"$group_by\"}"
256164

257-
# Add timeout and better error handling for saved filters
258165
if curl_with_retry "$P_URL/api/v1/filters" "POST" "$json" "application/json" 3 10; then
259-
[[ "$SILENT" != "true" ]] && echo "✓ Saved Filter: $name"
260166
saved_success_count=$((saved_success_count + 1))
261-
else
262-
[[ "$SILENT" != "true" ]] && echo "✗ Failed after retries: $name"
263167
fi
264168

265-
# Small delay between requests
266169
sleep 0.5
267-
filter_number=$((filter_number + 1))
268170
done
269-
270-
[[ "$SILENT" != "true" ]] && print_success "Created $saved_success_count/10 saved filters"
271171
}
272172

273173
# Create all filters
274174
create_sql_filters
275175
create_saved_filters
276176

277-
print_success "Filter creation completed successfully!"
278-
279-
# Always exit with success if we get here
280177
exit 0

0 commit comments

Comments
 (0)