ray-llm
63 строки · 2.1 Кб
1# This is a lightweight fork of Opentelemetry's Threading instrmentor which
2# since it hasn't been merged yet, we're pulling in directly. Pulled from
3# https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1582 on
4# 2023-09-12 by thomas@.
5
6# Copyright The OpenTelemetry Authors
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19
20# pylint: disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module
21
22import threading # pylint: disable=import-self23from typing import Collection24
25from opentelemetry import context, trace26from opentelemetry.instrumentation.instrumentor import BaseInstrumentor27from opentelemetry.trace import (28get_current_span,29get_tracer,30get_tracer_provider,31)
32
33
34class _InstrumentedThread(threading.Thread):35_tracer: trace.Tracer36_parent_span: trace.Span37
38def start(self):39self._parent_span = get_current_span()40super().start()41
42def run(self):43parent_span = self._parent_span or get_current_span()44ctx = trace.set_span_in_context(parent_span)45context.attach(ctx)46super().run()47
48
49class ThreadingInstrumentor(BaseInstrumentor): # pylint: disable=empty-docstring50original_threadcls = threading.Thread51
52def instrumentation_dependencies(self) -> Collection[str]:53return ()54
55def _instrument(self, *args, **kwargs):56tracer_provider = kwargs.get("tracer_provider", None) or get_tracer_provider()57
58tracer = get_tracer(__name__, "0.0.1", tracer_provider)59threading.Thread = _InstrumentedThread60_InstrumentedThread._tracer = tracer61
62def _uninstrument(self, **kwargs):63threading.Thread = self.original_threadcls64