Skip to content

Commit ad7d210

Browse files
committed
Doc updates, API referece, cookbook WIP
Signed-off-by: Prasad Mujumdar <prasad@okahu.ai>
1 parent 79f1a4e commit ad7d210

3 files changed

Lines changed: 199 additions & 29 deletions

File tree

Monocle_scopes.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

documentation/Monocle_Cookbook.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This cookbook provides receipts of various instrumenation solution with Monocle
2+
3+
## Generate out of box telemetry, without any code chante
4+
If you have a python app that run locally ie ```python my-app.py [args]``` (as opposed to hosting in a cloud serverless container like AWS Lambda or Azure Function), you can use Monocle package to enable telemetry with any code change
5+
```shell
6+
python -m monocle_apptrace my-app.py [args]
7+
```
8+
This will genearate the trace files monocle_trace_*.json in the local directory
9+
10+
## Instrument your app to enable Monocle telemetry
11+
- Python
12+
Install monocle package or add `monocle_telemetry` in your ```requirements.txt``` file.
13+
```shell
14+
pip install monocle_telemetry
15+
```
16+
Import the package and add Monocle a single line of code to enable Monocle telemetry
17+
```python
18+
from monocle_apptrace import setup_monocle_telemetry
19+
setup_monocle_telemetry(workflow_name="your-app-name")
20+
```
21+
Now when you run the application, it will generate the trace files `monocle_trace_*.json` in the directory where the application is ran.
22+
23+
- Typescript
24+
Get the Monocle package
25+
26+
```shell
27+
npm install --save monacle2ai
28+
```
29+
Instrument your app code
30+
```js
31+
const { setupMonocle } = require("monacle2ai")
32+
setup_monocle_telemetry(workflow_name="your-app-name")
33+
```
34+
35+
## Track application business logic coded in a top level application method/API
36+
Consider a chatbot application with a method called conversation() that implements a chat conversion thread with end user. When the
37+
```python
38+
...
39+
def conversation():
40+
while True:
41+
...
42+
message = input("How can I help you:")
43+
cleaned_message = gaurdrail_chai(message) ==> GenAI code
44+
result = rag_chat_chain.invoke(message) ==> GenAI code
45+
```
46+
47+
The above code will generate two traces (one per chain invocation). All the spans in these traces will have an attribute called `Conversaion` with a unique value.
48+
```json
49+
"attributes": {
50+
"span.type": "inference",
51+
...
52+
"scope.conversation": "0xcb80e6f772968ed50ead80657b09cf52",
53+
```

documentation/Monocle_User_Guide.md

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,155 @@ Monocle exporters handle storing the trace for future analysis. By default each
209209
|OpenSearch|||
210210

211211
## Using scopes
212-
Imagine you have a chatbot application that supports a long conversion ie multiple question/answer back and forth between end user and bot. It uses various genAI tech components like LLMs and vector stores. A simple instrumentation will generate a trace per genAI API call (eg invocation of a framework chat or direct OpenAI API). As the app developer or owner, you are more interested in tracking the conversions than just APIs. The scopes in Monocle enables that use case.
213-
You can set the scope in application either programatically or declaratively. You can specific a value for scope or Monocle will generate a unique value (GUID) which gives you options to choose what's best suited for your use case. Please see the [Monocle scopes guide](Monocle_scopes.md) for the details and examples.
214-
212+
Imagine you have a chatbot application that supports a long conversion ie multiple question/answer back and forth between end user and bot. It uses various genAI tech components/services like LLMs and vector stores. A simple instrumentation will generate a trace per genAI API call (eg invocation of a framework chat or direct OpenAI API). As the app developer or owner, you are more interested in tracking the conversions than just APIs. The scopes in Monocle enables that use case.
213+
You can set the scope in application either programatically or declaratively. You can specific a value for scope or Monocle will generate a unique value (GUID) which gives you options to choose what's best suited for your use case. Please see the [Monocle cookbook](Monocle_scopes.md) for the details and examples.
215214

216215
## Extending Monocle
217216
If you are using a genAI technology that's not yet supported by Monocle out of the box or have you own proparitory code, you can extend monocle to generate traces in the Monocle format. Please refer to [extending monocle guide](Extending_monocle.md)
218217

218+
## Monocle API Referece
219+
### Python APIs
220+
#### [`setup_monocle_telemetry`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L153)
221+
```python3
222+
def setup_monocle_telemetry(
223+
workflow_name: str,
224+
span_processors: List[opentelemetry.sdk.trace.SpanProcessor] = None,
225+
span_handlers: Dict[str, monocle_apptrace.instrumentation.common.span_handler.SpanHandler] = None,
226+
wrapper_methods: List[Union[dict, monocle_apptrace.instrumentation.common.wrapper_method.WrapperMethod]] = None,
227+
union_with_default_methods: bool = True
228+
) -> None
229+
```
230+
231+
Set up Monocle telemetry for the application.
232+
233+
**Parameters:**
234+
235+
| Name | Type | Description | Default |
236+
|---|---|---|---|
237+
| workflow_name | str | The name of the workflow to be used as the service name in telemetry. | None |
238+
| span_processors | List[SpanProcessor] | Custom span processors to use instead of the default ones. If None, <br>BatchSpanProcessors with Monocle exporters will be used. | ones |
239+
| span_handlers | Dict[str, SpanHandler] | Dictionary of span handlers to be used by the instrumentor, mapping handler names to handler objects. | None |
240+
| wrapper_methods | List[Union[dict, WrapperMethod]] | Custom wrapper methods for instrumentation. If None, default methods will be used. | methods |
241+
| union_with_default_methods | bool, default=True | If True, combine the provided wrapper_methods with the default methods.<br>If False, only use the provided wrapper_methods. | methods |
242+
243+
### [`start_trace`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L196)
244+
245+
```python3
246+
def start_trace(
247+
248+
)
249+
```
250+
251+
Starts a new trace. All the spans created after this call will be part of the same trace.
252+
253+
**Returns:**
254+
255+
| Type | Description |
256+
|---|---|
257+
| Token | A token representing the attached context for the workflow span.<br>This token is to be used later to stop the current trace.<br>Returns None if tracing fails. |
258+
259+
**Raises:**
260+
261+
| Type | Description |
262+
|---|---|
263+
| Exception | The function catches all exceptions internally and logs a warning. |
264+
265+
### [`stop_scope`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L209)
266+
267+
```python3
268+
def stop_scope(
269+
token: object
270+
) -> None
271+
```
272+
273+
Stop the active scope. All the spans created after this will not have the scope attached.
274+
275+
**Parameters:**
276+
277+
| Name | Type | Description | Default |
278+
|---|---|---|---|
279+
| token | None | The token that was returned when the scope was started. | None |
280+
281+
**Returns:**
282+
283+
| Type | Description |
284+
|---|---|
285+
| None | None |
286+
287+
#### [`start_scope`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L229)
288+
289+
```python3
290+
def start_scope(
291+
scope_name: str,
292+
scope_value: str = None
293+
) -> object
294+
```
295+
296+
Start a new scope with the given name and and optional value. If no value is provided, a random UUID will be generated.
297+
298+
All the spans, across traces created after this call will have the scope attached until the scope is stopped.
299+
300+
**Parameters:**
301+
302+
| Name | Type | Description | Default |
303+
|---|---|---|---|
304+
| scope_name | None | The name of the scope. | None |
305+
| scope_value | None | Optional value of the scope. If None, a random UUID will be generated. | None |
306+
307+
**Returns:**
308+
309+
| Type | Description |
310+
|---|---|
311+
| Token | A token representing the attached context for the scope. This token is to be used later to stop the current scope. |
312+
313+
#### [`stop_scope`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L232)
314+
315+
```python3
316+
def stop_scope(
317+
token: object
318+
) -> None
319+
```
320+
321+
Stop the active scope. All the spans created after this will not have the scope attached.
322+
323+
**Parameters:**
324+
325+
| Name | Type | Description | Default |
326+
|---|---|---|---|
327+
| token | None | The token that was returned when the scope was started. | None |
328+
329+
**Returns:**
330+
331+
| Type | Description |
332+
|---|---|
333+
| None | None |
334+
335+
#### [`monocle_trace_scope`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L244)
336+
337+
```python3
338+
def monocle_trace_scope(
339+
scope_name: str,
340+
scope_value: str = None
341+
)
342+
```
343+
344+
Context manager to start and stop a scope. All the spans, across traces created within the encapsulated code will have the scope attached.
345+
346+
**Parameters:**
347+
348+
| Name | Type | Description | Default |
349+
|---|---|---|---|
350+
| scope_name | None | The name of the scope. | None |
351+
| scope_value | None | Optional value of the scope. If None, a random UUID will be generated. | None |
352+
353+
#### [`monocle_trace_http_route`](https://github.com/monocle2ai/monocle/blob/main/src/monocle_apptrace/instrumentation/common/instrumentor.py#L264)
354+
355+
```python3
356+
def monocle_trace_http_route(
357+
func
358+
)
359+
```
219360

361+
Decorator to start and stop a continue traces and scope for a http route. It will also initiate new scopes from the http headers if configured in ``monocle_scopes.json``
220362

363+
All the spans, across traces created in the route will have the scope attached.

0 commit comments

Comments
 (0)