// specifyInstanceOfDynamicMetric returns specified namespace of incoming cataloged metric's namespace // based on requested metric namespace func specifyInstanceOfDynamicMetric(catalogedNamespace core.Namespace, requestedNamespace core.Namespace) core.Namespace { specifiedNamespace := make(core.Namespace, len(catalogedNamespace)) copy(specifiedNamespace, catalogedNamespace) _, indexes := catalogedNamespace.IsDynamic() for _, index := range indexes { if len(requestedNamespace) > index { // use namespace's element of requested metric declared in task manifest // to specify a dynamic instance of the cataloged metric specifiedNamespace[index].Value = requestedNamespace[index].Value } } return specifiedNamespace }
// GetMetric retrieves a metric for a given requested namespace and version. // If provided a version of -1 the latest plugin will be returned. func (mc *metricCatalog) GetMetric(requested core.Namespace, version int) (*metricType, error) { mc.mutex.Lock() defer mc.mutex.Unlock() var ns core.Namespace catalogedmt, err := mc.tree.GetMetric(requested.Strings(), version) if err != nil { log.WithFields(log.Fields{ "_module": "control", "_file": "metrics.go,", "_block": "get-metric", "error": err, }).Error("error getting metric") return nil, err } ns = catalogedmt.Namespace() if isDynamic, _ := ns.IsDynamic(); isDynamic { // when namespace is dynamic and the cataloged namespace (e.g. ns=/intel/mock/*/bar) is different than // the requested (e.g. requested=/intel/mock/host0/bar), than specify an instance of dynamic element, // so as a result the dynamic element will have set a value (e.g. ns[2].Value equals "host0") if ns.String() != requested.String() { ns = specifyInstanceOfDynamicMetric(ns, requested) } } returnedmt := &metricType{ Plugin: catalogedmt.Plugin, namespace: ns, version: catalogedmt.Version(), lastAdvertisedTime: catalogedmt.LastAdvertisedTime(), tags: catalogedmt.Tags(), policy: catalogedmt.Plugin.ConfigPolicy.Get(catalogedmt.Namespace().Strings()), config: catalogedmt.Config(), unit: catalogedmt.Unit(), description: catalogedmt.Description(), subscriptions: catalogedmt.SubscriptionCount(), } return returnedmt, nil }