Skip to content

Commit

Permalink
Merge pull request #94 from bukosabino/develop-adding-ip-client
Browse files Browse the repository at this point in the history
Adding to the trace the IP of the client
  • Loading branch information
bukosabino authored Jul 4, 2024
2 parents fbad69d + c69404f commit 95ac71f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 17 deletions.
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

0 comments on commit 95ac71f

Please sign in to comment.