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
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()
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
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