Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

botocore: add basic tracing for bedrock ConverseStream #3204

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Jan 23, 2025

Description

This introduces basic tracing for the ConverseStream service. I've chosen to delay the closing of the span after the response has been consumed instead of buffering all the response chunks since the buffer it may become big.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.

  • tox -e py310-test-instrumentation-botocore

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@xrmx xrmx requested a review from a team as a code owner January 23, 2025 16:53
Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Do you mind checking if this langchain code produces spans as intended?

from langchain_aws import ChatBedrock


def main():
    llm = ChatBedrock(
        beta_use_converse_api=True,
        model_id='amazon.titan-text-lite-v1',
        streaming=True,
    )
    response_stream = llm.stream("Write a short poem on OpenTelemetry.")
    for chunk in response_stream:
        for item in chunk.content:
            if 'text' in item:
                print(item['text'], end="")

if __name__ == "__main__":
    main()

@xrmx
Copy link
Contributor Author

xrmx commented Jan 24, 2025

from langchain_aws import ChatBedrock


def main():
    llm = ChatBedrock(
        beta_use_converse_api=True,
        model_id='amazon.titan-text-lite-v1',
        streaming=True,
    )
    response_stream = llm.stream("Write a short poem on OpenTelemetry.")
    for chunk in response_stream:
        for item in chunk.content:
            if 'text' in item:
                print(item['text'], end="")

if __name__ == "__main__":
    main()

It does:

{
    "name": "chat amazon.titan-text-lite-v1",
    "context": {
        "trace_id": "0xa584c79bfbd24ee4a69a470f62f5a785",
        "span_id": "0x330a7997ae2fd3c8",
        "trace_state": "[]"
    },
    "kind": "SpanKind.CLIENT",
    "parent_id": null,
    "start_time": "2025-01-24T08:43:40.741219Z",
    "end_time": "2025-01-24T08:43:44.649476Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "rpc.system": "aws-api",
        "rpc.service": "Bedrock Runtime",
        "rpc.method": "ConverseStream",
        "aws.region": "us-east-1",
        "gen_ai.system": "aws.bedrock",
        "gen_ai.request.model": "amazon.titan-text-lite-v1",
        "gen_ai.operation.name": "chat",
        "aws.request_id": "c41388e1-9406-4376-8d43-8df022879225",
        "retry_attempts": 0,
        "http.status_code": 200,
        "gen_ai.usage.input_tokens": 12,
        "gen_ai.usage.output_tokens": 128,
        "gen_ai.response.finish_reasons": [
            "max_tokens"
        ]
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.30.0.dev0",
            "telemetry.auto.version": "0.51b0.dev",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}

Copy link

linux-foundation-easycla bot commented Jan 24, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.



# pylint: disable=abstract-method
class ConverseStreamWrapper(ObjectProxy):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if stream terminates with an error? what if user closes it before receiving response fully? We should try to cover as much as we can and I believe OpenAI instrumentation does it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll cover more cases in followup PRs, thanks for reminding about this.

Copy link
Contributor Author

@xrmx xrmx Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in the checklist to not forgetting #3210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.