File size: 5,382 Bytes
ce676f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
from typing import Optional
import logging
from haystack.tracing.logging_tracer import LoggingTracer
from haystack import component, Pipeline
from haystack import tracing
@component
class Hello:
@component.output_types(output=str)
def run(self, word: Optional[str]):
return {"output": f"Hello, {word}!"}
@component
class FailingComponent:
@component.output_types(output=str)
def run(self, word: Optional[str]):
raise Exception("Failing component")
class TestLoggingTracer:
def test_init(self) -> None:
tracer = LoggingTracer()
assert tracer.tags_color_strings == {}
tracer = LoggingTracer(tags_color_strings={"tag_name": "color_string"})
assert tracer.tags_color_strings == {"tag_name": "color_string"}
def test_logging_tracer(self, caplog) -> None:
tracer = LoggingTracer()
caplog.set_level(logging.DEBUG)
with tracer.trace("test") as span:
span.set_tag("key", "value")
assert "Operation: test" in caplog.text
assert "key=value" in caplog.text
assert len(caplog.records) == 2
# structured logging
assert caplog.records[0].operation_name == "test"
assert caplog.records[1].tag_name == "key"
assert caplog.records[1].tag_value == "value"
def test_tracing_complex_values(self, caplog) -> None:
tracer = LoggingTracer()
caplog.set_level(logging.DEBUG)
with tracer.trace("test") as span:
span.set_tag("key", {"a": 1, "b": [2, 3, 4]})
assert "Operation: test" in caplog.text
assert "key={'a': 1, 'b': [2, 3, 4]}" in caplog.text
assert len(caplog.records) == 2
# structured logging
assert caplog.records[0].operation_name == "test"
assert caplog.records[1].tag_name == "key"
assert caplog.records[1].tag_value == {"a": 1, "b": [2, 3, 4]}
def test_apply_color_strings(self, caplog) -> None:
tracer = LoggingTracer(tags_color_strings={"key": "color_string"})
caplog.set_level(logging.DEBUG)
with tracer.trace("test") as span:
span.set_tag("key", "value")
assert "color_string" in caplog.text
def test_logging_pipeline(self, caplog) -> None:
pipeline = Pipeline()
pipeline.add_component("hello", Hello())
pipeline.add_component("hello2", Hello())
pipeline.connect("hello.output", "hello2.word")
tracing.enable_tracing(LoggingTracer())
caplog.set_level(logging.DEBUG)
pipeline.run(data={"word": "world"})
records = caplog.records
assert any(
record.operation_name == "haystack.component.run" for record in records if hasattr(record, "operation_name")
)
assert any(
record.operation_name == "haystack.pipeline.run" for record in records if hasattr(record, "operation_name")
)
tags_records = [record for record in records if hasattr(record, "tag_name")]
assert any(record.tag_name == "haystack.component.name" for record in tags_records)
assert any(record.tag_value == "hello" for record in tags_records)
tracing.disable_tracing()
def test_logging_pipeline_with_content_tracing(self, caplog) -> None:
pipeline = Pipeline()
pipeline.add_component("hello", Hello())
tracing.tracer.is_content_tracing_enabled = True
tracing.enable_tracing(LoggingTracer())
caplog.set_level(logging.DEBUG)
pipeline.run(data={"word": "world"})
records = caplog.records
tags_records = [record for record in records if hasattr(record, "tag_name")]
input_tag_value = [
record.tag_value for record in tags_records if record.tag_name == "haystack.component.input"
][0]
assert input_tag_value == {"word": "world"}
output_tag_value = [
record.tag_value for record in tags_records if record.tag_name == "haystack.component.output"
][0]
assert output_tag_value == {"output": "Hello, world!"}
tracing.tracer.is_content_tracing_enabled = False
tracing.disable_tracing()
def test_logging_pipeline_on_failure(self, caplog) -> None:
"""
Test that the LoggingTracer also logs events when a component fails.
"""
pipeline = Pipeline()
pipeline.add_component("failing_component", FailingComponent())
tracing.enable_tracing(LoggingTracer())
caplog.set_level(logging.DEBUG)
try:
pipeline.run(data={"word": "world"})
except:
pass
records = caplog.records
assert any(
record.operation_name == "haystack.component.run" for record in records if hasattr(record, "operation_name")
)
assert any(
record.operation_name == "haystack.pipeline.run" for record in records if hasattr(record, "operation_name")
)
tags_records = [record for record in records if hasattr(record, "tag_name")]
assert any(record.tag_name == "haystack.component.name" for record in tags_records)
assert any(record.tag_value == "failing_component" for record in tags_records)
tracing.disable_tracing()
|