in entrypoint.go [101:180]
func main() {
// SIGINT handles Ctrl+C locally.
// SIGTERM handles Cloud Run termination signal.
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
ctx := context.Background()
lastRawConfig, err := getRawUserConfig(userConfigFile)
if err != nil {
log.Fatal(err)
}
// Generate the OTel config for the first time.
err = generateOtelConfig(ctx, userConfigFile)
if err != nil {
log.Fatal(err)
}
entrypointMux := http.NewServeMux()
entrypointMux.HandleFunc(livenessProbePath, healthcheckHandler)
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%d", livenessProbePort), entrypointMux)
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// Spin up new-subprocess that runs the OTel collector and store the PID.
// This OTel collector should use the generated config.
var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, /* stdin is not needed for the collector */
os.Stdout, os.Stderr}
collectorProcess, err := os.StartProcess("./rungmpcol", []string{"./rungmpcol", "--config", otelConfigFile}, &procAttr)
if err != nil {
log.Fatal(err)
}
log.Printf("entrypoint: started OTel successfully")
refreshTicker := time.NewTicker(configRefreshInterval)
for {
select {
case <-refreshTicker.C:
rawConfig, err := getRawUserConfig(userConfigFile)
if err != nil {
log.Fatal(err)
}
// Check if we're using the default config. Only reload if something
// has changed since the last time we checked.
if rawConfig == lastRawConfig {
continue
}
// Something changed since the last time we checked the config.
err = generateOtelConfig(ctx, userConfigFile)
if err != nil {
log.Fatal(err)
}
lastRawConfig = rawConfig
// Signal the OTel collector to reload its config
collectorProcess.Signal(syscall.SIGHUP)
log.Println("entrypoint: reloaded OTel config")
case sig := <-signalChan:
// Wait for signals from Cloud Run. Signal the sub process appropriately
// after making relevant changes to the config and/or health signals.
// TODO(b/307317433): Consider having a timeout to shutdown the subprocess
// non-gracefully.
log.Printf("entrypoint: %s signal caught", sig)
collectorProcess.Signal(sig)
processState, err := collectorProcess.Wait()
if err != nil {
log.Fatalf(processState.String(), err)
}
log.Print("entrypoint: sidecar exited")
return
}
}
}