From 94696e81fa44e5b751fa85d327d9be9164e81593 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 4 Jan 2021 01:12:25 -0500 Subject: [PATCH] fix: ensure the vector container does not exit when no configured sinks are in place --- docs/deployment/logs.md | 6 ------ plugins/logs/functions.go | 22 ++++++++++++++++++++++ plugins/logs/subcommands.go | 5 +++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/docs/deployment/logs.md b/docs/deployment/logs.md index df7998894..466ebac33 100644 --- a/docs/deployment/logs.md +++ b/docs/deployment/logs.md @@ -73,8 +73,6 @@ dokku logs:vector-start This will start a new container named `vector` with Dokku's vector config mounted and ready for use. If a running container already exists, this command will do nothing. Additionally, if a container exists but is not running, this command will attempt to start the container. -By default, the vector container will do nothing and may exit immediately. It is necessary to configure a sink in order to ensure the vector container runs as necessary. - While the default vector image is hardcoded, users may specify an alternative via the `--vector-image` flag: ```shell @@ -101,8 +99,6 @@ The `vector` container will be stopped and removed from the system. If the conta #### Configuring a log sink -> If no sinks are specified, the vector container will exit. - Vector uses the concept of log "sinks" to send logs to a given endpoint. Log sinks may be configured globally or on a per-app basis by specifying a `sink` in DSN form with the `logs:set` command. Specifying a sink value will reload any running vector container. ```shell @@ -131,8 +127,6 @@ As with app-specific sink settings, the global value may also be cleared by sett dokku logs:set --global sink ``` -If no sink is specified, the vector container may exit prematurely. - ##### Log Sink DSN Format The DSN form of a sink is as follows: diff --git a/plugins/logs/functions.go b/plugins/logs/functions.go index 99c1db2c1..50c2b5e71 100644 --- a/plugins/logs/functions.go +++ b/plugins/logs/functions.go @@ -123,6 +123,10 @@ func valueToConfig(appName string, value string) (vectorSink, error) { if appName == "--global" { i = "inputs[]=docker-global-source" } + if appName == "--null" { + i = "inputs[]=docker-null-source" + } + initialQuery := fmt.Sprintf("%s&%s", t, i) query := u.RawQuery if query == "" { @@ -189,6 +193,24 @@ func writeVectorConfig() error { data.Sinks["docker-global-sink"] = sink } + if len(data.Sources) == 0 { + // pull from no containers + data.Sources["docker-null-source"] = vectorSource{ + Type: "docker_logs", + IncludeLabels: []string{"com.dokku.vector-null"}, + } + } + + if len(data.Sinks) == 0 { + // write logs to a blackhole + sink, err := valueToConfig("--null", "blackhole://?print_amount=1") + if err != nil { + return err + } + + data.Sinks["docker-null-sink"] = sink + } + b, err := json.MarshalIndent(data, "", " ") if err != nil { return err diff --git a/plugins/logs/subcommands.go b/plugins/logs/subcommands.go index ba59c17ff..1dd4e0975 100644 --- a/plugins/logs/subcommands.go +++ b/plugins/logs/subcommands.go @@ -104,6 +104,11 @@ func CommandVectorLogs() error { func CommandVectorStart(vectorImage string) error { common.LogInfo2("Starting vector container") if !common.ContainerExists(vectorContainerName) { + common.LogVerbose("Ensuring vector configuration exists") + if err := writeVectorConfig(); err != nil { + return err + } + return startVectorContainer(vectorImage) }