OpenTelemetry SageMaker Instrumentation

0.58.0 · active · verified Fri Apr 10

The opentelemetry-instrumentation-sagemaker library provides OpenTelemetry tracing for interactions with Amazon SageMaker, specifically focusing on model invocations made via the Boto3 client. It captures telemetry data related to SageMaker endpoint calls, enabling observability into ML workflows. The library is currently at version 0.58.0 and maintains a rapid release cadence, often releasing weekly or bi-weekly.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to enable OpenTelemetry SageMaker instrumentation and manually configure the SDK to export traces to the console. It uses a mocked boto3 client to simulate an `invoke_endpoint` call without requiring actual AWS credentials, allowing you to see the generated spans. The `TRACELOOP_TRACE_CONTENT` environment variable is set to 'false' to prevent logging of sensitive request/response data.

import os
from unittest.mock import MagicMock

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.sagemaker import SageMakerInstrumentor

# Set up OpenTelemetry SDK for console output
provider = TracerProvider()
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)

# Instrument SageMaker (Boto3 calls)
SageMakerInstrumentor().instrument()

# Disable logging of request/response bodies for privacy (optional but recommended)
os.environ['TRACELOOP_TRACE_CONTENT'] = 'false'

# Mock boto3 client to avoid actual AWS calls
mock_client = MagicMock()
mock_client.invoke_endpoint.return_value = {
    'Body': MagicMock(read=lambda: b'{"predictions": [0.9]}'),
    'ContentType': 'application/json',
    'InvokedProductionVariant': 'variant-1'
}

# Use the instrumented client (even if mocked, instrumentation hooks will be called)
print('Invoking SageMaker endpoint (mocked)...')
response = mock_client.invoke_endpoint(
    EndpointName='my-ml-endpoint',
    ContentType='application/json',
    Body=b'{"instances": [[1,2,3]]}'
)

print(f"Mocked response: {response['Body'].read().decode()}")
print("Check console for OpenTelemetry trace output.")

# To uninstrument (optional)
# SageMakerInstrumentor().uninstrument()

view raw JSON →