2016-09-02 22:05:09 +00:00
|
|
|
// Copyright 2016 Circonus, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
package checkmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/circonus-labs/circonus-gometrics/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsMetricActive checks whether a given metric name is currently active(enabled)
|
|
|
|
func (cm *CheckManager) IsMetricActive(name string) bool {
|
|
|
|
active, _ := cm.availableMetrics[name]
|
|
|
|
return active
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateMetric determines if a given metric should be activated
|
|
|
|
func (cm *CheckManager) ActivateMetric(name string) bool {
|
|
|
|
active, exists := cm.availableMetrics[name]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !active && cm.forceMetricActivation {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
// AddMetricTags updates check bundle metrics with tags
|
|
|
|
func (cm *CheckManager) AddMetricTags(metricName string, tags []string, appendTags bool) bool {
|
|
|
|
tagsUpdated := false
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
if appendTags && len(tags) == 0 {
|
2016-11-02 19:34:30 +00:00
|
|
|
return tagsUpdated
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
currentTags, exists := cm.metricTags[metricName]
|
|
|
|
if !exists {
|
2016-11-10 21:17:55 +00:00
|
|
|
foundMetric := false
|
2016-11-02 19:34:30 +00:00
|
|
|
|
2016-11-10 21:17:55 +00:00
|
|
|
for _, metric := range cm.checkBundle.Metrics {
|
|
|
|
if metric.Name == metricName {
|
|
|
|
foundMetric = true
|
2017-01-04 21:47:38 +00:00
|
|
|
currentTags = metric.Tags
|
2016-11-10 21:17:55 +00:00
|
|
|
break
|
2016-11-02 19:34:30 +00:00
|
|
|
}
|
2016-11-10 21:17:55 +00:00
|
|
|
}
|
2016-11-02 19:34:30 +00:00
|
|
|
|
2016-11-10 21:17:55 +00:00
|
|
|
if !foundMetric {
|
2017-01-04 21:47:38 +00:00
|
|
|
currentTags = []string{}
|
2016-11-02 19:34:30 +00:00
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
action := ""
|
2016-11-10 21:17:55 +00:00
|
|
|
if appendTags {
|
2017-01-04 21:47:38 +00:00
|
|
|
numNewTags := countNewTags(currentTags, tags)
|
2016-11-10 21:17:55 +00:00
|
|
|
if numNewTags > 0 {
|
|
|
|
action = "Added"
|
2017-01-04 21:47:38 +00:00
|
|
|
currentTags = append(currentTags, tags...)
|
2016-11-10 21:17:55 +00:00
|
|
|
tagsUpdated = true
|
2016-11-02 19:34:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-01-04 21:47:38 +00:00
|
|
|
if len(tags) != len(currentTags) {
|
|
|
|
action = "Set"
|
|
|
|
currentTags = tags
|
|
|
|
tagsUpdated = true
|
|
|
|
} else {
|
|
|
|
numNewTags := countNewTags(currentTags, tags)
|
|
|
|
if numNewTags > 0 {
|
|
|
|
action = "Set"
|
|
|
|
currentTags = tags
|
|
|
|
tagsUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagsUpdated {
|
|
|
|
cm.metricTags[metricName] = currentTags
|
2016-11-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
if cm.Debug && action != "" {
|
2016-11-10 21:17:55 +00:00
|
|
|
cm.Log.Printf("[DEBUG] %s metric tag(s) %s %v\n", action, metricName, tags)
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
return tagsUpdated
|
|
|
|
}
|
|
|
|
|
|
|
|
// addNewMetrics updates a check bundle with new metrics
|
|
|
|
func (cm *CheckManager) addNewMetrics(newMetrics map[string]*api.CheckBundleMetric) bool {
|
|
|
|
updatedCheckBundle := false
|
|
|
|
|
|
|
|
if cm.checkBundle == nil || len(newMetrics) == 0 {
|
|
|
|
return updatedCheckBundle
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.cbmu.Lock()
|
2016-11-10 21:17:55 +00:00
|
|
|
defer cm.cbmu.Unlock()
|
2016-11-02 19:34:30 +00:00
|
|
|
|
|
|
|
numCurrMetrics := len(cm.checkBundle.Metrics)
|
2016-07-22 19:49:23 +00:00
|
|
|
numNewMetrics := len(newMetrics)
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
if numCurrMetrics+numNewMetrics >= cap(cm.checkBundle.Metrics) {
|
2016-07-22 19:49:23 +00:00
|
|
|
nm := make([]api.CheckBundleMetric, numCurrMetrics+numNewMetrics)
|
2016-11-02 19:34:30 +00:00
|
|
|
copy(nm, cm.checkBundle.Metrics)
|
|
|
|
cm.checkBundle.Metrics = nm
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
cm.checkBundle.Metrics = cm.checkBundle.Metrics[0 : numCurrMetrics+numNewMetrics]
|
2016-07-22 19:49:23 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, metric := range newMetrics {
|
2016-11-02 19:34:30 +00:00
|
|
|
cm.checkBundle.Metrics[numCurrMetrics+i] = *metric
|
2016-07-22 19:49:23 +00:00
|
|
|
i++
|
2016-11-02 19:34:30 +00:00
|
|
|
updatedCheckBundle = true
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
if updatedCheckBundle {
|
|
|
|
cm.forceCheckUpdate = true
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:34:30 +00:00
|
|
|
return updatedCheckBundle
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// inventoryMetrics creates list of active metrics in check bundle
|
|
|
|
func (cm *CheckManager) inventoryMetrics() {
|
|
|
|
availableMetrics := make(map[string]bool)
|
|
|
|
for _, metric := range cm.checkBundle.Metrics {
|
|
|
|
availableMetrics[metric.Name] = metric.Status == "active"
|
|
|
|
}
|
|
|
|
cm.availableMetrics = availableMetrics
|
|
|
|
}
|
2016-11-02 19:34:30 +00:00
|
|
|
|
|
|
|
// countNewTags returns a count of new tags which do not exist in the current list of tags
|
|
|
|
func countNewTags(currTags []string, newTags []string) int {
|
|
|
|
if len(newTags) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currTags) == 0 {
|
|
|
|
return len(newTags)
|
|
|
|
}
|
|
|
|
|
|
|
|
newTagCount := 0
|
|
|
|
|
|
|
|
for _, newTag := range newTags {
|
|
|
|
found := false
|
|
|
|
for _, currTag := range currTags {
|
|
|
|
if newTag == currTag {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
newTagCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newTagCount
|
|
|
|
}
|