<p align="center">
    <a target="_blank" href="https://phoenix.arize.com" style="background:none">
        <img alt="arize banner" src="https://storage.googleapis.com/arize-assets/arize-logo-white.jpg"  width="auto" height="auto"></img>
    </a>
    <br/>
    <br/>
    <a href="https://docs.arize.com/">
        <img src="https://img.shields.io/static/v1?message=Docs&logo=data:image/png;base64, &labelColor=grey&color=blue&logoColor=white&label=%20"/>
    </a>
    <a target="_blank" href="https://join.slack.com/t/arize-ai/shared_invite/zt-1px8dcmlf-fmThhDFD_V_48oU7ALan4Q">
        <img src="https://img.shields.io/static/v1?message=Community&logo=slack&labelColor=grey&color=blue&logoColor=white&label=%20"/>
    </a>
    <br/>
    <a target="_blank" href="https://pypi.org/project/arize-otel/">
        <img src="https://img.shields.io/pypi/v/arize-otel">
    </a>
    <a target="_blank" href="https://pypi.org/project/arize-otel/">
        <img src="https://img.shields.io/pypi/status/arize-otel?style=flat&logo=ticktick&logoColor=white"/>
    </a>
    <a target="_blank" href="https://pypi.org/project/arize-otel/">
        <img src="https://img.shields.io/pypi/pyversions/arize-otel?logo=python&logoColor=white">
    </a>
    <a target="_blank" href="https://github.com/Arize-ai/arize-otel-python/blob/main/LICENSE">
        <img src="https://img.shields.io/pypi/l/arize-otel">
    </a>

</p>

---

## Overview

The `arize-otel` package is meant to be a very lightweight convenience package to help set up OpenTelemetry for tracing LLM applications
and send the traces to [Arize](https://arize.com/), [Phoenix](https://phoenix.arize.com/), or custom collectors.

## Installation

Install `arize-otel` using `pip`

```bash
pip install arize-otel
```

## Quickstart

You only need one import to use this package:

```python
from arize_otel import register_otel, Endpoints
```

The following examples showcase how to use `register_otel` to setup Opentelemetry in order to send traces to a collector. However,
this is **NOT** the same as [instrumenting](https://docs.arize.com/phoenix/tracing/concepts-tracing/how-does-tracing-work)
your application. For instance, you can use any of our [OpenInference AutoInstrumentators](https://github.com/Arize-ai/openinference).
Assuming we use the OpenAI AutoInstrumentation, we need to run `instrument()` _after_ using `register_otel`:

```python
# Setup OTEL via our convenience function
register_otel(
    # See details in examples below...
)

# Instrument your application using OpenInference AutoInstrumentators
from openinference.instrumentation.openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument()

```

The above code snippet will yield a fully setup and instrumented application. It is worth noting that this is completely **optional**. The usage of this
package is for convenience only, you can set up OpenTelemetry and send traces to Arize and Phoenix without installing this or any other package from Arize.

In the following sections we have examples on how to use the `register_otel` function:

### Send traces to Arize

To send traces to Arize you need to authenticate via the space and API keys. You can find them in the Space Settings page in the Arize platform. In addition,
you'll need to specify the model ID, an unique name to identify your model in the Arize platform. Optionally, you can set the model version, which serves to
to group a subset of data, given the same model ID, to compare and track changes.

```python
register_otel(
    endpoints = Endpoints.ARIZE,
    space_key = "your-arize-space-key",
    api_key = "your-arize-api-key",
    model_id = "your-model-id",
    model_version = "your-model-version", # OPTIONAL
)
```

### Send traces to Phoenix

To send traces to your local Phoenix server you just need to provide the correct endpoint. In the example below we have specified the local Phoenix endpoint,
but you can specify your own (explained in an example below). Optionally, you can specify a project to send the traces to. A project is a collection of traces that
are related to a single application or service. You can have multiple projects, each with multiple traces.

```python
register_otel(
    endpoints = Endpoints.PHOENIX_LOCAL,
    project_name = "your-project-name", # OPTIONAL
)
```

### Send traces to Hosted Phoenix

To send traces to your [Hosted Phoenix](https://docs.arize.com/phoenix/hosted-phoenix), also known as [Llamatrace](https://llamatrace.com/login), you just need to provide the correct endpoint. In the example below we have specified said endpoint, as well as the Phoenix API key required. Optionally, you can specify a project to which to send the traces, exactly as above with a local Phoenix instance.

```python
register_otel(
    endpoints = Endpoints.HOSTED_PHOENIX,
    api_key = "your-hosted-phoenix-api-key",
    project_name = "your-project-name", # OPTIONAL
)
```


### Send traces to Custom Endpoint

Sending traces to a collector on a custom endpoint is simple, you just need to provide the endpoint. If this endpoint corresponds to an Arize or Phoenix deployment,
you can add any of the options described in the examples above.

```python
register_otel(
    endpoints = "https://my-custom-endpoint"
    # any other options...
)
```

### Send traces to Multiple Endpoints

In this example we send traces to the default Arize and Phoenix endpoints, as well as to a third custom one. We also set all the options mentioned until now.

```python
register_otel(
    endpoints = [
        Endpoints.ARIZE,
        Endpoints.PHOENIX_LOCAL,
        "https://my-custom-endpoint",
    ]
    space_key = "your-space-key",
    api_key = "your-api-key",
    model_id = "your-model-id",
    model_version = "your-model-version", # OPTIONAL
    project_name = "your-project-name", # OPTIONAL
)
```

### Debug

As you're setting up your tracing, it is helpful to print to console the spans created. You can achieve this by setting `log_to_console=True`.

```python
register_otel(
    # other options...
    log_to_console=True
)
```

## Questions?

Find us in our [Slack Community](https://join.slack.com/t/arize-ai/shared_invite/zt-1px8dcmlf-fmThhDFD_V_48oU7ALan4Q) or email support@arize.com

## Copyright, Patent, and License

Copyright 2024 Arize AI, Inc. All Rights Reserved.

This software is licensed under the terms of the 3-Clause BSD License. See [LICENSE](https://github.com/Arize-ai/arize-otel-python/blob/main/LICENSE).
