|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community |
| 4 | +Edition) available. |
| 5 | +Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 6 | +Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +http://opensource.org/licenses/MIT |
| 9 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +specific language governing permissions and limitations under the License. |
| 12 | +""" |
| 13 | +import copy |
| 14 | +from contextlib import contextmanager |
| 15 | +from functools import wraps |
| 16 | + |
| 17 | +from opentelemetry import trace |
| 18 | +from opentelemetry.sdk.trace.export import SpanProcessor |
| 19 | +from opentelemetry.trace import SpanKind |
| 20 | + |
| 21 | + |
| 22 | +class AttributeInjectionSpanProcessor(SpanProcessor): |
| 23 | + """Span处理器,用于在Span开始时设置属性""" |
| 24 | + |
| 25 | + def __init__(self, attributes): |
| 26 | + self.attributes = attributes |
| 27 | + |
| 28 | + def on_start(self, span: trace.Span, parent_context): |
| 29 | + if not isinstance(span, trace.Span): |
| 30 | + return |
| 31 | + |
| 32 | + for key, value in self.attributes.items(): |
| 33 | + span.set_attribute(key, value) |
| 34 | + |
| 35 | + def on_end(self, span: trace.Span): |
| 36 | + # Implement custom logic if needed on span end |
| 37 | + pass |
| 38 | + |
| 39 | + |
| 40 | +def propagate_attributes(attributes: dict): |
| 41 | + """把attributes设置到span上,并继承到后面所有span |
| 42 | +
|
| 43 | + :param attributes: 默认属性 |
| 44 | + """ |
| 45 | + |
| 46 | + provider = trace.get_tracer_provider() |
| 47 | + |
| 48 | + # Add a span processor that sets attributes on every new span |
| 49 | + provider.add_span_processor(AttributeInjectionSpanProcessor(attributes)) |
| 50 | + |
| 51 | + |
| 52 | +@contextmanager |
| 53 | +def start_trace(span_name: str, propagate: bool = False, **attributes): |
| 54 | + """Start a trace |
| 55 | +
|
| 56 | + :param span_name: 自定义Span名称 |
| 57 | + :param propagate: 是否需要传播 |
| 58 | + :param attributes: 需要跟span增加的属性, 默认为空 |
| 59 | + :yield: 当前上下文的Span |
| 60 | + """ |
| 61 | + tracer = trace.get_tracer(__name__) |
| 62 | + |
| 63 | + span_attributes = {f"bk_sops.{key}": value for key, value in attributes.items()} |
| 64 | + |
| 65 | + # 设置需要传播的属性 |
| 66 | + if propagate: |
| 67 | + propagate_attributes(span_attributes) |
| 68 | + |
| 69 | + with tracer.start_as_current_span(span_name, kind=SpanKind.SERVER) as span: |
| 70 | + # 如果不进行传播,则在当前span手动配置需要添加的属性 |
| 71 | + for attr_key, attr_value in span_attributes.items(): |
| 72 | + span.set_attribute(attr_key, attr_value) |
| 73 | + |
| 74 | + yield span |
| 75 | + |
| 76 | + |
| 77 | +def trace_view(propagate: bool = True, attr_keys=None, **default_attributes): |
| 78 | + """用来装饰view的trace装饰器 |
| 79 | +
|
| 80 | + :param propagate: 是否需要传播 |
| 81 | + :param attr_keys: 需要从request和url中获取的属性 |
| 82 | + :param default_attributes: 默认属性 |
| 83 | + :return: view_func |
| 84 | + """ |
| 85 | + attr_keys = attr_keys or [] |
| 86 | + |
| 87 | + def decorator(view_func): |
| 88 | + @wraps(view_func) |
| 89 | + def _wrapped_view(request, *args, **kwargs): |
| 90 | + attributes = copy.deepcopy(default_attributes) |
| 91 | + |
| 92 | + for attr_key in attr_keys: |
| 93 | + # 需要的属性只要在kwargs, request.GET, request.POST中就可以 |
| 94 | + for scope in (kwargs, request.GET, request.POST): |
| 95 | + if attr_key in scope: |
| 96 | + attributes[attr_key] = kwargs[attr_key] |
| 97 | + break |
| 98 | + |
| 99 | + with start_trace(view_func.__name__, propagate, **attributes): |
| 100 | + return view_func(request, *args, **kwargs) |
| 101 | + |
| 102 | + return _wrapped_view |
| 103 | + |
| 104 | + return decorator |
0 commit comments