Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User-Agent HTTP request header #11

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ loki-exporter: loki_exporter.sh loki_exporter.init loki_exporter.conf
for f in loki_exporter.init loki_exporter.conf; do \
install -m 644 $(TOPDIR)/$${f} $(TOPDIR)/$@/files/ ; \
done
install -m 755 $(TOPDIR)/loki_exporter.sh $(TOPDIR)/$@/files/loki_exporter.sh
sed \
-e "s,%% VERSION %%,$(VERSION),g" \
-e "s,%% BUILD_ID %%,$(BUILD),g" \
< $(TOPDIR)/loki_exporter.sh > $(TOPDIR)/$@/files/loki_exporter.sh
chmod 755 $(TOPDIR)/$@/files/loki_exporter.sh

.PHONY: package
package: loki-exporter ## Build OpenWRT package
Expand Down
47 changes: 39 additions & 8 deletions loki_exporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ^^^ the above line is purely for shellcheck to treat this as a bash-like script
# (OpenWRT's ash from busybox is kinda similar but there still could be issues)

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

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

USER_AGENT="openwrt-loki-exporter/%% VERSION %%"
BUILD_ID="%% BUILD_ID %%"

_curl_bulk_cmd() {
if [ "${AUTOTEST-0}" -eq 1 ]; then
_CURL_BULK_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Connection: close")
_CURL_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Connection: close")
curl --no-progress-meter -fv \
-H "Content-Type: application/json" \
-H "Content-Encoding: gzip" \
-H "User-Agent: ${USER_AGENT}" \
-H "Connection: close" \
"$@"
else
_CURL_BULK_CMD=(curl -fsS -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close")
_CURL_CMD=(curl -fsS -H "Content-Type: application/json" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close")
curl -fsS \
-H "Content-Type: application/json" \
-H "Content-Encoding: gzip" \
-H "User-Agent: ${USER_AGENT}" \
-H "Authorization: Basic ${LOKI_AUTH_HEADER}" \
-H "Connection: close" \
"$@"
fi
}

_curl_cmd() {
if [ "${AUTOTEST-0}" -eq 1 ]; then
curl --no-progress-meter -fv \
-H "Content-Type: application/json" \
-H "User-Agent: ${USER_AGENT}" \
-H "Connection: close" \
"$@"
else
curl -fsS \
-H "Content-Type: application/json" \
-H "User-Agent: ${USER_AGENT}" \
-H "Authorization: Basic ${LOKI_AUTH_HEADER}" \
-H "Connection: close" \
"$@"
fi
}

_setup() {
mkfifo "${PIPE_NAME}"
echo "started with BOOT=${BOOT}" >&2
echo "openwrt-loki-exporter/${BUILD_ID} started with BOOT=${BOOT}" >&2
}

_teardown() {
Expand Down Expand Up @@ -73,7 +104,7 @@ _do_bulk_post() {
echo "${post_body}" | gzip >"${_log_file}.payload.gz"
rm -f "${_log_file}"

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

if ! "${_CURL_CMD[@]}" -d "${post_body}" "${LOKI_PUSH_URL}"; then
if ! _curl_cmd -d "${post_body}" "${LOKI_PUSH_URL}"; then
echo "POST FAILED: '${post_body}'"
fi

Expand Down