Skip to content

Commit

Permalink
Application name
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Dec 16, 2024
1 parent 870fc96 commit 0ad6240
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions statshouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ 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
network := args.Network
statsHouseAddr := args.StatsHouseAddr
defaultEnv := args.DefaultEnv
if logf == nil {
logf = log.Printf
}
Expand All @@ -230,6 +243,7 @@ func NewClient(logf LoggerFunc, network string, statsHouseAddr string, defaultEn
close: make(chan chan struct{}),
w: map[metricKey]*bucket{},
wn: map[metricKeyNamed]*bucket{},
app: args.AppName,
env: defaultEnv,
regularFuncs: map[int]func(*Client){},
tsUnixSec: uint32(time.Now().Unix()),
Expand Down Expand Up @@ -265,6 +279,7 @@ type Client struct {

// transport
transportMu sync.RWMutex
app string
env string // if set, will be put into key0/env
network string
addr string
Expand Down Expand Up @@ -293,6 +308,14 @@ type Client struct {
bucketCount *atomic.Int32
}

type ConfigureArgs struct {
Logger LoggerFunc
AppName string
DefaultEnv string
Network string // default "udp"
StatsHouseAddr string // default "127.0.0.1:13337"
}

type packet struct {
buf []byte
maxSize int
Expand All @@ -312,6 +335,7 @@ type tcpConn struct {
wouldBlockSize atomic.Int32

*Client
app string
env string
net.Conn

Expand All @@ -322,13 +346,13 @@ type tcpConn struct {
closeErr chan error
}

func (c *Client) netDial(env, network, addr string) (netConn, error) {
conn, err := net.Dial(network, addr)
func (c *Client) netDial() (netConn, error) {
conn, err := net.Dial(c.network, c.addr)
if err != nil {
c.rareLog("[statshouse] failed to dial statshouse: %v", err)
return nil, err
}
if network != "tcp" {
if c.network != "tcp" {
return &datagramConn{Conn: conn}, nil
}
_, err = conn.Write([]byte("statshousev1"))
Expand All @@ -339,7 +363,8 @@ func (c *Client) netDial(env, network, addr string) (netConn, error) {
}
t := &tcpConn{
Client: c,
env: env,
app: c.app,
env: c.env,
Conn: conn,
r: make(chan []byte, tcpConnBucketCount),
w: make(chan []byte, tcpConnBucketCount),
Expand Down Expand Up @@ -428,8 +453,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 {
Expand Down Expand Up @@ -519,6 +545,19 @@ 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
network := args.Network
statsHouseAddr := args.StatsHouseAddr
env := args.DefaultEnv
if logf == nil {
logf = log.Printf
}
Expand All @@ -529,6 +568,7 @@ func (c *Client) configure(logf LoggerFunc, network string, statsHouseAddr strin
// then transport
c.transportMu.Lock()
defer c.transportMu.Unlock()
c.app = args.AppName
c.env = env
c.network = network
c.addr = statsHouseAddr
Expand Down Expand Up @@ -778,7 +818,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()
if err != nil {
c.rareLog("[statshouse] failed to dial statshouse: %v", err)
} else {
Expand Down

0 comments on commit 0ad6240

Please sign in to comment.