def test_multiple_stream(): sys.stdout = ThreadPrinter() sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}'] col = MockStreamCollector("MyStreamCollector", 99) col.start() t_end = time.time() + 5 # wait for our collector to print its preamble while len(sys.stdout.lines) == 0 and time.time() < t_end: time.sleep(.1) resp = json.loads(sys.stdout.lines[0]) client = StreamCollectorStub( grpc.insecure_channel(resp["ListenAddress"])) metric = snap.Metric( namespace=[snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="int")], version=1, unit="some unit", description="some description", config={"send_multiple": True} ) mtr = iter([CollectArg(metric).pb]) metrics = client.StreamMetrics(mtr) a = next(metrics) assert len(a.Metrics_Reply.metrics) == 3 col.stop()
def update_catalog(self, config): LOG.debug("GetMetricTypes called") while self.init_lock: if not self.oslo_listener: LOG.debug("Starting oslo.messaging listener") self.oslo_listener = messaging.get_notification_listener( messaging.get_transport( cfg.CONF, config["transport_url"]), [messaging.Target(topic='notifications')], [NotificationHandler()], 'threading', allow_requeue=True) self.oslo_listener.start() metrics = [] for key in ("cpu_cstate", "io_bandwith", "memory_bandwith"): metric = snap.Metric( namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="node_manager"), snap.NamespaceElement(value="cups"), snap.NamespaceElement(value=key) ], version=1, tags={"mtype": "gauge"}, description="CUPS {}".format(key.replace('_', ' ')), ) metric.namespace.add_dynamic_element("uuid", "node UUID") metrics.append(metric) return metrics
def test_process(processor_client): now = time.time() metrics = [ snap.Metric( namespace=[ snap.NamespaceElement(value="org"), snap.NamespaceElement(value="metric"), snap.NamespaceElement(value="foo") ], version=1, unit="some unit", description="some description", timestamp=now, ) ] config = snap.ConfigMap(foo="bar") reply = processor_client.Process( _ProcessArg(metrics=metrics, config=config).pb) assert reply.error == "" assert len(reply.metrics) == 1 for m in reply.metrics: assert "processed" in m.Tags assert m.Tags["processed"] == "true" assert "foo" in m.Tags assert m.Tags["foo"] == "bar"
def test_stream_max_metrics_buffer(): sys.stdout = ThreadPrinter() sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}'] col = MockStreamCollector("MyStreamCollector", 99) col.start() t_end = time.time() + 5 # wait for our collector to print its preamble while len(sys.stdout.lines) == 0 and time.time() < t_end: time.sleep(.1) resp = json.loads(sys.stdout.lines[0]) client = StreamCollectorStub( grpc.insecure_channel(resp["ListenAddress"])) metric = snap.Metric( namespace=[snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="int")], version=1, config={"max-metrics-buffer": 5}, unit="some unit", description="some description") col_arg = CollectArg(metric).pb mtr = iter([col_arg]) metrics = client.StreamMetrics(mtr) start_waiting_for_new_metric = time.time() a = next(metrics) retrieve_metric_time = time.time() assert round(retrieve_metric_time - start_waiting_for_new_metric) == 5 assert len(a.Metrics_Reply.metrics) == 5 col.stop()
def update_catalog(self, config): LOG.debug("GetMetricTypes called") metrics = [] for key in ("float64", "int64", "string"): metric = snap.Metric( namespace=[ snap.NamespaceElement(value="random"), snap.NamespaceElement(value=key) ], version=1, tags={"mtype": "gauge"}, description="Random {}".format(key), ) metrics.append(metric) metric = snap.Metric(version=1, Description="dynamic element example") # adds namespace elements (static and dynamic) via namespace methods metric.namespace.add_static_element("random") metric.namespace.add_dynamic_element("pid", "current pid") metric.namespace.add_static_element("uid") metrics.append(metric) # metric is added with the namespace defined in the constructor metric = snap.Metric(namespace=[ snap.NamespaceElement(value="random"), snap.NamespaceElement(name="pid", description="current pid"), snap.NamespaceElement(value="gid") ], description="dynamic element example", version=1) metrics.append(metric) return metrics
def update_catalog(self, config): metrics = [ snap.Metric( namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="int") ], unit="some unit", description="some description", ) ] return metrics
def stream(self, metrics): LOG.debug("Metrics collection") metrics_to_stream = [] metric = snap.Metric(namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="int") ], version=1, tags={"mtype": "counter"}, description="Random int", data=random.randint(1, 100), timestamp=time.time()) metrics_to_stream.append(metric) metric = snap.Metric(namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="float") ], version=1, tags={"mtype": "counter"}, description="Random float", data=random.random(), timestamp=time.time()) metrics_to_stream.append(metric) time.sleep(1) return metrics_to_stream
def update_catalog(self, config): LOG.debug("GetMetricTypes called") metrics = [] for key in ("float64", "int64", "string"): metric = snap.Metric( namespace=[ snap.NamespaceElement(value="random"), snap.NamespaceElement(value=key) ], version=1, tags={"mtype": "gauge"}, Description="Random {}".format(key), ) metrics.append(metric) return metrics
def update_catalog(self, config): now = time.time() metrics = [ snap.Metric( namespace=[ snap.NamespaceElement(value="acme"), snap.NamespaceElement(value="sk8"), snap.NamespaceElement(value="matix") ], unit="some unit", description="some description", timestamp=now, ) ] return metrics
def stream(self, requested_metrics): delay = 1 if "stream_delay" in requested_metrics[0].config: delay = requested_metrics[0].config["stream_delay"] now = time.time() metric = snap.Metric(namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="streaming"), snap.NamespaceElement(value="random"), snap.NamespaceElement(value="int") ], version=1, tags={"mtype": "counter"}, description="some description", timestamp=now, data=200) time.sleep(delay) return metric
def test_publish(publisher_client): now = time.time() metrics = [ snap.Metric( namespace=[ snap.NamespaceElement(value="org"), snap.NamespaceElement(value="metric"), snap.NamespaceElement(value="foo") ], version=1, unit="some unit", description="some description", timestamp=now, ) ] config = snap.ConfigMap(foo="bar", port=911, debug=True, availability=99.9) reply = publisher_client.Publish( _PublishArg(metrics=metrics, config=config).pb) assert reply.error == ""
def update_catalog(self, config): LOG.debug("GetMetricTypes called") metrics = [] keys = ("float64", "int64", "string") self._args.required_config = False #print("self._args.required_config", self._args.required_config) #print("snap.Publisher ") for key in ("float", "int"): metric = snap.Metric( namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="logs"), snap.NamespaceElement(value="*") ], data="str", version=1, tags={"mtype": "gauge"}, description="http publisher {}".format(key), ) metrics.append(metric) return metrics
def test_ovs_collect(self, test_topology): plugin = Ovs("ovs", 1) metrics = plugin.collect([ snap.Metric(namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="ovs"), snap.NamespaceElement(value="*"), snap.NamespaceElement(value="tx_bytes") ]), snap.Metric(namespace=[ snap.NamespaceElement(value="intel"), snap.NamespaceElement(value="ovs"), snap.NamespaceElement(value="*"), snap.NamespaceElement(value="rx_bytes") ]), ]) assert len(metrics) > 0
def test_collect(collector_client): now = time.time() metric = snap.Metric( namespace=[snap.NamespaceElement(value="org"), snap.NamespaceElement(value="metric"), snap.NamespaceElement(value="foo")], version=1, config={"foo": "bar"}, unit="some unit", description="some description", timestamp=now) reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert len(reply.metrics) == 1 assert reply.metrics[0].Version == 2 assert reply.metrics[0].float64_data == 99.9 assert snap.Metric(pb=reply.metrics[0]).data == 99.9 reply2 = collector_client.CollectMetrics(MetricsArg(metric).pb) assert (reply2.metrics[0].Timestamp.sec + (reply2.metrics[0].Timestamp.nsec * 10 ** -9)) > ( reply.metrics[0].Timestamp.sec + (reply.metrics[0].Timestamp.nsec * 10 ** -9)) # collect bytes metric.config.clear() metric.config["bytes"] = True reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert snap.Metric(pb=reply.metrics[0]).data == 'qwerty' # collect string metric.config.clear() metric.config["string"] = True reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert snap.Metric(pb=reply.metrics[0]).data == "qwerty" assert isinstance(snap.Metric(pb=reply.metrics[0]).data, basestring) # collect int metric.config.clear() metric.config["int32"] = True reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert snap.Metric(pb=reply.metrics[0]).data == 99 assert isinstance(snap.Metric(pb=reply.metrics[0]).data, bigint) # collect int64 (long) metric.config.clear() metric.config["int64"] = True reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert snap.Metric(pb=reply.metrics[0]).data == 99 assert isinstance(snap.Metric(pb=reply.metrics[0]).data, bigint) # collect int metric.config.clear() metric.config["bool"] = True reply = collector_client.CollectMetrics(MetricsArg(metric).pb) assert reply.error == '' assert bool(snap.Metric(pb=reply.metrics[0]).data) is True