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

Adding to the trace the IP of the client #94

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fastapi import FastAPI, Request

from src.initialize import initialize_app, initialize_logging
from src.utils import get_ip_client, inject_additional_attributes, timeit
from src.utils import inject_additional_attributes, timeit
from langtrace_python_sdk import SendUserFeedback, langtrace
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span

Expand Down Expand Up @@ -131,7 +131,7 @@ async def qa(
# logger.info(messages)
response, span_id, trace_id = await inject_additional_attributes(
lambda: call_llm_api(model_name=model_name, messages=messages),
{"db.collection.name": collection_name, "service.ip": get_ip_client(request)}
{"db.collection.name": collection_name, "service.ip": request.client.host}
)
answer = response.choices[0].message.content
logger.info(answer)
Expand Down Expand Up @@ -211,9 +211,3 @@ async def sleep():
async def asleep():
await asyncio.sleep(5)
return {"status": "OK"}


@APP.get("/get-ip")
async def get_ip(request: Request):
client_host = request.client.host
return {"ip": client_host}
9 changes: 0 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,3 @@ async def inject_additional_attributes(fn, attributes=None):
context.attach(new_ctx)

return await fn()


def get_ip_client(request: Request):
x_forwarded_for = request.headers.get('x-forwarded-for')
if x_forwarded_for:
ip_client = x_forwarded_for.split(',')[0]
else:
ip_client = request.client.host
return ip_client
Loading