|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "intro", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "[](https://colab.research.google.com/github/openlayer-ai/openlayer-python/blob/main/examples/tracing/offline_buffering.ipynb)\n", |
| 9 | + "\n", |
| 10 | + "\n", |
| 11 | + "# <a id=\"top\">Offline buffering for tracing</a>\n", |
| 12 | + "\n", |
| 13 | + "This notebook shows how to use offline buffering to handle network failures gracefully when tracing with Openlayer." |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "id": "install", |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "!pip install openlayer" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "id": "setup", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## 1. Configure with offline buffering" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "id": "config", |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "from openlayer.lib import trace, configure\n", |
| 42 | + "\n", |
| 43 | + "\n", |
| 44 | + "# Define a callback for when traces fail to send\n", |
| 45 | + "def on_failure(trace_data, config, error): # noqa: ARG001\n", |
| 46 | + " # Failed traces are automatically buffered when offline_buffer_enabled=True\n", |
| 47 | + " pass\n", |
| 48 | + "\n", |
| 49 | + "# Configure tracer with offline buffering\n", |
| 50 | + "configure(\n", |
| 51 | + " api_key=\"your_api_key\",\n", |
| 52 | + " inference_pipeline_id=\"your_pipeline_id\",\n", |
| 53 | + " on_flush_failure=on_failure,\n", |
| 54 | + " offline_buffer_enabled=True,\n", |
| 55 | + " offline_buffer_path=\"./buffer\",\n", |
| 56 | + " max_buffer_size=100\n", |
| 57 | + ")" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "id": "usage", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "## 2. Use tracing normally" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "id": "trace", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "@trace()\n", |
| 76 | + "def process_query(query: str) -> str:\n", |
| 77 | + " return f\"Processed: {query}\"\n", |
| 78 | + "\n", |
| 79 | + "# If network fails, traces are automatically buffered\n", |
| 80 | + "result = process_query(\"What is AI?\")\n", |
| 81 | + "result" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "status", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "## 3. Check buffer status" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "id": "check", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "from openlayer.lib.tracing import tracer\n", |
| 100 | + "\n", |
| 101 | + "status = tracer.get_buffer_status()\n", |
| 102 | + "status" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "id": "replay", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "## 4. Replay buffered traces" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "id": "replay-code", |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "# When connectivity is restored, replay failed traces\n", |
| 121 | + "result = tracer.replay_buffered_traces()\n", |
| 122 | + "result" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "id": "summary", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "## Summary\n", |
| 131 | + "\n", |
| 132 | + "With offline buffering:\n", |
| 133 | + "\n", |
| 134 | + "- ✅ Failed traces are automatically saved locally\n", |
| 135 | + "- ✅ No data loss during network outages \n", |
| 136 | + "- ✅ Easy replay when connectivity returns\n", |
| 137 | + "- ✅ Custom failure callbacks for monitoring" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
| 141 | + "metadata": { |
| 142 | + "kernelspec": { |
| 143 | + "display_name": "Python 3 (ipykernel)", |
| 144 | + "language": "python", |
| 145 | + "name": "python3" |
| 146 | + }, |
| 147 | + "language_info": { |
| 148 | + "codemirror_mode": { |
| 149 | + "name": "ipython", |
| 150 | + "version": 3 |
| 151 | + }, |
| 152 | + "file_extension": ".py", |
| 153 | + "mimetype": "text/x-python", |
| 154 | + "name": "python", |
| 155 | + "nbconvert_exporter": "python", |
| 156 | + "pygments_lexer": "ipython3", |
| 157 | + "version": "3.9.18" |
| 158 | + } |
| 159 | + }, |
| 160 | + "nbformat": 4, |
| 161 | + "nbformat_minor": 5 |
| 162 | +} |
0 commit comments