Files
opentelemetry-python-contrib/scripts/generate_instrumentation_bootstrap.py
Michael Safyan c09a299010 Initial version of instrumentation for the Google GenAI SDK (github.com/googleapis/python-genai) (#3256)
* Begin instrumentation of GenAI SDK.

* Snapshot current state.

* Created minimal tests and got first test to pass.

* Added test for span attributes.

* Ensure that token counts work.

* Add more tests.

* Make it easy to turn off instrumentation for streaming and async to allow for rapid iteration.

* Add licenses and fill out main README.rst.

* Add a changelog file.

* Fill out 'requirements.txt' and 'README.rst' for the manual instrumentation example.

* Add missing exporter dependency for the manual instrumentation example.

* Fill out rest of the zero-code example.

* Add minimal tests for async, streaming cases.

* Update sync test to use indirection on top of 'client.models.generate_content' to simplify test reuse.

* Fix ruff check issues.

* Add subproject to top-level project build mechanism.

* Simplify invocation of pylint.

* Fix 'make test' command and lint issues.

* Add '.dev' suffix to version per feedback on pull request #3256

* Fix README.rst files for the examples.

* Add specific versions for the examples.

* Revamp 'make test' to not require local 'tox.ini' configuration.

* Extend separators per review comment.

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>

* Fix version conflict caused by non-hermetic requirements.

* Fix typo on the comment line.

* Add test for the use of the 'vertex_ai' system, and improve how this system is determined.

* Factor out testing logic to enable sharing with the async code.

* Addressed minor lint issues.

* Make it clearer that nonstreaming_base is a helper module that is not invoked directly.

* Integrate feedback from related pull request #3268.

* Update workflows with 'tox -e generate-workflows'.

* Improve data model and add some rudimentary type checking.

* Accept only 'true' for a true value to align with other code.

* Update the scope name used.

* Add **kwargs to patched methods to prevent future breakage due to the addition of future keyword arguments.

* Remove redundant list conversion in call to "sorted".

Co-authored-by: Aaron Abbott <aaronabbott@google.com>

* Reformat with 'tox -e ruff'.

* Fix failing lint workflow.

* Fix failing lint workflow.

* Exclude Google GenAI instrumentation from the bootstrap code for now.

* Minor improvements to the tooling shell files.

* Fix typo flagged by codespell spellchecker.

* Increase alignment with broader repo practices.

* Add more TODOs and documentation to clarify the intended work scope.

* Remove unneeded accessor from OTelWrapper.

* Add more comments to the tests.

* Reformat with ruff.

* Change 'desireable' to 'desirable' per codespell spellchecker.

* Make tests pass without pythonpath

* Fix new lint errors showing up after change

* Revert "Fix new lint errors showing up after change"

This reverts commit 567adc62a706035ad8ac5e29316c7a6f8d4c7909.

pylint ignore instead

* Add TODO item required/requested from code review.

Co-authored-by: Aaron Abbott <aaronabbott@google.com>

* Simplify changelog per PR feedback.

* Remove square brackets from model name in span name per PR feedback.

* Misc test cleanup. Now that scripts are invoked solely through pytest via tox, remove main functions and hash bang lines.

* Improve quality of event logging.

* Update operation name to use a constant for consistency.

* Reformat with ruff.

* Exclude opentelemetry-instrumentation-google-genai from root uv workspace

Until https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3300 is fixed.

---------

Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Co-authored-by: Aaron Abbott <aaronabbott@google.com>
2025-02-27 11:37:40 -05:00

128 lines
3.8 KiB
Python
Executable File

#!/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",
"opentelemetry-instrumentation-vertexai", # not released yet
]
# We should not put any version limit for instrumentations that are released independently
unversioned_packages = [
"opentelemetry-instrumentation-openai-v2",
"opentelemetry-instrumentation-vertexai",
"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(
unversioned_packages=unversioned_packages
):
pkg_name = pkg.get("name")
if pkg_name in packages_to_exclude:
continue
if not pkg["instruments"]:
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"])],
)
)
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()