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

add instance type tag option on autothrottle configuration #262

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions cmd/autothrottle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
AppKey string
NetworkTXQuery string
BrokerIDTag string
InstanceTypeTag string
MetricsWindow int
ZKAddr string
ZKPrefix string
Expand All @@ -51,6 +52,7 @@ func init() {
flag.StringVar(&Config.AppKey, "app-key", "", "Datadog app key")
flag.StringVar(&Config.NetworkTXQuery, "net-tx-query", "avg:system.net.bytes_sent{service:kafka} by {host}", "Datadog query for broker outbound bandwidth by host")
flag.StringVar(&Config.BrokerIDTag, "broker-id-tag", "broker_id", "Datadog host tag for broker ID")
flag.StringVar(&Config.InstanceTypeTag, "instance-type-tag", "instance-type", "Datadog instance-type tag for broker")
flag.IntVar(&Config.MetricsWindow, "metrics-window", 120, "Time span of metrics required (seconds)")
flag.StringVar(&Config.ZKAddr, "zk-addr", "localhost:2181", "ZooKeeper connect string (for broker metadata or rebuild-topic lookups)")
flag.StringVar(&Config.ZKPrefix, "zk-prefix", "", "ZooKeeper namespace prefix")
Expand Down Expand Up @@ -106,11 +108,12 @@ func main() {

// Init a Kafka metrics fetcher.
km, err := datadog.NewHandler(&datadog.Config{
APIKey: Config.APIKey,
AppKey: Config.AppKey,
NetworkTXQuery: Config.NetworkTXQuery,
BrokerIDTag: Config.BrokerIDTag,
MetricsWindow: Config.MetricsWindow,
APIKey: Config.APIKey,
AppKey: Config.AppKey,
NetworkTXQuery: Config.NetworkTXQuery,
BrokerIDTag: Config.BrokerIDTag,
InstanceTypeTag: Config.InstanceTypeTag,
MetricsWindow: Config.MetricsWindow,
})
if err != nil {
log.Fatal(err)
Expand Down
31 changes: 18 additions & 13 deletions kafkametrics/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@ type Config struct {
// BrokerIDTag is the host tag name
// for Kafka broker IDs.
BrokerIDTag string
// InstanceTypeTag is the instance type tag name
// for Kafka broker Instance.
InstanceTypeTag string
// MetricsWindow specifies the window size of
// timeseries data to evaluate in seconds.
// All values for the window are averaged.
MetricsWindow int
}

type ddHandler struct {
c *dd.Client
netTXQuery string
brokerIDTag string
metricsWindow int
tagCache map[string][]string
keysRegex *regexp.Regexp
redactionSub []byte
c *dd.Client
netTXQuery string
brokerIDTag string
instanceTypeTag string
metricsWindow int
tagCache map[string][]string
keysRegex *regexp.Regexp
redactionSub []byte
}

// NewHandler takes a *Config and
Expand All @@ -57,12 +61,13 @@ func NewHandler(c *Config) (kafkametrics.Handler, error) {
keysRegex := regexp.MustCompile(fmt.Sprintf("%s|%s", c.APIKey, c.AppKey))

h := &ddHandler{
netTXQuery: createNetTXQuery(c),
metricsWindow: c.MetricsWindow,
brokerIDTag: c.BrokerIDTag,
tagCache: make(map[string][]string),
keysRegex: keysRegex,
redactionSub: []byte("xxx"),
netTXQuery: createNetTXQuery(c),
metricsWindow: c.MetricsWindow,
brokerIDTag: c.BrokerIDTag,
instanceTypeTag: c.InstanceTypeTag,
tagCache: make(map[string][]string),
keysRegex: keysRegex,
redactionSub: []byte("xxx"),
}

client := dd.NewClient(c.APIKey, c.AppKey)
Expand Down
4 changes: 2 additions & 2 deletions kafkametrics/datadog/datadog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestPopulateFromTagMap(t *testing.T) {

// Test with complete input.
tagMap := mockTagMap()
err := populateFromTagMap(b, map[string][]string{}, tagMap, "broker_id")
err := populateFromTagMap(b, map[string][]string{}, tagMap, "broker_id", "instance-type")
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
Expand All @@ -119,7 +119,7 @@ func TestPopulateFromTagMap(t *testing.T) {

// Test with incomplete input.
tagMap[rndBroker] = tagMap[rndBroker][1:]
err = populateFromTagMap(b, map[string][]string{}, tagMap, "broker_id")
err = populateFromTagMap(b, map[string][]string{}, tagMap, "broker_id", "instance-type")
if err == nil {
t.Errorf("Expected error, got nil")
}
Expand Down
7 changes: 4 additions & 3 deletions kafkametrics/datadog/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (h *ddHandler) brokerMetricsFromList(l []*kafkametrics.Broker) (kafkametric
}

brokers := kafkametrics.BrokerMetrics{}
errs = populateFromTagMap(brokers, h.tagCache, tags, h.brokerIDTag)
errs = populateFromTagMap(brokers, h.tagCache, tags, h.brokerIDTag, h.instanceTypeTag)
if errs != nil {
errs = append(errors, errs...)
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (h *ddHandler) getHostTagMap(l []*kafkametrics.Broker) (map[*kafkametrics.B
// to []string unparsed host tag key:value pairs, and a broker ID tag key
// populates the kafkametrics.BrokerMetrics with tags of interest.
// An error describing any missing tags is returned.
func populateFromTagMap(bm kafkametrics.BrokerMetrics, c map[string][]string, t map[*kafkametrics.Broker][]string, btag string) []error {
func populateFromTagMap(bm kafkametrics.BrokerMetrics, c map[string][]string, t map[*kafkametrics.Broker][]string, btag string, itag string) []error {
var missingTags bytes.Buffer

for b, ht := range t {
Expand All @@ -133,7 +133,7 @@ func populateFromTagMap(bm kafkametrics.BrokerMetrics, c map[string][]string, t
}

// Get instance type.
it = valFromTags(ht, "instance-type")
it = valFromTags(ht, itag)
if it != "" {
// Cache this broker's tags. In case additional tags are populated
// in the future, we should only cache brokers that have
Expand All @@ -153,6 +153,7 @@ func populateFromTagMap(bm kafkametrics.BrokerMetrics, c map[string][]string, t
b.ID = id
b.InstanceType = it
bm[id] = b

}

if missingTags.String() != "" {
Expand Down