I hope someone can give me some advice. I am new to Golang and still learning.
I have tried to write a prometheus exporter script for some routing software ( and I know I could be re-inventng the wheel here) but I would like to figure what is wrong with my Go program.
It seems when my Go code runs the http server works fine but hangs but any other functions outside do not seen to execute. My code is below,
`package main
//Import required libs
import (
"os"
"os/exec"
"log"
"time"
"net/http"
"fmt"
)
const portNum string = ":8080"
//function for get OpenBGPd metrics
func get_metrics() {
cmd := exec.Command("sudo", "bgpctl", "show", "metrics")
// open the out file for writing
outfile, err := os.Create("./out.txt")
if err != nil {
panic(err)
}
defer outfile.Close()
cmd.Stdout = outfile
err = cmd.Start(); if err != nil {
panic(err)
}
cmd.Wait()
}
func httpserv() {
get_metrics()
log.Println("Metics got!")
log.Println("Starting our simple http server.")
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
log.Println("Started on port", portNum)
fmt.Println("To close connection CTRL+C :-)")
//Spinning up the server.
err := http.ListenAndServe(portNum, nil)
if err != nil {
log.Fatal(err)
}
}
func main() {
httpserv()
for {
get_metrics()
log.Println("Metics got!")
time.Sleep(2 * time.Second)
}
}
`
Any tips or suggestions would be helpful
If I separate the functions in to 2 separate programs and run them they work fine.
MoShivji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.