github.com/blend/go-sdk@v1.20220411.3/cmd/stats/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"encoding/json"
    12  	"flag"
    13  	"log"
    14  	"os"
    15  
    16  	"github.com/blend/go-sdk/statsd"
    17  )
    18  
    19  var (
    20  	flagBindAddr = flag.String("bind-addr", bindAddr(), "The bind address for the server")
    21  )
    22  
    23  func bindAddr() string {
    24  	if value := os.Getenv("BIND_ADDR"); value != "" {
    25  		return value
    26  	}
    27  	return "127.0.0.1:0"
    28  }
    29  
    30  func main() {
    31  	flag.Parse()
    32  
    33  	logger := log.New(os.Stdout, "statsd|", log.LstdFlags)
    34  
    35  	server := &statsd.Server{
    36  		Addr: *flagBindAddr,
    37  		Log:  logger,
    38  		Handler: func(metrics ...statsd.Metric) {
    39  			printer := json.NewEncoder(os.Stdout)
    40  			printer.SetIndent("", "  ")
    41  			for _, metric := range metrics {
    42  				_ = printer.Encode(metric)
    43  			}
    44  		},
    45  	}
    46  	if err := server.Start(); err != nil {
    47  		logger.Fatal(err)
    48  	}
    49  }