51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
func main() {
|
|
p := NewProgress()
|
|
reg := prometheus.NewPedanticRegistry()
|
|
|
|
reg.MustRegister(
|
|
p,
|
|
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
|
collectors.NewGoCollector(),
|
|
)
|
|
|
|
e := echo.New()
|
|
e.GET("/metrics", func(c echo.Context) error {
|
|
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(c.Response(), c.Request())
|
|
return nil
|
|
})
|
|
e.POST("/:streamName", progressHandler(p))
|
|
|
|
e.Logger.Fatal(e.Start(":1323"))
|
|
}
|
|
|
|
func progressHandler(p *Progress) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
streamName := c.Param("streamName")
|
|
if streamName == "" {
|
|
return c.String(400, "streamName is required")
|
|
}
|
|
p.AddStream(streamName)
|
|
defer p.RemoveStream(streamName)
|
|
|
|
fmt.Println("streamName: ", streamName)
|
|
|
|
s := bufio.NewScanner(c.Request().Body)
|
|
for s.Scan() {
|
|
p.IngestProgressValue(streamName, s.Text())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
}
|