|
| 1 | +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +# or more contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from opentelemetry.instrumentation.bootstrap import run as orig_run |
| 18 | +from opentelemetry.instrumentation.bootstrap_gen import ( |
| 19 | + default_instrumentations as gen_default_instrumentations, |
| 20 | +) |
| 21 | +from opentelemetry.instrumentation.bootstrap_gen import ( |
| 22 | + libraries as gen_libraries, |
| 23 | +) |
| 24 | +from packaging.requirements import Requirement |
| 25 | + |
| 26 | + |
| 27 | +# the instrumentations available in opentelemetry-bootstrap we want to skip |
| 28 | +_EXCLUDED_INSTRUMENTATIONS = {"opentelemetry-instrumentation-openai-v2"} |
| 29 | + |
| 30 | +# update with: |
| 31 | +# $ python3.12 scripts/build_edot_bootstrap_instrumentations.py | ruff format - |
| 32 | +_EDOT_INSTRUMENTATIONS = [ |
| 33 | + { |
| 34 | + "library": "openai >= 1.2.0", |
| 35 | + "instrumentation": "elastic-opentelemetry-instrumentation-openai==0.4.0", |
| 36 | + } |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +def _get_instrumentation_name(library_entry): |
| 41 | + instrumentation = library_entry["instrumentation"] |
| 42 | + instrumentation_name = Requirement(instrumentation) |
| 43 | + return instrumentation_name.name |
| 44 | + |
| 45 | + |
| 46 | +def run() -> None: |
| 47 | + """This is a tiny wrapper around the upstream opentelemetry-boostrap implementation that let us decide which instrumentation to use""" |
| 48 | + libraries = [ |
| 49 | + lib for lib in gen_libraries if _get_instrumentation_name(lib) not in _EXCLUDED_INSTRUMENTATIONS |
| 50 | + ] + _EDOT_INSTRUMENTATIONS |
| 51 | + return orig_run(default_instrumentations=gen_default_instrumentations, libraries=libraries) |
0 commit comments