checkmgr/check.go
func (cm *CheckManager) UpdateCheck(newMetrics map[string]*api.CheckBundleMetric) {
	// only if check manager is enabled
	if !cm.enabled {
		return
	}

	// only if checkBundle has been populated
	if cm.checkBundle == nil {
		return
	}

	// only if there is *something* to update
	if !cm.forceCheckUpdate && len(newMetrics) == 0 && len(cm.metricTags) == 0 {
		return
	}

	// refresh check bundle (in case there were changes made by other apps or in UI)
	cid := cm.checkBundle.CID
	checkBundle, err := cm.apih.FetchCheckBundle(api.CIDType(&cid))
	if err != nil {
		cm.Log.Printf("[ERROR] unable to fetch up-to-date check bundle %v", err)
		return
	}
	cm.cbmu.Lock()
	cm.checkBundle = checkBundle
	cm.cbmu.Unlock()

	// check metric_limit and see if it’s 0, if so, don't even bother to try to update the check.

	cm.addNewMetrics(newMetrics)

	if len(cm.metricTags) > 0 {
		// note: if a tag has been added (queued) for a metric which never gets sent
		//       the tags will be discarded. (setting tags does not *create* metrics.)
		for metricName, metricTags := range cm.metricTags {
			for metricIdx, metric := range cm.checkBundle.Metrics {
				if metric.Name == metricName {
					cm.checkBundle.Metrics[metricIdx].Tags = metricTags
					break
				}
			}
			cm.mtmu.Lock()
			delete(cm.metricTags, metricName)
			cm.mtmu.Unlock()
		}
		cm.forceCheckUpdate = true
	}

	if cm.forceCheckUpdate {
		newCheckBundle, err := cm.apih.UpdateCheckBundle(cm.checkBundle)
		if err != nil {
			cm.Log.Printf("[ERROR] updating check bundle %v", err)
			return
		}

		cm.forceCheckUpdate = false
		cm.cbmu.Lock()
		cm.checkBundle = newCheckBundle
		cm.cbmu.Unlock()
		cm.inventoryMetrics()
	}

}
func (cm *CheckManager) UpdateCheck(newMetrics map[string]*api.CheckBundleMetric) {
	// only if check manager is enabled
	if !cm.enabled {
		return
	}

	// only if checkBundle has been populated
	if cm.checkBundle == nil {
		return
	}

	// only if there is *something* to update
	if !cm.forceCheckUpdate && len(newMetrics) == 0 && len(cm.metricTags) == 0 {
		return
	}

	// refresh check bundle (in case there were changes made by other apps or in UI)
	cid := cm.checkBundle.CID
	checkBundle, err := cm.apih.FetchCheckBundle(api.CIDType(&cid))
	if err != nil {
		cm.Log.Printf("[ERROR] unable to fetch up-to-date check bundle %v", err)
		return
	}
	cm.cbmu.Lock()
	cm.checkBundle = checkBundle
	cm.cbmu.Unlock()

	// check metric_limit and see if it’s 0, if so, don't even bother to try to update the check.

	cm.addNewMetrics(newMetrics)

	if len(cm.metricTags) > 0 {
		// note: if a tag has been added (queued) for a metric which never gets sent
		//       the tags will be discarded. (setting tags does not *create* metrics.)
		for metricName, metricTags := range cm.metricTags {
			for metricIdx, metric := range cm.checkBundle.Metrics {
				if metric.Name == metricName {
					cm.checkBundle.Metrics[metricIdx].Tags = metricTags
					break
				}
			}
			cm.mtmu.Lock()
			delete(cm.metricTags, metricName)
			cm.mtmu.Unlock()
		}
		cm.forceCheckUpdate = true
	}

	if cm.forceCheckUpdate {
		newCheckBundle, err := cm.apih.UpdateCheckBundle(cm.checkBundle)
		if err != nil {
			cm.Log.Printf("[ERROR] updating check bundle %v", err)
			return
		}

		cm.forceCheckUpdate = false
		cm.cbmu.Lock()
		cm.checkBundle = newCheckBundle
		cm.cbmu.Unlock()
		cm.inventoryMetrics()
	}

}
func ( cm * CheckManager ) UpdateCheck ( newMetrics map [ string ] * api . CheckBundleMetric ) { // only if check manager is enabled if ! cm . enabled { return } // only if checkBundle has been populated if cm . checkBundle == nil { return } // only if there is *something* to update if ! cm . forceCheckUpdate && len ( newMetrics ) == 0 && len ( cm . metricTags ) == 0 { return } // refresh check bundle (in case there were changes made by other apps or in UI) cid := cm . checkBundle . CID checkBundle , err := cm . apih . FetchCheckBundle ( api . CIDType ( & cid ) ) if err != nil { cm . Log . Printf ( " " , err ) return } cm . cbmu . Lock ( ) cm . checkBundle = checkBundle cm . cbmu . Unlock ( ) // check metric_limit and see if it’s 0, if so, don't even bother to try to update the check. cm . addNewMetrics ( newMetrics ) if len ( cm . metricTags ) > 0 { // note: if a tag has been added (queued) for a metric which never gets sent //       the tags will be discarded. (setting tags does not *create* metrics.) for metricName , metricTags := range cm . metricTags { for metricIdx , metric := range cm . checkBundle . Metrics { if metric . Name == metricName { cm . checkBundle . Metrics [ metricIdx ] . Tags = metricTags break } } cm . mtmu . Lock ( ) delete ( cm . metricTags , metricName ) cm . mtmu . Unlock ( ) } cm . forceCheckUpdate = true } if cm . forceCheckUpdate { newCheckBundle , err := cm . apih . UpdateCheckBundle ( cm . checkBundle ) if err != nil { cm . Log . Printf ( " " , err ) return } cm . forceCheckUpdate = false cm . cbmu . Lock ( ) cm . checkBundle = newCheckBundle cm . cbmu . Unlock ( ) cm . inventoryMetrics ( ) } }
// UpdateCheck determines if the check needs to be updated (new metrics, tags, etc.)
UpdateCheck determines if the check needs to be updated ( new metrics tags etc . )