func()

in strategy/sampling/centralized.go [245:343]


func (ss *CentralizedStrategy) refreshManifest() (err error) {
	// Explicitly recover from panics since this is the entry point for a long-running goroutine
	// and we can not allow a panic to propagate to the application code.
	defer func() {
		if r := recover(); r != nil {
			// Resort to bring rules array into consistent state.
			ss.manifest.sort()

			err = fmt.Errorf("%v", r)
		}
	}()

	// Compute 'now' before calling GetSamplingRules to avoid marking manifest as
	// fresher than it actually is.
	now := ss.clock.Now().Unix()

	// Get sampling rules from proxy
	records, err := ss.proxy.GetSamplingRules()
	if err != nil {
		return
	}

	// Set of rules to exclude from pruning
	actives := map[*CentralizedRule]bool{}

	// Create missing rules. Update existing ones.
	failed := false
	for _, record := range records {
		// Extract rule from record
		svcRule := record.SamplingRule
		if svcRule == nil {
			logger.Debug("Sampling rule missing from sampling rule record.")
			failed = true
			continue
		}

		if svcRule.RuleName == nil {
			logger.Debug("Sampling rule without rule name is not supported")
			failed = true
			continue
		}

		// Only sampling rule with version 1 is valid
		if svcRule.Version == nil {
			logger.Debug("Sampling rule without version number is not supported: ", *svcRule.RuleName)
			failed = true
			continue
		}
		version := *svcRule.Version
		if version != int64(1) {
			logger.Debug("Sampling rule without version 1 is not supported: ", *svcRule.RuleName)
			failed = true
			continue
		}

		if len(svcRule.Attributes) != 0 {
			logger.Debug("Sampling rule with non nil Attributes is not applicable: ", *svcRule.RuleName)
			continue
		}

		if svcRule.ResourceARN == nil {
			logger.Debug("Sampling rule without ResourceARN is not applicable: ", *svcRule.RuleName)
			continue
		}

		resourceARN := *svcRule.ResourceARN
		if resourceARN != "*" {
			logger.Debug("Sampling rule with ResourceARN not equal to * is not applicable: ", *svcRule.RuleName)
			continue
		}

		// Create/update rule
		r, putErr := ss.manifest.putRule(svcRule)
		if putErr != nil {
			failed = true
			logger.Debugf("Error occurred creating/updating rule. %v", putErr)
		} else if r != nil {
			actives[r] = true
		}
	}

	// Set err if updates failed
	if failed {
		err = errors.New("error occurred creating/updating rules")
	}

	// Prune inactive rules
	ss.manifest.prune(actives)

	// Re-sort to fix matching priorities
	ss.manifest.sort()

	// Update refreshedAt timestamp
	ss.manifest.mu.Lock()
	ss.manifest.refreshedAt = now
	ss.manifest.mu.Unlock()

	return
}