Last active
September 13, 2022 02:21
-
-
Save technillogue/07f26843114ec2baf0a0ac861687cb85 to your computer and use it in GitHub Desktop.
log shipping without sidecars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import logging | |
import os | |
import sys | |
original_stdout = os.dup(sys.stdout.fileno()) | |
original_stderr = os.dup(sys.stderr.fileno()) | |
# create a temporary buffer in memory | |
temp_buffer_fd = os.memfd_create("temp_buffer") | |
# point stdout to that buffer, | |
# sticking all of our logging until vector starts in that buffer | |
os.dup2(temp_buffer_fd, sys.stdout.fileno(), inheritable=False) | |
os.dup2(temp_buffer_fd, sys.stderr.fileno(), inheritable=False) | |
logging.warning("log from before event loop") | |
async def init_vector() -> asyncio.subprocess.Process: | |
# asyncio.StreamWriter doesn't have a fd, so use an explicit pipe | |
read_pipe, write_pipe = os.pipe() | |
tee = await asyncio.create_subprocess_shell( | |
# use process substitution to copy tee's output to both vectory and original stdout | |
# copy the pipeout to both vector's stdin and actual stdout | |
"tee 2>&1 >(vector -c ~/.vector/config/vector.toml)", | |
# if we just set stdin to just PIPE, it would be a StreamWriter and not have a .fileno() | |
stdin=read_pipe, | |
stdout=original_stdout, | |
shell=True, | |
executable="/bin/bash", | |
) | |
# Cause tee's stdin to get a copy of our stdin/stdout (as well as that | |
# of any child processes we spawn) | |
os.dup2(write_pipe, sys.stdout.fileno()) | |
os.dup2(write_pipe, sys.stderr.fileno()) | |
# print all of the earlier logs for vector to collect | |
temp_file = os.fdopen(temp_buffer_fd, mode="r") | |
temp_file.seek(0) | |
print(temp_file.read(), end="") | |
return tee | |
async def main() -> None: | |
vector = await init_vector() | |
await (await asyncio.create_subprocess_shell("echo log from subprocess")).wait() | |
await vector.wait() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[sources.stdin] | |
type = "stdin" | |
[sinks.honeycomb] | |
type = "honeycomb" | |
inputs = ["stdin"] | |
api_key = "${HONEYCOMB_API_KEY}" | |
dataset = "vector-test" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment