grafana/pkg/build/cmd/publishmetrics.go
Kevin Minehart d3af3e0431
CI: Move grabpl publish-metrics (#55042)
* move grabpl publish-metrics

* grabpl -> ./bin/build

* fix lint error
2022-09-12 13:28:08 -05:00

42 lines
920 B
Go

package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"regexp"
"github.com/grafana/grafana/pkg/build/metrics"
"github.com/urfave/cli/v2"
)
func PublishMetrics(c *cli.Context) error {
apiKey := c.Args().Get(0)
input, err := io.ReadAll(os.Stdin)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Reading from stdin failed: %s", err), 1)
}
reMetrics := regexp.MustCompile(`(?ms)^Metrics: (\{.+\})`)
ms := reMetrics.FindSubmatch(input)
if len(ms) == 0 {
return cli.NewExitError(fmt.Sprintf("Input on wrong format: %q", string(input)), 1)
}
m := map[string]string{}
if err := json.Unmarshal(ms[1], &m); err != nil {
return cli.NewExitError(fmt.Sprintf("decoding metrics failed: %s", err), 1)
}
log.Printf("Received metrics %+v", m)
if err := metrics.Publish(m, apiKey); err != nil {
return cli.NewExitError(fmt.Sprintf("publishing metrics failed: %s", err), 1)
}
return nil
}