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 42900a5
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions statshouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,33 @@ 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,
},
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()),
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -312,6 +332,7 @@ type tcpConn struct {
wouldBlockSize atomic.Int32

*Client
app string
env string
net.Conn

Expand All @@ -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)
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 42900a5

Please sign in to comment.