130 lines
2.9 KiB
TypeScript
130 lines
2.9 KiB
TypeScript
import { constants, copyFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { Alpine } from "../alpine.js";
|
|
import { buildRepro, reproRun } from "../helpers/repro-run.js";
|
|
|
|
const parsersPath = "/etc/fluent-bit/parsers.conf";
|
|
|
|
export async function installFluentBit(alpine: Alpine): Promise<void> {
|
|
const bin = await buildFluentBit();
|
|
await saveParsers(alpine);
|
|
await alpine.addPackages(["musl-fts", "yaml"]);
|
|
await copyFile(
|
|
bin,
|
|
alpine.path("/usr/local/bin/fluent-bit"),
|
|
constants.COPYFILE_FICLONE
|
|
);
|
|
}
|
|
|
|
// ## Script generators
|
|
|
|
// Returns a logScript to be used with runit for logging to Loki
|
|
export function runitLokiLogger(parser: FluentBitParser, name: string): string {
|
|
return `#!/bin/sh
|
|
exec chpst -u nobody:nobody /usr/local/bin/fluent-bit \
|
|
--parser='${parsersPath}' \
|
|
--input=stdin \
|
|
--prop=parser='${parser}' \
|
|
--output=loki \
|
|
--prop=labels='job=fluentbit,stream=${name}'
|
|
`;
|
|
}
|
|
|
|
// ## Parsers
|
|
|
|
export enum FluentBitParser {
|
|
Json = "json",
|
|
Logfmt = "logfmt",
|
|
// Raw toma todo lo que haya en una línea y lo guarda en `message`. No recomendado.
|
|
Raw = "raw",
|
|
|
|
Forgejo = "forgejo",
|
|
Ntpsec = "ntpsec",
|
|
Dhcpcd = "dhcpcd",
|
|
}
|
|
|
|
async function saveParsers(alpine: Alpine): Promise<void> {
|
|
// https://github.com/fluent/fluent-bit/blob/master/conf/parsers.conf
|
|
await alpine.writeFile(
|
|
parsersPath,
|
|
// https://rubular.com/
|
|
`
|
|
[PARSER]
|
|
name logfmt
|
|
format logfmt
|
|
|
|
[PARSER]
|
|
name raw
|
|
format regex
|
|
regex ^(?<message>.*?)$
|
|
|
|
[PARSER]
|
|
name forgejo
|
|
format regex
|
|
regex ^((?<time>\\d{4}\\/\\d{2}\\/\\d{2} \\d{2}:\\d{2}:\\d{2}?) )?((?<trace>.+:.+:.+\\(\\)?) \\[(?<level>.?)\\] )?(?<message>.*?)$
|
|
|
|
[PARSER]
|
|
name ntpsec
|
|
format regex
|
|
regex ^(?<time>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}) (?<process>.+\\[\\d+\\]): (?<component>\\w+): (?<message>.+)$
|
|
|
|
[PARSER]
|
|
name dhcpcd
|
|
format regex
|
|
regex ^((?<interface>\\w+): )?(?<message>.+)$
|
|
`
|
|
);
|
|
}
|
|
|
|
// ## Build binary
|
|
|
|
function buildFluentBit(): Promise<string> {
|
|
const version = "v2.0.9";
|
|
return buildRepro(
|
|
"fluentbit",
|
|
version,
|
|
`#!/bin/sh -e
|
|
runprint() {
|
|
echo "==> $@"
|
|
"$@"
|
|
}
|
|
|
|
runprint apk add --quiet \
|
|
make gcc g++ patch musl-dev openssl-dev linux-headers \
|
|
bison cmake flex musl-fts-dev gtest-dev yaml-dev zlib-dev
|
|
|
|
wget https://github.com/fluent/fluent-bit/archive/refs/tags/${version}.tar.gz -O- | tar zx
|
|
cd fluent-bit-*
|
|
|
|
patch --strip=1 <<'EOF'
|
|
--- a/lib/chunkio/src/CMakeLists.txt
|
|
+++ b/lib/chunkio/src/CMakeLists.txt
|
|
@@ -12,6 +12,7 @@
|
|
)
|
|
|
|
set(libs cio-crc32)
|
|
+set(libs \${libs} fts)
|
|
|
|
if(\${CMAKE_SYSTEM_NAME} MATCHES "Windows")
|
|
set(src
|
|
EOF
|
|
|
|
runprint cmake -B build \
|
|
-DFLB_CORO_STACK_SIZE=24576 \
|
|
.
|
|
runprint make -C build
|
|
|
|
ls build/bin/
|
|
|
|
mv build/bin/fluent-bit /fluent-bit
|
|
`,
|
|
(dir) =>
|
|
reproRun({
|
|
cwd: dir,
|
|
command: "/src/build",
|
|
cache: [],
|
|
}),
|
|
(dir) => join(dir, "rootfs/fluent-bit")
|
|
);
|
|
}
|