def test_metrics_labelged_without_span(self, monkeypatch,
                                           telemetry: TelemetryFixture):
        monkeypatch.setenv('METRICS_LABEL_label1', 'label1_value')
        monkeypatch.setenv('METRICS_LABEL_label2', 'label2_value')
        monkeypatch.setenv('METRICS_ATTRIBUTE_ATTRIB1', 'attrib1_value')
        monkeypatch.setenv('METRICS_ATTRIBUTE_ATTRIB2', 'attrib2_value')

        # test that we include the environment labelger by default
        telemetry.initialize()

        # environment labels should win over any locally-specified labels to preserve ops behavior
        telemetry.counter('category1',
                          'counter1',
                          1,
                          labels={'label1': 'label1_override'})
        telemetry.collect()

        assert telemetry.get_counter('category1.counter1',
                                     labels={
                                         'label1': 'label1_value',
                                         'label2': 'label2_value'
                                     }).value == 1

        Environment._clear()
    def test_span_inheritance(self, telemetry: TelemetryFixture):
        with telemetry.span('test', 'span1', attributes={TestAttributes.ATTRIB1: 'attrib1', TestAttributes.LABEL1: 'label1'}) as span1:
            telemetry.counter('test', 'counter1')

            with telemetry.span('test', 'span2', attributes={TestAttributes.ATTRIB2: 'attrib2', TestAttributes.LABEL2: 'label2'}) as span2:
                telemetry.counter('test', 'counter2')

                with telemetry.span('test', 'span3') as span3:
                    span3.set_label('label3', 'label3')
                    span3.set_attribute('attrib3', 'attrib3')

                    telemetry.counter('test', 'counter3', labels={'counter_label': 'counter_label'})

                    assert span3.attributes == {'attrib1': 'attrib1',
                                                'attrib2': 'attrib2',
                                                'attrib3': 'attrib3',
                                                'label1': 'label1',
                                                'label2': 'label2',
                                                'label3': 'label3',
                                                Attributes.TRACE_ID.name: str(span3.context.trace_id),
                                                Attributes.TRACE_SPAN_ID.name: str(span3.context.span_id),
                                                Attributes.TRACE_IS_REMOTE.name: False,
                                                Attributes.TRACE_CATEGORY.name: 'test',
                                                Attributes.TRACE_NAME.name: 'test.span3'
                                                }

                    assert span3.labels == {Attributes.TRACE_CATEGORY.name: 'test',
                                            Attributes.TRACE_NAME.name: 'test.span3',
                                            'label1': 'label1',
                                            'label2': 'label2',
                                            'label3': 'label3'}

                    assert telemetry.current_span.qname == 'test.span3'
                    assert span3.qname == 'test.span3'

        telemetry.collect()

        assert telemetry.get_counter('test.counter1', labels={'label1': 'label1',
                                                              Attributes.TRACE_CATEGORY.name: 'test',
                                                              Attributes.TRACE_NAME.name: 'test.span1'}).value == 1
        assert telemetry.get_counter('test.counter2', labels={'label1': 'label1',
                                                              'label2': 'label2',
                                                              Attributes.TRACE_CATEGORY.name: 'test',
                                                              Attributes.TRACE_NAME.name: 'test.span2'}).value == 1
        assert telemetry.get_counter('test.counter3', labels={'label1': 'label1',
                                                              'label2': 'label2',
                                                              'label3': 'label3',
                                                              'counter_label': 'counter_label',
                                                              Attributes.TRACE_CATEGORY.name: 'test',
                                                              Attributes.TRACE_NAME.name: 'test.span3'}).value == 1
        assert len(telemetry.get_finished_spans()) == 3
Beispiel #3
0
    def test_counter(self, telemetry: TelemetryFixture):
        telemetry.counter("category1", "counter1", 1)
        telemetry.counter("category1", "counter2", 2)
        telemetry.counter("category1", "counter3", 2)
        telemetry.counter("category1", "counter3", 1)
        telemetry.counter("category1",
                          "counter4",
                          1,
                          labels={'label1': 'label1'})

        telemetry.collect()

        assert telemetry.get_counter('category1.counter1').value == 1
        assert telemetry.get_counter('category1.counter2').value == 2
        assert telemetry.get_counter('category1.counter3').value == 3
        assert telemetry.get_counter('category1.counter4',
                                     labels={
                                         'label1': 'label1'
                                     }).value == 1
Beispiel #4
0
    def test_http_server(self, monkeypatch, telemetry: TelemetryFixture):
        address = 'localhost:19102'
        monkeypatch.setenv('METRICS_EXPORTERS', 'prometheus')
        monkeypatch.setenv('METRICS_PROMETHEUS_PREFIX', 'test_prefix')
        monkeypatch.setenv('METRICS_INTERVAL', '5')
        monkeypatch.setenv('METRICS_PROMETHEUS_BIND_ADDRESS', address)

        telemetry.initialize()

        http = urllib3.PoolManager()

        with telemetry.span("category1",
                            "span1",
                            attributes={
                                TestAttributes.ATTRIB1: "attrib1",
                                TestAttributes.LABEL1: 'label1'
                            }) as span:
            time.sleep(.5)

        telemetry.counter("category1", "counter1", 2.0)

        def gauge(obv: Observer):
            obv.observe(1, {Attributes.ENV: 'test'})

        telemetry.gauge("category1", "gauge", gauge)

        # wait for Prometheus collection interval to pass (METRICS_INTERVAL)
        time.sleep(5)

        telemetry.collect()

        response = http.request('GET', 'http://localhost:19102/metrics')

        def fetch_metric(name: str, labels: dict = {}):
            response = http.request('GET', 'http://localhost:19102/metrics')
            lines = response.data.decode('utf8').split('\n')

            matches = list(
                filter(lambda line: not line.startswith("#") and name in line,
                       lines))
            if len(matches) == 0:
                pytest.fail(f"Metric not found: {name}")
            elif len(matches) == 1:
                return float(matches[0].split(' ')[1])
            else:
                pytest.fail(f"More than one match for metric: {name}")

        assert fetch_metric('test_prefix_trace_duration_count') == 1.0
        assert fetch_metric('test_prefix_trace_duration_sum') >= 500

        assert fetch_metric('test_prefix_category1_gauge') == 1.0

        # double-check that metrics continue to be returned on duplicate fetches
        assert fetch_metric('test_prefix_trace_duration_count') == 1.0
        assert fetch_metric('test_prefix_trace_duration_sum') >= 500

        with telemetry.span("category1",
                            "span1",
                            attributes={
                                TestAttributes.ATTRIB1: "attrib1",
                                TestAttributes.LABEL1: 'label1'
                            }) as span:
            time.sleep(.5)

        # wait for Prometheus collection interval to pass (METRICS_INTERVAL)
        time.sleep(2)

        telemetry.collect()

        assert fetch_metric('test_prefix_trace_duration_count') == 2.0
        assert fetch_metric('test_prefix_trace_duration_sum') >= 1000

        telemetry.shutdown()