forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdemo.sh
More file actions
executable file
·353 lines (297 loc) · 10 KB
/
demo.sh
File metadata and controls
executable file
·353 lines (297 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/sh
set -eu
# Constants
ELASTIC_STACK_VERSION="9.3.0"
ENV_OVERRIDE_FILE=".env.override"
NAMESPACE="opentelemetry-operator-system"
HELM_REPO_NAME="open-telemetry"
HELM_REPO_URL='https://open-telemetry.github.io/opentelemetry-helm-charts'
DEMO_RELEASE="my-otel-demo"
DEMO_CHART="open-telemetry/opentelemetry-demo"
DEMO_HELM_VERSION='0.40.7'
KUBE_STACK_RELEASE="opentelemetry-kube-stack"
KUBE_STACK_CHART="open-telemetry/opentelemetry-kube-stack"
KUBE_STACK_VERSION='0.14.12'
KUBE_STACK_VALUES_URL='https://raw.githubusercontent.com/elastic/elastic-agent/refs/tags/v'$ELASTIC_STACK_VERSION'/deploy/helm/edot-collector/kube-stack/managed_otlp/values.yaml'
SECRET_NAME='elastic-secret-otel'
DOCKER_COLLECTOR_CONFIG='./src/otel-collector/otelcol-elastic-config.yaml'
COLLECTOR_CONTRIB_IMAGE=docker.elastic.co/elastic-agent/elastic-agent:$ELASTIC_STACK_VERSION
# Detect sed variant: GNU sed uses --version, BSD sed doesn't
# GNU sed: sed -i (no empty string needed)
# BSD sed: sed -i '' (empty string required)
if sed --version >/dev/null 2>&1; then
SED_IS_BSD="false"
else
SED_IS_BSD="true"
fi
# Portable sed in-place editing function
# Usage: sed_in_place "s/pattern/replacement/g" file
sed_in_place() {
if [ "$SED_IS_BSD" = "true" ]; then
sed -i '' "$@"
else
sed -i "$@"
fi
}
# Variables
platform=""
destroy="false"
self_hosted="false"
upstream="false"
elastic_otlp_endpoint=""
elastic_otlp_api_key=""
usage() {
echo "Usage: $0 [docker|k8s] [self-hosted]"
echo
echo "Options:"
echo " docker Deploy to Docker (requires Elastic Cloud credentials)"
echo " docker self-hosted Deploy to Docker with local start-local backend"
echo " docker upstream Deploy to Docker with no EDOT, vanilla OTel"
echo " k8s Deploy to Kubernetes"
echo " k8s upstream Deploy to Kubernetes with no EDOT, vanilla OTel"
echo
echo "Self-hosted mode requires start-local with EDOT:"
echo " curl -fsSL https://elastic.co/start-local | sh -s -- --edot"
echo
echo "To destroy: $0 destroy [docker|k8s]"
exit 1
}
parse_args() {
# Support legacy 3-argument format for CI/tests
if [ -n "${CI:-}" ] && [ $# -eq 3 ] && [ "${1#-}" = "$1" ]; then
# First arg doesn't start with dash, assume legacy positional format
platform="$1"
elastic_otlp_endpoint="$2"
elastic_otlp_api_key="$3"
return
fi
while [ $# -gt 0 ]; do
case "$1" in
k8s) platform="k8s"; shift ;;
docker) platform="docker"; shift ;;
self-hosted) self_hosted="true"; shift ;;
upstream) upstream="true"; shift;;
destroy)
destroy="true"
shift;
if [ "${1:-}" = "docker" ] || [ "${1:-}" = "k8s" ]; then
platform="$1"
shift
fi
;;
*) shift ;;
esac
done
}
update_env_var() {
VAR="$1"
VAL="$2"
tmp=$(mktemp) || exit 1
if grep -q "^$VAR=" "$ENV_OVERRIDE_FILE"; then
sed_in_place "s|^$VAR=.*|$VAR=\"$VAL\"|" "$ENV_OVERRIDE_FILE"
else
echo "$VAR=\"$VAL\"" >>"$ENV_OVERRIDE_FILE"
fi
}
# Read a secret from the terminal without echo and assign it to a variable by name
# Usage: read_secret variable_name "Prompt: "
read_secret() {
var_name="$1"
prompt="$2"
printf "%s" "$prompt"
stty -echo 2>/dev/null || :
trap 'stty echo 2>/dev/null' 0 INT TERM HUP
read -r "${var_name?}"
stty echo 2>/dev/null || :
trap - 0 INT TERM HUP
echo
}
ensure_env_values() {
if [ -n "${CI:-}" ]; then
return 0
fi
echo
if ! check_existing_credentials; then
if [ -z "$elastic_otlp_endpoint" ]; then
printf "🔑 Enter your Elastic OTLP endpoint: "
read -r elastic_otlp_endpoint
fi
fi
if [ -z "$elastic_otlp_api_key" ]; then
read_secret elastic_otlp_api_key "🔑 Enter your Elastic API key: "
fi
echo
}
check_existing_credentials() {
if [ ! -f "$ENV_OVERRIDE_FILE" ]; then
return 1
fi
elastic_otlp_endpoint=$(grep "^ELASTIC_OTLP_ENDPOINT=" "$ENV_OVERRIDE_FILE" | cut -d'=' -f2- | tr -d '"')
elastic_otlp_api_key=$(grep "^ELASTIC_OTLP_API_KEY=" "$ENV_OVERRIDE_FILE" | cut -d'=' -f2- | tr -d '"')
if [ -n "$elastic_otlp_endpoint" ] && [ -n "$elastic_otlp_api_key" ] &&
[ "$elastic_otlp_endpoint" != "YOUR_ENDPOINT" ] &&
[ "$elastic_otlp_api_key" != "YOUR_API_KEY" ]; then
echo "✅ Using existing credentials from $ENV_OVERRIDE_FILE"
return 0
fi
elastic_otlp_endpoint=""
elastic_otlp_api_key=""
return 1
}
start_docker() {
ensure_env_values
update_env_var "ELASTIC_OTLP_ENDPOINT" "$elastic_otlp_endpoint"
update_env_var "ELASTIC_OTLP_API_KEY" "$elastic_otlp_api_key"
update_env_var "OTEL_COLLECTOR_CONFIG" "$DOCKER_COLLECTOR_CONFIG"
update_env_var "COLLECTOR_CONTRIB_IMAGE" "$COLLECTOR_CONTRIB_IMAGE"
export ELASTIC_OTLP_ENDPOINT="$elastic_otlp_endpoint"
export ELASTIC_OTLP_API_KEY="$elastic_otlp_api_key"
make start
}
start_docker_upstream() {
ensure_env_values
export ELASTIC_OTLP_ENDPOINT="$elastic_otlp_endpoint"
export ELASTIC_OTLP_API_KEY="$elastic_otlp_api_key"
export OTEL_COLLECTOR_CONFIG_EXTRAS="./src/otel-collector/otelcol-config-extras-elastic.yml"
# Here we use docker compose instead of make start as we do not want to use the .env.override file.
# This allows us to run the upstream demo, no EDOT collector and no EDOT SDKs.
docker compose --env-file .env \
-f docker-compose.yml \
-f docker-compose.elastic.yml \
up --force-recreate --remove-orphans --detach
}
start_docker_self_hosted() {
echo
echo "🏠 Self-hosted mode: forwarding telemetry to local start-local collector"
echo
update_env_var "COLLECTOR_CONTRIB_IMAGE" "$COLLECTOR_CONTRIB_IMAGE"
# Use docker compose with the self-hosted override file to connect
# the otel-collector to the elastic-start-local network
docker compose --env-file .env --env-file .env.override \
-f docker-compose.yml \
-f docker-compose.elastic-self-hosted.yml \
up --force-recreate --remove-orphans --detach
echo ""
echo "OpenTelemetry Demo is running."
echo "Go to http://localhost:8080 for the demo UI."
}
ensure_k8s_prereqs() {
helm repo add "$HELM_REPO_NAME" "$HELM_REPO_URL" --force-update
kubectl create namespace "$NAMESPACE" --dry-run=client -o yaml | kubectl apply -f -
}
apply_k8s_secret() {
ensure_env_values
kubectl create secret generic "$SECRET_NAME" \
--namespace "$NAMESPACE" \
--from-literal=elastic_otlp_endpoint="$elastic_otlp_endpoint" \
--from-literal=elastic_api_key="$elastic_otlp_api_key" \
--dry-run=client -o yaml | kubectl apply -f -
}
install_kube_stack() {
helm upgrade --install "$KUBE_STACK_RELEASE" "$KUBE_STACK_CHART" \
--namespace "$NAMESPACE" \
--values "$KUBE_STACK_VALUES_URL" \
--values kubernetes/elastic-helm/kube-stack-overrides.yml \
--version "$KUBE_STACK_VERSION"
}
install_demo_chart() {
helm upgrade --install "$DEMO_RELEASE" "$DEMO_CHART" --version "$DEMO_HELM_VERSION" -f kubernetes/elastic-helm/demo.yml
}
start_k8s() {
ensure_k8s_prereqs
apply_k8s_secret
update_env_var "ELASTIC_OTLP_ENDPOINT" "$elastic_otlp_endpoint"
update_env_var "ELASTIC_OTLP_API_KEY" "$elastic_otlp_api_key"
install_kube_stack
install_demo_chart
}
start_k8s_upstream() {
ensure_env_values
export ELASTIC_OTLP_ENDPOINT="$elastic_otlp_endpoint"
export ELASTIC_OTLP_API_KEY="$elastic_otlp_api_key"
kubectl create secret generic elastic-secret-otel \
--from-literal=elastic_otlp_endpoint="$elastic_otlp_endpoint" \
--from-literal=elastic_api_key="$elastic_otlp_api_key" \
--dry-run=client -o yaml | kubectl apply -f -
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm install my-otel-demo open-telemetry/opentelemetry-demo -f ./kubernetes/elastic-helm/demo-upstream.yml
}
destroy_docker() {
echo
make stop
echo
}
destroy_k8s() {
echo
helm uninstall "$DEMO_RELEASE" --ignore-not-found
helm uninstall "$KUBE_STACK_RELEASE" -n "$NAMESPACE" --ignore-not-found
kubectl delete secret "$SECRET_NAME" -n "$NAMESPACE" --ignore-not-found
kubectl delete namespace "$NAMESPACE" --ignore-not-found --wait=false --timeout=60s
echo
}
main() {
echo '----------------------------------------------------'
echo '🚀 OpenTelemetry Demo with Elastic Observability'
echo '----------------------------------------------------'
parse_args "$@"
if [ "$destroy" = "true" ]; then
if [ -z "$platform" ]; then
echo "⌛️ Destroying Docker and Kubernetes resources..."
destroy_docker
destroy_k8s
echo "✅ Done! Destroyed Docker and Kubernetes resources."
exit 0
fi
if [ "$platform" = "docker" ]; then
echo "⌛️ Destroying Docker resources..."
destroy_docker
echo "✅ Done! Destroyed Docker resources."
exit 0
fi
if [ "$platform" = "k8s" ]; then
echo "⌛️ Destroying Kubernetes resources..."
destroy_k8s
echo "✅ Done! Destroyed Kubernetes resources."
exit 0
fi
usage
fi
if [ "$platform" != "docker" ] && [ "$platform" != "k8s" ]; then
usage
fi
echo
if [ "$self_hosted" = "true" ]; then
echo "⌛️ Starting OTel Demo + EDOT on '$platform' (self-hosted mode)..."
elif [ "$upstream" = "true" ]; then
echo "⌛️ Starting OTel Demo '$platform' (no EDOT)..."
else
echo "⌛️ Starting OTel Demo + EDOT on '$platform'..."
fi
if [ "$platform" = "docker" ]; then
if [ "$self_hosted" = "true" ]; then
start_docker_self_hosted
elif [ "$upstream" = "true" ]; then
start_docker_upstream
else
start_docker
fi
else
if [ "$upstream" = "true" ]; then
start_k8s_upstream
else
start_k8s
fi
fi
echo
if [ "$self_hosted" = "true" ]; then
echo "🎉 OTel Demo and EDOT are running on '$platform'; data is flowing to local Elasticsearch."
echo
echo "📊 Open Kibana at http://localhost:5601"
echo "🛒 Open Demo at http://localhost:8080"
elif [ "$upstream" = "true" ]; then
echo "🎉 OTel Demo and OTel collector are running on '$platform'; data is flowing to Elastic."
else
echo "🎉 OTel Demo and EDOT are running on '$platform'; data is flowing to Elastic."
fi
}
main "$@"