Ejemplo n.º 1
0
    def test_span_events(self, telemetry: TelemetryFixture):
        with telemetry.span('test', 'span1', attributes={TestAttributes.LABEL1: 'span1_value1'}) as span1:
            span1.set_attribute('label2', 'span1_value2')

            with telemetry.span('test', 'span2', attributes={TestAttributes.LABEL2: 'span2_value2'}) as span2:
                span2.add_event('TestEvent1', {'string_field': 'string_value', 'int_field': 10})
                with telemetry.span('test', 'span3', attributes={TestAttributes.LABEL3: 'span3_value3'}) as span3:
                    span3.add_event('TestEvent2', {'string_field': 'string_value', 'int_field': 20})

                    assert len(span3.events()) == 1
                    assert len(span2.events()) == 1
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def test_invalid_labels(self, telemetry: TelemetryFixture, caplog):
        telemetry.enable_log_record_capture(caplog)

        invalid_names = [
            '',
            'invalid ',
            '(invalid )',
        ]

        with telemetry.span('test', 'span1') as span:
            for name in invalid_names:
                span.set_label(name, "value")
                telemetry.caplog.assert_log_contains(
                    f"attribute/label name must match the pattern: _*[a-zA-Z0-9_.\-]+ (name={name})", 'WARNING')

            span.set_label("foo", 1)
            telemetry.caplog.assert_log_contains(
                f"label value for must be a string! (name=foo, value=1)", 'WARNING')

            span.set_label("foo", 1.0)
            telemetry.caplog.assert_log_contains(
                f"label value for must be a string! (name=foo, value=1.0)", 'WARNING')

            span.set_label("foo", True)
            telemetry.caplog.assert_log_contains(
                f"label value for must be a string! (name=foo, value=True)", 'WARNING')
Ejemplo n.º 4
0
    def test_third_party_instrumentor(self, telemetry: TelemetryFixture):
        import requests
        from telemetry.api.listeners.span import LabelAttributes, InstrumentorSpanListener

        RequestsInstrumentor().instrument()

        telemetry.initialize()
        telemetry.add_span_processor(InstrumentorSpanListener(
            LabelAttributes('component', 'http.status_code', 'http.method'), 'requests'))

        responses.add_passthru('http://localhost:1234/does_not_exist')

        with telemetry.span('test_category', 'span1', attributes={TestAttributes.LABEL1: 'l1'}) as span:
            try:
                with requests.get('http://localhost:1234/does_not_exist') as response:
                    pass
            except:
                pass

        telemetry.collect()

        assert telemetry.get_value_recorder(name='trace.duration',
                                            labels={'component': 'http',
                                                    'http.method': 'GET',
                                                    TestAttributes.LABEL1.name: 'l1',
                                                    Attributes.TRACE_CATEGORY.name: 'requests',
                                                    Attributes.TRACE_NAME.name: 'requests.HTTP GET',
                                                    Attributes.TRACE_STATUS.name: 'ERROR'}).count == 1
    def test_environment_attributes(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')

        # need to initialize again after environment is updated
        telemetry.initialize()

        with telemetry.span("category1", 'span1') as span:
            logging.info("In span")

        telemetry.collect()

        assert len(
            telemetry.get_finished_spans(
                name_filter=lambda name: name == 'category1.span1',
                attribute_filter=lambda a: a.get('attrib1') == 'attrib1_value'
                and a.get('attrib2') == 'attrib2_value')) == 1

        assert telemetry.get_value_recorder('trace.duration',
                                            labels={
                                                Attributes.TRACE_CATEGORY.name:
                                                'category1',
                                                Attributes.TRACE_NAME.name:
                                                'category1.span1',
                                                Attributes.TRACE_STATUS.name:
                                                'OK',
                                                'label1': 'label1_value',
                                                'label2': 'label2_value'
                                            }).count == 1
Ejemplo n.º 6
0
    def test_instrumentors(self, telemetry: TelemetryFixture):
        telemetry.record_value("category1", "value1", 1)
        with telemetry.span("span_category1", "span1") as span:
            pass

        telemetry.collect()

        assert len(telemetry.get_metrics()) == 2
        assert len(
            telemetry.get_metrics(
                instrumentor_filter=lambda name: name == "default")) == 2
Ejemplo n.º 7
0
    def test_invalid_attributes(self, telemetry: TelemetryFixture, caplog):
        telemetry.enable_log_record_capture(caplog)

        invalid_names = [
            '',
            'invalid ',
            '(invalid )',
        ]

        with telemetry.span('test', 'span1') as span:
            for name in invalid_names:
                span.set_attribute(name, "value")
                telemetry.caplog \
                    .assert_log_contains(f"attribute/label name must match the pattern: _*[a-zA-Z0-9_.\\-]+ (name={name})", 'WARNING')
    def test_environment_attributes_override(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')

        # need to initialize again after environment is updated
        telemetry.initialize()

        # environment labels should win over any locally-specified labels to preserve ops behavior
        with telemetry.span("category1",
                            'span1',
                            attributes={
                                TestAttributes.ATTRIB1: 'attrib1_override',
                                TestAttributes.LABEL1: 'label1_value'
                            }) as span:
            pass

        telemetry.collect()

        spans = telemetry.get_finished_spans()
        assert len(
            telemetry.get_finished_spans(
                name_filter=lambda name: name == 'category1.span1',
                attribute_filter=lambda a: a.get('attrib1'
                                                 ) == 'attrib1_override')) == 1

        assert telemetry.get_value_recorder('trace.duration',
                                            labels={
                                                Attributes.TRACE_CATEGORY.name:
                                                'category1',
                                                Attributes.TRACE_NAME.name:
                                                'category1.span1',
                                                Attributes.TRACE_STATUS.name:
                                                'OK',
                                                TestAttributes.LABEL1.name:
                                                'label1_value',
                                                TestAttributes.LABEL2.name:
                                                'label2_value'
                                            }).count == 1

        Environment._clear()
Ejemplo n.º 9
0
    def test_span_listener(self, telemetry: TelemetryFixture):
        from opentelemetry.sdk.trace import SpanProcessor
        class Customlabelger(SpanProcessor):
            def on_start(self, span: "Span", parent_context: Optional[context_api.Context] = None) -> None:
                wrapped = Span(span)
                wrapped.set_attribute('hostname', 'localhost')
                wrapped.set_label('env', 'test')

        telemetry.add_span_processor(Customlabelger())

        with telemetry.span("category1", "span1") as span:
            assert span.labels['env'] == 'test'

        telemetry.collect()

        assert telemetry.get_value_recorder(name='trace.duration',
                                            labels={'env': 'test',
                                                    Attributes.TRACE_CATEGORY.name: 'category1',
                                                    Attributes.TRACE_NAME.name: 'category1.span1',
                                                    Attributes.TRACE_STATUS.name: 'OK'}).count == 1
    def test_labelger_empty(self, monkeypatch, telemetry: TelemetryFixture):
        # need to initialize again after environment is updated
        telemetry.initialize()

        with telemetry.span("category1", 'span1') as span:
            pass

        telemetry.collect()

        assert telemetry.get_value_recorder('trace.duration',
                                            labels={
                                                Attributes.TRACE_CATEGORY.name:
                                                'category1',
                                                Attributes.TRACE_NAME.name:
                                                'category1.span1',
                                                Attributes.TRACE_STATUS.name:
                                                'OK'
                                            }).count == 1

        Environment._clear()
Ejemplo n.º 11
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()