From 42900a5b0f7ddebc101110701604b4c42b4b9aee Mon Sep 17 00:00:00 2001 From: Mikhail Alpinskiy Date: Mon, 16 Dec 2024 17:41:37 +0300 Subject: [PATCH] Application name --- statshouse.go | 58 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/statshouse.go b/statshouse.go index e438c7f..1a6501a 100644 --- a/statshouse.go +++ b/statshouse.go @@ -215,14 +215,24 @@ func StopRegularMeasurement(id int) { // Specifying empty StatsHouse address will make the client silently discard all metrics. // if you get compiler error after updating to recent version of library, pass statshouse.DefaultNetwork to network parameter func NewClient(logf LoggerFunc, network string, statsHouseAddr string, defaultEnv string) *Client { + return NewClientEx(ConfigureArgs{ + Logger: logf, + Network: network, + StatsHouseAddr: statsHouseAddr, + DefaultEnv: defaultEnv, + }) +} + +func NewClientEx(args ConfigureArgs) *Client { + logf := args.Logger if logf == nil { logf = log.Printf } - maxSize := maxPayloadSize(network) + maxSize := maxPayloadSize(args.Network) c := &Client{ logF: logf, - addr: statsHouseAddr, - network: network, + addr: args.StatsHouseAddr, + network: args.Network, packet: packet{ buf: make([]byte, batchHeaderLen, maxSize), maxSize: maxSize, @@ -230,7 +240,8 @@ func NewClient(logf LoggerFunc, network string, statsHouseAddr string, defaultEn close: make(chan chan struct{}), w: map[metricKey]*bucket{}, wn: map[metricKeyNamed]*bucket{}, - env: defaultEnv, + app: args.AppName, + env: args.DefaultEnv, regularFuncs: map[int]func(*Client){}, tsUnixSec: uint32(time.Now().Unix()), } @@ -265,6 +276,7 @@ type Client struct { // transport transportMu sync.RWMutex + app string env string // if set, will be put into key0/env network string addr string @@ -293,6 +305,14 @@ type Client struct { bucketCount *atomic.Int32 } +type ConfigureArgs struct { + Logger LoggerFunc + AppName string // application name + DefaultEnv string // default environment + Network string // default "udp" + StatsHouseAddr string // default "127.0.0.1:13337" +} + type packet struct { buf []byte maxSize int @@ -312,6 +332,7 @@ type tcpConn struct { wouldBlockSize atomic.Int32 *Client + app string env string net.Conn @@ -322,7 +343,7 @@ type tcpConn struct { closeErr chan error } -func (c *Client) netDial(env, network, addr string) (netConn, error) { +func (c *Client) netDial(env, network, addr, app string) (netConn, error) { conn, err := net.Dial(network, addr) if err != nil { c.rareLog("[statshouse] failed to dial statshouse: %v", err) @@ -339,6 +360,7 @@ func (c *Client) netDial(env, network, addr string) (netConn, error) { } t := &tcpConn{ Client: c, + app: app, env: env, Conn: conn, r: make(chan []byte, tcpConnBucketCount), @@ -428,8 +450,9 @@ func (t *tcpConn) send() { name: "__src_client_write_err", } fillTag(&k, "0", t.env) - fillTag(&k, "1", "1") // lang: golang - fillTag(&k, "2", "1") // kind: would block + fillTag(&k, "1", "1") // lang: golang + fillTag(&k, "2", "1") // kind: would block + fillTag(&k, "3", t.app) // application name p.sendValues(nil, &k, "", 0, 0, []float64{float64(n)}) p.writeBatchHeader() if _, err = t.Conn.Write(p.buf); err != nil { @@ -519,6 +542,16 @@ func (c *Client) callRegularFuncs(regularCache []func(*Client)) []func(*Client) } func (c *Client) configure(logf LoggerFunc, network string, statsHouseAddr string, env string) { + c.configureEx(ConfigureArgs{ + Logger: logf, + Network: network, + StatsHouseAddr: statsHouseAddr, + DefaultEnv: env, + }) +} + +func (c *Client) configureEx(args ConfigureArgs) { + logf := args.Logger if logf == nil { logf = log.Printf } @@ -529,9 +562,10 @@ func (c *Client) configure(logf LoggerFunc, network string, statsHouseAddr strin // then transport c.transportMu.Lock() defer c.transportMu.Unlock() - c.env = env - c.network = network - c.addr = statsHouseAddr + c.app = args.AppName + c.env = args.DefaultEnv + c.network = args.Network + c.addr = args.StatsHouseAddr if c.conn != nil { err := c.conn.Close() if err != nil { @@ -540,7 +574,7 @@ func (c *Client) configure(logf LoggerFunc, network string, statsHouseAddr strin c.conn = nil } - if maxSize := maxPayloadSize(network); maxSize != c.packet.maxSize { + if maxSize := maxPayloadSize(args.Network); maxSize != c.packet.maxSize { c.packet = packet{ buf: make([]byte, batchHeaderLen, maxSize), maxSize: maxSize, @@ -778,7 +812,7 @@ func (c *Client) flush() { c.writeBatchHeader() if c.conn == nil && c.addr != "" { - conn, err := c.netDial(c.env, c.network, c.addr) + conn, err := c.netDial(c.env, c.network, c.addr, c.app) if err != nil { c.rareLog("[statshouse] failed to dial statshouse: %v", err) } else {