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 #2
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
    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_mixin(self, telemetry: TelemetryFixture, caplog):
        example = ExampleClass()
        example.method1()
        example.method2()
        try:
            example.error()  # raises exception
        except:
            pass

        telemetry.collect()

        assert example.telemetry_category == 'tests.example.ExampleClass'

        # method1 (direct)
        assert telemetry.get_counter('tests.example.ExampleClass.method1_counter').value == 1

        # method1 (direct, inside span)
        assert telemetry.get_counter('tests.example.ExampleClass.method1_counter_inside_span', labels={'label1': 'value1',
                                                                                                       Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                                                                       Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).value == 1

        # method2 (direct)
        assert telemetry.get_counter('tests.example.ExampleClass.method2_counter').value == 1

        # method2 (direct, inside span)
        assert telemetry.get_counter('tests.example.ExampleClass.method2_counter_inside_span',
                                     labels={'label2': 'value2',
                                             Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                             Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).value == 1  # method2 (inside span)

        # method2 (indirect)
        assert telemetry.get_counter('tests.example.ExampleClass.method2_counter', labels={'label1': 'value1',
                                                                                           Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                                                           Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).value == 1

        # method2 (indirect, inside span)
        assert telemetry.get_counter('tests.example.ExampleClass.method2_counter_inside_span',
                                     labels={'label1': 'value1', 'label2': 'value2',
                                             Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                             Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).value == 1

        assert len(telemetry.get_counters()) == 7

        # method1 (direct)
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label1': 'value1',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).count == 1

        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label1': 'value1',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).sum >= 100
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label1': 'value1',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).min >= 100
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label1': 'value1',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}).max >= 100

        # method2 (direct)
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label2': 'value2',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).count == 1
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label2': 'value2',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).sum >= 100
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label2': 'value2',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).min >= 100
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label2': 'value2',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).max >= 100

        # method2 (indirect)
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={'label1': 'value1',
                                                    'label2': 'value2',
                                                    Attributes.TRACE_STATUS.name: 'OK',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).count == 1

        # error (direct)
        assert telemetry.get_value_recorder('trace.duration',
                                            labels={Attributes.TRACE_STATUS.name: 'ERROR',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.error'}).count == 1

        assert len(telemetry.get_value_recorders()) == 4