Skip to content

Commit d773397

Browse files
authored
Add User-Agent HTTP request header (#11)
* Add User-Agent header * Use functions instead of array expansion to run curl It turned out ash does not have bash-like array support at all.
1 parent c3a89b5 commit d773397

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ loki-exporter: loki_exporter.sh loki_exporter.init loki_exporter.conf
244244
for f in loki_exporter.init loki_exporter.conf; do \
245245
install -m 644 $(TOPDIR)/$${f} $(TOPDIR)/$@/files/ ; \
246246
done
247-
install -m 755 $(TOPDIR)/loki_exporter.sh $(TOPDIR)/$@/files/loki_exporter.sh
247+
sed \
248+
-e "s,%% VERSION %%,$(VERSION),g" \
249+
-e "s,%% BUILD_ID %%,$(BUILD),g" \
250+
< $(TOPDIR)/loki_exporter.sh > $(TOPDIR)/$@/files/loki_exporter.sh
251+
chmod 755 $(TOPDIR)/$@/files/loki_exporter.sh
248252

249253
.PHONY: package
250254
package: loki-exporter ## Build OpenWRT package

loki_exporter.sh

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ^^^ the above line is purely for shellcheck to treat this as a bash-like script
55
# (OpenWRT's ash from busybox is kinda similar but there still could be issues)
66

7-
_TMPDIR=$(mktemp -d -p /tmp loki_exporter.XXXXXX)
7+
_TMPDIR="$(mktemp -d -p /tmp loki_exporter.XXXXXX)"
88
PIPE_NAME="/${_TMPDIR}/loki_exporter.pipe"
99
BULK_DATA="/${_TMPDIR}/loki_exporter.boot"
1010

@@ -16,17 +16,48 @@ LOKI_BULK_TEMPLATE_FOOTER="]}]}"
1616
DATETIME_STR_FORMAT="%a %b %d %H:%M:%S %Y"
1717
OS=$(uname -s | tr "[:upper:]" "[:lower:]")
1818

19+
USER_AGENT="openwrt-loki-exporter/%% VERSION %%"
20+
BUILD_ID="%% BUILD_ID %%"
21+
22+
_curl_bulk_cmd() {
1923
if [ "${AUTOTEST-0}" -eq 1 ]; then
20-
_CURL_BULK_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Connection: close")
21-
_CURL_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Connection: close")
24+
curl --no-progress-meter -fv \
25+
-H "Content-Type: application/json" \
26+
-H "Content-Encoding: gzip" \
27+
-H "User-Agent: ${USER_AGENT}" \
28+
-H "Connection: close" \
29+
"$@"
2230
else
23-
_CURL_BULK_CMD=(curl -fsS -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close")
24-
_CURL_CMD=(curl -fsS -H "Content-Type: application/json" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close")
31+
curl -fsS \
32+
-H "Content-Type: application/json" \
33+
-H "Content-Encoding: gzip" \
34+
-H "User-Agent: ${USER_AGENT}" \
35+
-H "Authorization: Basic ${LOKI_AUTH_HEADER}" \
36+
-H "Connection: close" \
37+
"$@"
2538
fi
39+
}
40+
41+
_curl_cmd() {
42+
if [ "${AUTOTEST-0}" -eq 1 ]; then
43+
curl --no-progress-meter -fv \
44+
-H "Content-Type: application/json" \
45+
-H "User-Agent: ${USER_AGENT}" \
46+
-H "Connection: close" \
47+
"$@"
48+
else
49+
curl -fsS \
50+
-H "Content-Type: application/json" \
51+
-H "User-Agent: ${USER_AGENT}" \
52+
-H "Authorization: Basic ${LOKI_AUTH_HEADER}" \
53+
-H "Connection: close" \
54+
"$@"
55+
fi
56+
}
2657

2758
_setup() {
2859
mkfifo "${PIPE_NAME}"
29-
echo "started with BOOT=${BOOT}" >&2
60+
echo "openwrt-loki-exporter/${BUILD_ID} started with BOOT=${BOOT}" >&2
3061
}
3162

3263
_teardown() {
@@ -73,7 +104,7 @@ _do_bulk_post() {
73104
echo "${post_body}" | gzip >"${_log_file}.payload.gz"
74105
rm -f "${_log_file}"
75106

76-
if ! "${_CURL_BULK_CMD[@]}" --data-binary "@${_log_file}.payload.gz" "${LOKI_PUSH_URL}" >"${_log_file}.payload.gz-response" 2>&1; then
107+
if ! _curl_bulk_cmd --data-binary "@${_log_file}.payload.gz" "${LOKI_PUSH_URL}" >"${_log_file}.payload.gz-response" 2>&1; then
77108
echo "BULK POST FAILED: leaving ${_log_file}.payload.gz for now"
78109
fi
79110
}
@@ -211,7 +242,7 @@ _main_loop() {
211242
post_body="${post_body/TIMESTAMP/$ts_ns}"
212243
post_body="${post_body/MESSAGE/$msg}"
213244

214-
if ! "${_CURL_CMD[@]}" -d "${post_body}" "${LOKI_PUSH_URL}"; then
245+
if ! _curl_cmd -d "${post_body}" "${LOKI_PUSH_URL}"; then
215246
echo "POST FAILED: '${post_body}'"
216247
fi
217248

0 commit comments

Comments
 (0)