Skip to content

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 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.

@xrmx xrmx merged commit 0bb1c42 into open-telemetry:main Jan 27, 2025
687 checks passed
aryabharat pushed a commit to aryabharat/opentelemetry-python-contrib that referenced this pull request Feb 2, 2025
…y#3204)

* Add tracing for ConverseStream

* Add converse stream example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.