howardyoo commented on code in PR #37948: URL: https://github.com/apache/airflow/pull/37948#discussion_r1518463666
########## airflow/traces/tracer.py: ########## @@ -0,0 +1,256 @@ +# +# 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 logging +import socket +import typing +from typing import ( + TYPE_CHECKING, + Callable, +) + +from airflow.configuration import conf +from airflow.typing_compat import Protocol + +log = logging.getLogger(__name__) + + +def gen_context(trace_id, span_id): + """Generate span context from trace_id and span_id.""" + from airflow.traces.otel_tracer import gen_context as otel_gen_context + + return otel_gen_context(trace_id, span_id) + + +def gen_links_from_kv_list(list): + """Generate links from kv list of {trace_id:int, span_id:int}.""" + from airflow.traces.otel_tracer import gen_links_from_kv_list + + return gen_links_from_kv_list(list) + + +def span(func): + """Decorate a function with span.""" + + def wrapper(*args, **kwargs): + func_name = func.__name__ + qual_name = func.__qualname__ + module_name = func.__module__ + if "." in qual_name: + component = f"{qual_name.rsplit('.', 1)[0]}" + else: + component = module_name + with Trace.start_span(span_name=func_name, component=component): + return func(*args, **kwargs) + + return wrapper + + +class DummyContext: + """If no Tracer is configured, DummyContext is used as a fallback.""" + + def __init__(self): + self.trace_id = 1 + + +class DummySpan: + """If no Tracer is configured, DummySpan is used as a fallback.""" + + def __enter__(self): + """Enter.""" + return self + + def __exit__(self, *args, **kwargs): + """Exit.""" + pass + + def __call__(self, obj): + """Call.""" + return obj + + def get_span_context(self): + """Get span context.""" + return DUMMY_CTX + + def set_attribute(self, key, value) -> None: + """Set an attribute to the span.""" + pass + + def set_attributes(self, attributes) -> None: + """Set multiple attributes at once.""" + pass + + def add_event( + self, + name: str, + attributes=None, Review Comment: The type hint comes from opentelemetry, but since there is a possibility that maybe sometime in the future we 'might' be able to support any other trace library, I do not want tracer.py to have any dependencies from opentelemetry. Any suggestions? -- 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]
