Skip to content

Commit

Permalink
feat: add instrumentation for app_id (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: Avram Tudor <tudor.avram@8x8.com>
  • Loading branch information
quitrk and Avram Tudor authored Nov 13, 2024
1 parent 05e6f14 commit dfd19d6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
7 changes: 6 additions & 1 deletion skynet/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ async def authorize(jwt_incoming: str) -> dict:
raise HTTPException(status_code=401, detail=f'Failed to retrieve public key. {kid}')

try:
return jwt.decode(jwt_incoming, public_key, algorithms=['RS256', 'HS512'], audience=asap_pub_keys_auds)
decoded = jwt.decode(jwt_incoming, public_key, algorithms=['RS256', 'HS512'], audience=asap_pub_keys_auds)

if decoded.get('appId') is None:
decoded['appId'] = kid

return decoded
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Expired token.")
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion skynet/modules/ttt/summaries/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async def update_done_job(job: Job, result: str, processor: Processors, has_fail
await db.lrem(RUNNING_JOBS_KEY, 0, job.id)

if updated_job.status != JobStatus.SKIPPED:
SUMMARY_DURATION_METRIC.observe(updated_job.computed_duration)
SUMMARY_DURATION_METRIC.observe(updated_job.computed_duration, {'app_id': updated_job.metadata.app_id})
SUMMARY_FULL_DURATION_METRIC.observe(updated_job.computed_full_duration)
SUMMARY_INPUT_LENGTH_METRIC.observe(len(updated_job.payload.text))

Expand Down
1 change: 1 addition & 0 deletions skynet/modules/ttt/summaries/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DocumentPayload(BaseModel):


class DocumentMetadata(BaseModel):
app_id: str | None = None
customer_id: str | None = None


Expand Down
16 changes: 10 additions & 6 deletions skynet/modules/ttt/summaries/v1/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ def get_customer_id(request: Request) -> str:
return id


def get_app_id(request: Request) -> str:
return request.state.decoded_jwt.get('appId') if hasattr(request.state, 'decoded_jwt') else None


def get_metadata(request: Request) -> DocumentMetadata:
return DocumentMetadata(app_id=get_app_id(request), customer_id=get_customer_id(request))


@api_version(1)
@router.post("/action-items")
async def get_action_items(payload: DocumentPayload, request: Request) -> JobId:
"""
Starts a job to extract action items from the given payload.
"""

return await create_job(
job_type=JobType.ACTION_ITEMS, payload=payload, metadata=DocumentMetadata(customer_id=get_customer_id(request))
)
return await create_job(job_type=JobType.ACTION_ITEMS, payload=payload, metadata=get_metadata(request))


@api_version(1)
Expand All @@ -37,9 +43,7 @@ async def get_summary(payload: DocumentPayload, request: Request) -> JobId:
Starts a job to summarize the given payload.
"""

return await create_job(
job_type=JobType.SUMMARY, payload=payload, metadata=DocumentMetadata(customer_id=get_customer_id(request))
)
return await create_job(job_type=JobType.SUMMARY, payload=payload, metadata=get_metadata(request))


@api_version(1)
Expand Down

0 comments on commit dfd19d6

Please sign in to comment.