|
| 1 | +## Get Latest Traces |
| 2 | + |
| 3 | +Retrieve traces within a specified time range. |
| 4 | + |
| 5 | +**Endpoint** <br> |
| 6 | +``` |
| 7 | +GET /api/{org_id}/{stream_name}/traces/latest |
| 8 | +``` |
| 9 | + |
| 10 | +**Parameters** <br> |
| 11 | + |
| 12 | +| Parameter | Type | Required | Description | |
| 13 | +|-----------|------|----------|-------------| |
| 14 | +| `org_id` | string | Yes | Your organization ID | |
| 15 | +| `stream_name` | string | Yes | Name of the trace stream | |
| 16 | +| `start_time` | integer | Yes | Start time in microseconds | |
| 17 | +| `end_time` | integer | Yes | End time in microseconds | |
| 18 | +| `from` | integer | Yes | Result start count (pagination offset) | |
| 19 | +| `size` | integer | Yes | Number of traces to return | |
| 20 | +| `filter` | string | No | Filter query for traces | |
| 21 | + |
| 22 | +**Example Request** <br> |
| 23 | +```bash |
| 24 | +curl -X GET \ |
| 25 | + "https://your-openobserve-instance/api/org_id/stream_name/traces/latest?&filter=&start_time=1751443100969000&end_time=1751444000969000&from=0&size=25 \ |
| 26 | + -H "Authorization: Basic <your-auth-token>" |
| 27 | +``` |
| 28 | +
|
| 29 | +**Response Format**<br> |
| 30 | +```json |
| 31 | +{ |
| 32 | + "total": 1, |
| 33 | + "trace_id": "b1eeb579ae863bdf9408e7d64c02d5d1", |
| 34 | + "hits": [ |
| 35 | + { |
| 36 | + "duration": 9, |
| 37 | + "end_time": 1751444644327767600, |
| 38 | + "first_event": { |
| 39 | + "_timestamp": 1751444644327758, |
| 40 | + "duration": 9, |
| 41 | + "end_time": 1751444644327767600, |
| 42 | + "operation_name": "infra:schema:get_versions", |
| 43 | + "service_name": "compactor", |
| 44 | + "span_status": "UNSET", |
| 45 | + "start_time": 1751444644327758300, |
| 46 | + "trace_id": "b1eeb579ae863bdf9408e7d64c02d5d1" |
| 47 | + }, |
| 48 | + "service_name": [ |
| 49 | + { |
| 50 | + "count": 1, |
| 51 | + "service_name": "compactor" |
| 52 | + } |
| 53 | + ], |
| 54 | + "spans": [1, 0], |
| 55 | + "start_time": 1751444644327758300, |
| 56 | + "trace_id": "b1eeb579ae863bdf9408e7d64c02d5d1" |
| 57 | + } |
| 58 | + ] |
| 59 | +} |
| 60 | +``` |
| 61 | +
|
| 62 | +**Response Fields** <br> |
| 63 | +
|
| 64 | +| Field | Description | |
| 65 | +|-------|-------------| |
| 66 | +| `total` | Total number of traces found | |
| 67 | +| `trace_id` | Unique identifier for the trace | |
| 68 | +| `hits` | Array of trace objects | |
| 69 | +| `duration` | Total duration of the trace in microseconds | |
| 70 | +| `start_time` | Trace start time in microseconds | |
| 71 | +| `end_time` | Trace end time in microseconds | |
| 72 | +| `first_event` | Details of the first span in the trace | |
| 73 | +| `service_name` | Array of services involved in the trace | |
| 74 | +| `spans` | Array indicating span counts | |
| 75 | +
|
| 76 | +## Get Spans Details |
| 77 | +
|
| 78 | +Retrieve detailed span information for a specific trace using the traces `/latest` endpoint with a `trace_id` filter. |
| 79 | +
|
| 80 | +### Using Search API |
| 81 | +
|
| 82 | +For complex queries, you can use the [search API](https://openobserve.ai/docs/api/search/search/) with SQL queries: |
| 83 | +```sql |
| 84 | +SELECT * FROM default WHERE trace_id = {trace_id} ORDER BY start_time |
| 85 | +``` |
| 86 | +
|
| 87 | +**Note:** Traces do not support full SQL queries in the traces interface, however, the search API supports SQL for trace data when needed for complex queries. |
| 88 | +
|
| 89 | +**Example:** |
| 90 | +```sql |
| 91 | +{ |
| 92 | + "query": { |
| 93 | + "sql": "SELECT * FROM default WHERE trace_id = b1eeb579ae863bdf9408e7d64c02d5d1" ORDER BY start_time, |
| 94 | + "start_time": 1751443100969000, |
| 95 | + "end_time": 1751444000969000, |
| 96 | + "from": 0, |
| 97 | + "size": 25 |
| 98 | + }, |
| 99 | + "search_type": "ui", |
| 100 | + "timeout": 0 |
| 101 | +} |
| 102 | +``` |
| 103 | +**Note:** |
| 104 | +
|
| 105 | +- When `size` is set to `25`, only the first `25` spans for the trace are returned. |
| 106 | +- To retrieve all spans, set `size` to `-1`. In this case, you do not need to define the `from` parameter. |
| 107 | +
|
| 108 | +## Error Handling |
| 109 | +
|
| 110 | +Common HTTP Status Codes: |
| 111 | +
|
| 112 | +- `200 OK`: Request successful |
| 113 | +- `400 Bad Request`: Invalid parameters or query format |
| 114 | +- `401 Unauthorized`: Invalid or missing authentication |
| 115 | +- `404 Not Found`: Stream or organization not found |
| 116 | +- `500 Internal Server Error`: Server error |
| 117 | +
|
| 118 | +**Error Response Format** <br> |
| 119 | +```json |
| 120 | +{ |
| 121 | + "error": { |
| 122 | + "type": "invalid_query", |
| 123 | + "message": "Invalid time range specified", |
| 124 | + "details": "start_time must be less than end_time" |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | +
|
| 129 | +## Best Practices |
| 130 | +
|
| 131 | +**Performance Optimization**: |
| 132 | +
|
| 133 | +1. **Use appropriate time ranges**: Avoid overly broad time ranges. |
| 134 | +2. **Implement pagination**: Use `from` and `size` parameters for large result sets. |
| 135 | +3. **Filter effectively**: Use specific filters to reduce result size. |
| 136 | +4. **Cache results**: Cache trace metadata for frequently accessed traces. |
| 137 | +
|
| 138 | +**Query Optimization**: |
| 139 | +
|
| 140 | +1. **Start with trace metadata**: Use the `/latest` endpoint first to get trace overview. |
| 141 | +2. **Fetch spans selectively**: Only fetch detailed spans when needed. |
| 142 | +3. **Use specific trace IDs**: When possible, query for specific trace IDs. |
| 143 | +
|
| 144 | +**Limitations**: |
| 145 | +
|
| 146 | +1. **SQL Query Support**: Full SQL queries are not supported in traces; use filter queries instead. |
| 147 | +2. **Time Range Requirement**: Start time and end time are mandatory for all queries. |
| 148 | +3. **Result Size Limits**: Large result sets should be paginated using `from` and `size`. |
| 149 | +
|
| 150 | +
|
| 151 | +
|
0 commit comments