Skip to content

Commit

Permalink
Don't allow inputs to overwrite host tag
Browse files Browse the repository at this point in the history
closes #1054

This affects tags in the following plugins:

- cassandra
- disque
- rethinkdb
  • Loading branch information
sparrc committed Apr 19, 2016
1 parent c737a19 commit 08fe8b6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 51 deletions.
14 changes: 5 additions & 9 deletions agent/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,13 @@ func (ac *accumulator) AddFields(
if tags == nil {
tags = make(map[string]string)
}
// Apply plugin-wide tags if set
for k, v := range ac.inputConfig.Tags {
if _, ok := tags[k]; !ok {
tags[k] = v
}
}
// Apply daemon-wide tags if set
for k, v := range ac.defaultTags {
if _, ok := tags[k]; !ok {
tags[k] = v
}
tags[k] = v
}
// Apply plugin-wide tags if set
for k, v := range ac.inputConfig.Tags {
tags[k] = v
}
ac.inputConfig.Filter.FilterTags(tags)

Expand Down
20 changes: 3 additions & 17 deletions plugins/inputs/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import (
"io/ioutil"
"net/http"
"net/url"
//"reflect"
"strings"
)

/*type Server struct {
Host string
Username string
Password string
Port string
}*/

type JolokiaClient interface {
MakeRequest(req *http.Request) (*http.Response, error)
}
Expand Down Expand Up @@ -55,12 +47,6 @@ type jmxMetric interface {
addTagsFields(out map[string]interface{})
}

func addServerTags(host string, tags map[string]string) {
if host != "" && host != "localhost" && host != "127.0.0.1" {
tags["host"] = host
}
}

func newJavaMetric(host string, metric string,
acc telegraf.Accumulator) *javaMetric {
return &javaMetric{host: host, metric: metric, acc: acc}
Expand Down Expand Up @@ -120,7 +106,7 @@ func (j javaMetric) addTagsFields(out map[string]interface{}) {

tokens := parseJmxMetricRequest(mbean)
addTokensToTags(tokens, tags)
addServerTags(j.host, tags)
tags["cassandra_host"] = j.host

if _, ok := tags["mname"]; !ok {
//Queries for a single value will not return a "name" tag in the response.
Expand Down Expand Up @@ -148,7 +134,7 @@ func addCassandraMetric(mbean string, c cassandraMetric,
fields := make(map[string]interface{})
tokens := parseJmxMetricRequest(mbean)
addTokensToTags(tokens, tags)
addServerTags(c.host, tags)
tags["cassandra_host"] = c.host
addValuesAsFields(values, fields, tags["mname"])
c.acc.AddFields(tokens["class"]+tokens["type"], fields, tags)

Expand Down Expand Up @@ -192,7 +178,7 @@ func (j *Cassandra) SampleConfig() string {
servers = ["myuser:mypassword@10.10.10.1:8778","10.10.10.2:8778",":8778"]
## List of metrics collected on above servers
## Each metric consists of a jmx path.
## This will collect all heap memory usage metrics from the jvm and
## This will collect all heap memory usage metrics from the jvm and
## ReadLatency metrics for all keyspaces and tables.
## "type=Table" in the query works with Cassandra3.0. Older versions might
## need to use "type=ColumnFamily"
Expand Down
40 changes: 20 additions & 20 deletions plugins/inputs/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ const validCassandraNestedMultiValueJSON = `
"status": 200,
"timestamp": 1458089184,
"value": {
"org.apache.cassandra.metrics:keyspace=test_keyspace1,name=ReadLatency,scope=test_table1,type=Table":
"org.apache.cassandra.metrics:keyspace=test_keyspace1,name=ReadLatency,scope=test_table1,type=Table":
{ "999thPercentile": 1.0,
"Count": 100,
"DurationUnit": "microseconds",
"OneMinuteRate": 1.0,
"RateUnit": "events/second",
"StdDev": null
},
"org.apache.cassandra.metrics:keyspace=test_keyspace2,name=ReadLatency,scope=test_table2,type=Table":
"org.apache.cassandra.metrics:keyspace=test_keyspace2,name=ReadLatency,scope=test_table2,type=Table":
{ "999thPercentile": 2.0,
"Count": 200,
"DurationUnit": "microseconds",
Expand Down Expand Up @@ -163,13 +163,13 @@ func TestHttpJsonJavaMultiValue(t *testing.T) {
"HeapMemoryUsage_used": 203288528.0,
}
tags1 := map[string]string{
"host": "10.10.10.10",
"mname": "HeapMemoryUsage",
"cassandra_host": "10.10.10.10",
"mname": "HeapMemoryUsage",
}

tags2 := map[string]string{
"host": "10.10.10.11",
"mname": "HeapMemoryUsage",
"cassandra_host": "10.10.10.11",
"mname": "HeapMemoryUsage",
}
acc.AssertContainsTaggedFields(t, "javaMemory", fields, tags1)
acc.AssertContainsTaggedFields(t, "javaMemory", fields, tags2)
Expand All @@ -190,8 +190,8 @@ func TestHttpJsonJavaMultiType(t *testing.T) {
}

tags := map[string]string{
"host": "10.10.10.10",
"mname": "ConcurrentMarkSweep",
"cassandra_host": "10.10.10.10",
"mname": "ConcurrentMarkSweep",
}
acc.AssertContainsTaggedFields(t, "javaGarbageCollector", fields, tags)
}
Expand Down Expand Up @@ -231,10 +231,10 @@ func TestHttpJsonCassandraMultiValue(t *testing.T) {
}

tags := map[string]string{
"host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace1",
"scope": "test_table",
"cassandra_host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace1",
"scope": "test_table",
}
acc.AssertContainsTaggedFields(t, "cassandraTable", fields, tags)
}
Expand Down Expand Up @@ -268,17 +268,17 @@ func TestHttpJsonCassandraNestedMultiValue(t *testing.T) {
}

tags1 := map[string]string{
"host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace1",
"scope": "test_table1",
"cassandra_host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace1",
"scope": "test_table1",
}

tags2 := map[string]string{
"host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace2",
"scope": "test_table2",
"cassandra_host": "10.10.10.10",
"mname": "ReadLatency",
"keyspace": "test_keyspace2",
"scope": "test_table2",
}

acc.AssertContainsTaggedFields(t, "cassandraTable", fields1, tags1)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/disque/disque.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (g *Disque) gatherServer(addr *url.URL, acc telegraf.Accumulator) error {
var read int

fields := make(map[string]interface{})
tags := map[string]string{"host": addr.String()}
tags := map[string]string{"disque_host": addr.String()}
for read < sz {
line, err := r.ReadString('\n')
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/rethinkdb/rethinkdb_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func (s *Server) getServerStatus() error {

func (s *Server) getDefaultTags() map[string]string {
tags := make(map[string]string)
tags["host"] = s.Url.Host
tags["hostname"] = s.serverStatus.Network.Hostname
tags["rethinkdb_host"] = s.Url.Host
tags["rethinkdb_hostname"] = s.serverStatus.Network.Hostname
return tags
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/rethinkdb/rethinkdb_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestGetDefaultTags(t *testing.T) {
in string
out string
}{
{"host", server.Url.Host},
{"hostname", server.serverStatus.Network.Hostname},
{"rethinkdb_host", server.Url.Host},
{"rethinkdb_hostname", server.serverStatus.Network.Hostname},
}
defaultTags := server.getDefaultTags()
for _, tt := range tagTests {
Expand Down

0 comments on commit 08fe8b6

Please sign in to comment.