Beispiel #1
0
    def test_summary(self):
        """Test that we can track summaries in Service303"""
        # Add a summary with a label to the regisry
        c = Summary('process_max_fds', 'A summary', ['result'], registry=self.registry)
        c.labels('success').observe(1.23)
        c.labels('failure').observe(2.34)

        # Build proto outputs
        summary1 = metrics_pb2.Summary(sample_count=1, sample_sum=1.23)
        summary2 = metrics_pb2.Summary(sample_count=1, sample_sum=2.34)
        metric1 = metrics_pb2.Metric(summary=summary1,
                                     timestamp_ms=1234000)
        metric2 = metrics_pb2.Metric(summary=summary2,
                                     timestamp_ms=1234000)
        family = metrics_pb2.MetricFamily(
            name=str(metricsd_pb2.process_max_fds),
            type=metrics_pb2.SUMMARY)
        metric1.label.add(
            name=str(metricsd_pb2.result),
            value='success')
        metric2.label.add(
            name=str(metricsd_pb2.result),
            value='failure')
        family.metric.extend([metric1, metric2])

        with unittest.mock.patch('time.time') as mock_time:
            mock_time.side_effect = lambda: 1234
            self.assertCountEqual(list(metrics_export.get_metrics(self.registry))[0].metric,
                             family.metric)
Beispiel #2
0
def process_metrics(client: CloudMetricsControllerStub, host_name: str,
                    service_name: str):
    """
    Get service metrics from the registry and push them to metricsd.

    Args:
        client (CloudMetricsControllerStub): metricsd client instance
        host_name (str): source host name
        service_name (str): source service name

    """
    logger.info(f"Processing Metrics for {service_name}")
    try:
        metric_families = list(get_metrics(prometheus_client.REGISTRY))
        if not metric_families:
            return
        container = RawMetricsContainer(
            hostName=host_name,
            families=metric_families,
            service=service_name,
        )
        client.PushRaw(
            request=container,
            metadata=((config.MAGMA_CLIENT_CERT_SERIAL_KEY,
                       config.MAGMA_CLIENT_CERT_SERIAL_VALUE), ),
        )
    except Exception as e:
        logger.error(f"Failed processing metrics for {service_name}: {e}")
Beispiel #3
0
    def test_gauge(self):
        """Test that we can track gauges in Service303"""
        # Add a gauge with a label to the regisry
        c = Gauge('process_max_fds', 'A gauge', ['result'],
                  registry=self.registry)

        # Create two series for value1 and value2
        c.labels('success').inc(1.23)
        c.labels('failure').inc(2.34)

        # Build proto outputs
        gauge1 = metrics_pb2.Gauge(value=1.23)
        gauge2 = metrics_pb2.Gauge(value=2.34)
        metric1 = metrics_pb2.Metric(gauge=gauge1,
                                     timestamp_ms=1234000)
        metric2 = metrics_pb2.Metric(gauge=gauge2,
                                     timestamp_ms=1234000)
        family = metrics_pb2.MetricFamily(
            name=str(metricsd_pb2.process_max_fds),
            type=metrics_pb2.GAUGE)
        metric1.label.add(
            name=str(metricsd_pb2.result),
            value='success')
        metric2.label.add(
            name=str(metricsd_pb2.result),
            value='failure')
        family.metric.extend([metric1, metric2])

        with unittest.mock.patch('time.time') as mock_time:
            mock_time.side_effect = lambda: 1234
            self.assertCountEqual(list(metrics_export.get_metrics(self.registry))[0].metric,
                             family.metric)
Beispiel #4
0
    def test_histogram(self):
        """Test that we can track histogram in Service303"""
        # Add a histogram with a label to the regisry
        c = Histogram('process_max_fds',
                      'A summary', ['result'],
                      registry=self.registry,
                      buckets=[0, 2, float('inf')])
        c.labels('success').observe(1.23)
        c.labels('failure').observe(2.34)

        # Build proto outputs
        histogram1 = metrics_pb2.Histogram(sample_count=1, sample_sum=1.23)
        histogram1.bucket.add(upper_bound=0, cumulative_count=0)
        histogram1.bucket.add(upper_bound=2, cumulative_count=1)
        histogram1.bucket.add(upper_bound=float('inf'), cumulative_count=1)
        histogram2 = metrics_pb2.Histogram(sample_count=1, sample_sum=2.34)
        histogram2.bucket.add(upper_bound=0, cumulative_count=0)
        histogram2.bucket.add(upper_bound=2, cumulative_count=0)
        histogram2.bucket.add(upper_bound=float('inf'), cumulative_count=1)
        metric1 = metrics_pb2.Metric(histogram=histogram1,
                                     timestamp_ms=1234000)
        metric2 = metrics_pb2.Metric(histogram=histogram2,
                                     timestamp_ms=1234000)
        family = metrics_pb2.MetricFamily(name=str(
            metricsd_pb2.process_max_fds),
                                          type=metrics_pb2.HISTOGRAM)
        metric1.label.add(name=str(metricsd_pb2.result), value='success')
        metric2.label.add(name=str(metricsd_pb2.result), value='failure')
        family.metric.extend([metric1, metric2])

        with unittest.mock.patch('time.time') as mock_time:
            mock_time.side_effect = lambda: 1234
            self.assertCountEqual(
                list(metrics_export.get_metrics(self.registry))[0].metric,
                family.metric)
Beispiel #5
0
 def GetMetrics(self, request, context):
     """
     Collects timeseries samples from prometheus python client on this
     process
     """
     metrics = MetricsContainer()
     metrics.family.extend(get_metrics())
     return metrics
Beispiel #6
0
    def test_metrics_defined(self):
        """ Test that all metrics are defined in proto enum """
        SUBSCRIBER_ICMP_LATENCY_MS.labels('IMSI00000001').observe(10.33)

        metrics_protos = list(metrics_export.get_metrics())
        for metrics_proto in metrics_protos:
            if metrics_proto.name == "subscriber_latency_ms":
                metric = metrics_proto.metric[0]
                self.assertEqual(metric.histogram.sample_sum, 10.33)
                self.assertEqual(metric.label[0].value, 'IMSI00000001')
Beispiel #7
0
    def test_metrics_defined(self):
        """ Test that all metrics are defined in proto enum """
        import magma.mobilityd.metrics
        # Avoid lint error about unused imports
        magma.mobilityd.metrics.IP_ALLOCATED_TOTAL.inc(1)

        metrics_protos = metrics_export.get_metrics()
        for metrics_proto in metrics_protos:
            # Check that all proto names have been mapped to numbers. Will
            # raise ValueError if not
            int(metrics_proto.name)
Beispiel #8
0
    def test_metrics_defined(self):
        """ Test that all metrics are defined in proto enum """
        import magma.enodebd.metrics
        # Avoid lint error about unused imports
        magma.enodebd.metrics.STAT_ENODEB_CONNECTED.set(1)

        metrics_protos = metrics_export.get_metrics()
        for metrics_proto in metrics_protos:
            # Check that all proto names have been mapped to numbers. Will
            # raise ValueError if not
            int(metrics_proto.name)
Beispiel #9
0
    def test_metrics_defined(self):
        """ Test that all metrics are defined in proto enum """
        import magma.subscriberdb.metrics
        # Avoid lint error about unused imports
        magma.subscriberdb.metrics.S6A_AUTH_SUCCESS_TOTAL.inc(1)

        metrics_protos = metrics_export.get_metrics()
        for metrics_proto in metrics_protos:
            # Check that all proto names have been mapped to numbers. Will
            # raise ValueError if not
            int(metrics_proto.name)
Beispiel #10
0
    def test_converted_enums(self):
        """ Test that metric names and labels are auto converted """
        # enum values (from metricsd.proto):
        # mme_new_association => 500, result => 0
        c = Counter('mme_new_association', 'A counter', ['result'],
                    registry=self.registry)

        c.labels('success').inc(1.23)

        metric_family = list(metrics_export.get_metrics(self.registry))[0]

        self.assertEqual(metric_family.name,
                         str(metricsd_pb2.mme_new_association))
        metric_labels = metric_family.metric[0].label
        # Order not guaranteed=
        self.assertEqual(metric_labels[0].name, str(metricsd_pb2.result))
        self.assertEqual(metric_labels[0].value, 'success')
Beispiel #11
0
    def test_counter(self):
        """Test that we can track counters in Service303"""
        # Add a counter with a label to the regisry
        process_max_metric_name = 'process_max_fds'
        c = Counter(
            process_max_metric_name,
            'A counter',
            ['result'],
            registry=self.registry,
        )

        # Create two series for value1 and value2
        c.labels('success').inc(1.23)
        c.labels('failure').inc(2.34)

        # Build proto outputs
        counter1 = metrics_pb2.Counter(value=1.23)
        counter2 = metrics_pb2.Counter(value=2.34)
        metric1 = metrics_pb2.Metric(
            counter=counter1,
            timestamp_ms=1234000,
        )
        metric2 = metrics_pb2.Metric(
            counter=counter2,
            timestamp_ms=1234000,
        )
        family = metrics_pb2.MetricFamily(
            name=process_max_metric_name,
            type=metrics_pb2.COUNTER,
        )
        metric1.label.add(
            name='result',
            value='success',
        )
        metric2.label.add(
            name='result',
            value='failure',
        )
        family.metric.extend([metric1, metric2])

        with unittest.mock.patch('time.time') as mock_time:
            mock_time.side_effect = lambda: 1234
            self.assertCountEqual(
                list(metrics_export.get_metrics(self.registry))[0].metric,
                family.metric,
            )
Beispiel #12
0
    def test_converted_enums(self):
        """ Test that metric names and labels are auto converted """
        mme_metric_name = 'mme_new_association'
        c = Counter(
            mme_metric_name,
            'A counter',
            ['result'],
            registry=self.registry,
        )

        c.labels('success').inc(1.23)

        metric_family = list(metrics_export.get_metrics(self.registry))[0]

        self.assertEqual(
            metric_family.name,
            mme_metric_name,
        )
        metric_labels = metric_family.metric[0].label
        # Order not guaranteed=
        self.assertEqual(metric_labels[0].name, 'result')
        self.assertEqual(metric_labels[0].value, 'success')