From 1effb0007085bcd81f44e7a9f676ab84bd90a5a1 Mon Sep 17 00:00:00 2001 From: Nghia Tran Date: Fri, 22 Dec 2023 14:02:57 -0800 Subject: [PATCH] Add /quitquitquit handler Signed-off-by: Nghia Tran --- otel-collector/cmd/otel-collector/main.go | 25 +++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/otel-collector/cmd/otel-collector/main.go b/otel-collector/cmd/otel-collector/main.go index 52f6b6b2..d323d21e 100644 --- a/otel-collector/cmd/otel-collector/main.go +++ b/otel-collector/cmd/otel-collector/main.go @@ -6,7 +6,9 @@ SPDX-License-Identifier: Apache-2.0 package main import ( + "context" "log" + "net/http" "os" "os/exec" ) @@ -18,11 +20,30 @@ func main() { log.Fatalf("error checking config.yaml: %v", err) } + log.Printf("config.yaml found at %s", configPath) + + ctx, cancel := context.WithCancel(context.Background()) + // run /otel-collector binary with --config config.yaml - cmd := exec.Command("otelcol-contrib", "--config", configPath) + cmd := exec.CommandContext(ctx, "otelcol-contrib", "--config", configPath) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { + + // Start the command but don't wait. + if err := cmd.Start(); err != nil { + log.Fatalf("failed to start otel-collector: %v", err) + } + + // Start a server that serves /quitquitquit on port 31415 + mux := http.NewServeMux() + srv := &http.Server{Addr: ":31415", Handler: mux} + mux.HandleFunc("/quitquitquit", func(w http.ResponseWriter, r *http.Request) { + go srv.Shutdown(ctx) + cancel() + }) + go srv.ListenAndServe() + + if err := cmd.Wait(); err != nil { log.Fatal(err) } }