Exemplo n.º 1
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
Exemplo n.º 2
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