|
| 1 | +"""Resource for tagging traces.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import uuid |
| 5 | + |
| 6 | +from lmnr.sdk.client.asynchronous.resources.base import BaseAsyncResource |
| 7 | +from lmnr.sdk.log import get_default_logger |
| 8 | + |
| 9 | +logger = get_default_logger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +class AsyncTags(BaseAsyncResource): |
| 13 | + """Resource for tagging traces.""" |
| 14 | + |
| 15 | + async def tag( |
| 16 | + self, |
| 17 | + trace_id: str | int | uuid.UUID, |
| 18 | + tags: list[str] | str, |
| 19 | + ): |
| 20 | + """Tag a trace with a list of tags. Note that the trace must be ended |
| 21 | + before tagging it. You may want to call `Laminar.flush()` after the |
| 22 | + trace that you want to tag. |
| 23 | +
|
| 24 | + Args: |
| 25 | + trace_id (str | int | uuid.UUID): The trace id to tag. |
| 26 | + tags (list[str] | str): The tag or list of tags to add to the trace. |
| 27 | +
|
| 28 | + Raises: |
| 29 | + ValueError: If the trace id is not a valid UUID. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + list[dict]: The response from the server. |
| 33 | +
|
| 34 | + Example: |
| 35 | + ```python |
| 36 | + from lmnr import Laminar, AsyncLaminarClient, observe |
| 37 | +
|
| 38 | + Laminar.initialize() |
| 39 | + client = AsyncLaminarClient() |
| 40 | + trace_id = None |
| 41 | +
|
| 42 | + @observe() |
| 43 | + def foo(): |
| 44 | + trace_id = Laminar.get_trace_id() |
| 45 | + pass |
| 46 | +
|
| 47 | + # make sure `foo` is called outside a trace context |
| 48 | + foo() |
| 49 | +
|
| 50 | + # or make sure the trace is ended by this point |
| 51 | + Laminar.flush() |
| 52 | +
|
| 53 | + await client.tags.tag(trace_id, "my_tag") |
| 54 | + ``` |
| 55 | + """ |
| 56 | + trace_tags = tags if isinstance(tags, list) else [tags] |
| 57 | + if isinstance(trace_id, uuid.UUID): |
| 58 | + trace_id = str(trace_id) |
| 59 | + elif isinstance(trace_id, int): |
| 60 | + trace_id = str(uuid.UUID(int=trace_id)) |
| 61 | + elif isinstance(trace_id, str): |
| 62 | + uuid.UUID(trace_id) # Will raise ValueError if invalid |
| 63 | + else: |
| 64 | + raise ValueError(f"Invalid trace id: {trace_id}") |
| 65 | + |
| 66 | + url = self._base_url + "/v1/tag" |
| 67 | + payload = { |
| 68 | + "traceId": trace_id, |
| 69 | + "names": trace_tags, |
| 70 | + } |
| 71 | + response = await self._client.post( |
| 72 | + url, |
| 73 | + content=json.dumps(payload), |
| 74 | + headers={ |
| 75 | + **self._headers(), |
| 76 | + }, |
| 77 | + ) |
| 78 | + |
| 79 | + if response.status_code == 404: |
| 80 | + logger.warning( |
| 81 | + f"Trace {trace_id} not found. The trace may have not been ended yet." |
| 82 | + ) |
| 83 | + return [] |
| 84 | + |
| 85 | + if response.status_code != 200: |
| 86 | + raise ValueError( |
| 87 | + f"Failed to tag trace: [{response.status_code}] {response.text}" |
| 88 | + ) |
| 89 | + return response.json() |
0 commit comments