func()

in pkg/profiling/continuous/checkers.go [277:348]


func (c *Checkers) queryPolicyUpdates(servicePolicies map[string]string) (map[string]*base.ServicePolicy, error) {
	queries := make([]*profilingv3.ContinuousProfilingServicePolicyQuery, 0)
	for k, v := range servicePolicies {
		queries = append(queries, &profilingv3.ContinuousProfilingServicePolicyQuery{
			ServiceName: k,
			Uuid:        v,
		})
	}
	policyUpdateCommands, err := c.continuousClient.QueryPolicies(c.ctx, &profilingv3.ContinuousProfilingPolicyQuery{Policies: queries})
	if err != nil {
		return nil, err
	}
	// no update
	if len(policyUpdateCommands.GetCommands()) == 0 {
		return nil, nil
	}

	var policyJSON string
	if len(policyUpdateCommands.GetCommands()) == 1 && policyUpdateCommands.GetCommands()[0].GetCommand() == "ContinuousProfilingPolicyQuery" {
		for _, arg := range policyUpdateCommands.GetCommands()[0].GetArgs() {
			if arg.GetKey() == "ServiceWithPolicyJSON" {
				policyJSON = arg.GetValue()
				break
			}
		}
	}
	if policyJSON == "" {
		return nil, fmt.Errorf("the query policy response not adapt")
	}

	updates := make([]*QueryPolicyUpdate, 0)
	err = json.Unmarshal([]byte(policyJSON), &updates)
	if err != nil {
		return nil, fmt.Errorf("error to unmarshal the policy updates: %v", err)
	}

	result := make(map[string]*base.ServicePolicy)
	for _, update := range updates {
		servicePolicy := &base.ServicePolicy{
			Service: update.ServiceName,
			UUID:    update.UUID,
		}
		for profilingType, checks := range update.Profiling {
			policy := &base.Policy{
				TargetProfilingType: profilingType,
				Items:               make(map[base.CheckType]*base.PolicyItem),
				ServicePolicy:       servicePolicy,
			}

			for checkType, item := range checks {
				if err := item.Validate(); err != nil {
					log.Warnf("cannot add the policy item, service name: %s, profiling type: %s, policy type: %s, error: %v",
						update.ServiceName, profilingType, checkType, err)
					continue
				}
				policy.Items[checkType] = &base.PolicyItem{
					Threshold: item.Threshold,
					Period:    item.Period,
					Count:     item.Count,
					URIList:   item.URIList,
					URIRegex:  item.URIRegex,
					Policy:    policy,
				}
			}

			servicePolicy.Policies = append(servicePolicy.Policies, policy)
		}

		result[update.ServiceName] = servicePolicy
	}
	return result, nil
}