forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_instrumentation_bootstrap.py
More file actions
executable file
·146 lines (126 loc) · 5.1 KB
/
generate_instrumentation_bootstrap.py
File metadata and controls
executable file
·146 lines (126 loc) · 5.1 KB
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
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# 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.
import ast
import logging
import os
import subprocess
import sys
import astor
from otel_packaging import (
get_instrumentation_packages,
root_path,
scripts_path,
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("instrumentation_list_generator")
_template = """
{header}
# DO NOT EDIT. THIS FILE WAS AUTOGENERATED FROM INSTRUMENTATION PACKAGES.
# RUN `python scripts/generate_instrumentation_bootstrap.py` TO REGENERATE.
{source}
"""
_source_tmpl = """
libraries = {}
default_instrumentations = []
"""
gen_path = os.path.join(
root_path,
"opentelemetry-instrumentation",
"src",
"opentelemetry",
"instrumentation",
"bootstrap_gen.py",
)
packages_to_exclude = [
# AWS Lambda instrumentation is excluded from the default list because it often
# requires specific configurations and dependencies that may not be set up
# in all environments. Instead, users who need AWS Lambda support can opt-in
# by manually adding it to their environment.
# See https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2787
"opentelemetry-instrumentation-aws-lambda",
# Google GenAI instrumentation is currently excluded because it is still in early
# development. This filter will get removed once it is further along in its
# development lifecycle and ready to be included by default.
"opentelemetry-instrumentation-google-genai",
# Langchain instrumentation is currently excluded because it is still in early
# development. This filter will get removed once it is further along in its
# development lifecycle and ready to be included by default.
"opentelemetry-instrumentation-langchain",
# Weaviate instrumentation is currently excluded because it is still in early
# development. This filter will get removed once it is further along in its
# development lifecycle and ready to be included by default.
"opentelemetry-instrumentation-weaviate",
# MCP instrumentation is currently excluded because it is still in early
# development. This filter will get removed once it is further along in its
# development lifecycle and ready to be included by default.
"opentelemetry-instrumentation-mcp",
]
# Static version specifiers for instrumentations that are released independently
independent_packages = {
"opentelemetry-instrumentation-openai-v2": "",
"opentelemetry-instrumentation-vertexai": ">=2.0b0",
"opentelemetry-instrumentation-google-genai": "",
}
def main():
# pylint: disable=no-member
default_instrumentations = ast.List(elts=[])
libraries = ast.List(elts=[])
for pkg in get_instrumentation_packages(
independent_packages=independent_packages
):
pkg_name = pkg.get("name")
if pkg_name in packages_to_exclude:
continue
if not pkg["instruments"] and not pkg["instruments-any"]:
default_instrumentations.elts.append(ast.Str(pkg["requirement"]))
for target_pkg in pkg["instruments"]:
libraries.elts.append(
ast.Dict(
keys=[ast.Str("library"), ast.Str("instrumentation")],
values=[ast.Str(target_pkg), ast.Str(pkg["requirement"])],
)
)
# instruments-any is an optional field that can be used instead of or in addition to _instruments. While _instruments is a list of dependencies, all of which are expected by the instrumentation, instruments-any is a list any of which but not all are expected.
for target_pkg in pkg["instruments-any"]:
libraries.elts.append(
ast.Dict(
keys=[ast.Str("library"), ast.Str("instrumentation")],
values=[ast.Str(target_pkg), ast.Str(pkg["requirement"])],
)
)
tree = ast.parse(_source_tmpl)
tree.body[0].value = libraries
tree.body[1].value = default_instrumentations
source = astor.to_source(tree)
with open(
os.path.join(scripts_path, "license_header.txt"), encoding="utf-8"
) as header_file:
header = header_file.read()
source = _template.format(header=header, source=source)
with open(gen_path, "w", encoding="utf-8") as gen_file:
gen_file.write(source)
subprocess.run(
[
sys.executable,
"scripts/eachdist.py",
"format",
"--path",
"opentelemetry-instrumentation/src",
],
check=True,
)
logger.info("generated %s", gen_path)
if __name__ == "__main__":
main()