Amazon SageMaker

Amazon SageMaker is AWS’s fully managed machine learning platform. It provides tools for building, training, and deploying ML models at scale, including real-time inference endpoints for LLMs and other models. Respan gives you full observability over every endpoint invocation, payload, and response — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai opentelemetry-instrumentation-sagemaker boto3
2

Set environment variables

$export AWS_ACCESS_KEY_ID="YOUR_AWS_ACCESS_KEY_ID"
$export AWS_SECRET_ACCESS_KEY="YOUR_AWS_SECRET_ACCESS_KEY"
$export AWS_REGION="us-east-1"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

AWS credentials are used to invoke SageMaker endpoints. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import os
2import json
3import boto3
4from respan import Respan
5from opentelemetry.instrumentation.sagemaker import SageMakerInstrumentor
6
7respan = Respan(instrumentations=[SageMakerInstrumentor()])
8
9client = boto3.client("sagemaker-runtime", region_name=os.environ["AWS_REGION"])
10
11payload = json.dumps({"inputs": "Say hello in three languages."})
12
13response = client.invoke_endpoint(
14 EndpointName="my-llm-endpoint",
15 ContentType="application/json",
16 Body=payload,
17)
18result = json.loads(response["Body"].read().decode())
19print(result)
20respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented endpoint invocation spans with payloads, latency, and tokens.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. SageMakerInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

1from respan import Respan
2from opentelemetry.instrumentation.sagemaker import SageMakerInstrumentor
3
4respan = Respan(
5 instrumentations=[SageMakerInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "sagemaker-api", "version": "1.0.0"},
8)

With propagate_attributes

1import json
2from respan import Respan, propagate_attributes
3from opentelemetry.instrumentation.sagemaker import SageMakerInstrumentor
4
5respan = Respan(instrumentations=[SageMakerInstrumentor()])
6
7def handle_request(user_id: str, prompt: str):
8 with propagate_attributes(
9 customer_identifier=user_id,
10 thread_identifier="conv_abc_123",
11 metadata={"plan": "pro"},
12 ):
13 response = client.invoke_endpoint(
14 EndpointName="my-llm-endpoint",
15 ContentType="application/json",
16 Body=json.dumps({"inputs": prompt}),
17 )
18 print(json.loads(response["Body"].read().decode()))
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.