Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: adding user agent string to client #296

Merged
merged 5 commits into from
Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ New Features / Enhancements:
* #291 - compile templates into `nsqadmin` binary
* #285/#288 - `nsq_tail` support for `-n #` to get recent # messages
* #287/#294 - nsqadmin support added for showing client attributes (sample rate, TLS, compression)
* #189/#296 - add client user agent to nsqadmin

### 0.2.24 - 2013-12-07

Expand Down
2 changes: 1 addition & 1 deletion nsqadmin/templates/channel.html.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func init() {
{{range .ChannelStats.Clients}}
<tr>
<td>{{.ClientIdentifier}}</td>
<td>{{.ClientVersion}}</td>
<td>{{.ClientVersion}} {{if ne .ClientUserAgent ""}}({{.ClientUserAgent}}){{end}}</td>
<td>
{{if gt .SampleRate 0}}
<span class="label label-info">Sampled {{.SampleRate}}%</span>
Expand Down
17 changes: 16 additions & 1 deletion nsqadmin/templates/node.html.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func init() {
<th></th>
<th>Client Host</th>
<th>Protocol</th>
<th>Attributes</th>
<th>NSQd Host</th>
<th>In-Flight</th>
<th>Ready Count</th>
Expand All @@ -140,7 +141,21 @@ func init() {
<tr>
<td></td>
<td>{{.ClientIdentifier}}</td>
<td>{{.ClientVersion}}</td>
<td>{{.ClientVersion}} {{if ne .ClientUserAgent ""}}({{.ClientUserAgent}}){{end}}</td>
<td>
{{if gt .SampleRate 0}}
<span class="label label-info">Sampled {{.SampleRate}}%</span>
{{end}}
{{if .TLS}}
<span class="label label-warning">TLS</span>
{{end}}
{{if .Deflate}}
<span class="label label-default">Delfate</span>
{{end}}
{{if .Snappy}}
<span class="label label-primary">Snappy</span>
{{end}}
</td>
<td>{{.HostAddress}}</td>
<td>{{.InFlightCount | commafy}}</td>
<td>{{.ReadyCount | commafy}}</td>
Expand Down
8 changes: 6 additions & 2 deletions nsqd/client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type IdentifyDataV2 struct {
DeflateLevel int `json:"deflate_level"`
Snappy bool `json:"snappy"`
SampleRate int32 `json:"sample_rate"`
UserAgent string `json:"user_agent"`
}

type ClientV2 struct {
Expand All @@ -42,8 +43,9 @@ type ClientV2 struct {

sync.Mutex

ID int64
context *Context
ID int64
context *Context
UserAgent string

// original connection
net.Conn
Expand Down Expand Up @@ -135,6 +137,7 @@ func (c *ClientV2) String() string {
func (c *ClientV2) Identify(data IdentifyDataV2) error {
c.ShortIdentifier = data.ShortId
c.LongIdentifier = data.LongId
c.UserAgent = data.UserAgent
err := c.SetHeartbeatInterval(data.HeartbeatInterval)
if err != nil {
return err
Expand All @@ -155,6 +158,7 @@ func (c *ClientV2) Stats() ClientStats {
Version: "V2",
RemoteAddress: c.RemoteAddr().String(),
Name: c.ShortIdentifier,
UserAgent: c.UserAgent,
State: atomic.LoadInt32(&c.State),
ReadyCount: atomic.LoadInt64(&c.ReadyCount),
InFlightCount: atomic.LoadInt64(&c.InFlightCount),
Expand Down
1 change: 1 addition & 0 deletions nsqd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type ClientStats struct {
TLS bool `json:"tls"`
Deflate bool `json:"deflate"`
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}

type Topics []*Topic
Expand Down
51 changes: 51 additions & 0 deletions nsqd/stats_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"github.com/bitly/go-nsq"
"github.com/bitly/nsq/util"
"github.com/bmizerany/assert"
"github.com/mreiferson/go-snappystream"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -36,3 +40,50 @@ func TestStats(t *testing.T) {
assert.Equal(t, len(stats[0].Channels[0].Clients), 1)
log.Printf("stats: %+v", stats)
}

func TestClientAttributes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

userAgent := "Test User Agent"

*verbose = true
options := NewNSQDOptions()
options.SnappyEnabled = true
tcpAddr, httpAddr, nsqd := mustStartNSQD(options)
defer nsqd.Exit()

conn, err := mustConnectNSQD(tcpAddr)
assert.Equal(t, err, nil)

data := identifyFeatureNegotiation(t, conn, map[string]interface{}{"snappy": true, "user_agent": userAgent})
r := struct {
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}{}
err = json.Unmarshal(data, &r)
assert.Equal(t, err, nil)
assert.Equal(t, r.Snappy, true)

compressConn := snappystream.NewReader(conn, snappystream.SkipVerifyChecksum)

w := snappystream.NewWriter(conn)

rw := readWriter{compressConn, w}

topicName := "test_client_attributes" + strconv.Itoa(int(time.Now().Unix()))
sub(t, rw, topicName, "ch")

err = nsq.Ready(1).Write(rw)
assert.Equal(t, err, nil)

testUrl := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)

statsData, err := util.ApiRequest(testUrl)
if err != nil {
t.Fatalf(err.Error())
}
client := statsData.Get("topics").GetIndex(0).Get("channels").GetIndex(0).Get("clients").GetIndex(0)
assert.Equal(t, client.Get("user_agent").MustString(), userAgent)
assert.Equal(t, client.Get("snappy").MustBool(), true)
}
5 changes: 3 additions & 2 deletions util/lookupd/lookupd.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,9 @@ func GetNSQDStats(nsqdHTTPAddrs []string, selectedTopic string) ([]*TopicStats,
connectedDuration := time.Now().Sub(connected).Seconds()

clientInfo := &ClientInfo{
HostAddress: addr,
ClientVersion: client.Get("version").MustString(),
HostAddress: addr,
ClientVersion: client.Get("version").MustString(),
ClientUserAgent: client.Get("user_agent").MustString(),
ClientIdentifier: fmt.Sprintf("%s:%s", client.Get("name").MustString(),
strings.Split(client.Get("remote_address").MustString(), ":")[1]),
ConnectedDuration: time.Duration(int64(connectedDuration)) * time.Second, // truncate to second
Expand Down
1 change: 1 addition & 0 deletions util/lookupd/statsinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (c *ChannelStats) Host() string {
type ClientInfo struct {
HostAddress string
ClientVersion string
ClientUserAgent string
ClientIdentifier string
ConnectedDuration time.Duration
InFlightCount int
Expand Down