-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from fastapi import FastAPI, Request | ||
|
||
from langsmith import traceable | ||
from langsmith.run_helpers import get_current_span, tracing_context | ||
|
||
fake_app = FastAPI() | ||
|
||
|
||
@traceable | ||
def fake_function(): | ||
span = get_current_span() | ||
assert span is not None | ||
parent_run = span.parent_run | ||
assert parent_run is not None | ||
assert "did-propagate" in span.tags | ||
assert span.metadata["some-cool-value"] == 42 | ||
return "Fake function response" | ||
|
||
|
||
@fake_app.post("/fake-route") | ||
async def fake_route(request: Request): | ||
with tracing_context(headers=request.headers): | ||
fake_function() | ||
return {"message": "Fake route response"} |
47 changes: 47 additions & 0 deletions
47
python/tests/integration_tests/test_context_propagation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import asyncio | ||
|
||
import pytest | ||
from httpx import AsyncClient | ||
|
||
from langsmith import traceable | ||
from langsmith.run_helpers import get_current_span | ||
from tests.integration_tests.fake_server import fake_app | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def event_loop(): | ||
loop = asyncio.get_event_loop() | ||
yield loop | ||
loop.close() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def fake_server(): | ||
from uvicorn import Config, Server | ||
|
||
config = Config(app=fake_app, loop="asyncio", port=8000, log_level="info") | ||
server = Server(config=config) | ||
|
||
asyncio.create_task(server.serve()) | ||
await asyncio.sleep(0.1) | ||
|
||
yield | ||
|
||
await server.shutdown() | ||
|
||
|
||
@traceable | ||
async def the_parent_function(): | ||
async with AsyncClient(app=fake_app, base_url="http://localhost:8000") as client: | ||
headers = {} | ||
if span := get_current_span(): | ||
headers.update(span.to_headers()) | ||
return await client.post("/fake-route", headers=headers) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_tracing_fake_server(fake_server): | ||
response = await the_parent_function( | ||
langsmith_extra={"metadata": {"some-cool-value": 42}, "tags": ["did-propagate"]} | ||
) | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters