From 8a26fed3fc3c7bb64b6b406a2bf3e4adff448381 Mon Sep 17 00:00:00 2001 From: Ilya Selivanau <25643404+OrangeFlag@users.noreply.github.com> Date: Tue, 30 Jun 2026 18:40:20 +0300 Subject: [PATCH] feat: add client hostname option --- client.go | 37 +++++++++++++++++++++++++++++-------- query.go | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index dac18fd8..5e1aa402 100644 --- a/client.go +++ b/client.go @@ -6,6 +6,8 @@ import ( "fmt" "io" "net" + "os" + "runtime" "strconv" "strings" "sync" @@ -35,6 +37,7 @@ type Client struct { info proto.ClientHello server proto.ServerHello version clientVersion + hostname string quotaKey string mux sync.Mutex @@ -447,15 +450,8 @@ func Connect(ctx context.Context, conn net.Conn, opt Options) (*Client, error) { func ConnectWithBuffer(ctx context.Context, conn net.Conn, opt Options, buf *proto.Buffer) (*Client, error) { opt.setDefaults() - clientName := proto.Name pkg := pkgVersion.Get() - if opt.ClientName == "" { - if pkg.Name != "" { - clientName = fmt.Sprintf("%s (%s)", clientName, pkg.Name) - } - } else { - clientName = fmt.Sprintf("%s %s", clientName, opt.ClientName) - } + clientName := formatClientName(opt.ClientName, pkg) ver := clientVersion{ Name: clientName, Major: pkg.Major, @@ -499,6 +495,7 @@ func ConnectWithBuffer(ctx context.Context, conn net.Conn, opt Options, buf *pro if opt.SSHSigner != nil { user = " SSH KEY AUTHENTICATION " + user } + hostname, _ := os.Hostname() c := &Client{ conn: conn, @@ -510,6 +507,7 @@ func ConnectWithBuffer(ctx context.Context, conn net.Conn, opt Options, buf *pro tracer: opt.tracer, meter: opt.meter, quotaKey: opt.QuotaKey, + hostname: hostname, readTimeout: opt.ReadTimeout, @@ -539,6 +537,29 @@ func ConnectWithBuffer(ctx context.Context, conn net.Conn, opt Options, buf *pro return c, nil } +func formatClientName(clientName string, pkg pkgVersion.Value) string { + libraryProduct := formatLibraryProduct(pkg) + metadata := formatClientMetadata() + if clientName == "" { + if pkg.Name != "" { + return fmt.Sprintf("%s (%s) %s", proto.Name, pkg.Name, metadata) + } + return fmt.Sprintf("%s %s", proto.Name, metadata) + } + return fmt.Sprintf("%s %s %s", clientName, libraryProduct, metadata) +} + +func formatLibraryProduct(pkg pkgVersion.Value) string { + if pkg.Raw != "" { + return proto.Name + "/" + strings.TrimPrefix(pkg.Raw, "v") + } + return fmt.Sprintf("%s/%d.%d.%d", proto.Name, pkg.Major, pkg.Minor, pkg.Patch) +} + +func formatClientMetadata() string { + return fmt.Sprintf("(lv:go/%s; os:%s)", strings.TrimPrefix(runtime.Version(), "go"), runtime.GOOS) +} + // A Dialer dials using a context. type Dialer interface { DialContext(ctx context.Context, network, address string) (net.Conn, error) diff --git a/query.go b/query.go index 469de9bb..7eab9a3d 100644 --- a/query.go +++ b/query.go @@ -102,7 +102,7 @@ func (c *Client) sendQuery(ctx context.Context, q Query) error { InitialQueryID: q.QueryID, InitialAddress: c.conn.LocalAddr().String(), OSUser: "", - ClientHostname: "", + ClientHostname: c.hostname, ClientName: c.version.Name, Span: trace.SpanContextFromContext(ctx),