コード例 #1
0
    def test_json_log_format(self, telemetry: TelemetryFixture, caplog):
        telemetry.enable_log_record_capture(caplog)

        example = ExampleClass()
        example.method1()

        telemetry.collect()
        method1_span = telemetry.get_finished_spans(name_filter=lambda name: name == 'tests.example.ExampleClass.method1')[0]
        method2_span = telemetry.get_finished_spans(name_filter=lambda name: name == 'tests.example.ExampleClass.method2')[0]

        log_record = telemetry.caplog.get_record(lambda l: l['message'] == 'method1 log')
        assert log_record['attributes'] == {TestAttributes.ATTRIB1.name: 'value1',
                                            TestAttributes.LABEL1.name: 'value1',
                                            Attributes.TRACE_ID.name: method1_span.context.trace_id,
                                            Attributes.TRACE_SPAN_ID.name: method1_span.context.span_id,
                                            Attributes.TRACE_IS_REMOTE.name: False,
                                            Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                            Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method1'}

        log_record = telemetry.caplog.get_record(lambda l: l['message'] == 'method2 log')
        assert log_record['attributes'] == {TestAttributes.ATTRIB1.name: 'value1',
                                            TestAttributes.ATTRIB2.name: 'value2',
                                            Attributes.TRACE_ID.name: method2_span.context.trace_id,
                                            Attributes.TRACE_SPAN_ID.name: method2_span.context.span_id,
                                            Attributes.TRACE_IS_REMOTE.name: False,
                                            Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                            Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2',
                                            'label1': 'value1',
                                            'label2': 'value2'}

        telemetry.collect()

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

        assert telemetry.get_value_recorder(name='trace.duration',
                                            labels={Attributes.TRACE_STATUS.name: 'OK', 'label1': 'value1', 'label2': 'value2',
                                                    Attributes.TRACE_CATEGORY.name: 'tests.example.ExampleClass',
                                                    Attributes.TRACE_NAME.name: 'tests.example.ExampleClass.method2'}).count == 1
        assert len(telemetry.get_value_recorders()) == 2
コード例 #2
0
    def test_decorator_argument_labeling(self, telemetry: TelemetryFixture,
                                         caplog):
        telemetry.enable_log_record_capture(caplog)

        example = DecoratorExample()
        example.method_trace_custom('foo')
        example.method_trace_custom('foo', 20)

        telemetry.collect()

        recorders = telemetry.get_value_recorders()

        assert telemetry.get_value_recorder(
            name='trace.duration',
            labels={
                Attributes.TRACE_CATEGORY.name: 'custom_category',
                Attributes.TRACE_NAME.name:
                'custom_category.method_trace_custom',
                Attributes.TRACE_STATUS.name: 'OK',
                'arg1': 'foo',
                'label1': 't1'
            }).count == 2

        rec = telemetry.caplog.get_record(
            lambda rec: rec['message'] == 'method_trace_custom log' and rec[
                'attributes']['arg2'] == 10)

        assert rec['attributes']['label1'] == 't1'
        assert rec['attributes']['arg1'] == 'foo'
        assert rec['attributes']['arg2'] == 10

        rec = telemetry.caplog.get_record(
            lambda rec: rec['message'] == 'method_trace_custom log' and rec[
                'attributes']['arg2'] == 20)

        assert rec['attributes']['label1'] == 't1'
        assert rec['attributes']['arg1'] == 'foo'
        assert rec['attributes']['arg2'] == 20
コード例 #3
0
    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