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