SageMaker (tracing)

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.

Create an account at platform.respan.ai and grab an API key.

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

See Amazon SageMaker gateway setup to route SageMaker calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-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 json
2import os
3
4import boto3
5from respan import Respan
6from respan_instrumentation_sagemaker import SageMakerInstrumentor
7
8respan = Respan(
9 api_key=os.environ["RESPAN_API_KEY"],
10 instrumentations=[SageMakerInstrumentor()],
11)
12
13client = boto3.client("sagemaker-runtime", region_name=os.environ["AWS_REGION"])
14
15payload = json.dumps({"inputs": "Reply with one concise sentence about tracing."}).encode("utf-8")
16
17response = client.invoke_endpoint(
18 EndpointName="my-llm-endpoint",
19 ContentType="application/json",
20 Accept="application/json",
21 Body=payload,
22)
23
24result = json.loads(response["Body"].read())
25print(result)
26
27respan.flush()
28respan.shutdown()
4

View your trace

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

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 respan_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
2import boto3
3
4from respan import Respan, propagate_attributes
5from respan_instrumentation_sagemaker import SageMakerInstrumentor
6
7respan = Respan(instrumentations=[SageMakerInstrumentor()])
8
9
10def handle_request(user_id: str, prompt: str):
11 with propagate_attributes(
12 customer_identifier=user_id,
13 thread_identifier="conv_abc_123",
14 metadata={"plan": "pro"},
15 ):
16 payload = json.dumps({"inputs": prompt}).encode("utf-8")
17 response = boto3.client("sagemaker-runtime", region_name="us-east-1").invoke_endpoint(
18 EndpointName="my-llm-endpoint",
19 ContentType="application/json",
20 Accept="application/json",
21 Body=payload,
22 )
23 print(json.loads(response["Body"].read()))
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.