Monitoring Amazon SageMaker AI

Amazon SageMaker AI provides capabilities for building, training, and deploying generative AI applications using foundation models. This guide shows you how to instrument an application using Amazon SageMaker AI endpoints with OpenLLMetry to send telemetry data to Instana.

Prerequisites

Make sure that the following prerequisites are met:

  • Python 3.8 or later
  • Amazon Web Services (AWS) credentials with SageMaker access
  • A deployed SageMaker AI endpoint
  • An Instana backend account with Generative AI observability enabled
  • Review of Getting Started guide on agent and agentless modes

Instrumenting your Amazon SageMaker AI application

  1. Install the required packages.

    pip install boto3 pandas numpy traceloop-sdk
  2. Export your AWS credentials.

    export AWS_ACCESS_KEY_ID="<your-access-key-id>"
    export AWS_SECRET_ACCESS_KEY="<your-secret-access-key>"

    To create or manage AWS access keys, see Amazon IAM documentation.

  3. Create a Python file with the following code.

    import boto3
    from traceloop.sdk import Traceloop
    from traceloop.sdk.decorators import workflow
    import pandas as pd
    
    # Initialize OpenLLMetry
    Traceloop.init(app_name="sagemaker_example", disable_batch=True)
    
    # Initialize SageMaker Runtime client
    runtime = boto3.Session().client('sagemaker-runtime', region_name="us-east-1")
    
    @workflow(name="sagemaker_example")
    def invoke_sagemaker_endpoint(endpoint):
        # Sample input data (1 row with 12 features)
        X_test = [[1, 8.2, 10.5, 1.5, 9.6, 1.3, 11.2, 2.9, 100, 11, 12.0, 13]]
    
        # Convert data to CSV format
        df = pd.DataFrame(X_test)
        csv_input = df.to_csv(index=False, header=False)
        csv_bytes = csv_input.encode('utf-8')
    
        # Invoke the SageMaker endpoint
        response = runtime.invoke_endpoint(
            EndpointName=endpoint,
            ContentType='text/csv',
            Body=csv_bytes
        )
    
        # Read and decode the prediction result
        return response['Body'].read().decode('utf-8')
    
    # Replace with your SageMaker endpoint name
    invoke_sagemaker_endpoint('<sagemaker-endpoint>')
    Note:
    The input data format depends on your deployed model's requirements. Adjust the data structure based on your specific model.
  4. Run your Amazon SageMaker AI application.

    python3 sagemaker_app.py

    The application will send data to your SageMaker AI endpoint and display the prediction results. OpenLLMetry automatically captures traces for each endpoint invocation and sends them to Instana.

  5. View data on Instana.

    After running your application, data will appear in the Instana Gen AI observability dashboard showing:

    • Endpoint name
    • Request and response payloads
    • Inference latency
    • Error information (if any)

Troubleshooting

For common issues such as traces not appearing or connection errors, see Troubleshooting.

Authentication errors

If you encounter authentication errors:
  1. Check whether your AWS credentials are set correctly
  2. Check whether your IAM user has the necessary SageMaker permissions
  3. Make sure that your credentials are not expired
  4. Verify the AWS region matches your endpoint's region

Endpoint not found errors

If you encounter endpoint not found errors:

  1. Verify the endpoint name is correct
  2. Check whether the endpoint is deployed and in "InService" status
  3. Make sure that the endpoint is in the same region as your client configuration
  4. Confirm your IAM policy includes sagemaker:InvokeEndpoint permission

Model input errors

If you encounter model input errors:
  1. Verify the input data format matches your model's expectations
  2. Check whether the content type is correct for your model
  3. Make sure that data types and dimensions are correct
  4. Review your model's input schema in the SageMaker console

Next steps