ferruzzi commented on code in PR #37948: URL: https://github.com/apache/airflow/pull/37948#discussion_r1516849855
########## tests/core/test_otel_tracer.py: ########## @@ -0,0 +1,155 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import json +import logging +from datetime import datetime +from unittest.mock import MagicMock, patch + +import pytest +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter + +from airflow.traces import TRACEPARENT, TRACESTATE, otel_tracer, utils +from airflow.traces.tracer import Trace +from tests.test_utils.config import env_vars + + [email protected] +def name(): + return "test_traces_run" + + +class TestOtelTrace: + @patch("opentelemetry.sdk.trace.export.ConsoleSpanExporter") + @patch("airflow.traces.otel_tracer.conf") + def test_tracer(self, conf_a, exporter): + # necessary to speed up the span to be emitted + with env_vars({"OTEL_BSP_SCHEDULE_DELAY": "1"}): + log = logging.getLogger("TestOtelTrace.test_tracer") + log.setLevel(logging.DEBUG) + # hijacking airflow conf with pre-defined + # values + conf_a.get.return_value = "abc" + conf_a.getint.return_value = 123 + """this will enable debug to set - which outputs the result + to the console exporter""" + conf_a.getboolean.return_value = True + + # mocking console exporter with in mem exporter for better assertion + in_mem_exporter = InMemorySpanExporter() + exporter.return_value = in_mem_exporter + + tracer = otel_tracer.get_otel_tracer(Trace) + assert conf_a.get.called + assert conf_a.getint.called + assert conf_a.getboolean.called + with tracer.start_span(span_name="span1") as s1: + with tracer.start_span(span_name="span2") as s2: + s2.set_attribute("attr2", "val2") + span2 = json.loads(s2.to_json()) + span1 = json.loads(s1.to_json()) + # assert the two span data + assert span1["name"] == "span1" + assert span2["name"] == "span2" + trace_id = span1["context"]["trace_id"] + s1_span_id = span1["context"]["span_id"] + assert span2["context"]["trace_id"] == trace_id + assert span2["parent_id"] == s1_span_id + assert span2["attributes"]["attr2"] == "val2" + assert span2["resource"]["attributes"]["service.name"] == "abc" + + @patch("opentelemetry.sdk.trace.export.ConsoleSpanExporter") + @patch("airflow.traces.otel_tracer.conf") + def test_dag_tracer(self, conf_a, exporter): + # necessary to speed up the span to be emitted + with env_vars({"OTEL_BSP_SCHEDULE_DELAY": "1"}): + log = logging.getLogger("TestOtelTrace.test_dag_tracer") + log.setLevel(logging.DEBUG) + conf_a.get.return_value = "abc" + conf_a.getint.return_value = 123 + """this will enable debug to set - which outputs the result + to the console exporter""" + conf_a.getboolean.return_value = True + + # mocking console exporter with in mem exporter for better assertion + in_mem_exporter = InMemorySpanExporter() + exporter.return_value = in_mem_exporter + + now = datetime.now() + dag_run = MagicMock() + dag_run.conf = { + TRACEPARENT: "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", Review Comment: Is this traceparent value significant? If that value is significant, maybe add a comment about where it comes from. If not consider replacing it with an obviously nonsense string like "parent_id" or something like that? Regardless of whether or not those particular values are significant, I think it may be easier to understand as: ``` parent_trace_id = "0af7651916cd43dd8448eb211c80319c" parent_span_id = "b7ad6b7169203331" ``` Then line 96 here would be ```TRACEPARENT: f"00-{parent_trace_id}-{parent_span_id}-001"``` and lines 111 and 113 would become more clear as ```assert span1["context"]["trace_id"] != f"0x{parent_trace_id}"``` etc ########## tests/core/test_otel_tracer.py: ########## @@ -0,0 +1,155 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import json +import logging +from datetime import datetime +from unittest.mock import MagicMock, patch + +import pytest +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter + +from airflow.traces import TRACEPARENT, TRACESTATE, otel_tracer, utils +from airflow.traces.tracer import Trace +from tests.test_utils.config import env_vars + + [email protected] +def name(): + return "test_traces_run" + + +class TestOtelTrace: + @patch("opentelemetry.sdk.trace.export.ConsoleSpanExporter") + @patch("airflow.traces.otel_tracer.conf") + def test_tracer(self, conf_a, exporter): + # necessary to speed up the span to be emitted + with env_vars({"OTEL_BSP_SCHEDULE_DELAY": "1"}): + log = logging.getLogger("TestOtelTrace.test_tracer") + log.setLevel(logging.DEBUG) + # hijacking airflow conf with pre-defined + # values + conf_a.get.return_value = "abc" + conf_a.getint.return_value = 123 + """this will enable debug to set - which outputs the result Review Comment: Here and below - Is there any particular reason this is a """ comment """ instead of a #comment ? We generally only use the triple quotes for documentation stuff, not for inline comments. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
