Registry / observability / opentelemetry-instrumentation-sklearn

opentelemetry-instrumentation-sklearn

JSON →
library0.46b0pypypiunverified

This library provides OpenTelemetry automatic instrumentation for the scikit-learn (sklearn) machine learning library. It enables the collection of telemetry data, such as traces and spans, for various scikit-learn operations like model training (`fit`) and prediction (`predict`). The project is actively maintained as part of the broader OpenTelemetry Python Contrib repository, with new versions released regularly as beta releases.

pip install opentelemetry-instrumentation-sklearn
INSTALL
IMPORT
SIG · OPENTELEMETRY-INST
O
opentelemetry-instrumentation-sklearn
observabilitypythonv0.46b0
Install
12.0s avg
Import
Disk
50MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.46b0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
installs and imports cleanly · install 0.0s · import 0.000s · 50.5MB
glibc
py 3.103.940 runs
installs and imports cleanly · install 12.0s · import 0.000s · 48MB
50MB installed
● package 50MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SklearnInstrumentor
from opentelemetry.instrumentation.sklearn import SklearnInstrumentor
from opentelemetry.instrumentation.sklearn import SklearnInstrumentor

This quickstart demonstrates how to instrument scikit-learn operations. It sets up a basic OpenTelemetry ConsoleSpanExporter to print traces to the console, initializes the `SklearnInstrumentor`, and then performs typical scikit-learn `fit` and `predict` operations. You should see spans generated for these activities in your console output.

from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry.instrumentation.sklearn import SklearnInstrumentor from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split # Configure OpenTelemetry Tracer resource = Resource.create({"service.name": "sklearn-app"}) provider = TracerProvider(resource=resource) processor = SimpleSpanProcessor(ConsoleSpanExporter()) provider.add_span_processor(processor) trace.set_tracer_provider(provider) # Initialize Sklearn Instrumentation # Ensure this is called BEFORE importing sklearn if using programmatic instrumentation SklearnInstrumentor().instrument() # Scikit-learn operations will now be traced iris = load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = LogisticRegression(max_iter=200) print("\n--- Training Model ---") model.fit(X_train, y_train) print("Model training complete.") print("\n--- Making Predictions ---") predictions = model.predict(X_test) print("Predictions made.")
Debug
Known issues
gotchaThe OpenTelemetry instrumentation should be initialized before the `sklearn` library is imported to ensure proper monkey-patching and tracing of operations. Importing `sklearn` components before calling `SklearnInstrumentor().instrument()` may result in untraced operations.
fix
Call `SklearnInstrumentor().instrument()` at the very beginning of your application's entry point, before any `import sklearn` statements or direct usage of scikit-learn objects.
affects: All
breakingA change in OpenTelemetry Python Contrib (around v0.53b0 / 1.32.0) altered how dependency checks are performed. Instrumentors now check for the instrumented library's presence and version *inside* the `instrument()` method. If the target library (scikit-learn in this case) is not installed, or its version is incompatible, `instrument()` may raise an `ImportError` or other exceptions.
fix
Ensure `scikit-learn` is installed and meets the version requirements of the `opentelemetry-instrumentation-sklearn` package. Review the `instrumentation_dependencies()` method in the source code or the OpenTelemetry documentation for precise version constraints.
affects: >=0.53b0 of opentelemetry-instrumentation (parent package), >=1.32.0 of opentelemetry-sdk
gotchaRunning multiple OpenTelemetry SDK components (e.g., multiple exporters or processors) can lead to duplicate telemetry. This is especially problematic in environments like 'Always On' Azure Functions or applications using pre-fork servers where processes might persist or get duplicated.
fix
Ensure only one instance of each OpenTelemetry exporter and processor is configured per telemetry signal (traces, metrics, logs) within your application's lifecycle. For pre-fork servers, consider programmatic auto-instrumentation or using a single worker for telemetry-sensitive operations to avoid issues with background threads and locks.
affects: All
Upgrade
Version history
0.46b0latest on PyPI · released May 31, 2024
Audit
Dependencies
scikit-learnrequiredThe library instruments scikit-learn; requires an installed version of scikit-learn to function.
opentelemetry-sdkrequiredCore OpenTelemetry SDK for trace/metric/log providers.
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources
opentelemetry-instrumentation-sklearn — pip install opentelemetry-instrumentation-sklearn · libregistry