cmd/metric-receiver: restore minimal metrics server

Change-Id: I33ac9d7ccf21f41ef3077c64506df63607ed6b15
This commit is contained in:
Egon Elbre 2020-10-14 20:01:29 +03:00
parent 02cbf1e72a
commit 20a50f0906

View File

@ -0,0 +1,49 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"flag"
"fmt"
"github.com/spf13/cobra"
"storj.io/common/telemetry"
"storj.io/private/process"
)
var (
addr = flag.String("addr", ":9000", "address to listen for metrics on")
)
func main() {
process.Exec(&cobra.Command{
Use: "metric-receiver",
Short: "receive metrics",
RunE: run,
})
}
func run(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
s, err := telemetry.Listen(*addr)
if err != nil {
return err
}
defer printError(s.Close)
fmt.Printf("listening on %s\n", s.Addr())
return s.Serve(ctx, telemetry.HandlerFunc(handle))
}
func handle(application, instance string, key []byte, val float64) {
fmt.Printf("%s %s %s %v\n", application, instance, string(key), val)
}
func printError(fn func() error) {
err := fn()
if err != nil {
fmt.Println(err)
}
}