-
Notifications
You must be signed in to change notification settings - Fork 626
/
instrumentor.py
137 lines (103 loc) · 4.4 KB
/
instrumentor.py
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
# Copyright The OpenTelemetry Authors
#
# Licensed 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.
# type: ignore
"""
OpenTelemetry Base Instrumentor
"""
from abc import ABC, abstractmethod
from logging import getLogger
from typing import Collection, Optional
from opentelemetry.instrumentation._semconv import (
_OpenTelemetrySemanticConventionStability,
)
from opentelemetry.instrumentation.dependencies import (
DependencyConflict,
get_dependency_conflicts,
)
_LOG = getLogger(__name__)
class BaseInstrumentor(ABC):
"""An ABC for instrumentors
Child classes of this ABC should instrument specific third
party libraries or frameworks either by using the
``opentelemetry-instrument`` command or by calling their methods
directly.
Since every third party library or framework is different and has different
instrumentation needs, more methods can be added to the child classes as
needed to provide practical instrumentation to the end user.
"""
_instance = None
_is_instrumented_by_opentelemetry = False
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
@property
def is_instrumented_by_opentelemetry(self):
return self._is_instrumented_by_opentelemetry
@abstractmethod
def instrumentation_dependencies(self) -> Collection[str]:
"""Return a list of python packages with versions that the will be instrumented.
The format should be the same as used in requirements.txt or pyproject.toml.
For example, if an instrumentation instruments requests 1.x, this method should look
like:
def instrumentation_dependencies(self) -> Collection[str]:
return ['requests ~= 1.0']
This will ensure that the instrumentation will only be used when the specified library
is present in the environment.
"""
def _instrument(self, **kwargs):
"""Instrument the library"""
@abstractmethod
def _uninstrument(self, **kwargs):
"""Uninstrument the library"""
def _check_dependency_conflicts(self) -> Optional[DependencyConflict]:
dependencies = self.instrumentation_dependencies()
return get_dependency_conflicts(dependencies)
def instrument(self, **kwargs):
"""Instrument the library
This method will be called without any optional arguments by the
``opentelemetry-instrument`` command.
This means that calling this method directly without passing any
optional values should do the very same thing that the
``opentelemetry-instrument`` command does.
"""
if self._is_instrumented_by_opentelemetry:
_LOG.warning("Attempting to instrument while already instrumented")
return None
# check if instrumentor has any missing or conflicting dependencies
skip_dep_check = kwargs.pop("skip_dep_check", False)
if not skip_dep_check:
conflict = self._check_dependency_conflicts()
if conflict:
_LOG.error(conflict)
return None
# initialize semantic conventions opt-in if needed
_OpenTelemetrySemanticConventionStability._initialize()
result = self._instrument( # pylint: disable=assignment-from-no-return
**kwargs
)
self._is_instrumented_by_opentelemetry = True
return result
def uninstrument(self, **kwargs):
"""Uninstrument the library
See ``BaseInstrumentor.instrument`` for more information regarding the
usage of ``kwargs``.
"""
if self._is_instrumented_by_opentelemetry:
result = self._uninstrument(**kwargs)
self._is_instrumented_by_opentelemetry = False
return result
_LOG.warning("Attempting to uninstrument while already uninstrumented")
return None
__all__ = ["BaseInstrumentor"]