in otelcollector/main/main.go [21:253]
func main() {
// Handle SIGTERM
go handleShutdown()
controllerType := shared.GetControllerType()
cluster := shared.GetEnv("CLUSTER", "")
clusterOverride := shared.GetEnv("CLUSTER_OVERRIDE", "")
aksRegion := shared.GetEnv("AKSREGION", "")
ccpMetricsEnabled := shared.GetEnv("CCP_METRICS_ENABLED", "false")
osType := os.Getenv("OS_TYPE")
if osType == "windows" {
shared.SetEnvVariablesForWindows()
}
if osType == "linux" {
outputFile := "/opt/inotifyoutput.txt"
if ccpMetricsEnabled != "true" { //data-plane
if err := shared.Inotify(outputFile, "/etc/config/settings"); err != nil {
log.Fatal(err)
}
if err := shared.Inotify(outputFile, "/etc/prometheus/certs"); err != nil {
log.Fatal(err)
}
} else { //control-plane
if err := shared.InotifyCCP(outputFile, "/etc/config/settings"); err != nil {
log.Fatal(err)
}
}
} else if osType == "windows" {
fmt.Println("Starting filesystemwatcher.ps1")
shared.StartCommand("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "C:\\opt\\scripts\\filesystemwatcher.ps1")
}
if ccpMetricsEnabled != "true" && osType == "linux" {
if err := shared.SetupArcEnvironment(); err != nil {
shared.EchoError(err.Error())
}
}
// Check if MODE environment variable is empty
mode := shared.GetEnv("MODE", "simple")
shared.EchoVar("MODE", mode)
shared.EchoVar("CONTROLLER_TYPE", shared.GetEnv("CONTROLLER_TYPE", ""))
shared.EchoVar("CLUSTER", cluster)
customEnvironment := shared.GetEnv("customEnvironment", "")
if ccpMetricsEnabled != "true" {
shared.SetupTelemetry(customEnvironment)
if err := shared.ConfigureEnvironment(); err != nil {
log.Fatalf("Error configuring environment: %v\n", err)
}
}
if ccpMetricsEnabled == "true" {
ccpconfigmapsettings.Configmapparserforccp()
} else {
configmapsettings.Configmapparser()
}
if ccpMetricsEnabled != "true" && osType == "linux" {
shared.StartCronDaemon()
}
var meConfigFile string
var fluentBitConfigFile string
meConfigFile, fluentBitConfigFile = shared.DetermineConfigFiles(controllerType, clusterOverride)
fmt.Println("meConfigFile:", meConfigFile)
fmt.Println("fluentBitConfigFile:", fluentBitConfigFile)
shared.WaitForTokenAdapter(ccpMetricsEnabled)
if ccpMetricsEnabled != "true" {
shared.SetEnvAndSourceBashrcOrPowershell("ME_CONFIG_FILE", meConfigFile, true)
shared.SetEnvAndSourceBashrcOrPowershell("customResourceId", cluster, true)
} else {
os.Setenv("ME_CONFIG_FILE", meConfigFile)
os.Setenv("customResourceId", cluster)
}
trimmedRegion := strings.ToLower(strings.ReplaceAll(aksRegion, " ", ""))
if ccpMetricsEnabled != "true" {
shared.SetEnvAndSourceBashrcOrPowershell("customRegion", trimmedRegion, true)
} else {
os.Setenv("customRegion", trimmedRegion)
}
fmt.Println("Waiting for 10s for token adapter sidecar to be up and running so that it can start serving IMDS requests")
time.Sleep(10 * time.Second)
if ccpMetricsEnabled != "true" {
if osType == "linux" {
fmt.Println("Starting MDSD")
shared.StartMdsdForOverlay()
} else {
fmt.Println("Starting MA")
shared.StartMA()
}
} else {
shared.StartMdsdForUnderlay()
}
if osType == "linux" {
// update this to use color coding
shared.PrintMdsdVersion()
}
fmt.Println("Waiting for 30s for MDSD to get the config and put them in place for ME")
time.Sleep(30 * time.Second)
fmt.Println("Starting Metrics Extension with config overrides")
if ccpMetricsEnabled != "true" {
if _, err := shared.StartMetricsExtensionForOverlay(meConfigFile); err != nil {
log.Fatalf("Error starting MetricsExtension: %v\n", err)
}
} else {
shared.StartMetricsExtensionWithConfigOverridesForUnderlay(meConfigFile)
}
// Start otelcollector
azmonOperatorEnabled := os.Getenv("AZMON_OPERATOR_ENABLED")
azmonUseDefaultPrometheusConfig := os.Getenv("AZMON_USE_DEFAULT_PROMETHEUS_CONFIG")
var collectorConfig string
if controllerType == "replicaset" && azmonOperatorEnabled == "true" {
fmt.Println("Starting otelcollector in replicaset with Target allocator settings")
if ccpMetricsEnabled == "true" {
collectorConfig = "/opt/microsoft/otelcollector/ccp-collector-config-replicaset.yml"
} else {
collectorConfig = "/opt/microsoft/otelcollector/collector-config-replicaset.yml"
configmapsettings.SetGlobalSettingsInCollectorConfig()
}
} else if azmonUseDefaultPrometheusConfig == "true" {
fmt.Println("Starting otelcollector with only default scrape configs enabled")
if ccpMetricsEnabled == "true" {
collectorConfig = "/opt/microsoft/otelcollector/ccp-collector-config-default.yml"
} else {
collectorConfig = "/opt/microsoft/otelcollector/collector-config-default.yml"
}
} else {
collectorConfig = "/opt/microsoft/otelcollector/collector-config.yml"
}
fmt.Println("startCommand otelcollector")
_, err := shared.StartCommandWithOutputFile("/opt/microsoft/otelcollector/otelcollector", []string{"--config", collectorConfig}, "/opt/microsoft/otelcollector/collector-log.txt")
if osType == "linux" {
shared.LogVersionInfo()
}
if ccpMetricsEnabled != "true" {
shared.StartFluentBit(fluentBitConfigFile)
// Run the command and capture the output
if osType == "linux" {
cmd := exec.Command("fluent-bit", "--version")
fluentBitVersion, err := cmd.Output()
if err != nil {
log.Fatalf("failed to run command: %v", err)
}
shared.EchoVar("FLUENT_BIT_VERSION", string(fluentBitVersion))
} else if osType == "windows" {
cmd := exec.Command("C:\\opt\\fluent-bit\\bin\\fluent-bit.exe", "--version")
fluentBitVersion, err := cmd.Output()
if err != nil {
log.Fatalf("failed to run command: %v", err)
}
shared.EchoVar("FLUENT_BIT_VERSION", string(fluentBitVersion))
}
}
if osType == "linux" {
// Start inotify to watch for changes
fmt.Println("Starting inotify for watching mdsd config update")
// Create an output file for inotify events
outputFile := "/opt/inotifyoutput-mdsd-config.txt"
_, err = os.Create(outputFile)
if err != nil {
log.Fatalf("Error creating output file: %v\n", err)
}
// Define the command to start inotify
inotifyCommand := exec.Command(
"inotifywait",
"/etc/mdsd.d/config-cache/metricsextension/TokenConfig.json",
"--daemon",
"--outfile", outputFile,
"--event", "ATTRIB",
"--event", "create",
"--event", "delete",
"--event", "modify",
"--format", "%e : %T",
"--timefmt", "+%s",
)
// Start the inotify process
err = inotifyCommand.Start()
if err != nil {
log.Fatalf("Error starting inotify process: %v\n", err)
}
}
// Setting time at which the container started running
epochTimeNow := time.Now().Unix()
epochTimeNowReadable := time.Unix(epochTimeNow, 0).Format(time.RFC3339)
// Writing the epoch time to a file
file, err := os.Create("/opt/microsoft/liveness/azmon-container-start-time")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%d", epochTimeNow))
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
// Printing the environment variable and the readable time
fmt.Printf("AZMON_CONTAINER_START_TIME=%d\n", epochTimeNow)
shared.FmtVar("AZMON_CONTAINER_START_TIME_READABLE", epochTimeNowReadable)
// Expose a health endpoint for liveness probe
http.HandleFunc("/health", healthHandler)
http.ListenAndServe(":8080", nil)
}