func memStat(ns core.Namespace, swagURL string) (*plugin.MetricType, error) { memType := ns.Strings()[3] switch { case regexp.MustCompile(`^/` + Vendor + `/` + Name + `/memory/free`).MatchString(ns.String()): metric, err := getMemStat(swagURL, memType) if err != nil { return nil, err } return &plugin.MetricType{ Namespace_: ns, Data_: metric, Timestamp_: time.Now(), }, nil case regexp.MustCompile(`^/` + Vendor + `/` + Name + `/memory/total`).MatchString(ns.String()): metric, err := getMemStat(swagURL, memType) if err != nil { return nil, err } return &plugin.MetricType{ Namespace_: ns, Data_: metric, Timestamp_: time.Now(), }, nil } return nil, fmt.Errorf("Unknown error processing %v", ns) }
// validateMetricNamespace validates metric namespace in terms of containing not allowed characters and ending with an asterisk func validateMetricNamespace(ns core.Namespace) error { value := "" for _, i := range ns { // A dynamic element requires the name while a static element does not. if i.Name != "" && i.Value != "*" { return errorMetricStaticElementHasName(i.Value, i.Name, ns.String()) } if i.Name == "" && i.Value == "*" { return errorMetricDynamicElementHasNoName(i.Value, ns.String()) } value += i.Value } for _, chars := range notAllowedChars { for _, ch := range chars { if strings.ContainsAny(value, ch) { return errorMetricContainsNotAllowedChars(ns.String()) } } } // plugin should NOT advertise metrics ending with a wildcard if strings.HasSuffix(value, "*") { return errorMetricEndsWithAsterisk(ns.String()) } return nil }
// validateMetricNamespace validates metric namespace in terms of containing not allowed characters and ending with an asterisk func validateMetricNamespace(ns core.Namespace) error { name := "" for _, i := range ns { name += i.Value } for _, chars := range notAllowedChars { for _, ch := range chars { if strings.ContainsAny(name, ch) { return errorMetricContainsNotAllowedChars(ns.String()) } } } // plugin should NOT advertise metrics ending with a wildcard if strings.HasSuffix(name, "*") { return errorMetricEndsWithAsterisk(ns.String()) } return nil }
// 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 }